payservedb 9.7.8 → 9.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "9.7.8",
3
+ "version": "9.7.9",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -27,23 +27,46 @@ const PropertyManagerContractSchema = new Schema(
27
27
  ref: 'Facility',
28
28
  required: [true, 'Facility ID is required']
29
29
  },
30
+ digitax: {
31
+
32
+ enabled: Boolean,
33
+
34
+ profileId: {
35
+ type: mongoose.Schema.Types.ObjectId
36
+ },
37
+
38
+ itemId: String,
39
+
40
+ itemName: String,
41
+
42
+ itemClassCode: String,
43
+
44
+ taxTypeCode: String,
45
+
46
+ synced: Boolean,
47
+
48
+ syncedAt: Date,
49
+
50
+ lastError: String
51
+
52
+ },
30
53
  // Lease term details
31
54
  startDate: {
32
55
  type: Date,
33
- required: function() {
56
+ required: function () {
34
57
  return this.status === 'Active';
35
58
  }
36
59
  },
37
60
  endDate: {
38
61
  type: Date,
39
- required: function() {
62
+ required: function () {
40
63
  return this.status === 'Active';
41
64
  }
42
65
  },
43
66
  // Payment details - aligned with LeaseAgreement
44
67
  paymentDueDate: {
45
68
  type: Number,
46
- required: function() {
69
+ required: function () {
47
70
  return this.status === 'Active';
48
71
  },
49
72
  min: [1, 'Payment due date must be between 1 and 31'],
@@ -53,19 +76,19 @@ const PropertyManagerContractSchema = new Schema(
53
76
  type: String,
54
77
  enum: ['Monthly', 'Quarterly', 'Annually'],
55
78
  default: 'Monthly',
56
- required: function() {
79
+ required: function () {
57
80
  return this.status === 'Active';
58
81
  }
59
82
  },
60
- nextInvoiceDate: {
61
- type: Date
83
+ nextInvoiceDate: {
84
+ type: Date
62
85
  },
63
- lastInvoiceDate: {
64
- type: Date
86
+ lastInvoiceDate: {
87
+ type: Date
65
88
  },
66
- autoSend: {
67
- type: Boolean,
68
- default: false
89
+ autoSend: {
90
+ type: Boolean,
91
+ default: false
69
92
  },
70
93
  balanceBroughtForward: {
71
94
  type: Number,
@@ -83,7 +106,7 @@ const PropertyManagerContractSchema = new Schema(
83
106
  required: [true, 'Management fee value is required'],
84
107
  min: [0, 'Management fee value cannot be negative'],
85
108
  validate: {
86
- validator: function(value) {
109
+ validator: function (value) {
87
110
  // If type is percentage, value should not exceed 100
88
111
  if (this.managementFee.type === 'percentage') {
89
112
  return value <= 100;
@@ -261,37 +284,37 @@ PropertyManagerContractSchema.pre('save', function (next) {
261
284
  next(new Error('End date must be after start date'));
262
285
  return;
263
286
  }
264
-
287
+
265
288
  // Validate units array
266
289
  if (!this.units || this.units.length === 0) {
267
290
  next(new Error('At least one unit must be specified'));
268
291
  return;
269
292
  }
270
-
293
+
271
294
  // Validate management fee
272
295
  if (!this.managementFee || !this.managementFee.type || this.managementFee.value === undefined) {
273
296
  next(new Error('Management fee type and value are required'));
274
297
  return;
275
298
  }
276
-
299
+
277
300
  // Validate management fee percentage
278
301
  if (this.managementFee.type === 'percentage' && this.managementFee.value > 100) {
279
302
  next(new Error('Management fee percentage cannot exceed 100%'));
280
303
  return;
281
304
  }
282
-
305
+
283
306
  // Validate management fee value is non-negative
284
307
  if (this.managementFee.value < 0) {
285
308
  next(new Error('Management fee value cannot be negative'));
286
309
  return;
287
310
  }
288
-
311
+
289
312
  // Validate payment due date range
290
313
  if (this.paymentDueDate && (this.paymentDueDate < 1 || this.paymentDueDate > 31)) {
291
314
  next(new Error('Payment due date must be between 1 and 31'));
292
315
  return;
293
316
  }
294
-
317
+
295
318
  // Note: startDate/endDate/paymentDueDate/frequency are no longer
296
319
  // required for Active status - contracts are always Active regardless
297
320
  // of whether a lease has populated those fields yet. They remain
@@ -303,9 +326,9 @@ PropertyManagerContractSchema.pre('save', function (next) {
303
326
  });
304
327
 
305
328
  // Method to sync with lease data
306
- PropertyManagerContractSchema.methods.syncWithLeaseData = async function(leaseData) {
329
+ PropertyManagerContractSchema.methods.syncWithLeaseData = async function (leaseData) {
307
330
  if (!leaseData) return;
308
-
331
+
309
332
  // Update contract fields from lease data
310
333
  this.startDate = leaseData.leaseTerms?.startDate || this.startDate;
311
334
  this.endDate = leaseData.leaseTerms?.endDate || this.endDate;
@@ -315,40 +338,40 @@ PropertyManagerContractSchema.methods.syncWithLeaseData = async function(leaseDa
315
338
  this.autoSend = leaseData.billingCycle?.autoSend !== undefined ? leaseData.billingCycle.autoSend : this.autoSend;
316
339
  this.nextInvoiceDate = leaseData.billingCycle?.nextInvoiceDate || this.nextInvoiceDate;
317
340
  this.lastInvoiceDate = leaseData.billingCycle?.lastInvoiceDate || this.lastInvoiceDate;
318
-
341
+
319
342
  // Automatically activate contract if all required fields are present
320
343
  if (this.startDate && this.endDate && this.paymentDueDate && this.frequency && this.status === 'Inactive') {
321
344
  this.status = 'Active';
322
345
  }
323
-
346
+
324
347
  return this.save();
325
348
  };
326
349
 
327
350
  // Method to check if contract can be activated
328
- PropertyManagerContractSchema.methods.canBeActivated = function() {
351
+ PropertyManagerContractSchema.methods.canBeActivated = function () {
329
352
  return !!(this.startDate && this.endDate && this.paymentDueDate && this.frequency);
330
353
  };
331
354
 
332
355
  // Method to get missing required fields
333
- PropertyManagerContractSchema.methods.getMissingRequiredFields = function() {
356
+ PropertyManagerContractSchema.methods.getMissingRequiredFields = function () {
334
357
  const requiredFields = ['startDate', 'endDate', 'paymentDueDate', 'frequency'];
335
358
  return requiredFields.filter(field => !this[field]);
336
359
  };
337
360
 
338
361
  // Virtual for contract completion status
339
- PropertyManagerContractSchema.virtual('isComplete').get(function() {
362
+ PropertyManagerContractSchema.virtual('isComplete').get(function () {
340
363
  return this.canBeActivated();
341
364
  });
342
365
 
343
366
  // Virtual for contract duration in days
344
- PropertyManagerContractSchema.virtual('durationInDays').get(function() {
367
+ PropertyManagerContractSchema.virtual('durationInDays').get(function () {
345
368
  if (!this.startDate || !this.endDate) return null;
346
369
  const diffTime = Math.abs(this.endDate - this.startDate);
347
370
  return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
348
371
  });
349
372
 
350
373
  // Virtual for contract remaining days
351
- PropertyManagerContractSchema.virtual('remainingDays').get(function() {
374
+ PropertyManagerContractSchema.virtual('remainingDays').get(function () {
352
375
  if (!this.endDate) return null;
353
376
  const now = new Date();
354
377
  if (now > this.endDate) return 0;
@@ -357,9 +380,9 @@ PropertyManagerContractSchema.virtual('remainingDays').get(function() {
357
380
  });
358
381
 
359
382
  // Virtual for management fee display
360
- PropertyManagerContractSchema.virtual('managementFeeDisplay').get(function() {
383
+ PropertyManagerContractSchema.virtual('managementFeeDisplay').get(function () {
361
384
  if (!this.managementFee) return null;
362
-
385
+
363
386
  if (this.managementFee.type === 'percentage') {
364
387
  return `${this.managementFee.value}%`;
365
388
  } else {
@@ -379,46 +402,46 @@ PropertyManagerContractSchema.index({ createdAt: -1 }); // For recent contracts
379
402
  PropertyManagerContractSchema.index({ startDate: 1, endDate: 1 }); // For date range queries
380
403
 
381
404
  // Compound indexes for common query patterns
382
- PropertyManagerContractSchema.index({
383
- facilityId: 1,
384
- propertyManager: 1,
385
- status: 1
405
+ PropertyManagerContractSchema.index({
406
+ facilityId: 1,
407
+ propertyManager: 1,
408
+ status: 1
386
409
  });
387
410
 
388
- PropertyManagerContractSchema.index({
389
- facilityId: 1,
390
- customerId: 1,
391
- status: 1
411
+ PropertyManagerContractSchema.index({
412
+ facilityId: 1,
413
+ customerId: 1,
414
+ status: 1
392
415
  });
393
416
 
394
417
  // Text index for searching contract names
395
- PropertyManagerContractSchema.index({
396
- contractName: 'text'
418
+ PropertyManagerContractSchema.index({
419
+ contractName: 'text'
397
420
  });
398
421
 
399
422
  // Static methods for common queries
400
- PropertyManagerContractSchema.statics.findActiveContractsByFacility = function(facilityId) {
423
+ PropertyManagerContractSchema.statics.findActiveContractsByFacility = function (facilityId) {
401
424
  return this.find({
402
425
  facilityId: facilityId,
403
426
  status: 'Active'
404
427
  }).populate('propertyManager', 'fullName email')
405
- .populate('units', 'name unitType')
406
- .populate('customerId', 'firstName lastName email');
428
+ .populate('units', 'name unitType')
429
+ .populate('customerId', 'firstName lastName email');
407
430
  };
408
431
 
409
- PropertyManagerContractSchema.statics.findContractsByPropertyManager = function(propertyManagerId, facilityId) {
432
+ PropertyManagerContractSchema.statics.findContractsByPropertyManager = function (propertyManagerId, facilityId) {
410
433
  return this.find({
411
434
  propertyManager: propertyManagerId,
412
435
  facilityId: facilityId
413
436
  }).populate('units', 'name unitType')
414
- .populate('customerId', 'firstName lastName email')
415
- .sort({ createdAt: -1 });
437
+ .populate('customerId', 'firstName lastName email')
438
+ .sort({ createdAt: -1 });
416
439
  };
417
440
 
418
- PropertyManagerContractSchema.statics.findExpiringContracts = function(facilityId, daysAhead = 30) {
441
+ PropertyManagerContractSchema.statics.findExpiringContracts = function (facilityId, daysAhead = 30) {
419
442
  const futureDate = new Date();
420
443
  futureDate.setDate(futureDate.getDate() + daysAhead);
421
-
444
+
422
445
  return this.find({
423
446
  facilityId: facilityId,
424
447
  status: 'Active',
@@ -427,15 +450,15 @@ PropertyManagerContractSchema.statics.findExpiringContracts = function(facilityI
427
450
  $lte: futureDate
428
451
  }
429
452
  }).populate('propertyManager', 'fullName email')
430
- .populate('units', 'name unitType')
431
- .populate('customerId', 'firstName lastName email')
432
- .sort({ endDate: 1 });
453
+ .populate('units', 'name unitType')
454
+ .populate('customerId', 'firstName lastName email')
455
+ .sort({ endDate: 1 });
433
456
  };
434
457
 
435
- PropertyManagerContractSchema.statics.findContractsRequiringInvoicing = function(facilityId) {
458
+ PropertyManagerContractSchema.statics.findContractsRequiringInvoicing = function (facilityId) {
436
459
  const today = new Date();
437
460
  today.setHours(0, 0, 0, 0);
438
-
461
+
439
462
  return this.find({
440
463
  facilityId: facilityId,
441
464
  status: 'Active',
@@ -443,21 +466,21 @@ PropertyManagerContractSchema.statics.findContractsRequiringInvoicing = function
443
466
  $lte: today
444
467
  }
445
468
  }).populate('propertyManager', 'fullName email')
446
- .populate('units', 'name unitType')
447
- .populate('customerId', 'firstName lastName email');
469
+ .populate('units', 'name unitType')
470
+ .populate('customerId', 'firstName lastName email');
448
471
  };
449
472
 
450
- PropertyManagerContractSchema.statics.findContractsByFeeType = function(facilityId, feeType) {
473
+ PropertyManagerContractSchema.statics.findContractsByFeeType = function (facilityId, feeType) {
451
474
  return this.find({
452
475
  facilityId: facilityId,
453
476
  'managementFee.type': feeType
454
477
  }).populate('propertyManager', 'fullName email')
455
- .populate('units', 'name unitType')
456
- .populate('customerId', 'firstName lastName email')
457
- .sort({ 'managementFee.value': -1 });
478
+ .populate('units', 'name unitType')
479
+ .populate('customerId', 'firstName lastName email')
480
+ .sort({ 'managementFee.value': -1 });
458
481
  };
459
482
 
460
- PropertyManagerContractSchema.statics.getContractStatistics = function(facilityId) {
483
+ PropertyManagerContractSchema.statics.getContractStatistics = function (facilityId) {
461
484
  return this.aggregate([
462
485
  {
463
486
  $match: { facilityId: mongoose.Types.ObjectId(facilityId) }
@@ -474,12 +497,12 @@ PropertyManagerContractSchema.statics.getContractStatistics = function(facilityI
474
497
  };
475
498
 
476
499
  // Instance methods for contract management
477
- PropertyManagerContractSchema.methods.calculateNextInvoiceDate = function() {
500
+ PropertyManagerContractSchema.methods.calculateNextInvoiceDate = function () {
478
501
  if (!this.lastInvoiceDate && !this.startDate) return null;
479
-
502
+
480
503
  const baseDate = this.lastInvoiceDate || this.startDate;
481
504
  const nextDate = new Date(baseDate);
482
-
505
+
483
506
  switch (this.frequency) {
484
507
  case 'Monthly':
485
508
  nextDate.setMonth(nextDate.getMonth() + 1);
@@ -493,61 +516,61 @@ PropertyManagerContractSchema.methods.calculateNextInvoiceDate = function() {
493
516
  default:
494
517
  nextDate.setMonth(nextDate.getMonth() + 1); // Default to monthly
495
518
  }
496
-
519
+
497
520
  // Set to the payment due date of the month
498
521
  if (this.paymentDueDate) {
499
522
  const lastDayOfMonth = new Date(nextDate.getFullYear(), nextDate.getMonth() + 1, 0).getDate();
500
523
  const dueDate = Math.min(this.paymentDueDate, lastDayOfMonth);
501
524
  nextDate.setDate(dueDate);
502
525
  }
503
-
526
+
504
527
  return nextDate;
505
528
  };
506
529
 
507
- PropertyManagerContractSchema.methods.updateInvoiceDates = async function(invoiceDate = new Date()) {
530
+ PropertyManagerContractSchema.methods.updateInvoiceDates = async function (invoiceDate = new Date()) {
508
531
  this.lastInvoiceDate = invoiceDate;
509
532
  this.nextInvoiceDate = this.calculateNextInvoiceDate();
510
533
  return this.save();
511
534
  };
512
535
 
513
- PropertyManagerContractSchema.methods.activateContract = async function() {
536
+ PropertyManagerContractSchema.methods.activateContract = async function () {
514
537
  if (!this.canBeActivated()) {
515
538
  throw new Error(`Cannot activate contract: missing required fields - ${this.getMissingRequiredFields().join(', ')}`);
516
539
  }
517
-
540
+
518
541
  this.status = 'Active';
519
-
542
+
520
543
  // Set next invoice date if not already set
521
544
  if (!this.nextInvoiceDate) {
522
545
  this.nextInvoiceDate = this.calculateNextInvoiceDate();
523
546
  }
524
-
547
+
525
548
  return this.save();
526
549
  };
527
550
 
528
- PropertyManagerContractSchema.methods.suspendContract = async function(reason) {
551
+ PropertyManagerContractSchema.methods.suspendContract = async function (reason) {
529
552
  this.status = 'Suspended';
530
553
  this.suspensionReason = reason;
531
554
  return this.save();
532
555
  };
533
556
 
534
- PropertyManagerContractSchema.methods.terminateContract = async function(reason, terminationDate = new Date()) {
557
+ PropertyManagerContractSchema.methods.terminateContract = async function (reason, terminationDate = new Date()) {
535
558
  this.status = 'Terminated';
536
559
  this.terminationReason = reason;
537
560
  this.terminationDate = terminationDate;
538
561
  return this.save();
539
562
  };
540
563
 
541
- PropertyManagerContractSchema.methods.completeContract = async function() {
564
+ PropertyManagerContractSchema.methods.completeContract = async function () {
542
565
  this.status = 'Completed';
543
566
  this.completionDate = new Date();
544
567
  return this.save();
545
568
  };
546
569
 
547
570
  // Method to calculate management fee for a given amount
548
- PropertyManagerContractSchema.methods.calculateManagementFee = function(baseAmount) {
571
+ PropertyManagerContractSchema.methods.calculateManagementFee = function (baseAmount) {
549
572
  if (!this.managementFee) return 0;
550
-
573
+
551
574
  if (this.managementFee.type === 'percentage') {
552
575
  return (baseAmount * this.managementFee.value) / 100;
553
576
  } else {
@@ -556,7 +579,7 @@ PropertyManagerContractSchema.methods.calculateManagementFee = function(baseAmou
556
579
  };
557
580
 
558
581
  // Method to get contract summary
559
- PropertyManagerContractSchema.methods.getContractSummary = function() {
582
+ PropertyManagerContractSchema.methods.getContractSummary = function () {
560
583
  return {
561
584
  id: this._id,
562
585
  contractName: this.contractName,
@@ -571,16 +594,16 @@ PropertyManagerContractSchema.methods.getContractSummary = function() {
571
594
  };
572
595
 
573
596
  // Pre-findOneAndUpdate middleware to maintain data integrity
574
- PropertyManagerContractSchema.pre(['findOneAndUpdate', 'updateOne', 'updateMany'], function() {
597
+ PropertyManagerContractSchema.pre(['findOneAndUpdate', 'updateOne', 'updateMany'], function () {
575
598
  const update = this.getUpdate();
576
-
599
+
577
600
  // If status is being changed to Active, ensure required fields are present
578
601
  if (update.$set && update.$set.status === 'Active') {
579
602
  const requiredFields = ['startDate', 'endDate', 'paymentDueDate', 'frequency'];
580
603
  // Note: We can't validate here because we don't have access to the full document
581
604
  // Validation should be done in the application logic before calling update
582
605
  }
583
-
606
+
584
607
  // Update the updatedAt field
585
608
  if (update.$set) {
586
609
  update.$set.updatedAt = new Date();
@@ -590,17 +613,17 @@ PropertyManagerContractSchema.pre(['findOneAndUpdate', 'updateOne', 'updateMany'
590
613
  });
591
614
 
592
615
  // Post-save middleware for logging and notifications
593
- PropertyManagerContractSchema.post('save', function(doc) {
616
+ PropertyManagerContractSchema.post('save', function (doc) {
594
617
  // Log contract creation/updates
595
618
  console.log(`Property Manager Contract ${doc.isNew ? 'created' : 'updated'}: ${doc.contractName} (${doc._id})`);
596
-
619
+
597
620
  // Here you could add webhook calls, email notifications, etc.
598
621
  // Example: notify property manager of contract status changes
599
622
  if (this.isModified('status')) {
600
623
  console.log(`Contract status changed to: ${doc.status}`);
601
624
  // Add notification logic here
602
625
  }
603
-
626
+
604
627
  // Log invoice date updates
605
628
  if (this.isModified('nextInvoiceDate') || this.isModified('lastInvoiceDate')) {
606
629
  console.log(`Invoice dates updated for contract: ${doc.contractName}`);
@@ -608,7 +631,7 @@ PropertyManagerContractSchema.post('save', function(doc) {
608
631
  });
609
632
 
610
633
  // Error handling for validation errors
611
- PropertyManagerContractSchema.post('save', function(error, doc, next) {
634
+ PropertyManagerContractSchema.post('save', function (error, doc, next) {
612
635
  if (error.name === 'ValidationError') {
613
636
  const messages = Object.values(error.errors).map(err => err.message);
614
637
  next(new Error(`Contract validation failed: ${messages.join(', ')}`));
@@ -618,9 +641,9 @@ PropertyManagerContractSchema.post('save', function(error, doc, next) {
618
641
  });
619
642
 
620
643
  // JSON transformation to remove sensitive data when returning to client
621
- PropertyManagerContractSchema.methods.toJSON = function() {
644
+ PropertyManagerContractSchema.methods.toJSON = function () {
622
645
  const contract = this.toObject();
623
-
646
+
624
647
  // Add computed fields
625
648
  contract.isComplete = this.isComplete;
626
649
  contract.canBeActivated = this.canBeActivated();
@@ -629,7 +652,7 @@ PropertyManagerContractSchema.methods.toJSON = function() {
629
652
  contract.remainingDays = this.remainingDays;
630
653
  contract.managementFeeDisplay = this.managementFeeDisplay;
631
654
  contract.contractSummary = this.getContractSummary();
632
-
655
+
633
656
  return contract;
634
657
  };
635
658