Skip to content

Commit

Permalink
feat: use platform files api in txn outbox service (#3019)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan-Fyle committed May 28, 2024
1 parent 80aa5e4 commit a869682
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
6 changes: 4 additions & 2 deletions src/app/core/services/platform/v1/spender/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlatformFile> {
createFile(data: PlatformFilePostRequestPayload): Observable<PlatformFile> {
const payload = { data };
return this.spenderPlatformV1ApiService
.post<PlatformApiResponse<PlatformFile>>('/files', payload)
.pipe(map((response) => response.data));
}

createFilesBulk(payload: PlatformFilePostRequestPayload[]): Observable<PlatformFile[]> {
createFilesBulk(data: PlatformFilePostRequestPayload[]): Observable<PlatformFile[]> {
const payload = { data };
return this.spenderPlatformV1ApiService
.post<PlatformApiResponse<PlatformFile[]>>('/files/bulk', payload)
.pipe(map((response) => response.data));
Expand Down
48 changes: 23 additions & 25 deletions src/app/core/services/transactions-outbox.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -202,7 +205,7 @@ export class TransactionsOutboxService {
});
}

async fileUpload(dataUrl: string, fileType: string, receiptCoordinates?: string): Promise<FileObject> {
async fileUpload(dataUrl: string, fileType: string): Promise<FileObject> {
return new Promise((resolve, reject) => {
let fileExtension = fileType;
let contentType = 'application/pdf';
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit a869682

Please sign in to comment.