diff --git a/src/app/core/services/expense-fields.service.ts b/src/app/core/services/expense-fields.service.ts index 4f864e67b0..6c1b4e6d52 100644 --- a/src/app/core/services/expense-fields.service.ts +++ b/src/app/core/services/expense-fields.service.ts @@ -12,6 +12,7 @@ import { OrgCategory } from '../models/v1/org-category.model'; import { AuthService } from './auth.service'; import { DateService } from './date.service'; import { SpenderPlatformV1ApiService } from './spender-platform-v1-api.service'; +import { cloneDeep } from 'lodash'; @Injectable({ providedIn: 'root', @@ -20,7 +21,7 @@ export class ExpenseFieldsService { constructor( private spenderPlatformV1ApiService: SpenderPlatformV1ApiService, private authService: AuthService, - private dateService: DateService, + private dateService: DateService ) {} @Cacheable() @@ -33,10 +34,10 @@ export class ExpenseFieldsService { is_enabled: 'eq.true', is_custom: 'eq.false', }, - }), + }) ), map((res) => this.transformFrom(res.data)), - map((res) => this.dateService.fixDates(res)), + map((res) => this.dateService.fixDates(res)) ); } @@ -131,14 +132,14 @@ export class ExpenseFieldsService { expenseFieldMap[expenseField.column_name] = expenseFieldsList; }); return expenseFieldMap; - }), + }) ); } filterByOrgCategoryId( tfcMap: Partial, fields: string[], - orgCategory: OrgCategory, + orgCategory: OrgCategory ): Observable> { const orgCategoryId = orgCategory && orgCategory.id; return of(fields).pipe( @@ -171,7 +172,7 @@ export class ExpenseFieldsService { } return filteredField; }) - .filter((filteredField) => !!filteredField), + .filter((filteredField) => !!filteredField) ), switchMap((fields) => from(fields)), map((field) => ({ @@ -180,7 +181,7 @@ export class ExpenseFieldsService { reduce((acc, curr) => { acc[curr.field] = curr; return acc; - }, {}), + }, {}) ); } @@ -194,7 +195,7 @@ export class ExpenseFieldsService { To handle both case added this, it can take the type based on use case, but, ideally, we should have a single type of response */ getDefaultTxnFieldValues( - txnFields: Partial | Partial, + txnFields: Partial | Partial ): Partial { const defaultValues = {}; for (const configurationColumn in txnFields) { @@ -210,7 +211,8 @@ export class ExpenseFieldsService { } private formatBillableFields(expenseFields: ExpenseField[]): ExpenseField[] { - return expenseFields.map((field) => { + const expenseFieldsCopy = cloneDeep(expenseFields); + return expenseFieldsCopy.map((field) => { if (!field.is_custom && field.field_name.toLowerCase() === 'billable') { field.default_value = field.default_value === 'true'; } diff --git a/src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts b/src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts index 1ac6d280f8..f5b8b3f7f8 100644 --- a/src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts +++ b/src/app/fyle/add-edit-per-diem/add-edit-per-diem-1.page.spec.ts @@ -52,6 +52,7 @@ import { txnFieldsData2 } from 'src/app/core/mock-data/expense-field-obj.data'; import { defaultTxnFieldValuesData2 } from 'src/app/core/mock-data/default-txn-field-values.data'; import { orgSettingsCCCDisabled } from 'src/app/core/mock-data/org-settings.data'; import { ExpenseType } from 'src/app/core/enums/expense-type.enum'; +import { expectedProjectsResponse } from 'src/app/core/test-data/projects.spec.data'; export function TestCases1(getTestBed) { return describe('add-edit-per-diem test cases set 1', () => { @@ -553,12 +554,13 @@ export function TestCases1(getTestBed) { ); expenseFieldsService.filterByOrgCategoryId.and.returnValue(of(mockTxnFieldData)); expenseFieldsService.getDefaultTxnFieldValues.and.returnValue(defaultTxnFieldValuesData2); + component.fg.controls.project.setValue(expectedProjectsResponse[0]); component.fg.controls.purpose.setValue(''); component.fg.controls.costCenter.setValue(null); component.fg.controls.from_dt.setValue('2023-01-01'); component.fg.controls.num_days.setValue(32); component.fg.controls.to_dt.setValue('2023-02-02'); - component.fg.controls.billable.setValue(true); + component.fg.controls.billable.setValue(null); component.setupTfcDefaultValues(); @@ -572,6 +574,7 @@ export function TestCases1(getTestBed) { expect(expenseFieldsService.getDefaultTxnFieldValues).toHaveBeenCalledOnceWith(mockTxnFieldData); expect(component.fg.controls.costCenter.value).toEqual(15818); expect(component.fg.controls.purpose.value).toEqual('test_term'); + expect(component.fg.controls.billable.value).toEqual(true); }); it('getPaymentModes(): should get payment modes', (done) => { diff --git a/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.ts b/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.ts index e1a94ca5ad..73589e16ba 100644 --- a/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.ts +++ b/src/app/fyle/add-edit-per-diem/add-edit-per-diem.page.ts @@ -548,7 +548,14 @@ export class AddEditPerDiemPage implements OnInit { for (const defaultValueColumn in defaultValues) { if (defaultValues.hasOwnProperty(defaultValueColumn)) { const control = keyToControlMap[defaultValueColumn]; - if (!control.value && defaultValueColumn !== 'billable') { + if (!control.value && !control.touched && defaultValueColumn !== 'billable') { + control.patchValue(defaultValues[defaultValueColumn]); + } else if ( + defaultValueColumn === 'billable' && + this.fg.controls.project.value && + (control.value === undefined || control.value === null) && + !control.touched + ) { control.patchValue(defaultValues[defaultValueColumn]); } }