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

improve the activity logs controller

Browse files
src/controllers/invoice/invoice.controller.ts CHANGED
@@ -445,28 +445,17 @@ const updateInvoiceDetails = async (invoiceId: number, billSplit: any[], userId:
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,
@@ -477,44 +466,69 @@ const updateInvoiceDetails = async (invoiceId: number, billSplit: any[], userId:
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),
@@ -525,7 +539,7 @@ const updateInvoiceDetails = async (invoiceId: number, billSplit: any[], userId:
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 || '',
 
445
  const newDetail: any = newInvoiceDetails[i];
446
  const existingDetail: any = existingInvoiceDetails[i] || {};
447
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
448
  if (!existingDetail.id) {
449
+ const newPortfolioName = newDetail.pw_portfolio_id
450
+ ? (await fetchPortfolioById(newDetail.pw_portfolio_id))?.name || ''
451
+ : 'null';
452
+ const newBuildingName = newDetail.pw_building_id
453
+ ? (await fetchBuildingsById(newDetail.pw_building_id))?.name || ''
454
+ : 'null';
455
+ const newUnitName = newDetail.pw_unit_id
456
+ ? (await fetchUnitsById(newDetail.pw_unit_id))?.name || ''
457
+ : 'null';
458
+
459
  const newValue = `${newPortfolioName}, ${newBuildingName}, ${newUnitName}`;
460
  await logInvoiceAction({
461
  invoice_id: invoiceId,
 
466
  new_value: newValue,
467
  });
468
  } else {
469
+ if (existingDetail.pw_portfolio_id !== newDetail.pw_portfolio_id)
470
+ {
471
+ const oldPortfolioName = existingDetail.pw_portfolio_id
472
+ ? (await fetchPortfolioById(existingDetail.pw_portfolio_id))?.name || 'Unknown Portfolio'
473
+ : 'null';
474
+ const newPortfolioName = newDetail.pw_portfolio_id
475
+ ? (await fetchPortfolioById(newDetail.pw_portfolio_id))?.name || ''
476
+ : 'null';
477
+ if (oldPortfolioName !== newPortfolioName) {
478
+ await logInvoiceAction({
479
+ invoice_id: invoiceId,
480
+ user_id: userId,
481
+ activity_type: 'update bill split',
482
+ field_name: 'portfolio',
483
+ old_value: oldPortfolioName,
484
+ new_value: newPortfolioName,
485
+ });
486
+ }
487
  }
488
+ if (existingDetail.pw_building_id !== newDetail.pw_building_id) {
489
+ const oldBuildingName = existingDetail.pw_building_id
490
+ ? (await fetchBuildingsById(existingDetail.pw_building_id))?.name || 'Unknown Building'
491
+ : 'null';
492
+ const newBuildingName = newDetail.pw_building_id
493
+ ? (await fetchBuildingsById(newDetail.pw_building_id))?.name || ''
494
+ : 'null';
495
+ if (oldBuildingName !== newBuildingName) {
496
+ await logInvoiceAction({
497
+ invoice_id: invoiceId,
498
+ user_id: userId,
499
+ activity_type: 'update bill split',
500
+ field_name: 'building',
501
+ old_value: oldBuildingName,
502
+ new_value: newBuildingName,
503
+ });
504
+ }
505
  }
506
+ if (existingDetail.pw_unit_id !== newDetail.pw_unit_id) {
507
+
508
+ const oldUnitName = existingDetail.pw_unit_id
509
+ ? (await fetchUnitsById(existingDetail.pw_unit_id))?.name || 'Unknown Unit'
510
+ : 'null';
511
+ const newUnitName = newDetail.pw_unit_id
512
+ ? (await fetchUnitsById(newDetail.pw_unit_id))?.name || ''
513
+ : 'null';
514
+
515
+ if (oldUnitName !== newUnitName) {
516
+ await logInvoiceAction({
517
+ invoice_id: invoiceId,
518
+ user_id: userId,
519
+ activity_type: 'update bill split',
520
+ field_name: 'unit',
521
+ old_value: oldUnitName,
522
+ new_value: newUnitName,
523
+ });
524
+ }
525
  }
526
 
527
  if (newDetail.amount !== existingDetail.amount) {
528
  await logInvoiceAction({
529
  invoice_id: invoiceId,
530
  user_id: userId,
531
+ activity_type: 'update bill split',
532
  field_name: 'amount',
533
  old_value: String(existingDetail.amount || 0),
534
  new_value: String(newDetail.amount || 0),
 
539
  await logInvoiceAction({
540
  invoice_id: invoiceId,
541
  user_id: userId,
542
+ activity_type: 'update bill split',
543
  field_name: 'description',
544
  old_value: existingDetail.description || '',
545
  new_value: newDetail.description || '',