From d324c3b605138b270b3eaec890debb59a3e5224d Mon Sep 17 00:00:00 2001 From: seaerchin <44049504+seaerchin@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:34:26 +0800 Subject: [PATCH 01/14] chore(ci): enhance mergify (#1245) * ci(mergify): fixup mergify * chore(mergify): dup rule from fe * refactor(mergify): automerge all non-major updates --- .github/mergify.yml | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/.github/mergify.yml b/.github/mergify.yml index b757de7a6..8eb8816e7 100644 --- a/.github/mergify.yml +++ b/.github/mergify.yml @@ -3,8 +3,9 @@ pull_request_rules: conditions: - author~=^dependabot\[bot\]$ - check-success~=lint + - check-success~=build - check-success~=test - - title~=bump [^\s]+ from ([\d]+)\..+ to \1\. + - -dependabot-update-type = version-update:semver-minor actions: review: type: APPROVE @@ -16,7 +17,8 @@ pull_request_rules: - author=snyk-bot - check-success~=lint - check-success~=test - - title~=^\[Snyk\] + - title~=^\[Snyk\]] + - base = develop actions: review: type: APPROVE @@ -34,4 +36,15 @@ pull_request_rules: - title~=(?i)\[wip\] actions: edit: - draft: True \ No newline at end of file + draft: True + + - name: Ping Isomer members for stale open PRs (>1 month since last activity) + conditions: + - updated-at<30 days ago + - -closed + actions: + request_reviews: + teams: + - "@isomerpages/iso-engineers" + comment: + message: This pull request has been stale for more than 30 days! Could someone please take a look at it @isomerpages/iso-engineers From a06afdddea52fa87124ec07ff228b0c4f6e8ec96 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 8 Apr 2024 14:35:42 +0000 Subject: [PATCH 02/14] build(deps): bump marked from 11.2.0 to 12.0.1 (#1219) Bumps [marked](https://github.com/markedjs/marked) from 11.2.0 to 12.0.1. - [Release notes](https://github.com/markedjs/marked/releases) - [Changelog](https://github.com/markedjs/marked/blob/master/.releaserc.json) - [Commits](https://github.com/markedjs/marked/compare/v11.2.0...v12.0.1) --- updated-dependencies: - dependency-name: marked dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 264cb6b40..b24a96dfe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -61,7 +61,7 @@ "jsdom": "^16.7.0", "jsonwebtoken": "^9.0.0", "lodash": "^4.17.21", - "marked": "^11.2.0", + "marked": "^12.0.1", "module-alias": "^2.2.2", "moment-timezone": "^0.5.35", "morgan": "~1.10.0", @@ -14893,9 +14893,9 @@ } }, "node_modules/marked": { - "version": "11.2.0", - "resolved": "https://registry.npmjs.org/marked/-/marked-11.2.0.tgz", - "integrity": "sha512-HR0m3bvu0jAPYiIvLUUQtdg1g6D247//lvcekpHO1WMvbwDlwSkZAX9Lw4F4YHE1T0HaaNve0tuAWuV1UJ6vtw==", + "version": "12.0.1", + "resolved": "https://registry.npmjs.org/marked/-/marked-12.0.1.tgz", + "integrity": "sha512-Y1/V2yafOcOdWQCX0XpAKXzDakPOpn6U0YLxTJs3cww6VxOzZV1BTOOYWLvH3gX38cq+iLwljHHTnMtlDfg01Q==", "bin": { "marked": "bin/marked.js" }, diff --git a/package.json b/package.json index 3060e042c..6d4640f0c 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "jsdom": "^16.7.0", "jsonwebtoken": "^9.0.0", "lodash": "^4.17.21", - "marked": "^11.2.0", + "marked": "^12.0.1", "module-alias": "^2.2.2", "moment-timezone": "^0.5.35", "morgan": "~1.10.0", From 08ec28a66a954863135fa651ac3d338d1898ca7f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 8 Apr 2024 14:35:46 +0000 Subject: [PATCH 03/14] build(deps): bump base-64 from 0.1.0 to 1.0.0 (#1254) Bumps [base-64](https://github.com/mathiasbynens/base64) from 0.1.0 to 1.0.0. - [Commits](https://github.com/mathiasbynens/base64/compare/v0.1.0...v1.0.0) --- updated-dependencies: - dependency-name: base-64 dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index b24a96dfe..c7453ebbe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,7 @@ "aws-sdk": "^2.1565.0", "axios": "^1.6.5", "axios-cache-interceptor": "^0.10.7", - "base-64": "^0.1.0", + "base-64": "^1.0.0", "bcryptjs": "^2.4.3", "bluebird": "^3.7.2", "body-parser": "^1.19.2", @@ -6424,9 +6424,9 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, "node_modules/base-64": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/base-64/-/base-64-0.1.0.tgz", - "integrity": "sha512-Y5gU45svrR5tI2Vt/X9GPd3L0HNIKzGu202EjxrXMpuc2V2CiKgemAbUUsqYmZJvPtCXoUKjNZwBJzsNScUbXA==" + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/base-64/-/base-64-1.0.0.tgz", + "integrity": "sha512-kwDPIFCGx0NZHog36dj+tHiwP4QMzsZ3AgMViUBKI0+V5n4U0ufTCUMhnQ04diaRI8EX/QcPfql7zlhZ7j4zgg==" }, "node_modules/base64-js": { "version": "1.5.1", diff --git a/package.json b/package.json index 6d4640f0c..d136fb9c7 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "aws-sdk": "^2.1565.0", "axios": "^1.6.5", "axios-cache-interceptor": "^0.10.7", - "base-64": "^0.1.0", + "base-64": "^1.0.0", "bcryptjs": "^2.4.3", "bluebird": "^3.7.2", "body-parser": "^1.19.2", From d493d4e22679213b6c5d73e481ce0d1344c23228 Mon Sep 17 00:00:00 2001 From: seaerchin <44049504+seaerchin@users.noreply.github.com> Date: Tue, 9 Apr 2024 10:26:12 +0800 Subject: [PATCH 04/14] fix(mergify): fix mistake (#1280) --- .github/mergify.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/mergify.yml b/.github/mergify.yml index 8eb8816e7..fad89a29b 100644 --- a/.github/mergify.yml +++ b/.github/mergify.yml @@ -5,7 +5,7 @@ pull_request_rules: - check-success~=lint - check-success~=build - check-success~=test - - -dependabot-update-type = version-update:semver-minor + - -dependabot-update-type = version-update:semver-major actions: review: type: APPROVE From 517fbca18b45c73df2f5d41058ea00693eacb2b9 Mon Sep 17 00:00:00 2001 From: seaerchin <44049504+seaerchin@users.noreply.github.com> Date: Tue, 9 Apr 2024 10:27:56 +0800 Subject: [PATCH 05/14] feat(dd): add source code linkage (#1279) * ci(env): inject dd env vars * refactor(datadog): shift envars to task def --- .aws/deploy/backend-task-definition.staging.json | 12 +++++++++++- .github/workflows/aws_deploy.yml | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.aws/deploy/backend-task-definition.staging.json b/.aws/deploy/backend-task-definition.staging.json index 3ca7c9d20..6d4eb5ce1 100644 --- a/.aws/deploy/backend-task-definition.staging.json +++ b/.aws/deploy/backend-task-definition.staging.json @@ -10,7 +10,17 @@ } ], "essential": true, - "environment": [{ "name": "ENV_TYPE", "value": "STAGING" }], + "environment": [ + { "name": "ENV_TYPE", "value": "STAGING" }, + { + "name": "DD_GIT_COMMIT_SHA", + "value": "" + }, + { + "name": "DD_GIT_REPOSITORY_URL", + "value": "github.com/isomerpages/isomercms-backend" + } + ], "mountPoints": [ { "sourceVolume": "ggs-efs", diff --git a/.github/workflows/aws_deploy.yml b/.github/workflows/aws_deploy.yml index 73d005178..730ea728f 100644 --- a/.github/workflows/aws_deploy.yml +++ b/.github/workflows/aws_deploy.yml @@ -114,6 +114,7 @@ jobs: sed -i 's//${{ secrets.AWS_ACCOUNT_ID }}/g' ${{ inputs.task-definition-path }} sed -i 's//${{ secrets.EFS_FILE_SYSTEM_ID }}/g' ${{ inputs.task-definition-path }} sed -i 's//${{ secrets.DD_API_KEY }}/g' ${{ inputs.task-definition-path }} + sed -i 's//${{ github.sha }}/g' ${{ inputs.task-definition-path }} - name: Replace variables in appspec run: | From 8494f908c4af6d620a0618489f7fcfa0f684d922 Mon Sep 17 00:00:00 2001 From: Timothee Groleau Date: Tue, 9 Apr 2024 10:31:05 +0800 Subject: [PATCH 06/14] fix: name auth routers (#1278) --- src/middleware/authorization.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/middleware/authorization.ts b/src/middleware/authorization.ts index da602c3f0..cd1ffe507 100644 --- a/src/middleware/authorization.ts +++ b/src/middleware/authorization.ts @@ -5,6 +5,7 @@ import { ForbiddenError } from "@errors/ForbiddenError" import UserWithSiteSessionData from "@classes/UserWithSiteSessionData" import { RequestHandler } from "@root/types" +import { nameAnonymousMethods } from "@root/utils/apm-utils" import AuthorizationMiddlewareService from "@services/middlewareServices/AuthorizationMiddlewareService" export class AuthorizationMiddleware { @@ -18,6 +19,7 @@ export class AuthorizationMiddleware { this.authorizationMiddlewareService = authorizationMiddlewareService // We need to bind all methods because we don't invoke them from the class directly autoBind(this) + nameAnonymousMethods(this) } // Allows access only to users using email login From 484756b5708ba50171ec6ff3a4882a51a501a506 Mon Sep 17 00:00:00 2001 From: seaerchin <44049504+seaerchin@users.noreply.github.com> Date: Tue, 9 Apr 2024 12:03:00 +0800 Subject: [PATCH 07/14] feat(task def): update env (#1282) --- .aws/deploy/backend-task-definition.prod.json | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.aws/deploy/backend-task-definition.prod.json b/.aws/deploy/backend-task-definition.prod.json index 47d8b56a4..40199691b 100644 --- a/.aws/deploy/backend-task-definition.prod.json +++ b/.aws/deploy/backend-task-definition.prod.json @@ -10,7 +10,17 @@ } ], "essential": true, - "environment": [{ "name": "ENV_TYPE", "value": "PROD" }], + "environment": [ + { "name": "ENV_TYPE", "value": "PROD" }, + { + "name": "DD_GIT_COMMIT_SHA", + "value": "" + }, + { + "name": "DD_GIT_REPOSITORY_URL", + "value": "github.com/isomerpages/isomercms-backend" + } + ], "mountPoints": [ { "sourceVolume": "ggs-efs", From 924974adbb00e21f328875f2512a67a7b28dc157 Mon Sep 17 00:00:00 2001 From: Alexander Lee Date: Tue, 9 Apr 2024 12:26:20 +0800 Subject: [PATCH 08/14] chore: change error handler to use logger.error (#1284) --- src/middleware/errorHandler.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/middleware/errorHandler.js b/src/middleware/errorHandler.js index 1b725b277..2e880a1e0 100644 --- a/src/middleware/errorHandler.js +++ b/src/middleware/errorHandler.js @@ -31,7 +31,7 @@ function errorHandler(err, req, res, next) { // Error handling for custom errors if (err.isIsomerError) { - logger.info(errMsg) + logger.error(errMsg) if (err.isV2Err) { return res.status(err.status).json({ error: IsomerError.toExternalRepresentation(err), @@ -47,7 +47,7 @@ function errorHandler(err, req, res, next) { } if (err.name === "PayloadTooLargeError") { // Error thrown by large payload is done by express - logger.info(errMsg) + logger.error(errMsg) return res.status(413).json({ error: { name: err.name, @@ -56,7 +56,7 @@ function errorHandler(err, req, res, next) { }, }) } - logger.info(`Unrecognized internal server error: ${errMsg}`) + logger.error(`Unrecognized internal server error: ${errMsg}`) return res.status(500).json({ error: { code: 500, From d3b1c9cebe6ceaec68326059725cf416eb747923 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 9 Apr 2024 07:48:05 +0000 Subject: [PATCH 09/14] build(deps-dev): bump @types/aws-lambda from 8.10.132 to 8.10.137 (#1286) Bumps [@types/aws-lambda](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/aws-lambda) from 8.10.132 to 8.10.137. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/aws-lambda) --- updated-dependencies: - dependency-name: "@types/aws-lambda" dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index c7453ebbe..2063c14d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -95,7 +95,7 @@ "devDependencies": { "@octokit/types": "^6.35.0", "@tsconfig/recommended": "^1.0.1", - "@types/aws-lambda": "^8.10.106", + "@types/aws-lambda": "^8.10.137", "@types/bcryptjs": "^2.4.4", "@types/bluebird": "^3.5.38", "@types/convict": "^6.1.1", @@ -5100,9 +5100,9 @@ "integrity": "sha512-ebDJ9b0e702Yr7pWgB0jzm+CX4Srzz8RcXtLJDJB+BSccqMa36uyH/zUsSYao5+BD1ytv3k3rPYCq4mAE1hsXA==" }, "node_modules/@types/aws-lambda": { - "version": "8.10.132", - "resolved": "https://registry.npmjs.org/@types/aws-lambda/-/aws-lambda-8.10.132.tgz", - "integrity": "sha512-fXP6xK+f0Ir9dt4Tp2NjMmu/nOcQb8e0c8b7z7ql1xo/r4h/uJjGe+1aeH11yhbWU2wakJ5i4gtQAviu6h8OOg==", + "version": "8.10.137", + "resolved": "https://registry.npmjs.org/@types/aws-lambda/-/aws-lambda-8.10.137.tgz", + "integrity": "sha512-YNFwzVarXAOXkjuFxONyDw1vgRNzyH8AuyN19s0bM+ChSu/bzxb5XPxYFLXoqoM+tvgzwR3k7fXcEOW125yJxg==", "dev": true }, "node_modules/@types/babel__core": { diff --git a/package.json b/package.json index d136fb9c7..de0a3ee2a 100644 --- a/package.json +++ b/package.json @@ -113,7 +113,7 @@ "devDependencies": { "@octokit/types": "^6.35.0", "@tsconfig/recommended": "^1.0.1", - "@types/aws-lambda": "^8.10.106", + "@types/aws-lambda": "^8.10.137", "@types/bcryptjs": "^2.4.4", "@types/bluebird": "^3.5.38", "@types/convict": "^6.1.1", From 0a92823615143bb9be40702f1b1668f03a8efd26 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 9 Apr 2024 07:54:24 +0000 Subject: [PATCH 10/14] build(deps): bump js-base64 from 3.7.6 to 3.7.7 (#1288) Bumps [js-base64](https://github.com/dankogai/js-base64) from 3.7.6 to 3.7.7. - [Commits](https://github.com/dankogai/js-base64/compare/3.7.6...3.7.7) --- updated-dependencies: - dependency-name: js-base64 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2063c14d8..2638ce36e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -57,7 +57,7 @@ "isomorphic-dompurify": "^0.27.0", "isomorphic-git": "^1.18.2", "joi": "^17.4.0", - "js-base64": "^3.7.5", + "js-base64": "^3.7.7", "jsdom": "^16.7.0", "jsonwebtoken": "^9.0.0", "lodash": "^4.17.21", @@ -14161,9 +14161,9 @@ } }, "node_modules/js-base64": { - "version": "3.7.6", - "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.6.tgz", - "integrity": "sha512-NPrWuHFxFUknr1KqJRDgUQPexQF0uIJWjeT+2KjEePhitQxQEx5EJBG1lVn5/hc8aLycTpXrDOgPQ6Zq+EDiTA==" + "version": "3.7.7", + "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.7.tgz", + "integrity": "sha512-7rCnleh0z2CkXhH67J8K1Ytz0b2Y+yxTPL+/KOJoa20hfnVQ/3/T6W/KflYI4bRHRagNeXeU2bkNGI3v1oS/lw==" }, "node_modules/js-tokens": { "version": "4.0.0", diff --git a/package.json b/package.json index de0a3ee2a..71c59d8ab 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,7 @@ "isomorphic-dompurify": "^0.27.0", "isomorphic-git": "^1.18.2", "joi": "^17.4.0", - "js-base64": "^3.7.5", + "js-base64": "^3.7.7", "jsdom": "^16.7.0", "jsonwebtoken": "^9.0.0", "lodash": "^4.17.21", From c96ec0e84e8baa2dd61e61b27219053a457c788a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 9 Apr 2024 08:01:11 +0000 Subject: [PATCH 11/14] build(deps): bump @aws-sdk/client-amplify from 3.540.0 to 3.549.0 (#1289) Bumps [@aws-sdk/client-amplify](https://github.com/aws/aws-sdk-js-v3/tree/HEAD/clients/client-amplify) from 3.540.0 to 3.549.0. - [Release notes](https://github.com/aws/aws-sdk-js-v3/releases) - [Changelog](https://github.com/aws/aws-sdk-js-v3/blob/main/clients/client-amplify/CHANGELOG.md) - [Commits](https://github.com/aws/aws-sdk-js-v3/commits/v3.549.0/clients/client-amplify) --- updated-dependencies: - dependency-name: "@aws-sdk/client-amplify" dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 342 ++++++++++++++++++++++++++++++++++++++++++---- package.json | 2 +- 2 files changed, 313 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2638ce36e..4575ceed7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.77.0", "hasInstallScript": true, "dependencies": { - "@aws-sdk/client-amplify": "^3.521.0", + "@aws-sdk/client-amplify": "^3.549.0", "@aws-sdk/client-cloudwatch-logs": "^3.521.0", "@aws-sdk/client-dynamodb": "^3.521.0", "@aws-sdk/client-secrets-manager": "^3.389.0", @@ -257,15 +257,15 @@ "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" }, "node_modules/@aws-sdk/client-amplify": { - "version": "3.540.0", - "resolved": "https://registry.npmjs.org/@aws-sdk/client-amplify/-/client-amplify-3.540.0.tgz", - "integrity": "sha512-Jb49iCe9wQYcVigftbbVwP0CG7i2Ubzox3w55Ww5lTqmR4KT3UqHsxL7wHvmVM0n/rCbQmJC3ufXAwVAYOIPtA==", + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-amplify/-/client-amplify-3.549.0.tgz", + "integrity": "sha512-DgV6Pu6Zv1JVCB/5A+vi4yV8x63zzD2Joih5ZlON4OqQ9GjMB9vfOn8ue1shVWTn92JM6Du3EibC8Ktx07CvlQ==", "dependencies": { "@aws-crypto/sha256-browser": "3.0.0", "@aws-crypto/sha256-js": "3.0.0", - "@aws-sdk/client-sts": "3.540.0", - "@aws-sdk/core": "3.535.0", - "@aws-sdk/credential-provider-node": "3.540.0", + "@aws-sdk/client-sts": "3.549.0", + "@aws-sdk/core": "3.549.0", + "@aws-sdk/credential-provider-node": "3.549.0", "@aws-sdk/middleware-host-header": "3.535.0", "@aws-sdk/middleware-logger": "3.535.0", "@aws-sdk/middleware-recursion-detection": "3.535.0", @@ -276,13 +276,110 @@ "@aws-sdk/util-user-agent-browser": "3.535.0", "@aws-sdk/util-user-agent-node": "3.535.0", "@smithy/config-resolver": "^2.2.0", - "@smithy/core": "^1.4.0", + "@smithy/core": "^1.4.1", "@smithy/fetch-http-handler": "^2.5.0", "@smithy/hash-node": "^2.2.0", "@smithy/invalid-dependency": "^2.2.0", "@smithy/middleware-content-length": "^2.2.0", "@smithy/middleware-endpoint": "^2.5.0", - "@smithy/middleware-retry": "^2.2.0", + "@smithy/middleware-retry": "^2.3.0", + "@smithy/middleware-serde": "^2.3.0", + "@smithy/middleware-stack": "^2.2.0", + "@smithy/node-config-provider": "^2.3.0", + "@smithy/node-http-handler": "^2.5.0", + "@smithy/protocol-http": "^3.3.0", + "@smithy/smithy-client": "^2.5.0", + "@smithy/types": "^2.12.0", + "@smithy/url-parser": "^2.2.0", + "@smithy/util-base64": "^2.3.0", + "@smithy/util-body-length-browser": "^2.2.0", + "@smithy/util-body-length-node": "^2.3.0", + "@smithy/util-defaults-mode-browser": "^2.2.0", + "@smithy/util-defaults-mode-node": "^2.3.0", + "@smithy/util-endpoints": "^1.2.0", + "@smithy/util-middleware": "^2.2.0", + "@smithy/util-retry": "^2.2.0", + "@smithy/util-utf8": "^2.3.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/client-sso": { + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-sso/-/client-sso-3.549.0.tgz", + "integrity": "sha512-lz+yflOAj5Q263FlCsKpNqttaCb2NPh8jC76gVCqCt7TPxRDBYVaqg0OZYluDaETIDNJi4DwN2Azcck7ilwuPw==", + "dependencies": { + "@aws-crypto/sha256-browser": "3.0.0", + "@aws-crypto/sha256-js": "3.0.0", + "@aws-sdk/core": "3.549.0", + "@aws-sdk/middleware-host-header": "3.535.0", + "@aws-sdk/middleware-logger": "3.535.0", + "@aws-sdk/middleware-recursion-detection": "3.535.0", + "@aws-sdk/middleware-user-agent": "3.540.0", + "@aws-sdk/region-config-resolver": "3.535.0", + "@aws-sdk/types": "3.535.0", + "@aws-sdk/util-endpoints": "3.540.0", + "@aws-sdk/util-user-agent-browser": "3.535.0", + "@aws-sdk/util-user-agent-node": "3.535.0", + "@smithy/config-resolver": "^2.2.0", + "@smithy/core": "^1.4.1", + "@smithy/fetch-http-handler": "^2.5.0", + "@smithy/hash-node": "^2.2.0", + "@smithy/invalid-dependency": "^2.2.0", + "@smithy/middleware-content-length": "^2.2.0", + "@smithy/middleware-endpoint": "^2.5.0", + "@smithy/middleware-retry": "^2.3.0", + "@smithy/middleware-serde": "^2.3.0", + "@smithy/middleware-stack": "^2.2.0", + "@smithy/node-config-provider": "^2.3.0", + "@smithy/node-http-handler": "^2.5.0", + "@smithy/protocol-http": "^3.3.0", + "@smithy/smithy-client": "^2.5.0", + "@smithy/types": "^2.12.0", + "@smithy/url-parser": "^2.2.0", + "@smithy/util-base64": "^2.3.0", + "@smithy/util-body-length-browser": "^2.2.0", + "@smithy/util-body-length-node": "^2.3.0", + "@smithy/util-defaults-mode-browser": "^2.2.0", + "@smithy/util-defaults-mode-node": "^2.3.0", + "@smithy/util-endpoints": "^1.2.0", + "@smithy/util-middleware": "^2.2.0", + "@smithy/util-retry": "^2.2.0", + "@smithy/util-utf8": "^2.3.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/client-sso-oidc": { + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-sso-oidc/-/client-sso-oidc-3.549.0.tgz", + "integrity": "sha512-FbB4A78ILAb8sM4TfBd+3CrQcfZIhe0gtVZNbaxpq5cJZh1K7oZ8vPfKw4do9JWkDUXPLsD9Bwz12f8/JpAb6Q==", + "dependencies": { + "@aws-crypto/sha256-browser": "3.0.0", + "@aws-crypto/sha256-js": "3.0.0", + "@aws-sdk/client-sts": "3.549.0", + "@aws-sdk/core": "3.549.0", + "@aws-sdk/middleware-host-header": "3.535.0", + "@aws-sdk/middleware-logger": "3.535.0", + "@aws-sdk/middleware-recursion-detection": "3.535.0", + "@aws-sdk/middleware-user-agent": "3.540.0", + "@aws-sdk/region-config-resolver": "3.535.0", + "@aws-sdk/types": "3.535.0", + "@aws-sdk/util-endpoints": "3.540.0", + "@aws-sdk/util-user-agent-browser": "3.535.0", + "@aws-sdk/util-user-agent-node": "3.535.0", + "@smithy/config-resolver": "^2.2.0", + "@smithy/core": "^1.4.1", + "@smithy/fetch-http-handler": "^2.5.0", + "@smithy/hash-node": "^2.2.0", + "@smithy/invalid-dependency": "^2.2.0", + "@smithy/middleware-content-length": "^2.2.0", + "@smithy/middleware-endpoint": "^2.5.0", + "@smithy/middleware-retry": "^2.3.0", "@smithy/middleware-serde": "^2.3.0", "@smithy/middleware-stack": "^2.2.0", "@smithy/node-config-provider": "^2.3.0", @@ -302,6 +399,187 @@ "@smithy/util-utf8": "^2.3.0", "tslib": "^2.6.2" }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "@aws-sdk/credential-provider-node": "^3.549.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/client-sts": { + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-sts/-/client-sts-3.549.0.tgz", + "integrity": "sha512-63IreJ598Dzvpb+6sy81KfIX5iQxnrWSEtlyeCdC2GO6gmSQVwJzc9kr5pAC83lHmlZcm/Q3KZr3XBhRQqP0og==", + "dependencies": { + "@aws-crypto/sha256-browser": "3.0.0", + "@aws-crypto/sha256-js": "3.0.0", + "@aws-sdk/core": "3.549.0", + "@aws-sdk/middleware-host-header": "3.535.0", + "@aws-sdk/middleware-logger": "3.535.0", + "@aws-sdk/middleware-recursion-detection": "3.535.0", + "@aws-sdk/middleware-user-agent": "3.540.0", + "@aws-sdk/region-config-resolver": "3.535.0", + "@aws-sdk/types": "3.535.0", + "@aws-sdk/util-endpoints": "3.540.0", + "@aws-sdk/util-user-agent-browser": "3.535.0", + "@aws-sdk/util-user-agent-node": "3.535.0", + "@smithy/config-resolver": "^2.2.0", + "@smithy/core": "^1.4.1", + "@smithy/fetch-http-handler": "^2.5.0", + "@smithy/hash-node": "^2.2.0", + "@smithy/invalid-dependency": "^2.2.0", + "@smithy/middleware-content-length": "^2.2.0", + "@smithy/middleware-endpoint": "^2.5.0", + "@smithy/middleware-retry": "^2.3.0", + "@smithy/middleware-serde": "^2.3.0", + "@smithy/middleware-stack": "^2.2.0", + "@smithy/node-config-provider": "^2.3.0", + "@smithy/node-http-handler": "^2.5.0", + "@smithy/protocol-http": "^3.3.0", + "@smithy/smithy-client": "^2.5.0", + "@smithy/types": "^2.12.0", + "@smithy/url-parser": "^2.2.0", + "@smithy/util-base64": "^2.3.0", + "@smithy/util-body-length-browser": "^2.2.0", + "@smithy/util-body-length-node": "^2.3.0", + "@smithy/util-defaults-mode-browser": "^2.2.0", + "@smithy/util-defaults-mode-node": "^2.3.0", + "@smithy/util-endpoints": "^1.2.0", + "@smithy/util-middleware": "^2.2.0", + "@smithy/util-retry": "^2.2.0", + "@smithy/util-utf8": "^2.3.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "@aws-sdk/credential-provider-node": "^3.549.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/core": { + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/core/-/core-3.549.0.tgz", + "integrity": "sha512-jC61OxJn72r/BbuDRCcluiw05Xw9eVLG0CwxQpF3RocxfxyZqlrGYaGecZ8Wy+7g/3sqGRC/Ar5eUhU1YcLx7w==", + "dependencies": { + "@smithy/core": "^1.4.1", + "@smithy/protocol-http": "^3.3.0", + "@smithy/signature-v4": "^2.2.0", + "@smithy/smithy-client": "^2.5.0", + "@smithy/types": "^2.12.0", + "fast-xml-parser": "4.2.5", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/credential-provider-http": { + "version": "3.535.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-http/-/credential-provider-http-3.535.0.tgz", + "integrity": "sha512-kdj1wCmOMZ29jSlUskRqN04S6fJ4dvt0Nq9Z32SA6wO7UG8ht6Ot9h/au/eTWJM3E1somZ7D771oK7dQt9b8yw==", + "dependencies": { + "@aws-sdk/types": "3.535.0", + "@smithy/fetch-http-handler": "^2.5.0", + "@smithy/node-http-handler": "^2.5.0", + "@smithy/property-provider": "^2.2.0", + "@smithy/protocol-http": "^3.3.0", + "@smithy/smithy-client": "^2.5.0", + "@smithy/types": "^2.12.0", + "@smithy/util-stream": "^2.2.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/credential-provider-ini": { + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-ini/-/credential-provider-ini-3.549.0.tgz", + "integrity": "sha512-k6IIrluZjQpzui5Din8fW3bFFhHaJ64XrsfYx0Ks1mb7xan84dJxmYP3tdDDmLzUeJv5h95ag88taHfjY9rakA==", + "dependencies": { + "@aws-sdk/client-sts": "3.549.0", + "@aws-sdk/credential-provider-env": "3.535.0", + "@aws-sdk/credential-provider-process": "3.535.0", + "@aws-sdk/credential-provider-sso": "3.549.0", + "@aws-sdk/credential-provider-web-identity": "3.549.0", + "@aws-sdk/types": "3.535.0", + "@smithy/credential-provider-imds": "^2.3.0", + "@smithy/property-provider": "^2.2.0", + "@smithy/shared-ini-file-loader": "^2.4.0", + "@smithy/types": "^2.12.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/credential-provider-node": { + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-node/-/credential-provider-node-3.549.0.tgz", + "integrity": "sha512-f3YgalsMuywEAVX4AUm9tojqrBdfpAac0+D320ePzas0Ntbp7ItYu9ceKIhgfzXO3No7P3QK0rCrOxL+ABTn8Q==", + "dependencies": { + "@aws-sdk/credential-provider-env": "3.535.0", + "@aws-sdk/credential-provider-http": "3.535.0", + "@aws-sdk/credential-provider-ini": "3.549.0", + "@aws-sdk/credential-provider-process": "3.535.0", + "@aws-sdk/credential-provider-sso": "3.549.0", + "@aws-sdk/credential-provider-web-identity": "3.549.0", + "@aws-sdk/types": "3.535.0", + "@smithy/credential-provider-imds": "^2.3.0", + "@smithy/property-provider": "^2.2.0", + "@smithy/shared-ini-file-loader": "^2.4.0", + "@smithy/types": "^2.12.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/credential-provider-sso": { + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-sso/-/credential-provider-sso-3.549.0.tgz", + "integrity": "sha512-BGopRKHs7W8zkoH8qmSHrjudj263kXbhVkAUPxVUz0I28+CZNBgJC/RfVCbOpzmysIQEpwSqvOv1y0k+DQzIJQ==", + "dependencies": { + "@aws-sdk/client-sso": "3.549.0", + "@aws-sdk/token-providers": "3.549.0", + "@aws-sdk/types": "3.535.0", + "@smithy/property-provider": "^2.2.0", + "@smithy/shared-ini-file-loader": "^2.4.0", + "@smithy/types": "^2.12.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/credential-provider-web-identity": { + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-web-identity/-/credential-provider-web-identity-3.549.0.tgz", + "integrity": "sha512-QzclVXPxuwSI7515l34sdvliVq5leroO8P7RQFKRgfyQKO45o1psghierwG3PgV6jlMiv78FIAGJBr/n4qZ7YA==", + "dependencies": { + "@aws-sdk/client-sts": "3.549.0", + "@aws-sdk/types": "3.535.0", + "@smithy/property-provider": "^2.2.0", + "@smithy/types": "^2.12.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@aws-sdk/client-amplify/node_modules/@aws-sdk/token-providers": { + "version": "3.549.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/token-providers/-/token-providers-3.549.0.tgz", + "integrity": "sha512-rJyeXkXknLukRFGuMQOgKnPBa+kLODJtOqEBf929SpQ96f1I6ytdndmWbB5B/OQN5Fu5DOOQUQqJypDQVl5ibQ==", + "dependencies": { + "@aws-sdk/client-sso-oidc": "3.549.0", + "@aws-sdk/types": "3.535.0", + "@smithy/property-provider": "^2.2.0", + "@smithy/shared-ini-file-loader": "^2.4.0", + "@smithy/types": "^2.12.0", + "tslib": "^2.6.2" + }, "engines": { "node": ">=14.0.0" } @@ -4488,15 +4766,15 @@ } }, "node_modules/@smithy/core": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@smithy/core/-/core-1.4.0.tgz", - "integrity": "sha512-uu9ZDI95Uij4qk+L6kyFjdk11zqBkcJ3Lv0sc6jZrqHvLyr0+oeekD3CnqMafBn/5PRI6uv6ulW3kNLRBUHeVw==", + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/@smithy/core/-/core-1.4.2.tgz", + "integrity": "sha512-2fek3I0KZHWJlRLvRTqxTEri+qV0GRHrJIoLFuBMZB4EMg4WgeBGfF0X6abnrNYpq55KJ6R4D6x4f0vLnhzinA==", "dependencies": { - "@smithy/middleware-endpoint": "^2.5.0", - "@smithy/middleware-retry": "^2.2.0", + "@smithy/middleware-endpoint": "^2.5.1", + "@smithy/middleware-retry": "^2.3.1", "@smithy/middleware-serde": "^2.3.0", "@smithy/protocol-http": "^3.3.0", - "@smithy/smithy-client": "^2.5.0", + "@smithy/smithy-client": "^2.5.1", "@smithy/types": "^2.12.0", "@smithy/util-middleware": "^2.2.0", "tslib": "^2.6.2" @@ -4642,9 +4920,9 @@ } }, "node_modules/@smithy/middleware-endpoint": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/@smithy/middleware-endpoint/-/middleware-endpoint-2.5.0.tgz", - "integrity": "sha512-OBhI9ZEAG8Xen0xsFJwwNOt44WE2CWkfYIxTognC8x42Lfsdf0VN/wCMqpdkySMDio/vts10BiovAxQp0T0faA==", + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@smithy/middleware-endpoint/-/middleware-endpoint-2.5.1.tgz", + "integrity": "sha512-1/8kFp6Fl4OsSIVTWHnNjLnTL8IqpIb/D3sTSczrKFnrE9VMNWxnrRKNvpUHOJ6zpGD5f62TPm7+17ilTJpiCQ==", "dependencies": { "@smithy/middleware-serde": "^2.3.0", "@smithy/node-config-provider": "^2.3.0", @@ -4659,28 +4937,32 @@ } }, "node_modules/@smithy/middleware-retry": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@smithy/middleware-retry/-/middleware-retry-2.2.0.tgz", - "integrity": "sha512-PsjDOLpbevgn37yJbawmfVoanru40qVA8UEf2+YA1lvOefmhuhL6ZbKtGsLAWDRnE1OlAmedsbA/htH6iSZjNA==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/@smithy/middleware-retry/-/middleware-retry-2.3.1.tgz", + "integrity": "sha512-P2bGufFpFdYcWvqpyqqmalRtwFUNUA8vHjJR5iGqbfR6mp65qKOLcUd6lTr4S9Gn/enynSrSf3p3FVgVAf6bXA==", "dependencies": { "@smithy/node-config-provider": "^2.3.0", "@smithy/protocol-http": "^3.3.0", "@smithy/service-error-classification": "^2.1.5", - "@smithy/smithy-client": "^2.5.0", + "@smithy/smithy-client": "^2.5.1", "@smithy/types": "^2.12.0", "@smithy/util-middleware": "^2.2.0", "@smithy/util-retry": "^2.2.0", "tslib": "^2.6.2", - "uuid": "^8.3.2" + "uuid": "^9.0.1" }, "engines": { "node": ">=14.0.0" } }, "node_modules/@smithy/middleware-retry/node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], "bin": { "uuid": "dist/bin/uuid" } @@ -4829,11 +5111,11 @@ } }, "node_modules/@smithy/smithy-client": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/@smithy/smithy-client/-/smithy-client-2.5.0.tgz", - "integrity": "sha512-DDXWHWdimtS3y/Kw1Jo46KQ0ZYsDKcldFynQERUGBPDpkW1lXOTHy491ALHjwfiBQvzsVKVxl5+ocXNIgJuX4g==", + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@smithy/smithy-client/-/smithy-client-2.5.1.tgz", + "integrity": "sha512-jrbSQrYCho0yDaaf92qWgd+7nAeap5LtHTI51KXqmpIFCceKU3K9+vIVTUH72bOJngBMqa4kyu1VJhRcSrk/CQ==", "dependencies": { - "@smithy/middleware-endpoint": "^2.5.0", + "@smithy/middleware-endpoint": "^2.5.1", "@smithy/middleware-stack": "^2.2.0", "@smithy/protocol-http": "^3.3.0", "@smithy/types": "^2.12.0", diff --git a/package.json b/package.json index 71c59d8ab..066caca7f 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "postinstall": "patch-package" }, "dependencies": { - "@aws-sdk/client-amplify": "^3.521.0", + "@aws-sdk/client-amplify": "^3.549.0", "@aws-sdk/client-cloudwatch-logs": "^3.521.0", "@aws-sdk/client-dynamodb": "^3.521.0", "@aws-sdk/client-secrets-manager": "^3.389.0", From d7503620b907c78e3e96fb2716ac297d8d20da5f Mon Sep 17 00:00:00 2001 From: Timothee Groleau Date: Thu, 11 Apr 2024 09:08:27 +0800 Subject: [PATCH 12/14] refactor(UserService): simplify login by using findOrCreate() (#1281) * refactor: simplify code by using findOrCreate() * fix: update user service login tests * feat: dont run the date update in the transaction * fix: fix test + test for update flow * fix: use the correct method upsert() --- src/services/identity/UsersService.ts | 36 +++++----------- .../identity/__tests__/UsersService.spec.ts | 43 ++++--------------- 2 files changed, 18 insertions(+), 61 deletions(-) diff --git a/src/services/identity/UsersService.ts b/src/services/identity/UsersService.ts index 7e621291b..b2e2352fb 100644 --- a/src/services/identity/UsersService.ts +++ b/src/services/identity/UsersService.ts @@ -136,37 +136,21 @@ class UsersService { } async login(githubId: string): Promise { - return this.sequelize.transaction(async (transaction) => { - // NOTE: The service's findOrCreate is not being used here as this requires an explicit transaction - let user = await this.repository.findOne({ - where: { githubId }, - transaction, - }) - - if (!user) { - user = await this.repository.create({ - githubId, - transaction, - }) - } - - user.lastLoggedIn = new Date() - return user.save({ transaction }) + const [user] = await this.repository.upsert({ + githubId, + lastLoggedIn: new Date(), }) + + return user } async loginWithEmail(email: string): Promise { - const parsedEmail = email.toLowerCase() - return this.sequelize.transaction(async (transaction) => { - // NOTE: The service's findOrCreate is not being used here as this requires an explicit transaction - const [user] = await this.repository.findOrCreate({ - where: { email: parsedEmail }, - transaction, - }) - user.lastLoggedIn = new Date() - - return user.save({ transaction }) + const [user] = await this.repository.upsert({ + email: email.toLowerCase(), + lastLoggedIn: new Date(), }) + + return user } async canSendEmailOtp(email: string) { diff --git a/src/services/identity/__tests__/UsersService.spec.ts b/src/services/identity/__tests__/UsersService.spec.ts index c74397dd1..c0f8a0537 100644 --- a/src/services/identity/__tests__/UsersService.spec.ts +++ b/src/services/identity/__tests__/UsersService.spec.ts @@ -26,6 +26,8 @@ const MockRepository = { findOne: jest.fn(), update: jest.fn(), create: jest.fn(), + findOrCreate: jest.fn(), + upsert: jest.fn(), } const MockSequelize = { @@ -99,52 +101,23 @@ describe("User Service", () => { }) }) - it("should return the result of calling the underlying `findOne` method on the db model when the user exists and set the lastLoggedIn", async () => { + it("should call `upsert` on the db model and set the lastLoggedIn", async () => { // Arrange + const startTime = Date.now() const mockDbUser = { - save: jest.fn().mockReturnThis(), githubId: mockGithubId, } - MockRepository.findOne.mockResolvedValue(mockDbUser) + MockRepository.upsert.mockResolvedValue([mockDbUser]) // Act const actual = await UsersService.login(mockGithubId) // Assert - expect(actual.lastLoggedIn).toBeDefined() - expect(actual.githubId).toBe(mockGithubId) - expect(MockRepository.create).not.toBeCalled() - expect(MockRepository.findOne).toBeCalledWith({ - where: { githubId: mockGithubId }, - transaction: "transaction", - }) - expect(MockSequelize.transaction).toBeCalled() - }) - - it("should call both `findOne` and `create` on the db model when the user does not exist and set the lastLoggedIn", async () => { - // Arrange - const mockDbUser = { - save: jest.fn().mockReturnThis(), - githubId: mockGithubId, - } - MockRepository.findOne.mockResolvedValue(null) - MockRepository.create.mockResolvedValue(mockDbUser) - - // Act - const actual = await UsersService.login(mockGithubId) - - // Assert - expect(actual.lastLoggedIn).toBeDefined() - expect(actual.githubId).toBe(mockGithubId) - expect(MockRepository.create).toBeCalledWith({ + expect(MockRepository.upsert).toBeCalledWith({ githubId: mockGithubId, - transaction: "transaction", + lastLoggedIn: expect.any(Date), }) - expect(MockRepository.findOne).toBeCalledWith({ - where: { githubId: mockGithubId }, - transaction: "transaction", - }) - expect(MockSequelize.transaction).toBeCalled() + expect(actual.githubId).toBe(mockGithubId) }) it("should allow whitelisted emails", async () => { From 67f0660ca9785773ff32b4e95b3f3c4c13f5631e Mon Sep 17 00:00:00 2001 From: Timothee Groleau Date: Thu, 11 Apr 2024 09:25:04 +0800 Subject: [PATCH 13/14] refactor(OTP): simplify code by using upsert() (#1283) * refactor: simplify code and unify behaviour * fix: use the correct method upsert --- src/services/identity/UsersService.ts | 53 ++++++--------------------- 1 file changed, 12 insertions(+), 41 deletions(-) diff --git a/src/services/identity/UsersService.ts b/src/services/identity/UsersService.ts index b2e2352fb..6a1cfbf9f 100644 --- a/src/services/identity/UsersService.ts +++ b/src/services/identity/UsersService.ts @@ -171,23 +171,16 @@ class UsersService { } async sendEmailOtp(email: string) { - const parsedEmail = email.toLowerCase() + const normalizedEmail = email.toLowerCase() const { otp, hashedOtp } = await this.otpService.generateLoginOtpWithHash() // Reset attempts to login - const otpEntry = await this.otpRepository.findOne({ - where: { email: parsedEmail }, + await this.otpRepository.upsert({ + email: normalizedEmail, + hashedOtp, + attempts: 0, + expiresAt: this.getOtpExpiry(), }) - if (!otpEntry) { - // create new entry - await this.createOtpEntry(parsedEmail, OtpType.Email, hashedOtp) - } else { - await otpEntry?.update({ - hashedOtp, - attempts: 0, - expiresAt: this.getOtpExpiry(), - }) - } const subject = "One-Time Password (OTP) for IsomerCMS" const html = `

Your OTP is ${otp}. It will expire in ${milliSecondsToMinutes( @@ -195,21 +188,19 @@ class UsersService { )} minutes. Please use this to verify your email address.

If your OTP does not work, please request for a new OTP.

IsomerCMS Support Team

` - await this.mailer.sendMail(parsedEmail, subject, html) + await this.mailer.sendMail(normalizedEmail, subject, html) } async sendSmsOtp(mobileNumber: string) { const { otp, hashedOtp } = await this.otpService.generateLoginOtpWithHash() // Reset attempts to login - const otpEntry = await this.otpRepository.findOne({ - where: { mobileNumber }, + await this.otpRepository.upsert({ + mobileNumber, + hashedOtp, + attempts: 0, + expiresAt: this.getOtpExpiry(), }) - if (!otpEntry) { - await this.createOtpEntry(mobileNumber, OtpType.Mobile, hashedOtp) - } else { - await otpEntry?.update({ hashedOtp, attempts: 0 }) - } const message = `Your OTP is ${otp}. It will expire in ${milliSecondsToMinutes( OTP_EXPIRY @@ -417,26 +408,6 @@ class UsersService { private getOtpExpiry() { return new Date(Date.now() + OTP_EXPIRY) } - - private async createOtpEntry( - key: string, - keyType: OtpType, - hashedOtp: string - ) { - if (keyType === OtpType.Email) { - await this.otpRepository.create({ - email: key.toLowerCase(), - hashedOtp, - expiresAt: this.getOtpExpiry(), - }) - } else { - await this.otpRepository.create({ - mobileNumber: key, - hashedOtp, - expiresAt: this.getOtpExpiry(), - }) - } - } } export default UsersService From 72f39bdc25f6afe82021ebbc630c6d3850ece1ae Mon Sep 17 00:00:00 2001 From: Harish V Date: Thu, 11 Apr 2024 15:11:54 +0800 Subject: [PATCH 14/14] chore: bump version to v0.78.0 --- CHANGELOG.md | 20 ++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ceafb050..376283d1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,30 @@ All notable changes to this project will be documented in this file. Dates are d Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +#### [v0.78.0](https://github.com/isomerpages/isomercms-backend/compare/v0.77.0...v0.78.0) + +- refactor(OTP): simplify code by using upsert() [`#1283`](https://github.com/isomerpages/isomercms-backend/pull/1283) +- refactor(UserService): simplify login by using findOrCreate() [`#1281`](https://github.com/isomerpages/isomercms-backend/pull/1281) +- build(deps): bump @aws-sdk/client-amplify from 3.540.0 to 3.549.0 [`#1289`](https://github.com/isomerpages/isomercms-backend/pull/1289) +- build(deps): bump js-base64 from 3.7.6 to 3.7.7 [`#1288`](https://github.com/isomerpages/isomercms-backend/pull/1288) +- build(deps-dev): bump @types/aws-lambda from 8.10.132 to 8.10.137 [`#1286`](https://github.com/isomerpages/isomercms-backend/pull/1286) +- chore: change error handler to use logger.error [`#1284`](https://github.com/isomerpages/isomercms-backend/pull/1284) +- feat(task def): update env [`#1282`](https://github.com/isomerpages/isomercms-backend/pull/1282) +- fix: name auth routers [`#1278`](https://github.com/isomerpages/isomercms-backend/pull/1278) +- feat(dd): add source code linkage [`#1279`](https://github.com/isomerpages/isomercms-backend/pull/1279) +- fix(mergify): fix mistake [`#1280`](https://github.com/isomerpages/isomercms-backend/pull/1280) +- build(deps): bump base-64 from 0.1.0 to 1.0.0 [`#1254`](https://github.com/isomerpages/isomercms-backend/pull/1254) +- build(deps): bump marked from 11.2.0 to 12.0.1 [`#1219`](https://github.com/isomerpages/isomercms-backend/pull/1219) +- chore(ci): enhance mergify [`#1245`](https://github.com/isomerpages/isomercms-backend/pull/1245) +- backport v0.77.0 [`#1277`](https://github.com/isomerpages/isomercms-backend/pull/1277) + #### [v0.77.0](https://github.com/isomerpages/isomercms-backend/compare/v0.76.0...v0.77.0) +> 8 April 2024 + - chore: index notifications table [`#1275`](https://github.com/isomerpages/isomercms-backend/pull/1275) - backport v0.76.0 [`#1274`](https://github.com/isomerpages/isomercms-backend/pull/1274) +- chore: bump version to v0.77.0 [`29def1e`](https://github.com/isomerpages/isomercms-backend/commit/29def1ef0da032d137c09c27e0e4d3b790afc2aa) #### [v0.76.0](https://github.com/isomerpages/isomercms-backend/compare/v0.75.0...v0.76.0) diff --git a/package-lock.json b/package-lock.json index 4575ceed7..b8fa017fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "isomercms", - "version": "0.77.0", + "version": "0.78.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "isomercms", - "version": "0.77.0", + "version": "0.78.0", "hasInstallScript": true, "dependencies": { "@aws-sdk/client-amplify": "^3.549.0", diff --git a/package.json b/package.json index 066caca7f..5addc62de 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "isomercms", - "version": "0.77.0", + "version": "0.78.0", "private": true, "scripts": { "build": "tsc -p tsconfig.build.json",