<commit_before>import Util from '../util'
import {ObjectId} from 'mongodb'
import {auth} from '../auth'

export const userResolver = (db) => ({

	createUser: async ({username, password, email}) => {

		//TODO: Improve error handling -> https://medium.com/@tarkus/validation-and-user-errors-in-graphql-mutations-39ca79cd00bf

		// Validate Password
		const err = Util.validatePassword(password)
		if (err.length > 0) {
			throw new Error('Invalid Password: \n' + err.join('\n'))
		}

		// Validate Email Address
		if (!Util.validateEmail(email)) {
			throw new Error('Email is not valid')
		}

		const userCollection = db.collection('User')


		const userExists = (await userCollection.findOne({$or: [{'eemail': email}, {'username': username}]})) != null

		if (userExists) {
			throw new Error('Username or email already taken')
		}


		const hashedPw = Util.hashPassword(password)
		const insertResult = await userCollection.insertOne({email: email, username: username, password: <PASSWORD>})

		if (insertResult.insertedCount) {
			const doc = insertResult.ops[0]
			return doc
		}
	},
	changeMe: async (user,{context}) => {
		Util.checkIfUserIsLoggedIn(context)
		const result = (await db.collection('User').findOneAndUpdate({_id: ObjectId(context.id)}, {$set:user}))
		if( result.ok !== 1){
			throw new Error('User could not be changed')
		}
		return result.value
	},
	setNote: async ({_id,value},{context}) => {
		Util.checkIfUserIsLoggedIn(context)


		const userCollection = db.collection('User')

		var result = null
		if( !_id ) {
			_id = ObjectId()
			result = (await userCollection.updateOne({_id: ObjectId(context.id)}, {$push: {note: {value: value, _id: _id}}}))
			if( result.modifiedCount !== 1){
				throw new Error('Note was not inserted')<commit_msg>Add username check<commit_after>import Util from '../util'
import {ObjectId} from 'mongodb'
import {auth} from '../auth'

export const userResolver = (db) => ({

	createUser: async ({username, password, email}) => {

		//TODO: Improve error handling -> https://medium.com/@tarkus/validation-and-user-errors-in-graphql-mutations-39ca79cd00bf

		// Validate Password
		const err = Util.validatePassword(password)
		if (err.length > 0) {
			throw new Error('Invalid Password: \n' + err.join('\n'))
		}

		// Validate Email Address
		if (!Util.validateEmail(email)) {
			throw new Error('Email is not valid')
		}

		const userCollection = db.collection('User')


		const userExists = (await userCollection.findOne({$or: [{'email': email}, {'username': username}]})) != null

		if (userExists) {
			throw new Error('Username or email already taken')
		}


		const hashedPw = Util.hashPassword(password)
		const insertResult = await userCollection.insertOne({email: email, username: username, password: <PASSWORD>})

		if (insertResult.insertedCount) {
			const doc = insertResult.ops[0]
			return doc
		}
	},
	changeMe: async (user,{context}) => {
		Util.checkIfUserIsLoggedIn(context)

		const userCollection = db.collection('User')

		let existingUser = (await userCollection.findOne({$or: [{'username': user.username}]}))
		if( existingUser!=null && existingUser._id.toString() !== context.id ){
			throw new Error(`Username ${user.username} already taken`)
		}else {


			const result = (await userCollection.findOneAndUpdate({_id: ObjectId(context.id)}, {$set: user}))
			if (result.ok !== 1) {
				throw new Error('User could not be changed')
			}
			return result.value
		}
	},
	setNote: async ({_id,value},{context}) => {
		Util.checkIfUserIsLoggedIn(context)


		const userCollection = db.collection('User')

		var result = null
		if( !_id ) {
			_id = ObjectId()
			result = (await userCollection.updateOne({_id: ObjectId(context.id)}, {$push: {note: {value: value, _id: _id}}}))
			if( result.modifiedCount !== 1){
				throw new Error('Note was not inserted')
{'fraction_non_alphanumeric': 0.15514993481095177, 'fraction_numerical': 0.005215123859191656, 'mean_word_length': 4.254794520547946, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '1735999', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1201, 'n_words': 314}