@tomei/rental 0.8.1 → 0.8.3

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.
Files changed (51) hide show
  1. package/.commitlintrc.json +22 -22
  2. package/.eslintrc +16 -16
  3. package/.eslintrc.js +35 -35
  4. package/.gitlab-ci.yml +16 -16
  5. package/.husky/commit-msg +15 -15
  6. package/.husky/pre-commit +7 -7
  7. package/.prettierrc +4 -4
  8. package/Jenkinsfile +51 -51
  9. package/README.md +8 -8
  10. package/dist/src/components/rental/rental.d.ts +3 -0
  11. package/dist/src/components/rental/rental.js +50 -32
  12. package/dist/src/components/rental/rental.js.map +1 -1
  13. package/dist/tsconfig.tsbuildinfo +1 -1
  14. package/jest.config.js +10 -10
  15. package/migrations/booking-table-migration.js +79 -79
  16. package/migrations/joint-hirer-table-migration.js +52 -52
  17. package/migrations/rental-aggrement-table-migration.js +22 -22
  18. package/migrations/rental-price-table-migration.js +32 -32
  19. package/migrations/rental-table-migrations.js +96 -96
  20. package/package.json +75 -75
  21. package/sonar-project.properties +12 -12
  22. package/src/components/agreement/agreement.repository.ts +54 -54
  23. package/src/components/agreement/agreement.ts +43 -43
  24. package/src/components/booking/booking.repository.ts +51 -51
  25. package/src/components/booking/booking.ts +291 -291
  26. package/src/components/joint-hirer/joint-hirer.repository.ts +54 -54
  27. package/src/components/rental/rental.repository.ts +51 -51
  28. package/src/components/rental/rental.ts +714 -670
  29. package/src/components/rental-price/rental-price.repository.ts +54 -54
  30. package/src/components/rental-price/rental-price.ts +100 -100
  31. package/src/database.ts +27 -27
  32. package/src/enum/account-type.enum.ts +4 -4
  33. package/src/enum/booking.enum.ts +5 -5
  34. package/src/enum/index.ts +5 -5
  35. package/src/enum/rental-status.enum.ts +39 -39
  36. package/src/index.ts +28 -28
  37. package/src/interfaces/agreement-attr.interface.ts +4 -4
  38. package/src/interfaces/booking-attr.interface.ts +19 -19
  39. package/src/interfaces/index.ts +13 -13
  40. package/src/interfaces/joint-hirer-attr.interface.ts +10 -10
  41. package/src/interfaces/rental-attr.interface.ts +25 -25
  42. package/src/interfaces/rental-find-all-search-attr.interface.ts +11 -11
  43. package/src/interfaces/rental-price-attr.interface.ts +7 -7
  44. package/src/models/agreement.entity.ts +26 -26
  45. package/src/models/booking.entity.ts +105 -105
  46. package/src/models/index.ts +13 -13
  47. package/src/models/joint-hirer.entity.ts +63 -63
  48. package/src/models/rental-price.entity.ts +38 -38
  49. package/src/models/rental.entity.ts +133 -133
  50. package/tsconfig.build.json +5 -5
  51. package/tsconfig.json +23 -23
@@ -1,670 +1,714 @@
1
- import { IRentalAttr } from '../../interfaces/rental-attr.interface';
2
- import { RentalStatusEnum } from '../../enum/rental-status.enum';
3
- import { RentalRepository } from './rental.repository';
4
- import { ClassError, ObjectBase } from '@tomei/general';
5
- import { ApplicationConfig } from '@tomei/config';
6
- import { LoginUser } from '@tomei/sso';
7
- import { RentalPrice } from '../rental-price/rental-price';
8
- import { Op, QueryTypes } from 'sequelize';
9
- import { ActionEnum, Activity } from '@tomei/activity-history';
10
- import { IRentalFindAllSearchAttr } from '../../interfaces/rental-find-all-search-attr.interface';
11
- import { RentalAccountTypeEnum } from '../../enum/account-type.enum';
12
- import { JointHirer } from '../joint-hirer/joint-hirer';
13
- import { JointHirerModel } from '../../models/joint-hirer.entity';
14
- import { AgreementRepository } from '../agreement/agreement.repository';
15
- import * as rentalDb from '../../database';
16
- import { RentalModel } from '../../models/rental.entity';
17
-
18
- export class Rental extends ObjectBase {
19
- ObjectId: string;
20
- ObjectName: string;
21
- ObjectType = 'Rental';
22
- TableName: string;
23
- CustomerId: string;
24
- CustomerType: string;
25
- ItemId: string;
26
- ItemType: string;
27
- PriceId: string;
28
- StartDateTime: Date;
29
- EndDateTime: Date;
30
- CancelRemarks: string;
31
- TerminateRemarks: string;
32
- AgreementNo: string;
33
- AccountType: RentalAccountTypeEnum;
34
- JointHirers: JointHirer[] = [];
35
- protected _Status: RentalStatusEnum;
36
- protected _EscheatmentYN: string = 'N';
37
- protected _CreatedById: string;
38
- protected _CreatedAt: Date;
39
- protected _UpdatedById: string;
40
- protected _UpdatedAt: Date;
41
- protected static _Repo = new RentalRepository();
42
- protected static _AgreementRepo = new AgreementRepository();
43
-
44
- get RentalId(): string {
45
- return this.ObjectId;
46
- }
47
-
48
- set RentalId(value: string) {
49
- this.ObjectId = value;
50
- }
51
-
52
- get Status(): RentalStatusEnum {
53
- return this._Status;
54
- }
55
-
56
- get EscheatmentYN(): string {
57
- return this._EscheatmentYN;
58
- }
59
-
60
- get CreatedById(): string {
61
- return this._CreatedById;
62
- }
63
-
64
- get CreatedAt(): Date {
65
- return this._CreatedAt;
66
- }
67
-
68
- get UpdatedById(): string {
69
- return this._UpdatedById;
70
- }
71
-
72
- get UpdatedAt(): Date {
73
- return this._UpdatedAt;
74
- }
75
-
76
- private setTerminated() {
77
- this._Status = RentalStatusEnum.TERMINATED;
78
- }
79
-
80
- protected constructor(rentalAttr?: IRentalAttr) {
81
- super();
82
- if (rentalAttr) {
83
- this.RentalId = rentalAttr.RentalId;
84
- this.CustomerId = rentalAttr.CustomerId;
85
- this.CustomerType = rentalAttr.CustomerType;
86
- this.ItemId = rentalAttr.ItemId;
87
- this.ItemType = rentalAttr.ItemType;
88
- this.PriceId = rentalAttr.PriceId;
89
- this.StartDateTime = rentalAttr.StartDateTime;
90
- this.EndDateTime = rentalAttr.EndDateTime;
91
- this.CancelRemarks = rentalAttr.CancelRemarks;
92
- this.TerminateRemarks = rentalAttr.TerminateRemarks;
93
- this.AgreementNo = rentalAttr.AgreementNo;
94
- this.AccountType = rentalAttr.AccountType;
95
- this._Status = rentalAttr.Status;
96
- this._EscheatmentYN = rentalAttr.EscheatmentYN;
97
- this._CreatedById = rentalAttr.CreatedById;
98
- this._CreatedAt = rentalAttr.CreatedAt;
99
- this._UpdatedById = rentalAttr.UpdatedById;
100
- this._UpdatedAt = rentalAttr.UpdatedAt;
101
- }
102
- }
103
-
104
- public static async init(dbTransaction?: any, rentalId?: string) {
105
- try {
106
- if (rentalId) {
107
- const rental = await Rental._Repo.findByPk(rentalId, dbTransaction);
108
- if (rental) {
109
- return new Rental(rental);
110
- } else {
111
- throw new ClassError('Rental', 'RentalErrMsg00', 'Rental Not Found');
112
- }
113
- }
114
- return new Rental();
115
- } catch (error) {
116
- throw new ClassError(
117
- 'Rental',
118
- 'RentalErrMsg00',
119
- 'Failed To Initialize Price',
120
- );
121
- }
122
- }
123
-
124
- /**
125
- * Create a new Rental record.
126
- * @param {RentalPrice} rentalPrice - The rental pricing information.
127
- * @param {LoginUser} loginUser - The user initiating the creation.
128
- * @param {any} [dbTransaction] - Optional database transaction for atomic operations.
129
- * @throws {ClassError} Throws an error if:
130
- * 1. loginUser does not have 'Rental - Create' privilege.
131
- * 2. Rental item is not available at the current date.
132
- * @returns {Promise<any>} A Promise resolving to the created rental record.
133
- */
134
- public async create(
135
- rentalPrice: RentalPrice,
136
- loginUser: LoginUser,
137
- jointHirers?: JointHirer[],
138
- dbTransaction?: any,
139
- ): Promise<any> {
140
- try {
141
- /*Part 1: Check Privilege*/
142
- const systemCode =
143
- ApplicationConfig.getComponentConfigValue('system-code');
144
- const isPrivileged = await loginUser.checkPrivileges(
145
- systemCode,
146
- 'Rental - Create',
147
- );
148
-
149
- if (!isPrivileged) {
150
- throw new ClassError(
151
- 'Rental',
152
- 'RentalErrMsg01',
153
- "You do not have 'Rental - Create' privilege.",
154
- );
155
- }
156
-
157
- /*Part 2: Make Sure Rental Item Available*/
158
- const isItemAvailable = await Rental.isItemAvailable(
159
- this.ItemId,
160
- this.ItemType,
161
- this.StartDateTime,
162
- this.EndDateTime,
163
- dbTransaction,
164
- );
165
-
166
- if (!isItemAvailable) {
167
- throw new ClassError(
168
- 'Rental',
169
- 'RentalErrMsg02',
170
- 'Rental Item is not available at current date.',
171
- );
172
- }
173
-
174
- // Part 3: Create Rental Agreement Record
175
- // Call Rental._AgreementRepo create method by passing:
176
- // AgreementNo: this.AgreementNo
177
- // Status: "Not Generated"
178
- // dbTransaction
179
- await Rental._AgreementRepo.create(
180
- {
181
- AgreementNo: this.AgreementNo,
182
- Status: 'Not Generated',
183
- },
184
- {
185
- transaction: dbTransaction,
186
- },
187
- );
188
-
189
- /*Part 4: Insert Rental & The Agreed Rental Price*/
190
- await rentalPrice.create(loginUser, dbTransaction);
191
-
192
- this.PriceId = rentalPrice.PriceId;
193
-
194
- this.RentalId = this.createId();
195
- if (!this.Status) {
196
- this._Status = RentalStatusEnum.ACTIVE;
197
- }
198
- this._CreatedById = loginUser.ObjectId;
199
- this._UpdatedById = loginUser.ObjectId;
200
- this._CreatedAt = new Date();
201
- this._UpdatedAt = new Date();
202
-
203
- const data = {
204
- RentalId: this.RentalId,
205
- CustomerId: this.CustomerId,
206
- CustomerType: this.CustomerType,
207
- ItemId: this.ItemId,
208
- ItemType: this.ItemType,
209
- PriceId: this.PriceId,
210
- StartDateTime: this.StartDateTime,
211
- EndDateTime: this.EndDateTime,
212
- CancelRemarks: this.CancelRemarks,
213
- TerminateRemarks: this.TerminateRemarks,
214
- AgreementNo: this.AgreementNo,
215
- AccountType: this.AccountType,
216
- Status: this.Status,
217
- EscheatmentYN: this.EscheatmentYN,
218
- CreatedById: this.CreatedById,
219
- CreatedAt: this.CreatedAt,
220
- UpdatedById: this.UpdatedById,
221
- UpdatedAt: this.UpdatedAt,
222
- };
223
-
224
- await Rental._Repo.create(data, {
225
- transaction: dbTransaction,
226
- });
227
-
228
- //For every data inside Param.jointHirers, update the rentalId and call jointHirer.create
229
- if (jointHirers) {
230
- for (let i = 0; i < jointHirers.length; i++) {
231
- jointHirers[i].RentalId = this.RentalId;
232
- await jointHirers[i].create(loginUser, dbTransaction);
233
- this.JointHirers.push(jointHirers[i]);
234
- }
235
- }
236
-
237
- /*Part 5: Record Create Rental Activity*/
238
- const activity = new Activity();
239
- activity.ActivityId = activity.createId();
240
- activity.Action = ActionEnum.ADD;
241
- activity.Description = 'Add Rental';
242
- activity.EntityType = 'Rental';
243
- activity.EntityId = this.RentalId;
244
- activity.EntityValueBefore = JSON.stringify({});
245
- activity.EntityValueAfter = JSON.stringify(data);
246
- await activity.create(loginUser.ObjectId, dbTransaction);
247
-
248
- /*Part 6: Return Result*/
249
- return this;
250
- } catch (error) {
251
- throw error;
252
- }
253
- }
254
-
255
- public static async isItemAvailable(
256
- itemId: string,
257
- itemType: string,
258
- startDateTime: Date,
259
- endDateTime: Date,
260
- dbTransaction?: any,
261
- ) {
262
- try {
263
- const item = await Rental._Repo.findOne({
264
- where: {
265
- ItemId: itemId,
266
- ItemType: itemType,
267
- StartDateTime: {
268
- [Op.lte]: endDateTime,
269
- },
270
- EndDateTime: {
271
- [Op.gte]: startDateTime,
272
- },
273
- },
274
- transaction: dbTransaction,
275
- });
276
-
277
- if (item) {
278
- return false;
279
- } else {
280
- return true;
281
- }
282
- } catch (error) {
283
- throw error;
284
- }
285
- }
286
-
287
- public static async findAll(
288
- loginUser: LoginUser,
289
- dbTransaction: any,
290
- page?: number,
291
- row?: number,
292
- search?: IRentalFindAllSearchAttr,
293
- ) {
294
- try {
295
- const systemCode = await ApplicationConfig.getComponentConfigValue(
296
- 'system-code',
297
- );
298
-
299
- const isPrivileged = await loginUser.checkPrivileges(
300
- systemCode,
301
- 'Rental - View',
302
- );
303
-
304
- if (!isPrivileged) {
305
- throw new ClassError(
306
- 'Rental',
307
- 'RentalErrMsg01',
308
- "You do not have 'Rental - View' privilege.",
309
- );
310
- }
311
-
312
- const queryObj: any = {};
313
-
314
- let options: any = {
315
- transaction: dbTransaction,
316
- order: [['CreatedAt', 'DESC']],
317
- };
318
-
319
- if (page && row) {
320
- options = {
321
- ...options,
322
- limit: row,
323
- offset: row * (page - 1),
324
- };
325
- }
326
-
327
- if (search) {
328
- Object.entries(search).forEach(([key, value]) => {
329
- if (key === 'StartDateTime') {
330
- queryObj[key] = {
331
- [Op.gte]: value,
332
- };
333
- } else if (key === 'EndDateTime') {
334
- queryObj[key] = {
335
- [Op.lte]: value,
336
- };
337
- } else if (key === 'CustomerId') {
338
- queryObj[key] = {
339
- [Op.substring]: value,
340
- };
341
-
342
- options.include = [
343
- {
344
- model: JointHirerModel,
345
- required: false,
346
- where: {
347
- CustomerId: {
348
- [Op.substring]: value,
349
- },
350
- },
351
- },
352
- ];
353
- } else {
354
- queryObj[key] = {
355
- [Op.substring]: value,
356
- };
357
- }
358
- });
359
-
360
- options = {
361
- ...options,
362
- where: queryObj,
363
- };
364
- }
365
-
366
- return await Rental._Repo.findAndCountAll(options);
367
- } catch (err) {
368
- throw err;
369
- }
370
- }
371
-
372
- public static async checkActiveByItemId(
373
- loginUser: LoginUser,
374
- itemId: string,
375
- itemType: string,
376
- dbTransaction?: any,
377
- ) {
378
- try {
379
- const systemCode = await ApplicationConfig.getComponentConfigValue(
380
- 'system-code',
381
- );
382
-
383
- const isPrivileged = await loginUser.checkPrivileges(
384
- systemCode,
385
- 'Rental - View',
386
- );
387
-
388
- if (!isPrivileged) {
389
- throw new ClassError(
390
- 'Rental',
391
- 'RentalErrMsg01',
392
- "You do not have 'Rental - View' privilege.",
393
- );
394
- }
395
-
396
- const queryObj: any = {
397
- ItemId: itemId,
398
- ItemType: itemType,
399
- Status: RentalStatusEnum.ACTIVE,
400
- };
401
-
402
- const options: any = {
403
- transaction: dbTransaction,
404
- where: queryObj,
405
- };
406
-
407
- const rental = await Rental._Repo.findOne(options);
408
-
409
- if (rental) {
410
- return true;
411
- } else {
412
- return false;
413
- }
414
- } catch (err) {
415
- throw err;
416
- }
417
- }
418
-
419
- /**
420
- * Sets the StartDateTime property of the Rental class as custom setter.
421
- * @param {Date} startDateTime - The start date and time of the rental.
422
- * @throws {ClassError} Throws an error if:
423
- * 1. startDateTime is a past date.
424
- * 2. startDateTime is more than the EndDateTime.
425
- * @returns {void}
426
- */
427
- setStartDateTime(startDateTime: Date): void {
428
- const startDateTimeObject = new Date(startDateTime);
429
- startDateTimeObject.setHours(0, 0, 0, 0);
430
-
431
- /*Part 1: Make Sure Future Date Time*/
432
- if (startDateTimeObject < new Date(new Date().setHours(0, 0, 0, 0))) {
433
- throw new ClassError(
434
- 'Rental',
435
- 'RentalErrMsg02',
436
- 'StartDateTime cannot be a past datetime.',
437
- );
438
- }
439
-
440
- /*Part 2: Make Sure Less Than End Date Time*/
441
- if (this.EndDateTime && startDateTimeObject > this.EndDateTime) {
442
- throw new ClassError(
443
- 'Rental',
444
- 'RentalErrMsg03',
445
- 'StartDateTime cannot be more than EndDateTime.',
446
- );
447
- }
448
-
449
- /*Part 3: Set Status Whether Reserved or Active*/
450
- if (startDateTimeObject > new Date(new Date().setHours(0, 0, 0, 0))) {
451
- this._Status = RentalStatusEnum.RESERVED;
452
- } else {
453
- this._Status = RentalStatusEnum.ACTIVE;
454
- }
455
- }
456
-
457
- public async periodEndProcess(
458
- loginUser: LoginUser,
459
- dbTransaction: any,
460
- stockInventory: any,
461
- ) {
462
- try {
463
- // Privilege Checking
464
- const systemCode = await ApplicationConfig.getComponentConfigValue(
465
- 'system-code',
466
- );
467
-
468
- const isPrivileged = await loginUser.checkPrivileges(
469
- systemCode,
470
- 'Rental PeriodEndProcess',
471
- );
472
-
473
- if (!isPrivileged) {
474
- throw new ClassError(
475
- 'Rental',
476
- 'RentalErrMsg04',
477
- "You do not have 'Rental - PeriodEndProcess' privilege.",
478
- );
479
- }
480
-
481
- // Validate Existing Rental
482
- if (this.AgreementNo === undefined || this.AgreementNo === null) {
483
- throw new ClassError(
484
- 'Rental',
485
- 'RentalErrMsg05',
486
- 'Rental must be existing rental.',
487
- );
488
- }
489
-
490
- // Validate Rental End Period
491
- if (this.EndDateTime === new Date(new Date().setHours(0, 0, 0, 0))) {
492
- throw new ClassError(
493
- 'Rental',
494
- 'RentalErrMsg06',
495
- 'Rental period is not ending today.',
496
- );
497
- }
498
-
499
- // Mark Rental Item as Available
500
- await stockInventory.setAvailable(loginUser, dbTransaction);
501
-
502
- // Capture Record Info Before Changes
503
- const entityValueBefore = {
504
- RentalId: this.RentalId,
505
- CustomerId: this.CustomerId,
506
- CustomerType: this.CustomerType,
507
- ItemId: this.ItemId,
508
- ItemType: this.ItemType,
509
- PriceId: this.PriceId,
510
- StartDateTime: this.StartDateTime,
511
- EndDateTime: this.EndDateTime,
512
- CancelRemarks: this.CancelRemarks,
513
- TerminateRemarks: this.TerminateRemarks,
514
- AgreementNo: this.AgreementNo,
515
- AccountType: this.AccountType,
516
- Status: this.Status,
517
- EscheatmentYN: this.EscheatmentYN,
518
- CreatedById: this.CreatedById,
519
- CreatedAt: this.CreatedAt,
520
- UpdatedById: this.UpdatedById,
521
- UpdatedAt: this.UpdatedAt,
522
- };
523
-
524
- // Mark Rental as Terminated and Capture Updater Info
525
- this.setTerminated();
526
- this._UpdatedById = loginUser.ObjectId;
527
- this._UpdatedAt = new Date();
528
-
529
- // Capture Record Info after Changes
530
- const entityValueAfter = {
531
- RentalId: this.RentalId,
532
- CustomerId: this.CustomerId,
533
- CustomerType: this.CustomerType,
534
- ItemId: this.ItemId,
535
- ItemType: this.ItemType,
536
- PriceId: this.PriceId,
537
- StartDateTime: this.StartDateTime,
538
- EndDateTime: this.EndDateTime,
539
- CancelRemarks: this.CancelRemarks,
540
- TerminateRemarks: this.TerminateRemarks,
541
- AgreementNo: this.AgreementNo,
542
- AccountType: this.AccountType,
543
- Status: this.Status,
544
- EscheatmentYN: this.EscheatmentYN,
545
- CreatedById: this.CreatedById,
546
- CreatedAt: this.CreatedAt,
547
- UpdatedById: this.UpdatedById,
548
- UpdatedAt: this.UpdatedAt,
549
- };
550
-
551
- // Record the Update Activity
552
- const activity = new Activity();
553
- activity.ActivityId = activity.createId();
554
- activity.Action = ActionEnum.UPDATE;
555
- activity.Description = 'Set Rental as Terminated';
556
- activity.EntityType = 'Rental';
557
- activity.EntityId = this.RentalId;
558
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
559
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
560
- await activity.create(loginUser.ObjectId, dbTransaction);
561
-
562
- return this;
563
- } catch (err) {
564
- throw err;
565
- }
566
- }
567
-
568
- async getCustomerActiveRentals(
569
- loginUser: LoginUser,
570
- dbTransaction: any,
571
- CustomerId: string,
572
- ): Promise<IRentalAttr[]> {
573
- try {
574
- // Part 1: Privilege Checking
575
- // Call loginUser.checkPrivileges() by passing:
576
- // SystemCode: <get_from_app_config>
577
- // PrivilegeCode: "RENTAL_VIEW"
578
- const systemCode =
579
- ApplicationConfig.getComponentConfigValue('system-code');
580
- const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
581
-
582
- if (!isPrivileged) {
583
- throw new ClassError(
584
- 'Rental',
585
- 'RentalErrMsg01',
586
- "You do not have 'Rental - View' privilege.",
587
- );
588
- }
589
-
590
- // Part 2: Retrieve Rentals and Returns
591
- // Call Rental._Repo findAll method by passing:
592
- // where:
593
- // Status: "Active"
594
- // [Op.OR]:
595
- // CustomerId: Params.CustomerId
596
- // '$JointHirers.CustomerId$': Params.CustomerId
597
- // include:
598
- // model: JointHirerModel
599
- // attributes: []
600
- const query = `
601
- SELECT
602
- r.*,
603
- jh.*
604
- FROM
605
- rental_Rental r
606
- LEFT JOIN
607
- rental_JointHirer jh ON r.RentalId = jh.RentalId
608
- WHERE
609
- r.Status = 'Active'
610
- AND (
611
- r.CustomerId = '${CustomerId}'
612
- OR r.RentalId IN (
613
- SELECT jh2.RentalId
614
- FROM rental_JointHirer jh2
615
- WHERE jh2.CustomerId = '${CustomerId}'
616
- )
617
- );
618
- `;
619
- const db = rentalDb.getConnection();
620
- const result = await db.query(query, {
621
- type: QueryTypes.SELECT,
622
- transaction: dbTransaction,
623
- model: RentalModel,
624
- mapToModel: true,
625
- nest: true,
626
- raw: true,
627
- });
628
-
629
- // Format the returned records
630
- const records: IRentalAttr[] = result.map((record: RentalModel) => {
631
- return {
632
- RentalId: record.RentalId,
633
- CustomerId: record.CustomerId,
634
- CustomerType: record.CustomerType,
635
- ItemId: record.ItemId,
636
- ItemType: record.ItemType,
637
- PriceId: record.PriceId,
638
- StartDateTime: record.StartDateTime,
639
- EndDateTime: record.EndDateTime,
640
- CancelRemarks: record.CancelRemarks,
641
- TerminateRemarks: record.TerminateRemarks,
642
- AgreementNo: record.AgreementNo,
643
- AccountType: record.AccountType,
644
- Status: record.Status,
645
- EscheatmentYN: record.EscheatmentYN,
646
- CreatedById: record.CreatedById,
647
- CreatedAt: record.CreatedAt,
648
- UpdatedById: record.UpdatedById,
649
- UpdatedAt: record.UpdatedAt,
650
- JointHirers: record.JointHirers.map((jh: JointHirerModel) => {
651
- return {
652
- HirerId: jh.HirerId,
653
- RentalId: jh.RentalId,
654
- CustomerId: jh.CustomerId,
655
- CustomerType: jh.CustomerType,
656
- CreatedById: jh.CreatedById,
657
- CreatedAt: jh.CreatedAt,
658
- UpdatedById: jh.UpdatedById,
659
- UpdatedAt: jh.UpdatedAt,
660
- };
661
- }),
662
- };
663
- });
664
- // Return the returned records.
665
- return records;
666
- } catch (error) {
667
- throw error;
668
- }
669
- }
670
- }
1
+ import { IRentalAttr } from '../../interfaces/rental-attr.interface';
2
+ import { RentalStatusEnum } from '../../enum/rental-status.enum';
3
+ import { RentalRepository } from './rental.repository';
4
+ import { ClassError, ObjectBase } from '@tomei/general';
5
+ import { ApplicationConfig } from '@tomei/config';
6
+ import { LoginUser } from '@tomei/sso';
7
+ import { RentalPrice } from '../rental-price/rental-price';
8
+ import { Op, QueryTypes } from 'sequelize';
9
+ import { ActionEnum, Activity } from '@tomei/activity-history';
10
+ import { IRentalFindAllSearchAttr } from '../../interfaces/rental-find-all-search-attr.interface';
11
+ import { RentalAccountTypeEnum } from '../../enum/account-type.enum';
12
+ import { JointHirer } from '../joint-hirer/joint-hirer';
13
+ import { JointHirerModel } from '../../models/joint-hirer.entity';
14
+ import { AgreementRepository } from '../agreement/agreement.repository';
15
+ import * as rentalDb from '../../database';
16
+ import { RentalModel } from '../../models/rental.entity';
17
+ import { JointHirerRepository } from '../joint-hirer/joint-hirer.repository';
18
+
19
+ export class Rental extends ObjectBase {
20
+ ObjectId: string;
21
+ ObjectName: string;
22
+ ObjectType = 'Rental';
23
+ TableName: string;
24
+ CustomerId: string;
25
+ CustomerType: string;
26
+ ItemId: string;
27
+ ItemType: string;
28
+ PriceId: string;
29
+ StartDateTime: Date;
30
+ EndDateTime: Date;
31
+ CancelRemarks: string;
32
+ TerminateRemarks: string;
33
+ AgreementNo: string;
34
+ AccountType: RentalAccountTypeEnum;
35
+ JointHirers: JointHirer[] = [];
36
+ protected _Status: RentalStatusEnum;
37
+ protected _EscheatmentYN: string = 'N';
38
+ protected _CreatedById: string;
39
+ protected _CreatedAt: Date;
40
+ protected _UpdatedById: string;
41
+ protected _UpdatedAt: Date;
42
+ protected static _Repo = new RentalRepository();
43
+ protected static _AgreementRepo = new AgreementRepository();
44
+ protected static _JointHirerRepo = new JointHirerRepository();
45
+
46
+ get RentalId(): string {
47
+ return this.ObjectId;
48
+ }
49
+
50
+ set RentalId(value: string) {
51
+ this.ObjectId = value;
52
+ }
53
+
54
+ get Status(): RentalStatusEnum {
55
+ return this._Status;
56
+ }
57
+
58
+ get EscheatmentYN(): string {
59
+ return this._EscheatmentYN;
60
+ }
61
+
62
+ get CreatedById(): string {
63
+ return this._CreatedById;
64
+ }
65
+
66
+ get CreatedAt(): Date {
67
+ return this._CreatedAt;
68
+ }
69
+
70
+ get UpdatedById(): string {
71
+ return this._UpdatedById;
72
+ }
73
+
74
+ get UpdatedAt(): Date {
75
+ return this._UpdatedAt;
76
+ }
77
+
78
+ private setTerminated() {
79
+ this._Status = RentalStatusEnum.TERMINATED;
80
+ }
81
+
82
+ protected constructor(rentalAttr?: IRentalAttr) {
83
+ super();
84
+ if (rentalAttr) {
85
+ this.RentalId = rentalAttr.RentalId;
86
+ this.CustomerId = rentalAttr.CustomerId;
87
+ this.CustomerType = rentalAttr.CustomerType;
88
+ this.ItemId = rentalAttr.ItemId;
89
+ this.ItemType = rentalAttr.ItemType;
90
+ this.PriceId = rentalAttr.PriceId;
91
+ this.StartDateTime = rentalAttr.StartDateTime;
92
+ this.EndDateTime = rentalAttr.EndDateTime;
93
+ this.CancelRemarks = rentalAttr.CancelRemarks;
94
+ this.TerminateRemarks = rentalAttr.TerminateRemarks;
95
+ this.AgreementNo = rentalAttr.AgreementNo;
96
+ this.AccountType = rentalAttr.AccountType;
97
+ this._Status = rentalAttr.Status;
98
+ this._EscheatmentYN = rentalAttr.EscheatmentYN;
99
+ this._CreatedById = rentalAttr.CreatedById;
100
+ this._CreatedAt = rentalAttr.CreatedAt;
101
+ this._UpdatedById = rentalAttr.UpdatedById;
102
+ this._UpdatedAt = rentalAttr.UpdatedAt;
103
+ }
104
+ }
105
+
106
+ public static async init(dbTransaction?: any, rentalId?: string) {
107
+ try {
108
+ if (rentalId) {
109
+ const rental = await Rental._Repo.findByPk(rentalId, dbTransaction);
110
+ if (rental) {
111
+ return new Rental(rental);
112
+ } else {
113
+ throw new ClassError('Rental', 'RentalErrMsg00', 'Rental Not Found');
114
+ }
115
+ }
116
+ return new Rental();
117
+ } catch (error) {
118
+ throw new ClassError(
119
+ 'Rental',
120
+ 'RentalErrMsg00',
121
+ 'Failed To Initialize Price',
122
+ );
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Create a new Rental record.
128
+ * @param {RentalPrice} rentalPrice - The rental pricing information.
129
+ * @param {LoginUser} loginUser - The user initiating the creation.
130
+ * @param {any} [dbTransaction] - Optional database transaction for atomic operations.
131
+ * @throws {ClassError} Throws an error if:
132
+ * 1. loginUser does not have 'Rental - Create' privilege.
133
+ * 2. Rental item is not available at the current date.
134
+ * @returns {Promise<any>} A Promise resolving to the created rental record.
135
+ */
136
+ public async create(
137
+ rentalPrice: RentalPrice,
138
+ loginUser: LoginUser,
139
+ jointHirers?: JointHirer[],
140
+ dbTransaction?: any,
141
+ ): Promise<any> {
142
+ try {
143
+ /*Part 1: Check Privilege*/
144
+ const systemCode =
145
+ ApplicationConfig.getComponentConfigValue('system-code');
146
+ const isPrivileged = await loginUser.checkPrivileges(
147
+ systemCode,
148
+ 'Rental - Create',
149
+ );
150
+
151
+ if (!isPrivileged) {
152
+ throw new ClassError(
153
+ 'Rental',
154
+ 'RentalErrMsg01',
155
+ "You do not have 'Rental - Create' privilege.",
156
+ );
157
+ }
158
+
159
+ /*Part 2: Make Sure Rental Item Available*/
160
+ const isItemAvailable = await Rental.isItemAvailable(
161
+ this.ItemId,
162
+ this.ItemType,
163
+ this.StartDateTime,
164
+ this.EndDateTime,
165
+ dbTransaction,
166
+ );
167
+
168
+ if (!isItemAvailable) {
169
+ throw new ClassError(
170
+ 'Rental',
171
+ 'RentalErrMsg02',
172
+ 'Rental Item is not available at current date.',
173
+ );
174
+ }
175
+
176
+ // Part 3: Create Rental Agreement Record
177
+ // Call Rental._AgreementRepo create method by passing:
178
+ // AgreementNo: this.AgreementNo
179
+ // Status: "Not Generated"
180
+ // dbTransaction
181
+ await Rental._AgreementRepo.create(
182
+ {
183
+ AgreementNo: this.AgreementNo,
184
+ Status: 'Not Generated',
185
+ },
186
+ {
187
+ transaction: dbTransaction,
188
+ },
189
+ );
190
+
191
+ /*Part 4: Insert Rental & The Agreed Rental Price*/
192
+ await rentalPrice.create(loginUser, dbTransaction);
193
+
194
+ this.PriceId = rentalPrice.PriceId;
195
+
196
+ this.RentalId = this.createId();
197
+ if (!this.Status) {
198
+ this._Status = RentalStatusEnum.ACTIVE;
199
+ }
200
+ this._CreatedById = loginUser.ObjectId;
201
+ this._UpdatedById = loginUser.ObjectId;
202
+ this._CreatedAt = new Date();
203
+ this._UpdatedAt = new Date();
204
+
205
+ const data = {
206
+ RentalId: this.RentalId,
207
+ CustomerId: this.CustomerId,
208
+ CustomerType: this.CustomerType,
209
+ ItemId: this.ItemId,
210
+ ItemType: this.ItemType,
211
+ PriceId: this.PriceId,
212
+ StartDateTime: this.StartDateTime,
213
+ EndDateTime: this.EndDateTime,
214
+ CancelRemarks: this.CancelRemarks,
215
+ TerminateRemarks: this.TerminateRemarks,
216
+ AgreementNo: this.AgreementNo,
217
+ AccountType: this.AccountType,
218
+ Status: this.Status,
219
+ EscheatmentYN: this.EscheatmentYN,
220
+ CreatedById: this.CreatedById,
221
+ CreatedAt: this.CreatedAt,
222
+ UpdatedById: this.UpdatedById,
223
+ UpdatedAt: this.UpdatedAt,
224
+ };
225
+
226
+ await Rental._Repo.create(data, {
227
+ transaction: dbTransaction,
228
+ });
229
+
230
+ //For every data inside Param.jointHirers, update the rentalId and call jointHirer.create
231
+ if (jointHirers) {
232
+ for (let i = 0; i < jointHirers.length; i++) {
233
+ jointHirers[i].RentalId = this.RentalId;
234
+ await jointHirers[i].create(loginUser, dbTransaction);
235
+ this.JointHirers.push(jointHirers[i]);
236
+ }
237
+ }
238
+
239
+ /*Part 5: Record Create Rental Activity*/
240
+ const activity = new Activity();
241
+ activity.ActivityId = activity.createId();
242
+ activity.Action = ActionEnum.ADD;
243
+ activity.Description = 'Add Rental';
244
+ activity.EntityType = 'Rental';
245
+ activity.EntityId = this.RentalId;
246
+ activity.EntityValueBefore = JSON.stringify({});
247
+ activity.EntityValueAfter = JSON.stringify(data);
248
+ await activity.create(loginUser.ObjectId, dbTransaction);
249
+
250
+ /*Part 6: Return Result*/
251
+ return this;
252
+ } catch (error) {
253
+ throw error;
254
+ }
255
+ }
256
+
257
+ public static async isItemAvailable(
258
+ itemId: string,
259
+ itemType: string,
260
+ startDateTime: Date,
261
+ endDateTime: Date,
262
+ dbTransaction?: any,
263
+ ) {
264
+ try {
265
+ const item = await Rental._Repo.findOne({
266
+ where: {
267
+ ItemId: itemId,
268
+ ItemType: itemType,
269
+ StartDateTime: {
270
+ [Op.lte]: endDateTime,
271
+ },
272
+ EndDateTime: {
273
+ [Op.gte]: startDateTime,
274
+ },
275
+ },
276
+ transaction: dbTransaction,
277
+ });
278
+
279
+ if (item) {
280
+ return false;
281
+ } else {
282
+ return true;
283
+ }
284
+ } catch (error) {
285
+ throw error;
286
+ }
287
+ }
288
+
289
+ public static async findAll(
290
+ loginUser: LoginUser,
291
+ dbTransaction: any,
292
+ page?: number,
293
+ row?: number,
294
+ search?: IRentalFindAllSearchAttr,
295
+ ) {
296
+ try {
297
+ const systemCode = await ApplicationConfig.getComponentConfigValue(
298
+ 'system-code',
299
+ );
300
+
301
+ const isPrivileged = await loginUser.checkPrivileges(
302
+ systemCode,
303
+ 'Rental - View',
304
+ );
305
+
306
+ if (!isPrivileged) {
307
+ throw new ClassError(
308
+ 'Rental',
309
+ 'RentalErrMsg01',
310
+ "You do not have 'Rental - View' privilege.",
311
+ );
312
+ }
313
+
314
+ const queryObj: any = {};
315
+
316
+ let options: any = {
317
+ transaction: dbTransaction,
318
+ order: [['CreatedAt', 'DESC']],
319
+ };
320
+
321
+ if (page && row) {
322
+ options = {
323
+ ...options,
324
+ limit: row,
325
+ offset: row * (page - 1),
326
+ };
327
+ }
328
+
329
+ if (search) {
330
+ Object.entries(search).forEach(([key, value]) => {
331
+ if (key === 'StartDateTime') {
332
+ queryObj[key] = {
333
+ [Op.gte]: value,
334
+ };
335
+ } else if (key === 'EndDateTime') {
336
+ queryObj[key] = {
337
+ [Op.lte]: value,
338
+ };
339
+ } else if (key === 'CustomerId') {
340
+ queryObj[key] = {
341
+ [Op.substring]: value,
342
+ };
343
+
344
+ options.include = [
345
+ {
346
+ model: JointHirerModel,
347
+ required: false,
348
+ where: {
349
+ CustomerId: {
350
+ [Op.substring]: value,
351
+ },
352
+ },
353
+ },
354
+ ];
355
+ } else {
356
+ queryObj[key] = {
357
+ [Op.substring]: value,
358
+ };
359
+ }
360
+ });
361
+
362
+ options = {
363
+ ...options,
364
+ where: queryObj,
365
+ };
366
+ }
367
+
368
+ return await Rental._Repo.findAndCountAll(options);
369
+ } catch (err) {
370
+ throw err;
371
+ }
372
+ }
373
+
374
+ public static async checkActiveByItemId(
375
+ loginUser: LoginUser,
376
+ itemId: string,
377
+ itemType: string,
378
+ dbTransaction?: any,
379
+ ) {
380
+ try {
381
+ const systemCode = await ApplicationConfig.getComponentConfigValue(
382
+ 'system-code',
383
+ );
384
+
385
+ const isPrivileged = await loginUser.checkPrivileges(
386
+ systemCode,
387
+ 'Rental - View',
388
+ );
389
+
390
+ if (!isPrivileged) {
391
+ throw new ClassError(
392
+ 'Rental',
393
+ 'RentalErrMsg01',
394
+ "You do not have 'Rental - View' privilege.",
395
+ );
396
+ }
397
+
398
+ const queryObj: any = {
399
+ ItemId: itemId,
400
+ ItemType: itemType,
401
+ Status: RentalStatusEnum.ACTIVE,
402
+ };
403
+
404
+ const options: any = {
405
+ transaction: dbTransaction,
406
+ where: queryObj,
407
+ };
408
+
409
+ const rental = await Rental._Repo.findOne(options);
410
+
411
+ if (rental) {
412
+ return true;
413
+ } else {
414
+ return false;
415
+ }
416
+ } catch (err) {
417
+ throw err;
418
+ }
419
+ }
420
+
421
+ /**
422
+ * Sets the StartDateTime property of the Rental class as custom setter.
423
+ * @param {Date} startDateTime - The start date and time of the rental.
424
+ * @throws {ClassError} Throws an error if:
425
+ * 1. startDateTime is a past date.
426
+ * 2. startDateTime is more than the EndDateTime.
427
+ * @returns {void}
428
+ */
429
+ setStartDateTime(startDateTime: Date): void {
430
+ const startDateTimeObject = new Date(startDateTime);
431
+ startDateTimeObject.setHours(0, 0, 0, 0);
432
+
433
+ /*Part 1: Make Sure Future Date Time*/
434
+ if (startDateTimeObject < new Date(new Date().setHours(0, 0, 0, 0))) {
435
+ throw new ClassError(
436
+ 'Rental',
437
+ 'RentalErrMsg02',
438
+ 'StartDateTime cannot be a past datetime.',
439
+ );
440
+ }
441
+
442
+ /*Part 2: Make Sure Less Than End Date Time*/
443
+ if (this.EndDateTime && startDateTimeObject > this.EndDateTime) {
444
+ throw new ClassError(
445
+ 'Rental',
446
+ 'RentalErrMsg03',
447
+ 'StartDateTime cannot be more than EndDateTime.',
448
+ );
449
+ }
450
+
451
+ /*Part 3: Set Status Whether Reserved or Active*/
452
+ if (startDateTimeObject > new Date(new Date().setHours(0, 0, 0, 0))) {
453
+ this._Status = RentalStatusEnum.RESERVED;
454
+ } else {
455
+ this._Status = RentalStatusEnum.ACTIVE;
456
+ }
457
+ }
458
+
459
+ public async periodEndProcess(
460
+ loginUser: LoginUser,
461
+ dbTransaction: any,
462
+ stockInventory: any,
463
+ ) {
464
+ try {
465
+ // Privilege Checking
466
+ const systemCode = await ApplicationConfig.getComponentConfigValue(
467
+ 'system-code',
468
+ );
469
+
470
+ const isPrivileged = await loginUser.checkPrivileges(
471
+ systemCode,
472
+ 'Rental – PeriodEndProcess',
473
+ );
474
+
475
+ if (!isPrivileged) {
476
+ throw new ClassError(
477
+ 'Rental',
478
+ 'RentalErrMsg04',
479
+ "You do not have 'Rental - PeriodEndProcess' privilege.",
480
+ );
481
+ }
482
+
483
+ // Validate Existing Rental
484
+ if (this.AgreementNo === undefined || this.AgreementNo === null) {
485
+ throw new ClassError(
486
+ 'Rental',
487
+ 'RentalErrMsg05',
488
+ 'Rental must be existing rental.',
489
+ );
490
+ }
491
+
492
+ // Validate Rental End Period
493
+ if (this.EndDateTime === new Date(new Date().setHours(0, 0, 0, 0))) {
494
+ throw new ClassError(
495
+ 'Rental',
496
+ 'RentalErrMsg06',
497
+ 'Rental period is not ending today.',
498
+ );
499
+ }
500
+
501
+ // Mark Rental Item as Available
502
+ await stockInventory.setAvailable(loginUser, dbTransaction);
503
+
504
+ // Capture Record Info Before Changes
505
+ const entityValueBefore = {
506
+ RentalId: this.RentalId,
507
+ CustomerId: this.CustomerId,
508
+ CustomerType: this.CustomerType,
509
+ ItemId: this.ItemId,
510
+ ItemType: this.ItemType,
511
+ PriceId: this.PriceId,
512
+ StartDateTime: this.StartDateTime,
513
+ EndDateTime: this.EndDateTime,
514
+ CancelRemarks: this.CancelRemarks,
515
+ TerminateRemarks: this.TerminateRemarks,
516
+ AgreementNo: this.AgreementNo,
517
+ AccountType: this.AccountType,
518
+ Status: this.Status,
519
+ EscheatmentYN: this.EscheatmentYN,
520
+ CreatedById: this.CreatedById,
521
+ CreatedAt: this.CreatedAt,
522
+ UpdatedById: this.UpdatedById,
523
+ UpdatedAt: this.UpdatedAt,
524
+ };
525
+
526
+ // Mark Rental as Terminated and Capture Updater Info
527
+ this.setTerminated();
528
+ this._UpdatedById = loginUser.ObjectId;
529
+ this._UpdatedAt = new Date();
530
+
531
+ // Capture Record Info after Changes
532
+ const entityValueAfter = {
533
+ RentalId: this.RentalId,
534
+ CustomerId: this.CustomerId,
535
+ CustomerType: this.CustomerType,
536
+ ItemId: this.ItemId,
537
+ ItemType: this.ItemType,
538
+ PriceId: this.PriceId,
539
+ StartDateTime: this.StartDateTime,
540
+ EndDateTime: this.EndDateTime,
541
+ CancelRemarks: this.CancelRemarks,
542
+ TerminateRemarks: this.TerminateRemarks,
543
+ AgreementNo: this.AgreementNo,
544
+ AccountType: this.AccountType,
545
+ Status: this.Status,
546
+ EscheatmentYN: this.EscheatmentYN,
547
+ CreatedById: this.CreatedById,
548
+ CreatedAt: this.CreatedAt,
549
+ UpdatedById: this.UpdatedById,
550
+ UpdatedAt: this.UpdatedAt,
551
+ };
552
+
553
+ // Record the Update Activity
554
+ const activity = new Activity();
555
+ activity.ActivityId = activity.createId();
556
+ activity.Action = ActionEnum.UPDATE;
557
+ activity.Description = 'Set Rental as Terminated';
558
+ activity.EntityType = 'Rental';
559
+ activity.EntityId = this.RentalId;
560
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
561
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
562
+ await activity.create(loginUser.ObjectId, dbTransaction);
563
+
564
+ return this;
565
+ } catch (err) {
566
+ throw err;
567
+ }
568
+ }
569
+
570
+ async getCustomerActiveRentals(
571
+ loginUser: LoginUser,
572
+ dbTransaction: any,
573
+ CustomerId: string,
574
+ ): Promise<IRentalAttr[]> {
575
+ try {
576
+ // Part 1: Privilege Checking
577
+ // Call loginUser.checkPrivileges() by passing:
578
+ // SystemCode: <get_from_app_config>
579
+ // PrivilegeCode: "RENTAL_VIEW"
580
+ const systemCode =
581
+ ApplicationConfig.getComponentConfigValue('system-code');
582
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
583
+
584
+ if (!isPrivileged) {
585
+ throw new ClassError(
586
+ 'Rental',
587
+ 'RentalErrMsg01',
588
+ "You do not have 'Rental - View' privilege.",
589
+ );
590
+ }
591
+
592
+ // Part 2: Retrieve Rentals and Returns
593
+ // Call Rental._Repo findAll method by passing:
594
+ // where:
595
+ // Status: "Active"
596
+ // [Op.OR]:
597
+ // CustomerId: Params.CustomerId
598
+ // '$JointHirers.CustomerId$': Params.CustomerId
599
+ // include:
600
+ // model: JointHirerModel
601
+ // attributes: []
602
+ const query = `
603
+ SELECT
604
+ r.*
605
+ FROM
606
+ rental_Rental r
607
+ LEFT JOIN
608
+ rental_JointHirer jh ON r.RentalId = jh.RentalId
609
+ WHERE
610
+ r.Status = 'Active'
611
+ AND (
612
+ r.CustomerId = '${CustomerId}'
613
+ OR r.RentalId IN (
614
+ SELECT jh2.RentalId
615
+ FROM rental_JointHirer jh2
616
+ WHERE jh2.CustomerId = '${CustomerId}'
617
+ )
618
+ );
619
+ `;
620
+ const db = rentalDb.getConnection();
621
+ const result = await db.query(query, {
622
+ type: QueryTypes.SELECT,
623
+ transaction: dbTransaction,
624
+ model: RentalModel,
625
+ mapToModel: true,
626
+ });
627
+
628
+ // Format the returned records
629
+ const records: IRentalAttr[] = result.map((record: RentalModel) => {
630
+ return {
631
+ RentalId: record.RentalId,
632
+ CustomerId: record.CustomerId,
633
+ CustomerType: record.CustomerType,
634
+ ItemId: record.ItemId,
635
+ ItemType: record.ItemType,
636
+ PriceId: record.PriceId,
637
+ StartDateTime: record.StartDateTime,
638
+ EndDateTime: record.EndDateTime,
639
+ CancelRemarks: record.CancelRemarks,
640
+ TerminateRemarks: record.TerminateRemarks,
641
+ AgreementNo: record.AgreementNo,
642
+ AccountType: record.AccountType,
643
+ Status: record.Status,
644
+ EscheatmentYN: record.EscheatmentYN,
645
+ CreatedById: record.CreatedById,
646
+ CreatedAt: record.CreatedAt,
647
+ UpdatedById: record.UpdatedById,
648
+ UpdatedAt: record.UpdatedAt,
649
+ };
650
+ });
651
+ // Return the returned records.
652
+ return records;
653
+ } catch (error) {
654
+ throw error;
655
+ }
656
+ }
657
+
658
+ async getCustomerIds(
659
+ loginUser: LoginUser,
660
+ dbTransaction: any,
661
+ ): Promise<IRentalAttr[]> {
662
+ try {
663
+ // Part 1: Privilege Checking
664
+ // Call loginUser.checkPrivileges() by passing:
665
+ // SystemCode: <get_from_app_config>
666
+ // PrivilegeCode: "RENTAL_VIEW"
667
+ const systemCode =
668
+ ApplicationConfig.getComponentConfigValue('system-code');
669
+ const isPrivileged = loginUser.checkPrivileges(systemCode, 'RENTAL_VIEW');
670
+
671
+ if (!isPrivileged) {
672
+ throw new ClassError(
673
+ 'Rental',
674
+ 'RentalErrMsg01',
675
+ "You do not have 'Rental - View' privilege.",
676
+ );
677
+ }
678
+
679
+ // Part 2: Validation
680
+ // Make sure this.RentalId exists, if not throw new ClassError by passing
681
+ if (!this.RentalId) {
682
+ throw new ClassError('Rental', 'RentalErrMsg01', 'RentalId is missing');
683
+ }
684
+
685
+ //Part 3: Retrieve Listing and Returns
686
+ // 3.1 Call Rental._JointHirerRepo findAll method by passing:
687
+ // where:
688
+ // RentalId: this.RentalId
689
+ // dbTransaction
690
+ const options: any = {
691
+ where: {
692
+ RentalId: this.RentalId,
693
+ Status: 'Active',
694
+ },
695
+ transaction: dbTransaction,
696
+ };
697
+
698
+ const joinHirers = await Rental._JointHirerRepo.findAll(options);
699
+
700
+ // 3.2 Create a new array variable and set its value to joint hirer
701
+ // array translated to only CustomerId. Then, append this.CustomerId to the array.
702
+ const customerIds = [];
703
+ for (let i = 0; i < joinHirers.length; i++) {
704
+ const jointHirer = joinHirers[i];
705
+ customerIds.push(jointHirer.CustomerId);
706
+ }
707
+
708
+ // 3.3 Return the array.
709
+ return customerIds;
710
+ } catch (error) {
711
+ throw error;
712
+ }
713
+ }
714
+ }