Skip to content

Commit

Permalink
Merge pull request #345 from pagopa/IOPID-1448-409-conflict
Browse files Browse the repository at this point in the history
[IOPID-1448] chore: add 409 management
  • Loading branch information
shadowsheep1 authored Feb 13, 2024
2 parents a630f39 + d59d4e7 commit 202bab7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/persistence/profile/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export const updateProfile = (req: Request): ProfileOperationsType["post"] => {
if (emailAlreadyTakenTestCheck) {
return emailAlreadyTakenTestCheck;
}

const profileVersionCheck = checkProfileVersion(maybeProfileToUpdate);
if (profileVersionCheck) {
return profileVersionCheck;
}
const clientProfileIncreased: Profile = {
...maybeProfileToUpdate.right,
version: parseInt(req.body.version, 10) + 1
Expand Down Expand Up @@ -108,6 +111,19 @@ export const setProfileEmailAlreadyTaken = (value: boolean) => {
};
};

const checkProfileVersion = (profile: ReturnType<typeof Profile.decode>) => {
if (E.isRight(profile) && profile.right.version !== currentProfile.version) {
return {
status: profileProblemsList.conflict.status,
payload: getProblemJson(
profileProblemsList.conflict.status,
profileProblemsList.conflict.detail,
profileProblemsList.conflict.detail
)
};
}
};

const handleAlreadyTakenTestEmail = (
email?: string
): CustomResponse | undefined => {
Expand All @@ -127,11 +143,15 @@ const handleAlreadyTakenTestEmail = (
};

// MARK: Problems
type ProfileProblemDetail = "Precondition Failed" | "BodyMalformed";
type ProfileProblemDetail =
| "Precondition Failed"
| "BodyMalformed"
| "Conflict";

type ProfileProblems = {
emailAlreadyTaken: ResponseProblem<ProfileProblemDetail>;
bodyMalformed: ResponseProblem<ProfileProblemDetail>;
conflict: ResponseProblem<ProfileProblemDetail>;
};

const profileProblemsList: ProfileProblems = {
Expand All @@ -142,6 +162,10 @@ const profileProblemsList: ProfileProblems = {
bodyMalformed: {
status: 400,
detail: "BodyMalformed"
},
conflict: {
status: 409,
detail: "Conflict"
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/routers/__tests__/profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ it("profile should return a valid updated profile (version increased)", async ()
is_email_enabled: true,
is_webhook_enabled: true,
email: "[email protected]" as EmailAddress,
version: 5
version: 1
};
const response = await request
.post(`${basePath}/profile`)
Expand Down

0 comments on commit 202bab7

Please sign in to comment.