diff --git a/backend/src/services/db-client.js b/backend/src/services/db-client.js index c51fd7e..2053214 100644 --- a/backend/src/services/db-client.js +++ b/backend/src/services/db-client.js @@ -285,27 +285,6 @@ class DBClient { }); } - async userNotExists(username, email) { - const client = await this.get(); - - return new Promise((resolve, reject) => { - client.db().collection('users').aggregate([ - { $match: { $or: [{ email }, { username }] } }, - { $project: { _id: 1, username: 1, email: 1 } }, - ]).toArray((err, result) => { - if (result.length > 0) { - const firstUser = result[0]; - if (firstUser.email === email) { - return reject(new Error('User with this email already exists')); - } else if (firstUser.username === username) { - return reject(new Error('User with this username already exists')); - } - } - resolve(); - }); - }); - } - /** * Creates a user in the database for a given User object * @@ -316,15 +295,15 @@ class DBClient { const client = await this.get(); return new Promise((resolve, reject) => { - this.userNotExists(user.username, user.email).then(() => { - client.db().collection('users').insertOne(user, (error, result) => { - if (error) { - return reject(error); + client.db().collection('users').insertOne(user, (error, result) => { + if (error) { + if (error.message.includes('email')) { + return reject(new Error('User with this email already exists')); + } else { + return reject(new Error('User with this username already exists')); } - resolve(result); - }); - }).catch((error) => { - return reject(error); + } + resolve(result); }); }); } diff --git a/docker-compose.yaml b/docker-compose.yaml index 5437881..5f7c236 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -9,7 +9,7 @@ services: volumes: - image_data:/data/images environment: - - DATABASE_URI=mongodb://db:27017/pixelgram + - DATABASE_URI=mongodb://database:27017/pixelgram - PORT=3000 - SECRET=7f40a8c2c68f47ef7b896586b012ae34