Initial commit
This commit is contained in:
42
shared/events.js
Normal file
42
shared/events.js
Normal file
@@ -0,0 +1,42 @@
|
||||
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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user