From c2f2ef7214e30ec542c59138d8ea9c846a99b9cb Mon Sep 17 00:00:00 2001 From: Murod Khaydarov Date: Mon, 24 Feb 2020 14:50:15 +0300 Subject: [PATCH 1/3] [issue-131]: Check email existance --- src/resolvers/user.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/resolvers/user.ts b/src/resolvers/user.ts index a81a0002..1d97b2a4 100644 --- a/src/resolvers/user.ts +++ b/src/resolvers/user.ts @@ -43,6 +43,14 @@ export default { ): Promise { let user; + user = await factories.usersFactory.findByEmail(email); + + if (user !== null) { + throw new AuthenticationError( + 'User with such email already registered' + ); + } + try { user = await factories.usersFactory.create(email); emailProvider.send(email, emailTemplatesNames.SUCCESSFUL_SIGN_UP, { From 17e2033e83a345b5d5218fd5a11dbe804159fcaa Mon Sep 17 00:00:00 2001 From: Murod Khaydarov Date: Mon, 24 Feb 2020 21:16:20 +0300 Subject: [PATCH 2/3] add new migration file --- migrate-mongo-config.js | 8 +++---- .../20200224203115-add-index-to-users.js | 22 +++++++++++++++++++ src/resolvers/user.ts | 8 ------- 3 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 migrations/20200224203115-add-index-to-users.js diff --git a/migrate-mongo-config.js b/migrate-mongo-config.js index 32a72a3e..92b55aaa 100644 --- a/migrate-mongo-config.js +++ b/migrate-mongo-config.js @@ -2,7 +2,7 @@ * @file Configuration for migrate-mongo (https://www.npmjs.com/package/migrate-mongo) */ -require('./src/env'); +require('dotenv').config(); const config = { mongodb: { @@ -16,8 +16,8 @@ const config = { /** * Removes a deprecation warning when connecting */ - useUnifiedTopology: true - } + useUnifiedTopology: true, + }, }, /** @@ -28,7 +28,7 @@ const config = { /** * The mongodb collection where the applied changes are stored. Only edit this when really necessary */ - changelogCollectionName: 'changelog' + changelogCollectionName: 'changelog', }; module.exports = config; diff --git a/migrations/20200224203115-add-index-to-users.js b/migrations/20200224203115-add-index-to-users.js new file mode 100644 index 00000000..70b8f791 --- /dev/null +++ b/migrations/20200224203115-add-index-to-users.js @@ -0,0 +1,22 @@ +/** + * @file Migration to add unique index for `email` field from users collection + */ +module.exports = { + async up(db) { + const collection = db.collection('users'); + + await collection.createIndex({ + email: 1, + }, { + unique: true, + }); + }, + + async down(db) { + const collection = db.collection('users'); + + await collection.dropIndex({ + email: 1, + }); + }, +}; diff --git a/src/resolvers/user.ts b/src/resolvers/user.ts index 1d97b2a4..a81a0002 100644 --- a/src/resolvers/user.ts +++ b/src/resolvers/user.ts @@ -43,14 +43,6 @@ export default { ): Promise { let user; - user = await factories.usersFactory.findByEmail(email); - - if (user !== null) { - throw new AuthenticationError( - 'User with such email already registered' - ); - } - try { user = await factories.usersFactory.create(email); emailProvider.send(email, emailTemplatesNames.SUCCESSFUL_SIGN_UP, { From 23f5da9fc31f417491091fb90b2979e20ca0e3e1 Mon Sep 17 00:00:00 2001 From: Murod Khaydarov Date: Wed, 26 Feb 2020 14:03:51 +0300 Subject: [PATCH 3/3] update --- README.md | 5 +++++ package.json | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0a78fab4..c91a09ec 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,8 @@ If the playground is turned on, you can access it via `/graphql` route. To execute the request, enter it in the input field on the left and click on the request execution button. On the right side you will see the result of the query. + +## Migrations + +Run `yarn migrations:up` command to apply migration revisions or +`yarn migrations:down` to rollback the last revision. diff --git a/package.json b/package.json index a300a99f..f79bed40 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "dev:up": "docker-compose -f docker-compose.dev.yml up -d", "dev:down": "docker-compose -f docker-compose.dev.yml down", "build": "tsc && yarn copy-email-templates", - "copy-email-templates": "copyfiles src/email/templates/*/*.txt build" + "copy-email-templates": "copyfiles src/email/templates/*/*.txt build", + "migrations:up": "docker-compose exec api yarn migrate-mongo up", + "migrations:down": "docker-compose exec api yarn migrate-mongo down" }, "husky": { "hooks": {