From a869682f400df2a0cd0c41dd16946d5b12b3c1f9 Mon Sep 17 00:00:00 2001 From: Chethan-Fyle <93918979+Chethan-Fyle@users.noreply.github.com> Date: Mon, 27 May 2024 17:54:56 +0530 Subject: [PATCH] feat: use platform files api in txn outbox service (#3019) --- .../platform/v1/spender/file.service.ts | 6 ++- .../services/transactions-outbox.service.ts | 48 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/app/core/services/platform/v1/spender/file.service.ts b/src/app/core/services/platform/v1/spender/file.service.ts index 9475d19990..0687e91064 100644 --- a/src/app/core/services/platform/v1/spender/file.service.ts +++ b/src/app/core/services/platform/v1/spender/file.service.ts @@ -12,13 +12,15 @@ import { PlatformApiResponse } from 'src/app/core/models/platform/platform-api-r export class SpenderFileService { constructor(private spenderPlatformV1ApiService: SpenderPlatformV1ApiService) {} - createFile(payload: PlatformFilePostRequestPayload): Observable { + createFile(data: PlatformFilePostRequestPayload): Observable { + const payload = { data }; return this.spenderPlatformV1ApiService .post>('/files', payload) .pipe(map((response) => response.data)); } - createFilesBulk(payload: PlatformFilePostRequestPayload[]): Observable { + createFilesBulk(data: PlatformFilePostRequestPayload[]): Observable { + const payload = { data }; return this.spenderPlatformV1ApiService .post>('/files/bulk', payload) .pipe(map((response) => response.data)); diff --git a/src/app/core/services/transactions-outbox.service.ts b/src/app/core/services/transactions-outbox.service.ts index dc2a638038..cba6b2ab10 100644 --- a/src/app/core/services/transactions-outbox.service.ts +++ b/src/app/core/services/transactions-outbox.service.ts @@ -19,6 +19,8 @@ import { FileObject } from '../models/file-obj.model'; import { OutboxQueue } from '../models/outbox-queue.model'; import { ExpensesService } from './platform/v1/spender/expenses.service'; import { SpenderReportsService } from './platform/v1/spender/reports.service'; +import { SpenderFileService } from './platform/v1/spender/file.service'; +import { PlatformFile } from '../models/platform/platform-file.model'; @Injectable({ providedIn: 'root', @@ -50,7 +52,8 @@ export class TransactionsOutboxService { private trackingService: TrackingService, private currencyService: CurrencyService, private orgUserSettingsService: OrgUserSettingsService, - private expensesService: ExpensesService + private expensesService: ExpensesService, + private spenderFileService: SpenderFileService ) { this.ROOT_ENDPOINT = environment.ROOT_URL; this.restoreQueue(); @@ -202,7 +205,7 @@ export class TransactionsOutboxService { }); } - async fileUpload(dataUrl: string, fileType: string, receiptCoordinates?: string): Promise { + async fileUpload(dataUrl: string, fileType: string): Promise { return new Promise((resolve, reject) => { let fileExtension = fileType; let contentType = 'application/pdf'; @@ -212,31 +215,26 @@ export class TransactionsOutboxService { contentType = 'image/jpeg'; } - this.fileService - .post({ + this.spenderFileService + .createFile({ name: '000.' + fileExtension, - receipt_coordinates: receiptCoordinates, + type: 'RECEIPT', }) .toPromise() - .then((fileObj: FileObject) => - this.fileService - .uploadUrl(fileObj.id) - .toPromise() - .then((url) => { - const uploadUrl = url; - // check from here - fetch(dataUrl) - .then((res) => res.blob()) - .then((blob) => { - this.uploadData(uploadUrl, blob, contentType) - .toPromise() - .then(() => resolve(fileObj)) - .catch((err) => { - reject(err); - }); - }); - }) - ) + .then((fileObj: PlatformFile) => { + const uploadUrl = fileObj.upload_url; + // check from here + return fetch(dataUrl) + .then((res) => res.blob()) + .then((blob) => + this.uploadData(uploadUrl, blob, contentType) + .toPromise() + .then(() => resolve(fileObj)) + .catch((err) => { + reject(err); + }) + ); + }) .catch((err) => { reject(err); }); @@ -324,7 +322,7 @@ export class TransactionsOutboxService { if (!entry.fileUploadCompleted) { if (entry.dataUrls && entry.dataUrls.length > 0) { entry.dataUrls.forEach((dataUrl) => { - const fileObjPromise = that.fileUpload(dataUrl.url, dataUrl.type, dataUrl.receiptCoordinates); + const fileObjPromise = that.fileUpload(dataUrl.url, dataUrl.type); fileObjPromiseArray.push(fileObjPromise); });