@tomei/rental 0.4.3 → 0.4.5

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 (64) hide show
  1. package/.commitlintrc.json +22 -22
  2. package/.eslintrc +16 -16
  3. package/.eslintrc.js +35 -35
  4. package/.husky/commit-msg +15 -15
  5. package/.husky/pre-commit +7 -7
  6. package/.prettierrc +4 -4
  7. package/Jenkinsfile +51 -51
  8. package/README.md +8 -8
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.js +17 -17
  11. package/dist/src/components/rental/rental.d.ts +45 -39
  12. package/dist/src/components/rental/rental.js +338 -331
  13. package/dist/src/components/rental/rental.js.map +1 -1
  14. package/dist/src/components/rental/rental.repository.d.ts +8 -8
  15. package/dist/src/components/rental/rental.repository.js +66 -66
  16. package/dist/src/components/rental-price/rental-price.d.ts +18 -12
  17. package/dist/src/components/rental-price/rental-price.js +78 -71
  18. package/dist/src/components/rental-price/rental-price.js.map +1 -1
  19. package/dist/src/components/rental-price/rental-price.repository.d.ts +8 -8
  20. package/dist/src/components/rental-price/rental-price.repository.js +66 -66
  21. package/dist/src/database.d.ts +4 -4
  22. package/dist/src/database.js +19 -19
  23. package/dist/src/enum/index.d.ts +2 -2
  24. package/dist/src/enum/index.js +5 -5
  25. package/dist/src/enum/rental-status.enum.d.ts +7 -7
  26. package/dist/src/enum/rental-status.enum.js +11 -11
  27. package/dist/src/index.d.ts +9 -9
  28. package/dist/src/index.js +30 -30
  29. package/dist/src/interfaces/index.d.ts +4 -4
  30. package/dist/src/interfaces/index.js +2 -2
  31. package/dist/src/interfaces/rental-attr.interface.d.ts +20 -20
  32. package/dist/src/interfaces/rental-attr.interface.js +2 -2
  33. package/dist/src/interfaces/rental-find-all-search-attr.interface.d.ts +10 -10
  34. package/dist/src/interfaces/rental-find-all-search-attr.interface.js +2 -2
  35. package/dist/src/interfaces/rental-price-attr.interface.d.ts +7 -7
  36. package/dist/src/interfaces/rental-price-attr.interface.js +2 -2
  37. package/dist/src/models/index.d.ts +3 -3
  38. package/dist/src/models/index.js +7 -7
  39. package/dist/src/models/rental-price.entity.d.ts +8 -8
  40. package/dist/src/models/rental-price.entity.js +58 -58
  41. package/dist/src/models/rental.entity.d.ts +23 -23
  42. package/dist/src/models/rental.entity.js +140 -140
  43. package/dist/tsconfig.tsbuildinfo +1 -1
  44. package/jest.config.js +10 -10
  45. package/migrations/rental-price-table-migration.js +32 -32
  46. package/migrations/rental-table-migrations.js +86 -86
  47. package/package.json +71 -71
  48. package/src/components/rental/rental.repository.ts +51 -51
  49. package/src/components/rental/rental.ts +511 -500
  50. package/src/components/rental-price/rental-price.repository.ts +54 -54
  51. package/src/components/rental-price/rental-price.ts +100 -89
  52. package/src/database.ts +21 -21
  53. package/src/enum/index.ts +3 -3
  54. package/src/enum/rental-status.enum.ts +29 -29
  55. package/src/index.ts +16 -16
  56. package/src/interfaces/index.ts +5 -5
  57. package/src/interfaces/rental-attr.interface.ts +21 -21
  58. package/src/interfaces/rental-find-all-search-attr.interface.ts +11 -11
  59. package/src/interfaces/rental-price-attr.interface.ts +7 -7
  60. package/src/models/index.ts +4 -4
  61. package/src/models/rental-price.entity.ts +38 -38
  62. package/src/models/rental.entity.ts +116 -116
  63. package/tsconfig.build.json +5 -5
  64. package/tsconfig.json +23 -23
@@ -1,500 +1,511 @@
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 } 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 * as cuid from 'cuid';
9
- import { Op } from 'sequelize';
10
- import { ActionEnum, Activity } from '@tomei/activity-history';
11
- import { IRentalFindAllSearchAttr } from '../../interfaces/rental-find-all-search-attr.interface';
12
-
13
- export class Rental {
14
- RentalId: string;
15
- CustomerId: string;
16
- CustomerType: string;
17
- ItemId: string;
18
- ItemType: string;
19
- PriceId: string;
20
- StartDateTime: Date;
21
- EndDateTime: Date;
22
- CancelRemarks: string;
23
- TerminateRemarks: string;
24
- AgreementNo: string;
25
- private _Status: RentalStatusEnum;
26
- private _EscheatmentYN: string = 'N';
27
- private _CreatedById: string;
28
- private _CreatedAt: Date;
29
- private _UpdatedById: string;
30
- private _UpdatedAt: Date;
31
- private static _Repo = new RentalRepository();
32
-
33
- get Status(): RentalStatusEnum {
34
- return this._Status;
35
- }
36
-
37
- get EscheatmentYN(): string {
38
- return this._EscheatmentYN;
39
- }
40
-
41
- get CreatedById(): string {
42
- return this._CreatedById;
43
- }
44
-
45
- get CreatedAt(): Date {
46
- return this._CreatedAt;
47
- }
48
-
49
- get UpdatedById(): string {
50
- return this._UpdatedById;
51
- }
52
-
53
- get UpdatedAt(): Date {
54
- return this._UpdatedAt;
55
- }
56
-
57
- private setTerminated() {
58
- this._Status = RentalStatusEnum.TERMINATED;
59
- }
60
-
61
- private constructor(rentalAttr?: IRentalAttr) {
62
- if (rentalAttr) {
63
- this.RentalId = rentalAttr.RentalId;
64
- this.CustomerId = rentalAttr.CustomerId;
65
- this.CustomerType = rentalAttr.CustomerType;
66
- this.ItemId = rentalAttr.ItemId;
67
- this.ItemType = rentalAttr.ItemType;
68
- this.PriceId = rentalAttr.PriceId;
69
- this.StartDateTime = rentalAttr.StartDateTime;
70
- this.EndDateTime = rentalAttr.EndDateTime;
71
- this.CancelRemarks = rentalAttr.CancelRemarks;
72
- this.TerminateRemarks = rentalAttr.TerminateRemarks;
73
- this.AgreementNo = rentalAttr.AgreementNo;
74
- this._Status = rentalAttr.Status;
75
- this._EscheatmentYN = rentalAttr.EscheatmentYN;
76
- this._CreatedById = rentalAttr.CreatedById;
77
- this._CreatedAt = rentalAttr.CreatedAt;
78
- this._UpdatedById = rentalAttr.UpdatedById;
79
- this._UpdatedAt = rentalAttr.UpdatedAt;
80
- }
81
- }
82
-
83
- public static async init(dbTransaction?: any, rentalId?: string) {
84
- try {
85
- if (rentalId) {
86
- const rental = await Rental._Repo.findByPk(rentalId, dbTransaction);
87
- if (rental) {
88
- return new Rental(rental);
89
- } else {
90
- throw new ClassError('Rental', 'RentalErrMsg00', 'Rental Not Found');
91
- }
92
- }
93
- return new Rental();
94
- } catch (error) {
95
- throw new ClassError(
96
- 'Rental',
97
- 'RentalErrMsg00',
98
- 'Failed To Initialize Price',
99
- );
100
- }
101
- }
102
-
103
- /**
104
- * Create a new Rental record.
105
- * @param {RentalPrice} rentalPrice - The rental pricing information.
106
- * @param {LoginUser} loginUser - The user initiating the creation.
107
- * @param {any} [dbTransaction] - Optional database transaction for atomic operations.
108
- * @throws {ClassError} Throws an error if:
109
- * 1. loginUser does not have 'Rental - Create' privilege.
110
- * 2. Rental item is not available at the current date.
111
- * @returns {Promise<any>} A Promise resolving to the created rental record.
112
- */
113
- public async create(
114
- rentalPrice: RentalPrice,
115
- loginUser: LoginUser,
116
- dbTransaction?: any,
117
- ): Promise<any> {
118
- try {
119
- /*Part 1: Check Privilege*/
120
- const systemCode =
121
- ApplicationConfig.getComponentConfigValue('system-code');
122
- const isPrivileged = await loginUser.checkPrivileges(
123
- systemCode,
124
- 'Rental - Create',
125
- );
126
-
127
- if (!isPrivileged) {
128
- throw new ClassError(
129
- 'Rental',
130
- 'RentalErrMsg01',
131
- "You do not have 'Rental - Create' privilege.",
132
- );
133
- }
134
-
135
- /*Part 2: Make Sure Rental Item Available*/
136
- const isItemAvailable = await this.isItemAvailable(
137
- this.ItemId,
138
- this.ItemType,
139
- this.StartDateTime,
140
- this.EndDateTime,
141
- dbTransaction,
142
- );
143
-
144
- if (!isItemAvailable) {
145
- throw new ClassError(
146
- 'Rental',
147
- 'RentalErrMsg02',
148
- 'Rental Item is not available at current date.',
149
- );
150
- }
151
-
152
- /*Part 3: Insert Rental & The Agreed Rental Price*/
153
- await rentalPrice.create(loginUser, dbTransaction);
154
-
155
- this.PriceId = rentalPrice.PriceId;
156
-
157
- this.RentalId = cuid();
158
- if (!this.Status) {
159
- this._Status = RentalStatusEnum.ACTIVE;
160
- }
161
- this._CreatedById = loginUser.ObjectId;
162
- this._UpdatedById = loginUser.ObjectId;
163
- this._CreatedAt = new Date();
164
- this._UpdatedAt = new Date();
165
-
166
- const data = {
167
- RentalId: this.RentalId,
168
- CustomerId: this.CustomerId,
169
- CustomerType: this.CustomerType,
170
- ItemId: this.ItemId,
171
- ItemType: this.ItemType,
172
- PriceId: this.PriceId,
173
- StartDateTime: this.StartDateTime,
174
- EndDateTime: this.EndDateTime,
175
- CancelRemarks: this.CancelRemarks,
176
- TerminateRemarks: this.TerminateRemarks,
177
- AgreementNo: this.AgreementNo,
178
- Status: this.Status,
179
- EscheatmentYN: this.EscheatmentYN,
180
- CreatedById: this.CreatedById,
181
- CreatedAt: this.CreatedAt,
182
- UpdatedById: this.UpdatedById,
183
- UpdatedAt: this.UpdatedAt,
184
- };
185
-
186
- await Rental._Repo.create(data, dbTransaction);
187
-
188
- /*Part 4: Record Create Rental Activity*/
189
- const activity = new Activity();
190
- activity.ActivityId = cuid();
191
- activity.Action = ActionEnum.ADD;
192
- activity.Description = 'Add Rental';
193
- activity.EntityType = 'Rental';
194
- activity.EntityId = this.RentalId;
195
- activity.EntityValueBefore = JSON.stringify({});
196
- activity.EntityValueAfter = JSON.stringify(data);
197
- await activity.create(loginUser, dbTransaction);
198
-
199
- /*Part 5: Return Result*/
200
- return this;
201
- } catch (error) {
202
- throw error;
203
- }
204
- }
205
-
206
- async isItemAvailable(
207
- itemId: string,
208
- itemType: string,
209
- startDateTime: Date,
210
- endDateTime: Date,
211
- dbTransaction?: any,
212
- ) {
213
- try {
214
- const item = await Rental._Repo.findOne({
215
- where: {
216
- ItemId: itemId,
217
- ItemType: itemType,
218
- StartDateTime: {
219
- [Op.lte]: endDateTime,
220
- },
221
- EndDateTime: {
222
- [Op.gte]: startDateTime,
223
- },
224
- },
225
- transaction: dbTransaction,
226
- });
227
-
228
- if (item) {
229
- return false;
230
- } else {
231
- return true;
232
- }
233
- } catch (error) {
234
- throw error;
235
- }
236
- }
237
-
238
- public static async findAll(
239
- loginUser: LoginUser,
240
- dbTransaction: any,
241
- page?: number,
242
- row?: number,
243
- search?: IRentalFindAllSearchAttr,
244
- ) {
245
- try {
246
- const systemCode = await ApplicationConfig.getComponentConfigValue(
247
- 'system-code',
248
- );
249
-
250
- const isPrivileged = await loginUser.checkPrivileges(
251
- systemCode,
252
- 'Rental - View',
253
- );
254
-
255
- if (!isPrivileged) {
256
- throw new ClassError(
257
- 'Rental',
258
- 'RentalErrMsg01',
259
- "You do not have 'Rental - View' privilege.",
260
- );
261
- }
262
-
263
- const queryObj: any = {};
264
-
265
- let options: any = {
266
- transaction: dbTransaction,
267
- };
268
-
269
- if (page && row) {
270
- options = {
271
- ...options,
272
- limit: row,
273
- offset: row * (page - 1),
274
- order: [['CreatedAt', 'DESC']],
275
- };
276
- }
277
-
278
- if (search) {
279
- Object.entries(search).forEach(([key, value]) => {
280
- if (key === 'StartDateTime') {
281
- queryObj[key] = {
282
- [Op.gte]: value,
283
- };
284
- } else if (key === 'EndDateTime') {
285
- queryObj[key] = {
286
- [Op.lte]: value,
287
- };
288
- } else {
289
- queryObj[key] = {
290
- [Op.substring]: value,
291
- };
292
- }
293
- });
294
-
295
- options = {
296
- ...options,
297
- where: queryObj,
298
- };
299
- }
300
-
301
- return await Rental._Repo.findAndCountAll(options);
302
- } catch (err) {
303
- throw err;
304
- }
305
- }
306
-
307
- public static async checkActiveByItemId(
308
- loginUser: LoginUser,
309
- itemId: string,
310
- itemType: string,
311
- dbTransaction?: any,
312
- ) {
313
- try {
314
- const systemCode = await ApplicationConfig.getComponentConfigValue(
315
- 'system-code',
316
- );
317
-
318
- const isPrivileged = await loginUser.checkPrivileges(
319
- systemCode,
320
- 'Rental - View',
321
- );
322
-
323
- if (!isPrivileged) {
324
- throw new ClassError(
325
- 'Rental',
326
- 'RentalErrMsg01',
327
- "You do not have 'Rental - View' privilege.",
328
- );
329
- }
330
-
331
- const queryObj: any = {
332
- ItemId: itemId,
333
- ItemType: itemType,
334
- Status: RentalStatusEnum.ACTIVE,
335
- };
336
-
337
- const options: any = {
338
- transaction: dbTransaction,
339
- where: queryObj,
340
- };
341
-
342
- const rental = await Rental._Repo.findOne(options);
343
-
344
- if (rental) {
345
- return true;
346
- } else {
347
- return false;
348
- }
349
- } catch (err) {
350
- throw err;
351
- }
352
- }
353
-
354
- /**
355
- * Sets the StartDateTime property of the Rental class as custom setter.
356
- * @param {Date} startDateTime - The start date and time of the rental.
357
- * @throws {ClassError} Throws an error if:
358
- * 1. startDateTime is a past date.
359
- * 2. startDateTime is more than the EndDateTime.
360
- * @returns {void}
361
- */
362
- setStartDateTime(startDateTime: Date): void {
363
- const startDateTimeObject = new Date(startDateTime);
364
- startDateTimeObject.setHours(0, 0, 0, 0);
365
-
366
- /*Part 1: Make Sure Future Date Time*/
367
- if (startDateTimeObject < new Date(new Date().setHours(0, 0, 0, 0))) {
368
- throw new ClassError(
369
- 'Rental',
370
- 'RentalErrMsg02',
371
- 'StartDateTime cannot be a past datetime.',
372
- );
373
- }
374
-
375
- /*Part 2: Make Sure Less Than End Date Time*/
376
- if (this.EndDateTime && startDateTimeObject > this.EndDateTime) {
377
- throw new ClassError(
378
- 'Rental',
379
- 'RentalErrMsg03',
380
- 'StartDateTime cannot be more than EndDateTime.',
381
- );
382
- }
383
-
384
- /*Part 3: Set Status Whether Reserved or Active*/
385
- if (startDateTimeObject > new Date(new Date().setHours(0, 0, 0, 0))) {
386
- this._Status = RentalStatusEnum.RESERVED;
387
- } else {
388
- this._Status = RentalStatusEnum.ACTIVE;
389
- }
390
- }
391
-
392
- public async periodEndProcess(
393
- loginUser: LoginUser,
394
- dbTransaction: any,
395
- stockInventory: any,
396
- ) {
397
- try {
398
- // Privilege Checking
399
- const systemCode = await ApplicationConfig.getComponentConfigValue(
400
- 'system-code',
401
- );
402
-
403
- const isPrivileged = await loginUser.checkPrivileges(
404
- systemCode,
405
- 'Rental – PeriodEndProcess',
406
- );
407
-
408
- if (!isPrivileged) {
409
- throw new ClassError(
410
- 'Rental',
411
- 'RentalErrMsg04',
412
- "You do not have 'Rental - PeriodEndProcess' privilege.",
413
- );
414
- }
415
-
416
- // Validate Existing Rental
417
- if (this.AgreementNo === undefined || this.AgreementNo === null) {
418
- throw new ClassError(
419
- 'Rental',
420
- 'RentalErrMsg05',
421
- 'Rental must be existing rental.',
422
- );
423
- }
424
-
425
- // Validate Rental End Period
426
- if (this.EndDateTime === new Date(new Date().setHours(0, 0, 0, 0))) {
427
- throw new ClassError(
428
- 'Rental',
429
- 'RentalErrMsg06',
430
- 'Rental period is not ending today.',
431
- );
432
- }
433
-
434
- // Mark Rental Item as Available
435
- await stockInventory.setAvailable(loginUser, dbTransaction);
436
-
437
- // Capture Record Info Before Changes
438
- const entityValueBefore = {
439
- RentalId: this.RentalId,
440
- CustomerId: this.CustomerId,
441
- CustomerType: this.CustomerType,
442
- ItemId: this.ItemId,
443
- ItemType: this.ItemType,
444
- PriceId: this.PriceId,
445
- StartDateTime: this.StartDateTime,
446
- EndDateTime: this.EndDateTime,
447
- CancelRemarks: this.CancelRemarks,
448
- TerminateRemarks: this.TerminateRemarks,
449
- AgreementNo: this.AgreementNo,
450
- Status: this.Status,
451
- EscheatmentYN: this.EscheatmentYN,
452
- CreatedById: this.CreatedById,
453
- CreatedAt: this.CreatedAt,
454
- UpdatedById: this.UpdatedById,
455
- UpdatedAt: this.UpdatedAt,
456
- };
457
-
458
- // Mark Rental as Terminated and Capture Updater Info
459
- this.setTerminated();
460
- this._UpdatedById = loginUser.ObjectId;
461
- this._UpdatedAt = new Date();
462
-
463
- // Capture Record Info after Changes
464
- const entityValueAfter = {
465
- RentalId: this.RentalId,
466
- CustomerId: this.CustomerId,
467
- CustomerType: this.CustomerType,
468
- ItemId: this.ItemId,
469
- ItemType: this.ItemType,
470
- PriceId: this.PriceId,
471
- StartDateTime: this.StartDateTime,
472
- EndDateTime: this.EndDateTime,
473
- CancelRemarks: this.CancelRemarks,
474
- TerminateRemarks: this.TerminateRemarks,
475
- AgreementNo: this.AgreementNo,
476
- Status: this.Status,
477
- EscheatmentYN: this.EscheatmentYN,
478
- CreatedById: this.CreatedById,
479
- CreatedAt: this.CreatedAt,
480
- UpdatedById: this.UpdatedById,
481
- UpdatedAt: this.UpdatedAt,
482
- };
483
-
484
- // Record the Update Activity
485
- const activity = new Activity();
486
- activity.ActivityId = cuid();
487
- activity.Action = ActionEnum.UPDATE;
488
- activity.Description = 'Set Rental as Terminated';
489
- activity.EntityType = 'Rental';
490
- activity.EntityId = this.RentalId;
491
- activity.EntityValueBefore = JSON.stringify(entityValueBefore);
492
- activity.EntityValueAfter = JSON.stringify(entityValueAfter);
493
- await activity.create(loginUser, dbTransaction);
494
-
495
- return this;
496
- } catch (err) {
497
- throw err;
498
- }
499
- }
500
- }
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 } from 'sequelize';
9
+ import { ActionEnum, Activity } from '@tomei/activity-history';
10
+ import { IRentalFindAllSearchAttr } from '../../interfaces/rental-find-all-search-attr.interface';
11
+
12
+ export class Rental extends ObjectBase {
13
+ ObjectId: string;
14
+ ObjectName: string;
15
+ ObjectType = 'Rental';
16
+ TableName: string;
17
+ CustomerId: string;
18
+ CustomerType: string;
19
+ ItemId: string;
20
+ ItemType: string;
21
+ PriceId: string;
22
+ StartDateTime: Date;
23
+ EndDateTime: Date;
24
+ CancelRemarks: string;
25
+ TerminateRemarks: string;
26
+ AgreementNo: string;
27
+ private _Status: RentalStatusEnum;
28
+ private _EscheatmentYN: string = 'N';
29
+ private _CreatedById: string;
30
+ private _CreatedAt: Date;
31
+ private _UpdatedById: string;
32
+ private _UpdatedAt: Date;
33
+ private static _Repo = new RentalRepository();
34
+
35
+ get RentalId(): string {
36
+ return this.ObjectId;
37
+ }
38
+
39
+ set RentalId(value: string) {
40
+ this.ObjectId = value;
41
+ }
42
+
43
+ get Status(): RentalStatusEnum {
44
+ return this._Status;
45
+ }
46
+
47
+ get EscheatmentYN(): string {
48
+ return this._EscheatmentYN;
49
+ }
50
+
51
+ get CreatedById(): string {
52
+ return this._CreatedById;
53
+ }
54
+
55
+ get CreatedAt(): Date {
56
+ return this._CreatedAt;
57
+ }
58
+
59
+ get UpdatedById(): string {
60
+ return this._UpdatedById;
61
+ }
62
+
63
+ get UpdatedAt(): Date {
64
+ return this._UpdatedAt;
65
+ }
66
+
67
+ private setTerminated() {
68
+ this._Status = RentalStatusEnum.TERMINATED;
69
+ }
70
+
71
+ private constructor(rentalAttr?: IRentalAttr) {
72
+ super();
73
+ if (rentalAttr) {
74
+ this.RentalId = rentalAttr.RentalId;
75
+ this.CustomerId = rentalAttr.CustomerId;
76
+ this.CustomerType = rentalAttr.CustomerType;
77
+ this.ItemId = rentalAttr.ItemId;
78
+ this.ItemType = rentalAttr.ItemType;
79
+ this.PriceId = rentalAttr.PriceId;
80
+ this.StartDateTime = rentalAttr.StartDateTime;
81
+ this.EndDateTime = rentalAttr.EndDateTime;
82
+ this.CancelRemarks = rentalAttr.CancelRemarks;
83
+ this.TerminateRemarks = rentalAttr.TerminateRemarks;
84
+ this.AgreementNo = rentalAttr.AgreementNo;
85
+ this._Status = rentalAttr.Status;
86
+ this._EscheatmentYN = rentalAttr.EscheatmentYN;
87
+ this._CreatedById = rentalAttr.CreatedById;
88
+ this._CreatedAt = rentalAttr.CreatedAt;
89
+ this._UpdatedById = rentalAttr.UpdatedById;
90
+ this._UpdatedAt = rentalAttr.UpdatedAt;
91
+ }
92
+ }
93
+
94
+ public static async init(dbTransaction?: any, rentalId?: string) {
95
+ try {
96
+ if (rentalId) {
97
+ const rental = await Rental._Repo.findByPk(rentalId, dbTransaction);
98
+ if (rental) {
99
+ return new Rental(rental);
100
+ } else {
101
+ throw new ClassError('Rental', 'RentalErrMsg00', 'Rental Not Found');
102
+ }
103
+ }
104
+ return new Rental();
105
+ } catch (error) {
106
+ throw new ClassError(
107
+ 'Rental',
108
+ 'RentalErrMsg00',
109
+ 'Failed To Initialize Price',
110
+ );
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Create a new Rental record.
116
+ * @param {RentalPrice} rentalPrice - The rental pricing information.
117
+ * @param {LoginUser} loginUser - The user initiating the creation.
118
+ * @param {any} [dbTransaction] - Optional database transaction for atomic operations.
119
+ * @throws {ClassError} Throws an error if:
120
+ * 1. loginUser does not have 'Rental - Create' privilege.
121
+ * 2. Rental item is not available at the current date.
122
+ * @returns {Promise<any>} A Promise resolving to the created rental record.
123
+ */
124
+ public async create(
125
+ rentalPrice: RentalPrice,
126
+ loginUser: LoginUser,
127
+ dbTransaction?: any,
128
+ ): Promise<any> {
129
+ try {
130
+ /*Part 1: Check Privilege*/
131
+ const systemCode =
132
+ ApplicationConfig.getComponentConfigValue('system-code');
133
+ const isPrivileged = await loginUser.checkPrivileges(
134
+ systemCode,
135
+ 'Rental - Create',
136
+ );
137
+
138
+ if (!isPrivileged) {
139
+ throw new ClassError(
140
+ 'Rental',
141
+ 'RentalErrMsg01',
142
+ "You do not have 'Rental - Create' privilege.",
143
+ );
144
+ }
145
+
146
+ /*Part 2: Make Sure Rental Item Available*/
147
+ const isItemAvailable = await this.isItemAvailable(
148
+ this.ItemId,
149
+ this.ItemType,
150
+ this.StartDateTime,
151
+ this.EndDateTime,
152
+ dbTransaction,
153
+ );
154
+
155
+ if (!isItemAvailable) {
156
+ throw new ClassError(
157
+ 'Rental',
158
+ 'RentalErrMsg02',
159
+ 'Rental Item is not available at current date.',
160
+ );
161
+ }
162
+
163
+ /*Part 3: Insert Rental & The Agreed Rental Price*/
164
+ await rentalPrice.create(loginUser, dbTransaction);
165
+
166
+ this.PriceId = rentalPrice.PriceId;
167
+
168
+ this.RentalId = this.createId();
169
+ if (!this.Status) {
170
+ this._Status = RentalStatusEnum.ACTIVE;
171
+ }
172
+ this._CreatedById = loginUser.ObjectId;
173
+ this._UpdatedById = loginUser.ObjectId;
174
+ this._CreatedAt = new Date();
175
+ this._UpdatedAt = new Date();
176
+
177
+ const data = {
178
+ RentalId: this.RentalId,
179
+ CustomerId: this.CustomerId,
180
+ CustomerType: this.CustomerType,
181
+ ItemId: this.ItemId,
182
+ ItemType: this.ItemType,
183
+ PriceId: this.PriceId,
184
+ StartDateTime: this.StartDateTime,
185
+ EndDateTime: this.EndDateTime,
186
+ CancelRemarks: this.CancelRemarks,
187
+ TerminateRemarks: this.TerminateRemarks,
188
+ AgreementNo: this.AgreementNo,
189
+ Status: this.Status,
190
+ EscheatmentYN: this.EscheatmentYN,
191
+ CreatedById: this.CreatedById,
192
+ CreatedAt: this.CreatedAt,
193
+ UpdatedById: this.UpdatedById,
194
+ UpdatedAt: this.UpdatedAt,
195
+ };
196
+
197
+ await Rental._Repo.create(data, dbTransaction);
198
+
199
+ /*Part 4: Record Create Rental Activity*/
200
+ const activity = new Activity();
201
+ activity.ActivityId = this.createId();
202
+ activity.Action = ActionEnum.ADD;
203
+ activity.Description = 'Add Rental';
204
+ activity.EntityType = 'Rental';
205
+ activity.EntityId = this.RentalId;
206
+ activity.EntityValueBefore = JSON.stringify({});
207
+ activity.EntityValueAfter = JSON.stringify(data);
208
+ await activity.create(loginUser.ObjectId, dbTransaction);
209
+
210
+ /*Part 5: Return Result*/
211
+ return this;
212
+ } catch (error) {
213
+ throw error;
214
+ }
215
+ }
216
+
217
+ async isItemAvailable(
218
+ itemId: string,
219
+ itemType: string,
220
+ startDateTime: Date,
221
+ endDateTime: Date,
222
+ dbTransaction?: any,
223
+ ) {
224
+ try {
225
+ const item = await Rental._Repo.findOne({
226
+ where: {
227
+ ItemId: itemId,
228
+ ItemType: itemType,
229
+ StartDateTime: {
230
+ [Op.lte]: endDateTime,
231
+ },
232
+ EndDateTime: {
233
+ [Op.gte]: startDateTime,
234
+ },
235
+ },
236
+ transaction: dbTransaction,
237
+ });
238
+
239
+ if (item) {
240
+ return false;
241
+ } else {
242
+ return true;
243
+ }
244
+ } catch (error) {
245
+ throw error;
246
+ }
247
+ }
248
+
249
+ public static async findAll(
250
+ loginUser: LoginUser,
251
+ dbTransaction: any,
252
+ page?: number,
253
+ row?: number,
254
+ search?: IRentalFindAllSearchAttr,
255
+ ) {
256
+ try {
257
+ const systemCode = await ApplicationConfig.getComponentConfigValue(
258
+ 'system-code',
259
+ );
260
+
261
+ const isPrivileged = await loginUser.checkPrivileges(
262
+ systemCode,
263
+ 'Rental - View',
264
+ );
265
+
266
+ if (!isPrivileged) {
267
+ throw new ClassError(
268
+ 'Rental',
269
+ 'RentalErrMsg01',
270
+ "You do not have 'Rental - View' privilege.",
271
+ );
272
+ }
273
+
274
+ const queryObj: any = {};
275
+
276
+ let options: any = {
277
+ transaction: dbTransaction,
278
+ };
279
+
280
+ if (page && row) {
281
+ options = {
282
+ ...options,
283
+ limit: row,
284
+ offset: row * (page - 1),
285
+ order: [['CreatedAt', 'DESC']],
286
+ };
287
+ }
288
+
289
+ if (search) {
290
+ Object.entries(search).forEach(([key, value]) => {
291
+ if (key === 'StartDateTime') {
292
+ queryObj[key] = {
293
+ [Op.gte]: value,
294
+ };
295
+ } else if (key === 'EndDateTime') {
296
+ queryObj[key] = {
297
+ [Op.lte]: value,
298
+ };
299
+ } else {
300
+ queryObj[key] = {
301
+ [Op.substring]: value,
302
+ };
303
+ }
304
+ });
305
+
306
+ options = {
307
+ ...options,
308
+ where: queryObj,
309
+ };
310
+ }
311
+
312
+ return await Rental._Repo.findAndCountAll(options);
313
+ } catch (err) {
314
+ throw err;
315
+ }
316
+ }
317
+
318
+ public static async checkActiveByItemId(
319
+ loginUser: LoginUser,
320
+ itemId: string,
321
+ itemType: string,
322
+ dbTransaction?: any,
323
+ ) {
324
+ try {
325
+ const systemCode = await ApplicationConfig.getComponentConfigValue(
326
+ 'system-code',
327
+ );
328
+
329
+ const isPrivileged = await loginUser.checkPrivileges(
330
+ systemCode,
331
+ 'Rental - View',
332
+ );
333
+
334
+ if (!isPrivileged) {
335
+ throw new ClassError(
336
+ 'Rental',
337
+ 'RentalErrMsg01',
338
+ "You do not have 'Rental - View' privilege.",
339
+ );
340
+ }
341
+
342
+ const queryObj: any = {
343
+ ItemId: itemId,
344
+ ItemType: itemType,
345
+ Status: RentalStatusEnum.ACTIVE,
346
+ };
347
+
348
+ const options: any = {
349
+ transaction: dbTransaction,
350
+ where: queryObj,
351
+ };
352
+
353
+ const rental = await Rental._Repo.findOne(options);
354
+
355
+ if (rental) {
356
+ return true;
357
+ } else {
358
+ return false;
359
+ }
360
+ } catch (err) {
361
+ throw err;
362
+ }
363
+ }
364
+
365
+ /**
366
+ * Sets the StartDateTime property of the Rental class as custom setter.
367
+ * @param {Date} startDateTime - The start date and time of the rental.
368
+ * @throws {ClassError} Throws an error if:
369
+ * 1. startDateTime is a past date.
370
+ * 2. startDateTime is more than the EndDateTime.
371
+ * @returns {void}
372
+ */
373
+ setStartDateTime(startDateTime: Date): void {
374
+ const startDateTimeObject = new Date(startDateTime);
375
+ startDateTimeObject.setHours(0, 0, 0, 0);
376
+
377
+ /*Part 1: Make Sure Future Date Time*/
378
+ if (startDateTimeObject < new Date(new Date().setHours(0, 0, 0, 0))) {
379
+ throw new ClassError(
380
+ 'Rental',
381
+ 'RentalErrMsg02',
382
+ 'StartDateTime cannot be a past datetime.',
383
+ );
384
+ }
385
+
386
+ /*Part 2: Make Sure Less Than End Date Time*/
387
+ if (this.EndDateTime && startDateTimeObject > this.EndDateTime) {
388
+ throw new ClassError(
389
+ 'Rental',
390
+ 'RentalErrMsg03',
391
+ 'StartDateTime cannot be more than EndDateTime.',
392
+ );
393
+ }
394
+
395
+ /*Part 3: Set Status Whether Reserved or Active*/
396
+ if (startDateTimeObject > new Date(new Date().setHours(0, 0, 0, 0))) {
397
+ this._Status = RentalStatusEnum.RESERVED;
398
+ } else {
399
+ this._Status = RentalStatusEnum.ACTIVE;
400
+ }
401
+ }
402
+
403
+ public async periodEndProcess(
404
+ loginUser: LoginUser,
405
+ dbTransaction: any,
406
+ stockInventory: any,
407
+ ) {
408
+ try {
409
+ // Privilege Checking
410
+ const systemCode = await ApplicationConfig.getComponentConfigValue(
411
+ 'system-code',
412
+ );
413
+
414
+ const isPrivileged = await loginUser.checkPrivileges(
415
+ systemCode,
416
+ 'Rental PeriodEndProcess',
417
+ );
418
+
419
+ if (!isPrivileged) {
420
+ throw new ClassError(
421
+ 'Rental',
422
+ 'RentalErrMsg04',
423
+ "You do not have 'Rental - PeriodEndProcess' privilege.",
424
+ );
425
+ }
426
+
427
+ // Validate Existing Rental
428
+ if (this.AgreementNo === undefined || this.AgreementNo === null) {
429
+ throw new ClassError(
430
+ 'Rental',
431
+ 'RentalErrMsg05',
432
+ 'Rental must be existing rental.',
433
+ );
434
+ }
435
+
436
+ // Validate Rental End Period
437
+ if (this.EndDateTime === new Date(new Date().setHours(0, 0, 0, 0))) {
438
+ throw new ClassError(
439
+ 'Rental',
440
+ 'RentalErrMsg06',
441
+ 'Rental period is not ending today.',
442
+ );
443
+ }
444
+
445
+ // Mark Rental Item as Available
446
+ await stockInventory.setAvailable(loginUser, dbTransaction);
447
+
448
+ // Capture Record Info Before Changes
449
+ const entityValueBefore = {
450
+ RentalId: this.RentalId,
451
+ CustomerId: this.CustomerId,
452
+ CustomerType: this.CustomerType,
453
+ ItemId: this.ItemId,
454
+ ItemType: this.ItemType,
455
+ PriceId: this.PriceId,
456
+ StartDateTime: this.StartDateTime,
457
+ EndDateTime: this.EndDateTime,
458
+ CancelRemarks: this.CancelRemarks,
459
+ TerminateRemarks: this.TerminateRemarks,
460
+ AgreementNo: this.AgreementNo,
461
+ Status: this.Status,
462
+ EscheatmentYN: this.EscheatmentYN,
463
+ CreatedById: this.CreatedById,
464
+ CreatedAt: this.CreatedAt,
465
+ UpdatedById: this.UpdatedById,
466
+ UpdatedAt: this.UpdatedAt,
467
+ };
468
+
469
+ // Mark Rental as Terminated and Capture Updater Info
470
+ this.setTerminated();
471
+ this._UpdatedById = loginUser.ObjectId;
472
+ this._UpdatedAt = new Date();
473
+
474
+ // Capture Record Info after Changes
475
+ const entityValueAfter = {
476
+ RentalId: this.RentalId,
477
+ CustomerId: this.CustomerId,
478
+ CustomerType: this.CustomerType,
479
+ ItemId: this.ItemId,
480
+ ItemType: this.ItemType,
481
+ PriceId: this.PriceId,
482
+ StartDateTime: this.StartDateTime,
483
+ EndDateTime: this.EndDateTime,
484
+ CancelRemarks: this.CancelRemarks,
485
+ TerminateRemarks: this.TerminateRemarks,
486
+ AgreementNo: this.AgreementNo,
487
+ Status: this.Status,
488
+ EscheatmentYN: this.EscheatmentYN,
489
+ CreatedById: this.CreatedById,
490
+ CreatedAt: this.CreatedAt,
491
+ UpdatedById: this.UpdatedById,
492
+ UpdatedAt: this.UpdatedAt,
493
+ };
494
+
495
+ // Record the Update Activity
496
+ const activity = new Activity();
497
+ activity.ActivityId = this.createId();
498
+ activity.Action = ActionEnum.UPDATE;
499
+ activity.Description = 'Set Rental as Terminated';
500
+ activity.EntityType = 'Rental';
501
+ activity.EntityId = this.RentalId;
502
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
503
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
504
+ await activity.create(loginUser.ObjectId, dbTransaction);
505
+
506
+ return this;
507
+ } catch (err) {
508
+ throw err;
509
+ }
510
+ }
511
+ }