Skip to content
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,26 @@ export class QbdDirectExportSettingsComponent implements OnInit{
});
}

defaultAccountsPayableAccountWatcher() {
this.exportSettingsForm.controls.employeeMapping.valueChanges.subscribe((employeeMapping) => {
if (employeeMapping === EmployeeFieldMapping.EMPLOYEE) {
if (this.exportSettingsForm.controls.nameInJE.value === EmployeeFieldMapping.EMPLOYEE && this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.value.detail.account_type === 'AccountsPayable') {
this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.patchValue(null);
} else if (this.exportSettingsForm.controls.defaultReimbursableAccountsPayableAccountName.value.detail.account_type === 'AccountsPayable') {
this.exportSettingsForm.controls.defaultReimbursableAccountsPayableAccountName.patchValue(null);
}
}
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add null checks and improve code robustness

The method has several potential issues that should be addressed:

  1. Missing null checks for value.detail which could cause runtime errors
  2. 'AccountsPayable' is used as a magic string
  3. Complex nested conditions could be simplified
  4. Missing error handling for the subscription

Consider this safer implementation:

 defaultAccountsPayableAccountWatcher() {
-  this.exportSettingsForm.controls.employeeMapping.valueChanges.subscribe((employeeMapping) => {
+  this.exportSettingsForm.controls.employeeMapping.valueChanges.subscribe({
+    next: (employeeMapping) => {
     if (employeeMapping === EmployeeFieldMapping.EMPLOYEE) {
-      if (this.exportSettingsForm.controls.nameInJE.value === EmployeeFieldMapping.EMPLOYEE && this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.value.detail.account_type === 'AccountsPayable') {
+      const cccAccount = this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.value?.detail;
+      const reimbursableAccount = this.exportSettingsForm.controls.defaultReimbursableAccountsPayableAccountName.value?.detail;
+      
+      if (this.exportSettingsForm.controls.nameInJE.value === EmployeeFieldMapping.EMPLOYEE && 
+          cccAccount?.account_type === QbdDirectAccountType.ACCOUNTS_PAYABLE) {
         this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.patchValue(null);
-      } else if (this.exportSettingsForm.controls.defaultReimbursableAccountsPayableAccountName.value.detail.account_type === 'AccountsPayable') {
+      } else if (reimbursableAccount?.account_type === QbdDirectAccountType.ACCOUNTS_PAYABLE) {
         this.exportSettingsForm.controls.defaultReimbursableAccountsPayableAccountName.patchValue(null);
       }
     }
+    },
+    error: (error) => {
+      console.error('Error in employeeMapping subscription:', error);
+    }
   });
 }

Also, consider adding an enum for account types:

enum QbdDirectAccountType {
  ACCOUNTS_PAYABLE = 'AccountsPayable',
  // ... other account types
}


defaultCCCAccountsPayableAccountWatcher() {
this.exportSettingsForm.controls.nameInJE.valueChanges.subscribe((nameInJE) => {
if (nameInJE === EmployeeFieldMapping.EMPLOYEE && this.exportSettingsForm.controls.employeeMapping.value === EmployeeFieldMapping.EMPLOYEE && this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.value.detail.account_type === 'AccountsPayable') {
this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.patchValue(null);
}
});
}
Comment on lines +246 to +252
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve error handling and code readability

Similar to the previous method, this one needs improvements in error handling and null safety.

Consider this safer implementation:

 defaultCCCAccountsPayableAccountWatcher() {
-  this.exportSettingsForm.controls.nameInJE.valueChanges.subscribe((nameInJE) => {
-    if (nameInJE === EmployeeFieldMapping.EMPLOYEE && this.exportSettingsForm.controls.employeeMapping.value === EmployeeFieldMapping.EMPLOYEE && this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.value.detail.account_type === 'AccountsPayable') {
-      this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.patchValue(null);
+  this.exportSettingsForm.controls.nameInJE.valueChanges.subscribe({
+    next: (nameInJE) => {
+      const isEmployeeMapping = this.exportSettingsForm.controls.employeeMapping.value === EmployeeFieldMapping.EMPLOYEE;
+      const cccAccount = this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.value?.detail;
+      
+      if (nameInJE === EmployeeFieldMapping.EMPLOYEE && 
+          isEmployeeMapping && 
+          cccAccount?.account_type === QbdDirectAccountType.ACCOUNTS_PAYABLE) {
+        this.exportSettingsForm.controls.defaultCCCAccountsPayableAccountName.patchValue(null);
+      }
+    },
+    error: (error) => {
+      console.error('Error in nameInJE subscription:', error);
    }
   });
 }

Committable suggestion skipped: line range outside the PR's diff.


destinationOptionsWatcher(detailAccountType: string[], destinationOptions: QbdDirectDestinationAttribute[]): DestinationAttribute[] {
return destinationOptions.filter((account: QbdDirectDestinationAttribute) => detailAccountType.includes(account.detail.account_type));
}
Expand All @@ -224,6 +244,10 @@ export class QbdDirectExportSettingsComponent implements OnInit{
this.reimbursableExpenseGroupWatcher();

this.cccExpenseGroupWatcher();

this.defaultAccountsPayableAccountWatcher();

this.defaultCCCAccountsPayableAccountWatcher();
Comment on lines +289 to +292
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Prevent memory leaks and improve watcher organization

The watchers are correctly integrated, but there are two concerns:

  1. Subscriptions aren't being cleaned up, which could lead to memory leaks
  2. Related watchers could be grouped together for better maintainability

Consider these improvements:

  1. Add subscription management:
private subscriptions: Subscription[] = [];

private exportsettingsWatcher(): void {
  // Group related watchers
  // Credit card related
  this.subscriptions.push(this.cccExportTypeWatcher());
  this.subscriptions.push(this.cccExpenseGroupWatcher());
  this.subscriptions.push(this.defaultCCCAccountsPayableAccountWatcher());

  // Employee mapping related
  this.subscriptions.push(this.employeeMappingWatcher());
  this.subscriptions.push(this.defaultAccountsPayableAccountWatcher());

  // Reimbursable related
  this.subscriptions.push(this.reimbursableExpenseGroupWatcher());
}

ngOnDestroy() {
  this.subscriptions.forEach(subscription => subscription.unsubscribe());
}
  1. Update watcher methods to return their subscriptions:
defaultAccountsPayableAccountWatcher(): Subscription {
  return this.exportSettingsForm.controls.employeeMapping.valueChanges.subscribe({
    // ... existing implementation
  });
}

}

private setupCCCExpenseGroupingDateOptions(): void {
Expand Down
Loading