Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): read partial counts off an aborted import's rejection
Yudhi Armyndharis60eb4968381f
diff --git a/src/modules/infra/infra-storage.controller.spec.ts b/src/modules/infra/infra-storage.controller.spec.tsindex 89d292bd4..aed2a53f0 100644--- a/src/modules/infra/infra-storage.controller.spec.ts+++ b/src/modules/infra/infra-storage.controller.spec.ts@@ -336,6 +336,36 @@ describe('InfraStorageController audit trail (light-dependency handlers)', () =>}});+ it('importStorage records how many entries an aborted import had already written', 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 abort = Object.assign(new Error('archive exceeds 100000 entries'), { imported: 100000, failed: 2 });+ const storageService = {+ importFromStream: jest.fn().mockRejectedValue(abort),+ getCurrentStorageType: () => 'local',+ };+ await expect(+ new InfraStorageController(storageService as never, audit as never).importStorage({+ filePath: 'data/exports/x.tar.gz',+ }),+ ).rejects.toBeInstanceOf(BadRequestException);+ expect(audit.logWarn).toHaveBeenCalledWith(AuditAction.INFRA_STORAGE_IMPORTED, {+ metadata: {+ aborted: true,+ error: 'archive exceeds 100000 entries',+ storageType: 'local',+ count: 100000,+ failed: 2,+ },+ });+ } finally {+ cwdSpy.mockRestore();+ (fs.existsSync as jest.Mock).mockReturnValue(false);+ }+ });+it('importStorage records an aborted import as a warning, since the entries before the abort were kept', async () => {const audit = { logInfo: jest.fn().mockResolvedValue(null), logWarn: jest.fn().mockResolvedValue(null) };const cwdSpy = jest.spyOn(process, 'cwd').mockReturnValue('/srv/openwa');diff --git a/src/modules/infra/infra-storage.controller.ts b/src/modules/infra/infra-storage.controller.tsindex 7f4c017a3..247a7da02 100644--- a/src/modules/infra/infra-storage.controller.ts+++ b/src/modules/infra/infra-storage.controller.ts@@ -273,9 +273,17 @@ export class InfraStorageController implements OnApplicationBootstrap {({ imported: count, failed } = await this.storageService.importFromStream(readStream));} catch (error) {const message = error instanceof Error ? error.message : String(error);- // An abort keeps the entries written before it (there is no rollback), so it is audited too.+ // An abort keeps the entries written before it (there is no rollback), so it is audited too,+ // with the partial counts when the import attaches them to its rejection, so the row says how much changed.+ const partial = error as { imported?: number; failed?: number } | null | undefined;await this.auditService?.logWarn(AuditAction.INFRA_STORAGE_IMPORTED, {- metadata: { aborted: true, error: message, storageType: this.storageService.getCurrentStorageType() },+ metadata: {+ aborted: true,+ error: message,+ storageType: this.storageService.getCurrentStorageType(),+ count: partial?.imported,+ failed: partial?.failed,+ },});throw new BadRequestException(`Storage import failed: ${message}`);}