From 503e03aae754d97d02ac7df640f136b94d6fa184 Mon Sep 17 00:00:00 2001 From: Soanvig Date: Thu, 28 Apr 2022 13:59:34 +0200 Subject: [PATCH] fix(irec): add filename to uploading files to IREC --- .../src/device/device.service.ts | 2 +- .../src/irec/irec.mock.service.ts | 5 ++++- .../origin-organization-irec-api/src/irec/irec.service.ts | 7 +++++-- .../issuer-irec-api-wrapper/src/IRECAPIClient.ts | 8 ++++++-- .../issuer-irec-api-wrapper/test/flow.test.ts | 4 ++-- .../handlers/create-irec-certification-request.handler.ts | 2 +- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/devices/origin-device-registry-irec-local-api/src/device/device.service.ts b/packages/devices/origin-device-registry-irec-local-api/src/device/device.service.ts index cbac974c21..7e01ec9bf9 100644 --- a/packages/devices/origin-device-registry-irec-local-api/src/device/device.service.ts +++ b/packages/devices/origin-device-registry-irec-local-api/src/device/device.service.ts @@ -80,7 +80,7 @@ export class DeviceService { ); fileIds = await this.irecService.uploadFiles( user.organizationId, - list.map((file) => file.data) + list.map((file) => ({ data: file.data, filename: file.filename })) ); } diff --git a/packages/organizations/origin-organization-irec-api/src/irec/irec.mock.service.ts b/packages/organizations/origin-organization-irec-api/src/irec/irec.mock.service.ts index 3a6e2d5416..835afe3709 100644 --- a/packages/organizations/origin-organization-irec-api/src/irec/irec.mock.service.ts +++ b/packages/organizations/origin-organization-irec-api/src/irec/irec.mock.service.ts @@ -264,7 +264,10 @@ export class IrecMockService implements IIrecService { params: CreateAccountParams ): Promise {} - async uploadFiles(user: UserIdentifier, files: Buffer[] | Blob[] | ReadStream[]) { + async uploadFiles( + user: UserIdentifier, + files: { data: Buffer | Blob | ReadStream; filename: string }[] + ) { return files.map(() => randomString()); } diff --git a/packages/organizations/origin-organization-irec-api/src/irec/irec.service.ts b/packages/organizations/origin-organization-irec-api/src/irec/irec.service.ts index 37e7527565..0047b93481 100644 --- a/packages/organizations/origin-organization-irec-api/src/irec/irec.service.ts +++ b/packages/organizations/origin-organization-irec-api/src/irec/irec.service.ts @@ -120,7 +120,10 @@ export interface IIrecService { getIssueRequest(user: UserIdentifier, code: string): Promise; - uploadFiles(user: UserIdentifier, files: Buffer[] | Blob[] | ReadStream[]): Promise; + uploadFiles( + user: UserIdentifier, + files: { data: Buffer | Blob | ReadStream; filename: string }[] + ): Promise; verifyIssueRequest(user: UserIdentifier, issueRequestCode: string): Promise; @@ -374,7 +377,7 @@ export class IrecService implements IIrecService { async uploadFiles( user: UserIdentifier, - files: Buffer[] | Blob[] | ReadStream[] + files: { data: Buffer | Blob | ReadStream; filename: string }[] ): Promise { const irecClient = await this.getIrecClient(user); return irecClient.file.upload(files); diff --git a/packages/traceability/issuer-irec-api-wrapper/src/IRECAPIClient.ts b/packages/traceability/issuer-irec-api-wrapper/src/IRECAPIClient.ts index 33e87b0926..a11d362d08 100644 --- a/packages/traceability/issuer-irec-api-wrapper/src/IRECAPIClient.ts +++ b/packages/traceability/issuer-irec-api-wrapper/src/IRECAPIClient.ts @@ -325,11 +325,15 @@ export class IRECAPIClient { const fileManagementUrl = `${this.endPointUrl}/api/irec/v1/file-management`; return { - upload: async (files: Buffer[] | Blob[] | ReadStream[]): Promise => { + upload: async ( + files: { data: Buffer | Blob | ReadStream; filename: string }[] + ): Promise => { const url = `${fileManagementUrl}/upload`; const data = new FormData(); - files.forEach((file: Buffer | Blob | ReadStream) => data.append('files', file)); + files.forEach((file) => + data.append('files', file.data, { filename: file.filename }) + ); const headers = data.getHeaders(); const response = await this.axiosInstance.post<{ file_uids: string[] }>(url, data, { diff --git a/packages/traceability/issuer-irec-api-wrapper/test/flow.test.ts b/packages/traceability/issuer-irec-api-wrapper/test/flow.test.ts index e0f858247a..ef2a6b7ee6 100644 --- a/packages/traceability/issuer-irec-api-wrapper/test/flow.test.ts +++ b/packages/traceability/issuer-irec-api-wrapper/test/flow.test.ts @@ -62,7 +62,7 @@ describe('API flows', () => { it('should pass create and approve device flow', async () => { const file = fs.createReadStream(`${__dirname}/file-sample_150kB.pdf`); - const [fileId] = await registrantClient.file.upload([file]); + const [fileId] = await registrantClient.file.upload([{ data: file, filename: 'test' }]); const params = getDeviceParams(); const createdDevice: Device = await registrantClient.device.create({ @@ -109,7 +109,7 @@ describe('API flows', () => { it('should pass create and approve issue flow', async () => { const file = fs.createReadStream(`${__dirname}/file-sample_150kB.pdf`); - const [fileId] = await registrantClient.file.upload([file]); + const [fileId] = await registrantClient.file.upload([{ data: file, filename: 'test' }]); const params = getDeviceParams(); const device: Device = await registrantClient.device.create(params); diff --git a/packages/traceability/issuer-irec-api/src/pods/certification-request/handlers/create-irec-certification-request.handler.ts b/packages/traceability/issuer-irec-api/src/pods/certification-request/handlers/create-irec-certification-request.handler.ts index c4e97f9752..7bf2bf73e9 100644 --- a/packages/traceability/issuer-irec-api/src/pods/certification-request/handlers/create-irec-certification-request.handler.ts +++ b/packages/traceability/issuer-irec-api/src/pods/certification-request/handlers/create-irec-certification-request.handler.ts @@ -46,7 +46,7 @@ export class CreateIrecCertificationRequestHandler ); fileIds = await this.irecService.uploadFiles( user.organizationId, - list.map((file) => file.data) + list.map((file) => ({ data: file.data, filename: file.filename })) ); }