fix(danisman): H1 — aynı adımda iki profil_guncelle birbirini ezmesin
profilAlanYaz oku → değiştir → yaz yerine tek SQL ifadesiyle yazar (json_set / json_remove); eşzamanlı iki alan yazımı artık ikisini de tutar. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import "server-only";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { appDb, schema } from "@/lib/appdb";
|
||||
import {
|
||||
PROFIL_ALANLARI,
|
||||
PROFIL_SEMASI,
|
||||
profilTemizle,
|
||||
type DanismanProfili,
|
||||
@@ -20,33 +21,38 @@ export async function profilGetir(userId: string): Promise<DanismanProfili> {
|
||||
/**
|
||||
* Tek alanı yazar (deger null → alan silinir). Değer şemadan geçmezse hata
|
||||
* fırlatır; çağıran taraf (araç) bunu modele "kaydedilmedi" diye döner.
|
||||
*
|
||||
* Yazım tek SQL ifadesidir (json_set / json_remove): model aynı adımda iki
|
||||
* alanı birden kaydedince oku → değiştir → yaz sırası birbirini eziyordu.
|
||||
*/
|
||||
export async function profilAlanYaz(
|
||||
userId: string,
|
||||
alan: ProfilAlani,
|
||||
deger: unknown,
|
||||
): Promise<DanismanProfili> {
|
||||
const mevcut = await profilGetir(userId);
|
||||
const yeni: Record<string, unknown> = { ...mevcut };
|
||||
if (!PROFIL_ALANLARI.includes(alan)) throw new Error(`Bilinmeyen alan: ${alan}`);
|
||||
const kolon = schema.danismanProfil.alanlar;
|
||||
const yol = `$.${alan}`;
|
||||
const now = new Date();
|
||||
if (deger === null || deger === undefined) {
|
||||
delete yeni[alan];
|
||||
await appDb
|
||||
.update(schema.danismanProfil)
|
||||
.set({ alanlar: sql`json_remove(${kolon}, ${yol})`, updatedAt: now })
|
||||
.where(eq(schema.danismanProfil.userId, userId));
|
||||
} else {
|
||||
const dogrulama = PROFIL_SEMASI.shape[alan].safeParse(deger);
|
||||
if (!dogrulama.success) {
|
||||
throw new Error(`Geçersiz değer: ${alan}`);
|
||||
}
|
||||
yeni[alan] = dogrulama.data;
|
||||
await appDb
|
||||
.insert(schema.danismanProfil)
|
||||
.values({ userId, alanlar: { [alan]: dogrulama.data }, updatedAt: now })
|
||||
.onConflictDoUpdate({
|
||||
target: schema.danismanProfil.userId,
|
||||
set: { alanlar: sql`json_set(${kolon}, ${yol}, ${dogrulama.data})`, updatedAt: now },
|
||||
});
|
||||
}
|
||||
const alanlar = PROFIL_SEMASI.parse(yeni);
|
||||
const now = new Date();
|
||||
await appDb
|
||||
.insert(schema.danismanProfil)
|
||||
.values({ userId, alanlar, updatedAt: now })
|
||||
.onConflictDoUpdate({
|
||||
target: schema.danismanProfil.userId,
|
||||
set: { alanlar, updatedAt: now },
|
||||
});
|
||||
return alanlar;
|
||||
return profilGetir(userId);
|
||||
}
|
||||
|
||||
export async function profilSil(userId: string): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user