From c518ed64938ffa01a06316b7089c038b6c91d9e3 Mon Sep 17 00:00:00 2001 From: Chethan-Fyle <93918979+Chethan-Fyle@users.noreply.github.com> Date: Mon, 20 May 2024 21:21:50 +0530 Subject: [PATCH] feat: replace files post api with platform attach receipt to expense api endpoint - part 2 (#3006) --- .../add-edit-expense-2.spec.ts | 24 +++++++------------ .../add-edit-expense/add-edit-expense.page.ts | 12 +++------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/app/fyle/add-edit-expense/add-edit-expense-2.spec.ts b/src/app/fyle/add-edit-expense/add-edit-expense-2.spec.ts index 8d125e2a47..2430dcb6e1 100644 --- a/src/app/fyle/add-edit-expense/add-edit-expense-2.spec.ts +++ b/src/app/fyle/add-edit-expense/add-edit-expense-2.spec.ts @@ -1455,35 +1455,27 @@ export function TestCases2(getTestBed) { }); }); - it('postToFileService(): should post files to file service', () => { - fileService.post.and.returnValue(of(null)); - - component.postToFileService(fileObjectData, 'tx5fBcPBAxLv'); - - expect(fileService.post).toHaveBeenCalledOnceWith({ ...fileObjectData, transaction_id: 'tx5fBcPBAxLv' }); - }); - - it('uploadFileAndPostToFileService(): should upload to file service', (done) => { + it('uploadFileAndAttachToExpense(): should upload to file service', (done) => { transactionOutboxService.fileUpload.and.resolveTo(fileObjectData); - spyOn(component, 'postToFileService').and.returnValue(of(fileObjectData)); + expensesService.attachReceiptToExpense.and.returnValue(of(platformExpenseWithExtractedData)); - component.uploadFileAndPostToFileService(fileObjectData, 'tx5fBcPBAxLv').subscribe(() => { + component.uploadFileAndAttachToExpense(fileObjectData, 'tx5fBcPBAxLv').subscribe(() => { expect(transactionOutboxService.fileUpload).toHaveBeenCalledOnceWith(fileObjectData.url, fileObjectData.type); - expect(component.postToFileService).toHaveBeenCalledOnceWith(fileObjectData, 'tx5fBcPBAxLv'); + expect(expensesService.attachReceiptToExpense).toHaveBeenCalledOnceWith('tx5fBcPBAxLv', fileObjectData.id); done(); }); }); it('uploadMultipleFiles(): should upload multiple files', (done) => { - const uploadSpy = spyOn(component, 'uploadFileAndPostToFileService'); + const uploadSpy = spyOn(component, 'uploadFileAndAttachToExpense'); uploadSpy.withArgs(fileObject7[0], 'tx5fBcPBAxLv').and.returnValue(of(fileObject7[0])); uploadSpy.withArgs(fileObject7[1], 'tx5fBcPBAxLv').and.returnValue(of(fileObject7[1])); component.uploadMultipleFiles(fileObject7, 'tx5fBcPBAxLv').subscribe((res) => { expect(res).toEqual(fileObject7); - expect(component.uploadFileAndPostToFileService).toHaveBeenCalledTimes(2); - expect(component.uploadFileAndPostToFileService).toHaveBeenCalledWith(fileObject7[0], 'tx5fBcPBAxLv'); - expect(component.uploadFileAndPostToFileService).toHaveBeenCalledWith(fileObject7[1], 'tx5fBcPBAxLv'); + expect(component.uploadFileAndAttachToExpense).toHaveBeenCalledTimes(2); + expect(component.uploadFileAndAttachToExpense).toHaveBeenCalledWith(fileObject7[0], 'tx5fBcPBAxLv'); + expect(component.uploadFileAndAttachToExpense).toHaveBeenCalledWith(fileObject7[1], 'tx5fBcPBAxLv'); done(); }); }); diff --git a/src/app/fyle/add-edit-expense/add-edit-expense.page.ts b/src/app/fyle/add-edit-expense/add-edit-expense.page.ts index 80879c4ba9..041fbb3078 100644 --- a/src/app/fyle/add-edit-expense/add-edit-expense.page.ts +++ b/src/app/fyle/add-edit-expense/add-edit-expense.page.ts @@ -4920,18 +4920,12 @@ export class AddEditExpensePage implements OnInit { } uploadMultipleFiles(fileObjs: FileObject[], txnId: string): Observable { - return forkJoin(fileObjs.map((file) => this.uploadFileAndPostToFileService(file, txnId))); + return forkJoin(fileObjs.map((file) => this.uploadFileAndAttachToExpense(file, txnId))); } - postToFileService(fileObj: FileObject, txnId: string): Observable { - const fileObjCopy = cloneDeep(fileObj); - fileObjCopy.transaction_id = txnId; - return this.fileService.post(fileObjCopy); - } - - uploadFileAndPostToFileService(file: FileObject, txnId: string): Observable { + uploadFileAndAttachToExpense(file: FileObject, txnId: string): Observable { return from(this.transactionOutboxService.fileUpload(file.url, file.type)).pipe( - switchMap((fileObj: FileObject) => this.postToFileService(fileObj, txnId)) + switchMap((fileObj: FileObject) => this.expensesService.attachReceiptToExpense(txnId, fileObj.id)) ); }