43 lines
829 B
JavaScript
43 lines
829 B
JavaScript
const path = require('path');
|
|
const { randomUUID } = require('crypto');
|
|
const { eventsDir } = require('./paths');
|
|
const { writeJson } = require('./fs');
|
|
|
|
function dayString(timestamp) {
|
|
return timestamp.slice(0, 10);
|
|
}
|
|
|
|
async function emitEvent({
|
|
type,
|
|
resource_type,
|
|
resource_id,
|
|
request_id,
|
|
correlation_id,
|
|
payload,
|
|
}) {
|
|
const timestamp = new Date().toISOString();
|
|
const event = {
|
|
event_id: randomUUID(),
|
|
schema_version: 'v1',
|
|
type,
|
|
resource_type,
|
|
resource_id,
|
|
timestamp,
|
|
request_id: request_id || null,
|
|
correlation_id: correlation_id || null,
|
|
payload: payload || {},
|
|
};
|
|
|
|
await writeJson(
|
|
path.join(eventsDir, dayString(timestamp), `${timestamp.replace(/[:.]/g, '-')}-${event.event_id}.json`),
|
|
event
|
|
);
|
|
|
|
return event;
|
|
}
|
|
|
|
module.exports = {
|
|
emitEvent,
|
|
};
|
|
|