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
14 changes: 11 additions & 3 deletions src/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ export class VConQueries {
}
}

// Insert main vcon
// Upsert main vcon — idempotent on re-submission of same UUID
// Set id = uuid so they match (id is the PK, uuid is the vCon document UUID)
const { data: vconData, error: vconError } = await this.supabase
.from('vcons')
.insert({
.upsert({
id: vcon.uuid, // Explicitly set id to match uuid
uuid: vcon.uuid,
vcon_version: vcon.vcon ?? '0.3.0',
Expand All @@ -85,7 +85,7 @@ export class VConQueries {
redacted: vcon.redacted || {},
appended: vcon.appended || {}, // ✅ Added per spec
tenant_id: tenantId, // ✅ Added for RLS multi-tenant support
})
}, { onConflict: 'id' })
.select('id, uuid')
.single();

Expand All @@ -97,6 +97,14 @@ export class VConQueries {
throw vconError;
}

// Delete existing child rows so re-submission replaces them cleanly
await Promise.all([
this.supabase.from('parties').delete().eq('vcon_id', vconData.id),
this.supabase.from('dialog').delete().eq('vcon_id', vconData.id),
this.supabase.from('analysis').delete().eq('vcon_id', vconData.id),
this.supabase.from('attachments').delete().eq('vcon_id', vconData.id),
]);

// Insert parties
if (vcon.parties.length > 0) {
const partiesData = vcon.parties.map((party, index) => ({
Expand Down
3 changes: 2 additions & 1 deletion tests/db-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('VConQueries', () => {
from: vi.fn(),
select: vi.fn(),
insert: vi.fn(),
upsert: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
eq: vi.fn(),
Expand Down Expand Up @@ -70,7 +71,7 @@ describe('VConQueries', () => {

expect(result.uuid).toBe(testVCon.uuid);
expect(mockSupabase.from).toHaveBeenCalledWith('vcons');
expect(mockSupabase.insert).toHaveBeenCalled();
expect(mockSupabase.upsert).toHaveBeenCalled();
});

it('should create vCon with all components (dialog, analysis, attachments)', async () => {
Expand Down
Loading