From b344a6082d7b23cacc0abe00491c242dc72dcdc7 Mon Sep 17 00:00:00 2001 From: Luca Cavallaro <4357400+lucacavallaro@users.noreply.github.com> Date: Mon, 18 Dec 2023 09:29:36 +0100 Subject: [PATCH] return precondition failed in update profile --- package.json | 2 +- src/controllers/profileController.ts | 2 ++ src/services/profileService.ts | 36 ++++++++++++++++++---------- yarn.lock | 8 +++---- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 4c56d7051..0e6b82e12 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "@pagopa/io-functions-commons": "28.9.0", "@pagopa/io-functions-eucovidcerts-sdk": "x", "@pagopa/io-spid-commons": "^13.3.0", - "@pagopa/ts-commons": "^12.3.0", + "@pagopa/ts-commons": "^12.4.0", "@pagopa/winston-ts": "^2.2.0", "applicationinsights": "^1.8.10", "body-parser": "^1.19.2", diff --git a/src/controllers/profileController.ts b/src/controllers/profileController.ts index 328abe220..d579b7701 100644 --- a/src/controllers/profileController.ts +++ b/src/controllers/profileController.ts @@ -8,6 +8,7 @@ import { IResponseErrorConflict, IResponseErrorInternal, IResponseErrorNotFound, + IResponseErrorPreconditionFailed, IResponseErrorTooManyRequests, IResponseErrorValidation, IResponseSuccessAccepted, @@ -76,6 +77,7 @@ export default class ProfileController { | IResponseErrorInternal | IResponseErrorConflict | IResponseErrorTooManyRequests + | IResponseErrorPreconditionFailed | IResponseSuccessJson > => withUserFromRequest(req, async (user) => diff --git a/src/services/profileService.ts b/src/services/profileService.ts index afd815bf6..7c3c81b85 100644 --- a/src/services/profileService.ts +++ b/src/services/profileService.ts @@ -7,12 +7,14 @@ import { IResponseErrorConflict, IResponseErrorInternal, IResponseErrorNotFound, + IResponseErrorPreconditionFailed, IResponseErrorTooManyRequests, IResponseSuccessAccepted, IResponseSuccessJson, ResponseErrorConflict, ResponseErrorInternal, ResponseErrorNotFound, + ResponseErrorPreconditionFailed, ResponseErrorTooManyRequests, ResponseSuccessAccepted, ResponseSuccessJson, @@ -174,6 +176,7 @@ export default class ProfileService { | IResponseErrorNotFound | IResponseErrorConflict | IResponseErrorTooManyRequests + | IResponseErrorPreconditionFailed | IResponseSuccessJson > => { const client = this.apiClient.getClient(); @@ -189,19 +192,28 @@ export default class ProfileService { fiscal_code: user.fiscal_code, }); - return withValidatedOrInternalError(validated, (response) => - response.status === 200 - ? ResponseSuccessJson(toInitializedProfile(response.value, user)) - : response.status === 404 - ? ResponseErrorNotFound("Not found", "User not found") - : response.status === 409 - ? ResponseErrorConflict( + return withValidatedOrInternalError(validated, (response) => { + switch (response.status) { + case 200: + return ResponseSuccessJson( + toInitializedProfile(response.value, user) + ); + case 404: + return ResponseErrorNotFound("Not found", "User not found"); + case 409: + return ResponseErrorConflict( response.value || "Cannot update profile with wrong version" - ) - : response.status === 429 - ? ResponseErrorTooManyRequests() - : unhandledResponseStatus(response.status) - ); + ); + case 412: + return ResponseErrorPreconditionFailed( + "The provided e-mail address is not unique" + ); + case 429: + return ResponseErrorTooManyRequests(); + default: + return unhandledResponseStatus(response.status); + } + }); }) ); }; diff --git a/yarn.lock b/yarn.lock index 3910c37fc..e8691dc6e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1123,10 +1123,10 @@ semver "^7.3.7" validator "^13.7.0" -"@pagopa/ts-commons@^12.3.0": - version "12.3.0" - resolved "https://registry.yarnpkg.com/@pagopa/ts-commons/-/ts-commons-12.3.0.tgz#54e661b71ef3b80e9a8474ba90088ffafb2c06c0" - integrity sha512-7fwd2EEhac6bmmfGdWYr5EGRjF1AwzaH3DHyg/jLcoKnQcHPWbt7tsvO0zSD/mxAbHZBrZ24zut0IK3eD3qNag== +"@pagopa/ts-commons@^12.4.0": + version "12.4.0" + resolved "https://registry.yarnpkg.com/@pagopa/ts-commons/-/ts-commons-12.4.0.tgz#58b46bd485ff0be7649cf662d9ebf0be38d8a6ff" + integrity sha512-eFDjCHmwepYPAR1+Hnm0BbXploOquZ20mnu0kKAmwLGE9Grr6WAPbjmf/5bheNWPDeD5rGUTv8ENoB2KJWOGbw== dependencies: abort-controller "^3.0.0" agentkeepalive "^4.1.4"