Skip to content
Closed
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
46 changes: 46 additions & 0 deletions src/audit-logs/audit-logs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,52 @@ describe('AuditLogs', () => {
});

describe('createEvent', () => {
it('omits occurred_at when occurredAt is not provided', async () => {
fetchOnce({ success: true });

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
const eventWithoutTimestamp: CreateAuditLogEventOptions = {
action: event.action,
actor: event.actor,
targets: event.targets,
context: event.context,
};

await workos.auditLogs.createEvent('org_123', eventWithoutTimestamp);

expect(fetchBody()).toMatchObject({
organization_id: 'org_123',
event: { action: event.action },
});
expect(fetchBody().event).not.toHaveProperty('occurred_at');
});

it('omits occurred_at when occurredAt is undefined', async () => {
fetchOnce({ success: true });

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

await workos.auditLogs.createEvent('org_123', {
...event,
occurredAt: undefined,
});

expect(fetchBody().event).not.toHaveProperty('occurred_at');
});

it('preserves an explicitly provided occurredAt', async () => {
fetchOnce({ success: true });

const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');

await workos.auditLogs.createEvent('org_123', {
...event,
occurredAt: new Date('2026-11-03T12:34:56.789Z'),
});

expect(fetchBody().event.occurred_at).toBe('2026-11-03T12:34:56.789Z');
});

describe('with an idempotency key', () => {
it('includes an idempotency key with request', async () => {
const workosSpy = jest.spyOn(WorkOS.prototype, 'post');
Expand Down
1 change: 0 additions & 1 deletion src/audit-logs/audit-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export class AuditLogs {
*
* To achieve idempotency, you can add `Idempotency-Key` request header to a Create Event request with a unique string as the value. Each subsequent request matching this unique string will return the same response. We suggest using [v4 UUIDs](https://en.wikipedia.org/wiki/Universally_unique_identifier) for idempotency keys to avoid collisions.
*
* Idempotency keys expire after 24 hours. The API will generate a new response if you submit a request with an expired key.
* @param payload - Object containing organizationId, event.
* @returns {Promise<void>}
* @throws {BadRequestException} 400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export interface AuditLogTarget {
export interface CreateAuditLogEventOptions {
action: string;
version?: number;
occurredAt: Date;
/** Defaults to the time WorkOS accepts the event when omitted. */
occurredAt?: Date;
actor: AuditLogActor;
targets: AuditLogTarget[];
context: {
Expand All @@ -30,7 +31,7 @@ export interface CreateAuditLogEventOptions {
export interface SerializedCreateAuditLogEventOptions {
action: string;
version?: number;
occurred_at: string;
occurred_at?: string;
actor: AuditLogActor;
targets: AuditLogTarget[];
context: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const serializeCreateAuditLogEventOptions = (
): SerializedCreateAuditLogEventOptions => ({
action: event.action,
version: event.version,
occurred_at: event.occurredAt.toISOString(),
...(event.occurredAt === undefined
? {}
: { occurred_at: event.occurredAt.toISOString() }),
actor: event.actor,
targets: event.targets,
context: {
Expand Down
Loading