Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(audit): break createdAt ties by id when listing audit rows

Yudhi Armyndharisaa0b74b56d4a
diff --git a/src/modules/audit/audit.service.spec.ts b/src/modules/audit/audit.service.spec.ts
index 2c360212d..fe0912451 100644
--- a/src/modules/audit/audit.service.spec.ts
+++ b/src/modules/audit/audit.service.spec.ts
@@ -45,13 +45,19 @@ describe('AuditService', () => {
expect(repo.findAndCount).toHaveBeenCalledWith(
expect.objectContaining({
where: { action: AuditAction.SESSION_STARTED, severity: AuditSeverity.INFO },
- order: { createdAt: 'DESC' },
+ order: { createdAt: 'DESC', id: 'DESC' },
take: 50,
skip: 0,
}),
);
});
+ it('findAll breaks createdAt ties by id, so offset pages over rows written in one second cannot overlap', async () => {
+ await service.findAll({ limit: 200, offset: 200 });
+ const arg = (repo.findAndCount.mock.calls as unknown[][])[0][0] as { order: Record<string, string> };
+ expect(arg.order).toEqual({ createdAt: 'DESC', id: 'DESC' });
+ });
+
it('findAll clamps an oversized limit to the max page size (prevents whole-table loads)', async () => {
await service.findAll({ limit: 99_999_999 });
const arg = (repo.findAndCount.mock.calls as unknown[][])[0][0] as { take: number };
diff --git a/src/modules/audit/audit.service.ts b/src/modules/audit/audit.service.ts
index a18b8f5f9..0cfbf2f6d 100644
--- a/src/modules/audit/audit.service.ts
+++ b/src/modules/audit/audit.service.ts
@@ -166,7 +166,9 @@ export class AuditService implements OnModuleInit, OnModuleDestroy {
const [data, total] = await this.auditRepository.findAndCount({
where,
- order: { createdAt: 'DESC' },
+ // createdAt has one-second precision, so rows written in the same second tie; the unique id gives
+ // them one fixed order, or separate LIMIT/OFFSET pages could repeat one row and skip another.
+ order: { createdAt: 'DESC', id: 'DESC' },
take,
// Clamp to a non-negative skip: a negative offset (e.g. from an unvalidated `?offset=-5`) would
// otherwise reach the query driver verbatim.