diff --git a/apps/web/src/actions/user-profile-mod.ts b/apps/web/src/actions/user-profile-mod.ts index 2cdaacf9..6a0fb7e9 100644 --- a/apps/web/src/actions/user-profile-mod.ts +++ b/apps/web/src/actions/user-profile-mod.ts @@ -10,7 +10,10 @@ import { decodeBase64AsFile } from "@/lib/utils/shared/files"; import { revalidatePath } from "next/cache"; import { getUser, getUserByTag } from "db/functions"; import { RegistrationSettingsFormValidator } from "@/validators/shared/RegistrationSettingsForm"; +import { UNIQUE_KEY_CONSTRAINT_VIOLATION_CODE } from "@/lib/constants"; import c from "config"; +import { DatabaseError } from "db/types"; + export const modifyRegistrationData = authenticatedAction .schema(RegistrationSettingsFormValidator) @@ -41,6 +44,7 @@ export const modifyRegistrationData = authenticatedAction }, ctx: { userId }, }) => { + await Promise.all([ // attempts to update both tables with Promise.all db @@ -75,6 +79,7 @@ export const modifyRegistrationData = authenticatedAction }) .where(eq(userHackerData.clerkID, userId)), ]).catch(async (err) => { + console.log(`Error occured at modify registration data: ${err}`); // If there's an error return { success: false, @@ -114,7 +119,6 @@ export const deleteResume = authenticatedAction }), ) .action(async ({ parsedInput: { oldFileLink } }) => { - console.log(oldFileLink); if (oldFileLink === c.noResumeProvidedURL) return null; await del(oldFileLink); }); @@ -133,14 +137,10 @@ export const modifyProfileData = authenticatedAction parsedInput: { bio, discord, pronouns, skills }, ctx: { userId }, }) => { - const user = await getUser(userId); - if (!user) { - throw new Error("User not found"); - } await db .update(userCommonData) .set({ pronouns, bio, skills, discord }) - .where(eq(userCommonData.clerkID, user.clerkID)); + .where(eq(userCommonData.clerkID, userId)); return { success: true, newPronouns: pronouns, @@ -171,18 +171,9 @@ export const modifyAccountSettings = authenticatedAction }, ctx: { userId }, }) => { - const user = await getUser(userId); - if (!user) throw new Error("User not found"); - let oldHackerTag = user.hackerTag; // change when hackertag is not PK on profileData table - if (oldHackerTag != hackerTag) - if (await getUserByTag(hackerTag)) - //if hackertag changed - // copied from /api/registration/create - return { - success: false, - message: "hackertag_not_unique", - }; - await db + + try{ + await db .update(userCommonData) .set({ firstName, @@ -191,6 +182,16 @@ export const modifyAccountSettings = authenticatedAction isSearchable: hasSearchableProfile, }) .where(eq(userCommonData.clerkID, userId)); + } + catch(err){ + if (err instanceof DatabaseError && err.code === UNIQUE_KEY_CONSTRAINT_VIOLATION_CODE) { + return { + success: false, + message: "hackertag_not_unique", + }; + } + throw err; + } return { success: true, newFirstName: firstName, diff --git a/apps/web/src/components/settings/RegistrationForm/RegisterFormSettings.tsx b/apps/web/src/components/settings/RegistrationForm/RegisterFormSettings.tsx index d42aa52f..768758de 100644 --- a/apps/web/src/components/settings/RegistrationForm/RegisterFormSettings.tsx +++ b/apps/web/src/components/settings/RegistrationForm/RegisterFormSettings.tsx @@ -61,29 +61,29 @@ interface RegistrationFormSettingsProps { export default function RegisterFormSettings({ user, - data, + data: originalData, }: RegistrationFormSettingsProps) { const form = useForm>({ resolver: zodResolver(RegistrationSettingsFormValidator), defaultValues: { - hackathonsAttended: data.hackathonsAttended, + hackathonsAttended: originalData.hackathonsAttended, dietaryRestrictions: user.dietRestrictions as any, - isEmailable: data.isEmailable, + isEmailable: originalData.isEmailable, accommodationNote: user.accommodationNote || "", age: user.age, ethnicity: user.ethnicity as any, gender: user.gender as any, - major: data.major, - github: data.GitHub ?? "", - heardAboutEvent: data.heardFrom as any, - levelOfStudy: data.levelOfStudy as any, - linkedin: data.LinkedIn ?? "", - personalWebsite: data.PersonalWebsite ?? "", + major: originalData.major, + github: originalData.GitHub ?? "", + heardAboutEvent: originalData.heardFrom as any, + levelOfStudy: originalData.levelOfStudy as any, + linkedin: originalData.LinkedIn ?? "", + personalWebsite: originalData.PersonalWebsite ?? "", race: user.race as any, shirtSize: user.shirtSize as any, - schoolID: data.schoolID, - softwareBuildingExperience: data.softwareExperience as any, - university: data.university, + schoolID: originalData.schoolID, + softwareBuildingExperience: originalData.softwareExperience as any, + university: originalData.university, phoneNumber: user.phoneNumber, countryOfResidence: user.countryOfResidence, }, @@ -92,8 +92,8 @@ export default function RegisterFormSettings({ const { isSubmitSuccessful, isSubmitted, errors } = form.formState; const hasErrors = !isSubmitSuccessful && isSubmitted; const [uploadedFile, setUploadedFile] = useState(null); - let oldResumeLink: string = data.resume ?? c.noResumeProvidedURL; - let f = new File([data.resume], oldResumeLink.split("/").pop()!); + let oldResumeLink: string = originalData.resume ?? c.noResumeProvidedURL; + let f = new File([originalData.resume], oldResumeLink.split("/").pop()!); useEffect(() => { if (oldResumeLink === c.noResumeProvidedURL) setUploadedFile(null); else setUploadedFile(f); @@ -111,7 +111,7 @@ export default function RegisterFormSettings({ if (shortID === "NOT_LOCAL_SCHOOL") { form.setValue("schoolID", ""); } else { - form.setValue("schoolID", data.schoolID); + form.setValue("schoolID", originalData.schoolID); } } }, [universityValue]); @@ -131,8 +131,8 @@ export default function RegisterFormSettings({ ); newResumeLink = newBlob.url; } - - const res = runModifyRegistrationData({ + const oldResume = originalData.resume; + runModifyRegistrationData({ age: data.age, gender: data.gender, race: data.race, @@ -156,7 +156,7 @@ export default function RegisterFormSettings({ uploadedFile: newResumeLink, }); - runDeleteResume({ oldFileLink: oldResumeLink }); + runDeleteResume({ oldFileLink: oldResume }); } const { execute: runModifyRegistrationData, status: loadingState } = @@ -175,7 +175,6 @@ export default function RegisterFormSettings({ ); }, }); - const { execute: runDeleteResume } = useAction(deleteResume); const onDrop = useCallback( @@ -186,10 +185,6 @@ export default function RegisterFormSettings({ ); } if (acceptedFiles.length > 0) { - console.log( - `Got accepted file! The length of the array is ${acceptedFiles.length}.`, - ); - console.log(acceptedFiles[0]); setUploadedFile(acceptedFiles[0]); setOldFile(false); } @@ -210,8 +205,7 @@ export default function RegisterFormSettings({
+ onSubmit={form.handleSubmit(onSubmit)}>