Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IOPID-1448] chore: add 409 management #345

Merged
merged 9 commits into from
Feb 13, 2024
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
Loading