forked from unimatrix/web
12 lines
1.2 KiB
JavaScript
12 lines
1.2 KiB
JavaScript
import {randomUUID} from 'node:crypto';
|
|
export async function createRealmEventRecord(tx,user,input,now=Date.now()){
|
|
const existing=await tx.first('SELECT * FROM jarvis_events WHERE request_id=? FOR UPDATE',[input.requestId]);
|
|
if(existing){
|
|
if(existing.actor_id!==user.id||existing.scope_kind!==input.kind||existing.scope_id!==input.scopeId||existing.title!==input.title||existing.description!==input.description||Number(existing.starts_at)!==input.startsAt||Number(existing.ends_at)!==input.endsAt)throw Object.assign(Error('Event reference already used.'),{status:409});
|
|
return {eventId:existing.id,replayed:true};
|
|
}
|
|
const id=randomUUID(),reference='EVT-'+id.toUpperCase();
|
|
await tx.query("INSERT INTO jarvis_events(id,request_id,reference,scope_kind,scope_id,title,description,starts_at,ends_at,state,revision,actor_id,created_at,updated_at) VALUES(?,?,?,?,?,?,?,?,?,'scheduled',1,?,?,?)",[id,input.requestId,reference,input.kind,input.scopeId,input.title,input.description,input.startsAt,input.endsAt,user.id,now,now]);
|
|
await tx.query('INSERT INTO jarvis_event_history VALUES(?,?,?,?,?)',[randomUUID(),id,user.id,'created',now]);
|
|
return {eventId:id,replayed:false};
|
|
}
|

Staff