first commit
This commit is contained in:
79
base44/functions/recordEvent/entry.ts
Normal file
79
base44/functions/recordEvent/entry.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { createClientFromRequest } from 'npm:@base44/sdk@0.8.31';
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
try {
|
||||
const base44 = createClientFromRequest(req);
|
||||
const user = await base44.auth.me();
|
||||
if (!user) return Response.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
const body = await req.json();
|
||||
const {
|
||||
event_type,
|
||||
severity = 'info',
|
||||
actor_type = 'system',
|
||||
actor_id,
|
||||
actor_name,
|
||||
subject_entity_type,
|
||||
subject_entity_id,
|
||||
subject_krow_id,
|
||||
context_summary,
|
||||
related_entities = [],
|
||||
metadata = {},
|
||||
tags = []
|
||||
} = body;
|
||||
|
||||
if (!event_type || !subject_entity_type || !subject_entity_id) {
|
||||
return Response.json({ error: 'event_type, subject_entity_type, and subject_entity_id are required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const event_id = `EVT-${Date.now()}-${Math.random().toString(36).substring(2, 8)}`;
|
||||
|
||||
const event = await base44.asServiceRole.entities.WorkforceEvent.create({
|
||||
event_id,
|
||||
event_type,
|
||||
severity,
|
||||
actor_type,
|
||||
actor_id: actor_id || user.id,
|
||||
actor_name: actor_name || user.full_name || 'System',
|
||||
subject_entity_type,
|
||||
subject_entity_id,
|
||||
subject_krow_id,
|
||||
context_summary: context_summary || `${event_type} on ${subject_entity_type}`,
|
||||
related_entities,
|
||||
metadata,
|
||||
reasoning_triggered: false,
|
||||
tags
|
||||
});
|
||||
|
||||
// Determine if this event should trigger reasoning
|
||||
const reasoningTriggers = [
|
||||
'shift.completed', 'shift.cancelled', 'shift.no_show',
|
||||
'order.created', 'order.cancelled', 'order.filled',
|
||||
'assignment.cancelled', 'invoice.overdue',
|
||||
'vendor.rate_changed', 'worker.availability_changed',
|
||||
'worker.certification_expired', 'budget.threshold_exceeded',
|
||||
'compliance.flagged', 'system.anomaly_detected'
|
||||
];
|
||||
|
||||
if (reasoningTriggers.includes(event_type)) {
|
||||
try {
|
||||
await base44.asServiceRole.functions.invoke('reasonAboutEvent', {
|
||||
event_id: event.id,
|
||||
event_type,
|
||||
subject_entity_type,
|
||||
subject_entity_id,
|
||||
context_summary,
|
||||
metadata,
|
||||
related_entities
|
||||
});
|
||||
} catch (reasonErr) {
|
||||
// Reasoning failure shouldn't fail the event recording
|
||||
console.log('Reasoning engine error (non-blocking):', reasonErr.message);
|
||||
}
|
||||
}
|
||||
|
||||
return Response.json({ success: true, event_id: event.id, event });
|
||||
} catch (error) {
|
||||
return Response.json({ error: error.message }, { status: 500 });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user