portal-auth P2(#24 #25):portal_user 模型+認證 API(不 merge,待總管審) (#51)
This commit was merged in pull request #51.
This commit is contained in:
@@ -101,14 +101,23 @@ export async function updateRecord(
|
||||
values: Record<string, string>,
|
||||
): Promise<RecordResult | null> {
|
||||
// Existing slot → entry_id + template_id for this record.
|
||||
// JOIN entries 帶回 owner_id:grow 路徑建新 entry 時要沿用 record 既有 owner_id
|
||||
//(portal-auth design §2.2 附帶修復——原本漏帶 → 孤兒 entry(owner_id=NULL),
|
||||
// owner-scoped 查詢(searchByTemplate / searchEntries)看不到該 slot 值)。
|
||||
const evRes = await db
|
||||
.prepare(`SELECT slot_name, entry_id, template_id FROM entry_values WHERE record_id = ?`)
|
||||
.prepare(
|
||||
`SELECT ev.slot_name AS slot_name, ev.entry_id AS entry_id, ev.template_id AS template_id, e.owner_id AS owner_id
|
||||
FROM entry_values ev JOIN entries e ON ev.entry_id = e.id
|
||||
WHERE ev.record_id = ?`,
|
||||
)
|
||||
.bind(recordId)
|
||||
.all<{ slot_name: string; entry_id: string; template_id: string }>();
|
||||
.all<{ slot_name: string; entry_id: string; template_id: string; owner_id: string | null }>();
|
||||
const evRows = evRes.results ?? [];
|
||||
if (evRows.length === 0) return null; // record does not exist
|
||||
|
||||
const templateId = evRows[0].template_id;
|
||||
// record 的歸屬=其既有 slot entries 的 owner_id(createRecord 寫入時同一值)。
|
||||
const recordOwnerId = evRows.find((r) => r.owner_id != null)?.owner_id ?? null;
|
||||
const slotToEntry = new Map(evRows.map((r) => [r.slot_name, r.entry_id]));
|
||||
|
||||
const tpl = await getTemplate(db, templateId);
|
||||
@@ -124,7 +133,8 @@ export async function updateRecord(
|
||||
await db.prepare(`UPDATE entries SET content = ?, updated_at = unixepoch() WHERE id = ?`).bind(content, entryId).run();
|
||||
} else {
|
||||
// valid template slot not yet on this record → grow it (create entry + link)
|
||||
const entry = await createEntry(db, { content, entry_type: 'value' });
|
||||
// owner_id 帶 record 既有歸屬(design §2.2 附帶修復,防孤兒 entry)
|
||||
const entry = await createEntry(db, { content, entry_type: 'value', owner_id: recordOwnerId });
|
||||
await db
|
||||
.prepare(`INSERT INTO entry_values (id, record_id, template_id, slot_name, entry_id) VALUES (?, ?, ?, ?, ?)`)
|
||||
.bind(uid('ev'), recordId, templateId, slot, entry.id)
|
||||
|
||||
Reference in New Issue
Block a user