Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function action({ params, request }: ActionFunctionArgs) {
});

if (updateResult instanceof Error) {
return data({ error: updateResult.message }, { status: 500 });
return data({ error: updateResult.message }, { status: 400 });
}

toast(session, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,33 @@ export async function updateMemberEmail({ email, id }: UpdateMemberEmailInput) {
return new Error('Could not find member.');
}

if (email === student.previousEmail) {
if (email.toLowerCase() === student.previousEmail.toLowerCase()) {
return new Error('This is already the email of this student.');
}

const existingEmail = await db
.selectFrom('studentEmails')
.select(['email', 'studentId'])
.where('email', 'ilike', email)
.where('studentId', '!=', student.id)
.executeTakeFirst();

if (existingEmail) {
return new Error('This emails belongs to another member.');
if (existingEmail && existingEmail.studentId !== student.id) {
return new Error('This email belongs to another member.');
}

// Reuse the stored spelling when promoting an existing secondary email.
email = existingEmail?.email ?? email;

await db.transaction().execute(async (trx) => {
await trx
.insertInto('studentEmails')
.values({
email,
studentId: student.id,
})
.execute();
if (!existingEmail) {
await trx
.insertInto('studentEmails')
.values({
email,
studentId: student.id,
})
.execute();
}

await trx
.updateTable('students')
Expand All @@ -48,6 +53,7 @@ export async function updateMemberEmail({ email, id }: UpdateMemberEmailInput) {
await trx
.deleteFrom('studentEmails')
.where('email', 'ilike', student.previousEmail)
.where('studentId', '=', student.id)
.execute();
});

Expand Down
Loading