Skip to content

Commit

Permalink
feat: replace files post api with platform attach receipt to expense …
Browse files Browse the repository at this point in the history
…api endpoint - part 2 (#3006)
  • Loading branch information
Chethan-Fyle authored May 20, 2024
1 parent 7f88e21 commit c518ed6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
24 changes: 8 additions & 16 deletions src/app/fyle/add-edit-expense/add-edit-expense-2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
12 changes: 3 additions & 9 deletions src/app/fyle/add-edit-expense/add-edit-expense.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4920,18 +4920,12 @@ export class AddEditExpensePage implements OnInit {
}

uploadMultipleFiles(fileObjs: FileObject[], txnId: string): Observable<unknown> {
return forkJoin(fileObjs.map((file) => this.uploadFileAndPostToFileService(file, txnId)));
return forkJoin(fileObjs.map((file) => this.uploadFileAndAttachToExpense(file, txnId)));
}

postToFileService(fileObj: FileObject, txnId: string): Observable<FileObject | unknown> {
const fileObjCopy = cloneDeep(fileObj);
fileObjCopy.transaction_id = txnId;
return this.fileService.post(fileObjCopy);
}

uploadFileAndPostToFileService(file: FileObject, txnId: string): Observable<FileObject[] | unknown> {
uploadFileAndAttachToExpense(file: FileObject, txnId: string): Observable<FileObject[] | unknown> {
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))
);
}

Expand Down

0 comments on commit c518ed6

Please sign in to comment.