Skip to content

Commit

Permalink
add edit-user endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
andyvo2004 committed Nov 17, 2024
1 parent 42f513c commit 2ef87ae
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
20 changes: 20 additions & 0 deletions server/mongodb/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ export const patientSignUp = async (
return result;
};

export const patientEdit = async (
data: Omit<
IUser,
"chapter" | "location" | "patientDetails" | "new" | "signedUp" | "role"
>,
): Promise<IUser | null> => {
const result = await User.findOneAndUpdate<IUser>(
{ 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,
Expand Down
51 changes: 51 additions & 0 deletions src/app/api/patient/edit-patient/route.ts
Original file line number Diff line number Diff line change
@@ -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;
},
});

0 comments on commit 2ef87ae

Please sign in to comment.