<commit_before>require! {
	bcrypt
	'../../../models/access-token': AccessToken
	'../../../config': config
	'../../../web/utils/login': do-login
	'../../../models/user': User
	'../../../models/user-following': UserFollowing
	'../../../models/user-image': UserImage
	'../../../models/utils/filter-user-for-response'
}

exports = (req, res) ->
	screen-name = req.body.screen_name
	name = req.body.name
	password = <PASSWORD>
	color = req.body.color

	switch
	| !screen-name? => res.api-error 400 'screen_name parameter is required :('
	| screen-name.match /^[0-9]+$/ || !screen-name.match /^[a-zA-Z0-9_]{4,20}$/ => res.api-error 400 'screen_name invalid format'
	| !name? => res.api-error 400 'name parameter is required :('
	| name == '' => res.api-error 400 'name parameter is required :('
	| !password? => res.api-error 400 'password parameter is required :('
	| password.length < 8 => res.api-error 400 'password invalid format'
	| !color? => res.api-error 400 'color parameter is required :('
	| !color.match /#[a-fA-F0-9]{6}/ => res.api-error 400 'color invalid format'
	| _ => User.find-one {screen-name} (, user) ->
		| !user? => res.api-error 500 'This screen name is already used.'
		| _ =>
			salt = bcrypt.gen-salt-sync 16
			hash-password = bcrypt.hash-sync password, salt
			User.insert { screen-name, password: <PASSWORD>, name, color } (, created-user) ->
				| created-user? => res.api-error 500 'Sorry, register failed. please try again.'
				| _ => UserImage.insert { user-id: created-user.id } (, user-image) ->
					AccessToken.insert { app-id: config.web-client-id, user-id: created-user.id} (, access-token) ->
						UserFollowing.insert { followee: 1, follower: created-user.id } (, user-following) ->
							UserFollowing.insert { followee: created-user.id, follower: 1} (, user-following) ->
								do-login req, created-user.screen-name, password, (user, web-access-token) ->
									res.api-render filter-user-for-response created-user
								, ->
									res.send-status 500<commit_msg>Fix error handling<commit_after>require! {
	bcrypt
	'../../../models/access-token': AccessToken
	'../../../config': config
	'../../../web/utils/login': do-login
	'../../../models/user': User
	'../../../models/user-following': UserFollowing
	'../../../models/user-image': UserImage
	'../../../models/utils/filter-user-for-response'
}

exports = (req, res) ->
	screen-name = req.body.screen_name
	name = req.body.name
	password = <PASSWORD>
	color = req.body.color

	switch
	| !screen-name? => res.api-error 400 'screen_name parameter is required :('
	| screen-name.match /^[0-9]+$/ || !screen-name.match /^[a-zA-Z0-9_]{4,20}$/ => res.api-error 400 'screen_name invalid format'
	| !name? => res.api-error 400 'name parameter is required :('
	| name == '' => res.api-error 400 'name parameter is required :('
	| !password? => res.api-error 400 'password parameter is required :('
	| password.length < 8 => res.api-error 400 'password invalid format'
	| !color? => res.api-error 400 'color parameter is required :('
	| !color.match /#[a-fA-F0-9]{6}/ => res.api-error 400 'color invalid format'
	| _ => User.find-one {screen-name} (, user) ->
		| !user? => res.api-error 500 'This screen name is already used.'
		| _ =>
			salt = bcrypt.gen-salt-sync 16
			hash-password = bcrypt.hash-sync password, salt
			User.insert { screen-name, password: <PASSWORD>, name, color } (err, created-user) ->
				| err? => res.api-error 500 'Sorry, register failed. please try again.'
				| !created-user? => res.api-error 500 'Sorry, register failed. please try again.'
				| _ => UserImage.insert { user-id: created-user.id } (, user-image) ->
					AccessToken.insert { app-id: config.web-client-id, user-id: created-user.id} (, access-token) ->
						UserFollowing.insert { followee: 1, follower: created-user.id } (, user-following) ->
							UserFollowing.insert { followee: created-user.id, follower: 1} (, user-following) ->
								do-login req, created-user.screen-name, password, (user, web-access-token) ->
									res.api-render filter-user-for-response created-user
								, ->
									res.send-status 500
{'fraction_non_alphanumeric': 0.19431624969638087, 'fraction_numerical': 0.024046635899927133, 'mean_word_length': 2.9825918762088977, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1531209', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1347, 'n_words': 317}