abhishek-akbari01 commited on
Commit
3145623
·
1 Parent(s): 95c9acc

fix activity log handling

Browse files
src/controllers/invoice/invoice.controller.ts CHANGED
@@ -12,7 +12,7 @@ import PwBuilding from "../../models/pwBuildings";
12
  import PwUnit from "../../models/pwUnits";
13
  import { logger } from '../../utils/logger';
14
  import ErrorLog from "../../models/errorLog";
15
- import { fetchBuildingPropertyManager, fetchWorkorderById } from "../../shared/services/propertyware.service";
16
  import { logInvoiceAction } from "../invoiceActivityLogs.controller";
17
  import InvoiceApproval from "../../models/invoiceApproval";
18
  import Role from "../../models/roles";
@@ -441,9 +441,97 @@ const updateInvoiceDetails = async (invoiceId: number, billSplit: any[], userId:
441
 
442
  const invoiceDetails = await InvoiceDetail.bulkCreate(newInvoiceDetails);
443
 
444
- // Log activity for each detail
445
- for (const detail of newInvoiceDetails) {
446
- await logInvoiceAction({ invoice_id: invoiceId, user_id: userId, activity_type: 'update', field_name: 'invoice_detail', old_value: JSON.stringify(existingInvoiceDetails), new_value: JSON.stringify(detail) });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
447
  }
448
 
449
  return invoiceDetails;
 
12
  import PwUnit from "../../models/pwUnits";
13
  import { logger } from '../../utils/logger';
14
  import ErrorLog from "../../models/errorLog";
15
+ import { fetchBuildingsById, fetchPortfolioById, fetchUnitsById, fetchWorkorderById, fetchBuildingPropertyManager } from "../../shared/services/propertyware.service";
16
  import { logInvoiceAction } from "../invoiceActivityLogs.controller";
17
  import InvoiceApproval from "../../models/invoiceApproval";
18
  import Role from "../../models/roles";
 
441
 
442
  const invoiceDetails = await InvoiceDetail.bulkCreate(newInvoiceDetails);
443
 
444
+ for (let i = 0; i < newInvoiceDetails.length; i++) {
445
+ const newDetail: any = newInvoiceDetails[i];
446
+ const existingDetail: any = existingInvoiceDetails[i] || {};
447
+
448
+ const oldPortfolioName = existingDetail.pw_portfolio_id
449
+ ? (await fetchPortfolioById(existingDetail.pw_portfolio_id))?.name || 'Unknown Portfolio'
450
+ : 'null';
451
+ const newPortfolioName = newDetail.pw_portfolio_id
452
+ ? (await fetchPortfolioById(newDetail.pw_portfolio_id))?.name || ''
453
+ : 'null';
454
+
455
+ const oldBuildingName = existingDetail.pw_building_id
456
+ ? (await fetchBuildingsById(existingDetail.pw_building_id))?.name || 'Unknown Building'
457
+ : 'null';
458
+ const newBuildingName = newDetail.pw_building_id
459
+ ? (await fetchBuildingsById(newDetail.pw_building_id))?.name || ''
460
+ : 'null';
461
+
462
+ const oldUnitName = existingDetail.pw_unit_id
463
+ ? (await fetchUnitsById(existingDetail.pw_unit_id))?.name || 'Unknown Unit'
464
+ : 'null';
465
+ const newUnitName = newDetail.pw_unit_id
466
+ ? (await fetchUnitsById(newDetail.pw_unit_id))?.name || ''
467
+ : 'null';
468
+
469
+ if (!existingDetail.id) {
470
+ const newValue = `${newPortfolioName}, ${newBuildingName}, ${newUnitName}`;
471
+ await logInvoiceAction({
472
+ invoice_id: invoiceId,
473
+ user_id: userId,
474
+ activity_type: 'create',
475
+ field_name: 'bills split',
476
+ old_value: "null",
477
+ new_value: newValue,
478
+ });
479
+ } else {
480
+ if (oldPortfolioName !== newPortfolioName) {
481
+ await logInvoiceAction({
482
+ invoice_id: invoiceId,
483
+ user_id: userId,
484
+ activity_type: 'update',
485
+ field_name: 'portfolio',
486
+ old_value: oldPortfolioName,
487
+ new_value: newPortfolioName,
488
+ });
489
+ }
490
+
491
+ if (oldBuildingName !== newBuildingName) {
492
+ await logInvoiceAction({
493
+ invoice_id: invoiceId,
494
+ user_id: userId,
495
+ activity_type: 'update',
496
+ field_name: 'building',
497
+ old_value: oldBuildingName,
498
+ new_value: newBuildingName,
499
+ });
500
+ }
501
+
502
+ if (oldUnitName !== newUnitName) {
503
+ await logInvoiceAction({
504
+ invoice_id: invoiceId,
505
+ user_id: userId,
506
+ activity_type: 'update',
507
+ field_name: 'unit',
508
+ old_value: oldUnitName,
509
+ new_value: newUnitName,
510
+ });
511
+ }
512
+
513
+ if (newDetail.amount !== existingDetail.amount) {
514
+ await logInvoiceAction({
515
+ invoice_id: invoiceId,
516
+ user_id: userId,
517
+ activity_type: 'update',
518
+ field_name: 'amount',
519
+ old_value: String(existingDetail.amount || 0),
520
+ new_value: String(newDetail.amount || 0),
521
+ });
522
+ }
523
+
524
+ if (newDetail.description !== existingDetail.description) {
525
+ await logInvoiceAction({
526
+ invoice_id: invoiceId,
527
+ user_id: userId,
528
+ activity_type: 'update',
529
+ field_name: 'description',
530
+ old_value: existingDetail.description || '',
531
+ new_value: newDetail.description || '',
532
+ });
533
+ }
534
+ }
535
  }
536
 
537
  return invoiceDetails;
src/shared/interfaces/InvoiceActivityLog.interface.ts CHANGED
@@ -4,7 +4,7 @@ export interface InvoiceActivityLogInterface {
4
  user_id: number;
5
  activity_type: string;
6
  field_name: string;
7
- old_value?: string;
8
  new_value?: string;
9
  created_at?: Date;
10
  }
 
4
  user_id: number;
5
  activity_type: string;
6
  field_name: string;
7
+ old_value?: string | null;
8
  new_value?: string;
9
  created_at?: Date;
10
  }
src/shared/services/propertyware.service.ts CHANGED
@@ -36,7 +36,7 @@ export const fetchBuildingPropertyManager = async (buildingId: number ) => {
36
  };
37
 
38
  export const fetchBuildingsById = async (buildingId: number) => {
39
- const response = await apiClientPW.get(`/portfolios/${buildingId}`);
40
  return response.data;
41
  };
42
 
 
36
  };
37
 
38
  export const fetchBuildingsById = async (buildingId: number) => {
39
+ const response = await apiClientPW.get(`/buildings/${buildingId}`);
40
  return response.data;
41
  };
42