Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): audit a storage import that wrote nothing as a warning

Yudhi Armyndharis9609372039a9
diff --git a/src/modules/infra/infra-storage.controller.spec.ts b/src/modules/infra/infra-storage.controller.spec.ts
index 524f99a67..346fe0302 100644
--- a/src/modules/infra/infra-storage.controller.spec.ts
+++ b/src/modules/infra/infra-storage.controller.spec.ts
@@ -303,7 +303,29 @@ describe('InfraStorageController audit trail (light-dependency handlers)', () =>
[AuditAction, { metadata: { count: number; storageType: string } }]
>;
expect(calls[0][0]).toBe(AuditAction.INFRA_STORAGE_IMPORTED);
- expect(calls[0][1].metadata).toEqual({ count: 5, storageType: 'local' });
+ expect(calls[0][1].metadata).toEqual({ count: 5, failed: 0, storageType: 'local' });
+ } finally {
+ cwdSpy.mockRestore();
+ (fs.existsSync as jest.Mock).mockReturnValue(false);
+ }
+ });
+
+ it('importStorage records an import that wrote nothing as a warning, with its failed count', async () => {
+ const audit = { logInfo: jest.fn().mockResolvedValue(null), logWarn: jest.fn().mockResolvedValue(null) };
+ const cwdSpy = jest.spyOn(process, 'cwd').mockReturnValue('/srv/openwa');
+ (fs.existsSync as jest.Mock).mockImplementation((p: string) => p === '/srv/openwa/data/exports/x.tar.gz');
+ try {
+ const storageService = {
+ importFromStream: jest.fn().mockResolvedValue({ imported: 0, failed: 4 }),
+ getCurrentStorageType: () => 'local',
+ };
+ await new InfraStorageController(storageService as never, audit as never).importStorage({
+ filePath: 'data/exports/x.tar.gz',
+ });
+ expect(audit.logInfo).not.toHaveBeenCalled();
+ expect(audit.logWarn).toHaveBeenCalledWith(AuditAction.INFRA_STORAGE_IMPORTED, {
+ metadata: { count: 0, failed: 4, storageType: 'local' },
+ });
} finally {
cwdSpy.mockRestore();
(fs.existsSync as jest.Mock).mockReturnValue(false);
diff --git a/src/modules/infra/infra-storage.controller.ts b/src/modules/infra/infra-storage.controller.ts
index c350ce62b..ed38557a4 100644
--- a/src/modules/infra/infra-storage.controller.ts
+++ b/src/modules/infra/infra-storage.controller.ts
@@ -261,15 +261,21 @@ export class InfraStorageController implements OnApplicationBootstrap {
}
const storageType = this.storageService.getCurrentStorageType();
- // Audit the bulk media-import (files written into the active storage backend).
- await this.auditService?.logInfo(AuditAction.INFRA_STORAGE_IMPORTED, {
- metadata: { count, storageType },
- });
-
// A bad or traversing entry is skipped by design, so one refusal does not fail the import. An
// archive whose every entry was refused (the store rejects writes) wrote nothing and says so.
+ const imported = !(failed > 0 && count === 0);
+
+ // Audit the bulk media-import (files written into the active storage backend); one that wrote
+ // nothing is recorded as a warning.
+ const audit = { metadata: { count, failed, storageType } };
+ if (imported) {
+ await this.auditService?.logInfo(AuditAction.INFRA_STORAGE_IMPORTED, audit);
+ } else {
+ await this.auditService?.logWarn(AuditAction.INFRA_STORAGE_IMPORTED, audit);
+ }
+
return {
- imported: !(failed > 0 && count === 0),
+ imported,
count,
failed,
storageType,