Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft changes - expenses post #3208

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/app/core/services/platform/v1/spender/expenses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,20 @@ export class ExpensesService {
.pipe(map((res) => res.data));
}

post(expense: Partial<Expense>): Observable<void> {
return this.spenderService.post<void>('/expenses', {
post(expense: Partial<Expense>): Observable<PlatformApiResponse<Expense>> {
return this.spenderService.post<PlatformApiResponse<Expense>>('/expenses', {
data: expense,
});
}

createFromFile(fileId: string, source: string): Observable<PlatformApiResponse<Expense[]>> {
return this.spenderService.post<PlatformApiResponse<Expense[]>>('/expenses/create_from_file/bulk', {
data: [
{
file_id: fileId,
source,
},
],
});
}
}
85 changes: 74 additions & 11 deletions src/app/core/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,11 @@
@CacheBuster({
cacheBusterNotifier: expensesCacheBuster$,
})
upsert(transaction: Partial<Transaction>): Observable<Partial<Transaction>> {
upsert(
transaction: Partial<Transaction>,
fileIds: string[] = []
): Observable<Partial<Transaction | PlatformExpense>> {
/** Only these fields will be of type text & custom fields */

Check failure on line 194 in src/app/core/services/transaction.service.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unexpected any. Specify a different type
const fieldsToCheck = ['purpose', 'vendor', 'train_travel_class', 'bus_travel_class'];

// Frontend should only send amount
Expand Down Expand Up @@ -227,19 +230,19 @@
if (transaction.from_dt) {
transaction.from_dt.setHours(12);
transaction.from_dt.setMinutes(0);
transaction.from_dt.setSeconds(0);

Check failure on line 233 in src/app/core/services/transaction.service.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unsafe member access .travel_classes on an `any` value
transaction.from_dt.setMilliseconds(0);
transaction.from_dt = this.timezoneService.convertToUtc(transaction.from_dt, offset);
}

Check failure on line 237 in src/app/core/services/transaction.service.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unsafe member access .travel_classes on an `any` value

Check failure on line 237 in src/app/core/services/transaction.service.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unsafe call of an `any` typed value
if (transaction.to_dt) {
transaction.to_dt.setHours(12);
transaction.to_dt.setMinutes(0);

Check failure on line 240 in src/app/core/services/transaction.service.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unsafe member access .claim_amount on an `any` value
transaction.to_dt.setSeconds(0);
transaction.to_dt.setMilliseconds(0);

Check failure on line 242 in src/app/core/services/transaction.service.ts

View workflow job for this annotation

GitHub Actions / Run linters

Unsafe return of an `any` typed value
transaction.to_dt = this.timezoneService.convertToUtc(transaction.to_dt, offset);
}

Check failure on line 245 in src/app/core/services/transaction.service.ts

View workflow job for this annotation

GitHub Actions / Run linters

Member createTxnWithFiles should be declared before all public instance method definitions
if (!transaction.source_account_id && !transaction.advance_wallet_id) {
transaction.source_account_id = txnAccount.source_account_id;
transaction.skip_reimbursement = txnAccount.skip_reimbursement;
Expand All @@ -253,27 +256,87 @@

const transactionCopy = this.utilityService.discardRedundantCharacters(transaction, fieldsToCheck);

return this.apiService.post<Transaction>('/transactions', transactionCopy);
// return this.apiService.post<Transaction>('/transactions', transactionCopy);
const expense = this.changeToPlatformExpense(transactionCopy, fileIds);
return this.expensesService.post(expense).pipe(map((result) => result.data));
})
);
}

changeToPlatformExpense(transaction: Partial<Transaction>, fileIds: string[]): PlatformExpense {
const expense: any = {
id: transaction.id,
spent_at: transaction.txn_dt,
category_id: transaction.org_category_id,
purpose: transaction.purpose,
source_account_id: transaction.source_account_id,
merchant: transaction.vendor,
project_id: transaction.project_id,
cost_center_id: transaction.cost_center_id,
foreign_currency: transaction.orig_currency,
foreign_amount: transaction.orig_amount,
source: transaction.source,
is_reimbursable: !transaction.skip_reimbursement,
tax_amount: transaction.tax_amount,
tax_group_id: transaction.tax_group_id,
is_billable: transaction.billable,
distance: transaction.distance,
distance_unit: transaction.distance_unit,
started_at: transaction.from_dt,
ended_at: transaction.to_dt,
locations: transaction.locations,
custom_fields: transaction.custom_properties,
per_diem_rate_id: transaction.per_diem_rate_id,
per_diem_num_days: transaction.num_days || 0,
mileage_rate_id: transaction.mileage_rate_id,
advance_wallet_id: transaction.advance_wallet_id,

// directly attach file instead of making additional API call
file_ids: fileIds,

report_id: transaction.report_id,
};

const travelClass1 =
transaction.taxi_travel_class ||
transaction.bus_travel_class ||
transaction.train_travel_class ||
transaction.flight_journey_travel_class;
if (travelClass1) {
expense.travel_classes = [travelClass1];
}

if (transaction.flight_return_travel_class) {
expense.travel_classes.push(transaction.flight_return_travel_class);
}

expense.claim_amount = transaction.amount;

return expense;
}

@CacheBuster({
cacheBusterNotifier: expensesCacheBuster$,
})
createTxnWithFiles(
txn: Partial<Transaction>,
fileUploads$: Observable<FileObject[]>
): Observable<Partial<Transaction>> {
const upsertTxn$ = this.upsert(txn);
return forkJoin([fileUploads$, upsertTxn$]).pipe(
switchMap(([fileObjs, transaction]) => {
): Observable<Partial<Transaction | PlatformExpense>> {
if (Object.keys(txn).length === 1 && txn.source) {
return fileUploads$.pipe(
switchMap((fileObjs) => {
const fileIds = fileObjs.map((fileObj) => fileObj.id);
if (fileIds.length > 0) {
return this.expensesService.createFromFile(fileIds[0], 'TPA').pipe(map((result) => result.data[0]));
}
})
);
}

return forkJoin([fileUploads$]).pipe(
switchMap(([fileObjs]) => {
const fileIds = fileObjs.map((fileObj) => fileObj.id);
if (fileIds.length > 0) {
return this.expensesService.attachReceiptsToExpense(transaction.id, fileIds).pipe(map(() => transaction));
} else {
return of(transaction);
}
return this.upsert(txn, fileIds);
})
);
}
Expand Down
Loading
Loading