From 2ef87ae9c3065d08a3c15a84d1c771680b26fd9d Mon Sep 17 00:00:00 2001 From: Andy Vo Date: Sat, 16 Nov 2024 22:24:40 -0500 Subject: [PATCH] add edit-user endpoint --- server/mongodb/actions/User.ts | 20 +++++++++ src/app/api/patient/edit-patient/route.ts | 51 +++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/app/api/patient/edit-patient/route.ts diff --git a/server/mongodb/actions/User.ts b/server/mongodb/actions/User.ts index 0d2896ee..10d45855 100644 --- a/server/mongodb/actions/User.ts +++ b/server/mongodb/actions/User.ts @@ -86,6 +86,26 @@ export const patientSignUp = async ( return result; }; +export const patientEdit = async ( + data: Omit< + IUser, + "chapter" | "location" | "patientDetails" | "new" | "signedUp" | "role" + >, +): Promise => { + const result = await User.findOneAndUpdate( + { email: data.email }, + { + $set: { + firstName: data.firstName, + lastName: data.lastName, + phoneNumber: data.phoneNumber, + birthDate: new Date(data.birthDate), + }, + }, + ); + return result; +}; + export const volunteerSignUp = async ( email: string, firstName: string, diff --git a/src/app/api/patient/edit-patient/route.ts b/src/app/api/patient/edit-patient/route.ts new file mode 100644 index 00000000..e3ddebcd --- /dev/null +++ b/src/app/api/patient/edit-patient/route.ts @@ -0,0 +1,51 @@ +import { getUserByEmail, patientEdit } from "@server/mongodb/actions/User"; +import APIWrapper from "@server/utils/APIWrapper"; +import { IUser } from "@/common_utils/types"; + +interface EditData { + email: string; + firstName: string; + lastName: string; + phoneNumber: string; + birthDate: string; +} + +export const POST = APIWrapper({ + config: { + requireToken: false, + }, + handler: async (req) => { + const editData: EditData = (await req.json()) as EditData; + + if (!editData) { + throw new Error("Missing request data"); + } + + if ( + editData.birthDate === undefined || + editData.email === undefined || + editData.firstName === undefined || + editData.lastName === undefined || + editData.phoneNumber === undefined + ) { + throw new Error("Missing parameter(s)"); + } + + const user = await getUserByEmail(editData.email); + if (!user) { + throw new Error("User not found."); + } + + const newSignUp = await patientEdit({ + email: editData.email, + firstName: editData.firstName, + lastName: editData.lastName, + phoneNumber: editData.phoneNumber, + birthDate: editData.birthDate, + } as Omit< + IUser, + "chapter" | "location" | "patientDetails" | "new" | "signedUp" | "role" + >); + return newSignUp; + }, +});