fix: Handle duplicate email in createUcastnik
If participant with same email exists, update their data and return existing record instead of throwing duplicate key error. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -45,6 +45,40 @@ export const getUcastnikById = async (id) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const createUcastnik = async (data) => {
|
export const createUcastnik = async (data) => {
|
||||||
|
// Check if participant with this email already exists
|
||||||
|
if (data.email) {
|
||||||
|
const [existing] = await db
|
||||||
|
.select()
|
||||||
|
.from(ucastnici)
|
||||||
|
.where(eq(ucastnici.email, data.email))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (existing) {
|
||||||
|
// Update existing participant with new data and return
|
||||||
|
const [updated] = await db
|
||||||
|
.update(ucastnici)
|
||||||
|
.set({
|
||||||
|
titul: data.titul || existing.titul,
|
||||||
|
meno: data.meno || existing.meno,
|
||||||
|
priezvisko: data.priezvisko || existing.priezvisko,
|
||||||
|
telefon: data.telefon || existing.telefon,
|
||||||
|
firma: data.firma || existing.firma,
|
||||||
|
firmaIco: data.firmaIco || existing.firmaIco,
|
||||||
|
firmaDic: data.firmaDic || existing.firmaDic,
|
||||||
|
firmaIcDph: data.firmaIcDph || existing.firmaIcDph,
|
||||||
|
firmaSidlo: data.firmaSidlo || existing.firmaSidlo,
|
||||||
|
mesto: data.mesto || existing.mesto,
|
||||||
|
ulica: data.ulica || existing.ulica,
|
||||||
|
psc: data.psc || existing.psc,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(ucastnici.id, existing.id))
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const [newUcastnik] = await db
|
const [newUcastnik] = await db
|
||||||
.insert(ucastnici)
|
.insert(ucastnici)
|
||||||
.values({
|
.values({
|
||||||
|
|||||||
Reference in New Issue
Block a user