Files
kolaytercih/src/lib/danisman-profil-db.ts
bilalgursen a3cb058a3b
All checks were successful
Deploy / deploy (push) Successful in 7m45s
feat: update app.db and BACKLOG.md with new decisions and adjustments
- Updated the app.db file to reflect recent changes in the application state.
- Modified BACKLOG.md to include new entries regarding the removal of certain wizard steps, adjustments to priority chips, and the introduction of special quota conditions for candidates.
- Added details on the decision to link priority chips to data sources and clarified the implications for user experience and data handling.
2026-10-06 17:17:49 +03:00

68 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import "server-only";
import { eq, sql } from "drizzle-orm";
import { appDb, schema } from "@/lib/appdb";
import {
PROFIL_ALANLARI,
PROFIL_SEMASI,
profilTemizle,
type DanismanProfili,
type ProfilAlani,
} from "@/lib/danisman-profil";
/** Kullanıcının danışman profili; satır yoksa boş nesne. Bozuk alanlar düşer. */
export async function profilGetir(userId: string): Promise<DanismanProfili> {
const satir = await appDb.query.danismanProfil.findFirst({
where: eq(schema.danismanProfil.userId, userId),
columns: { alanlar: true },
});
return profilTemizle(satir?.alanlar);
}
/**
* 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> {
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) {
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}`);
}
// Dizi değer (ozelDurum) JSON metni olarak bağlanır ve json() ile açılır;
// düz bağlanırsa SQLite onu tek bir dize olarak saklar.
const yeni = Array.isArray(dogrulama.data)
? sql`json(${JSON.stringify(dogrulama.data)})`
: sql`${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}, ${yeni})`, updatedAt: now },
});
}
return profilGetir(userId);
}
export async function profilSil(userId: string): Promise<void> {
await appDb
.delete(schema.danismanProfil)
.where(eq(schema.danismanProfil.userId, userId));
}