@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.
- package/.commitlintrc.json +22 -22
- package/.eslintrc +16 -16
- package/.eslintrc.js +35 -35
- package/.husky/commit-msg +15 -15
- package/.husky/pre-commit +7 -7
- package/.prettierrc +4 -4
- package/Jenkinsfile +51 -51
- package/README.md +8 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17 -17
- package/dist/src/components/rental/rental.d.ts +45 -39
- package/dist/src/components/rental/rental.js +338 -331
- package/dist/src/components/rental/rental.js.map +1 -1
- package/dist/src/components/rental/rental.repository.d.ts +8 -8
- package/dist/src/components/rental/rental.repository.js +66 -66
- package/dist/src/components/rental-price/rental-price.d.ts +18 -12
- package/dist/src/components/rental-price/rental-price.js +78 -71
- package/dist/src/components/rental-price/rental-price.js.map +1 -1
- package/dist/src/components/rental-price/rental-price.repository.d.ts +8 -8
- package/dist/src/components/rental-price/rental-price.repository.js +66 -66
- package/dist/src/database.d.ts +4 -4
- package/dist/src/database.js +19 -19
- package/dist/src/enum/index.d.ts +2 -2
- package/dist/src/enum/index.js +5 -5
- package/dist/src/enum/rental-status.enum.d.ts +7 -7
- package/dist/src/enum/rental-status.enum.js +11 -11
- package/dist/src/index.d.ts +9 -9
- package/dist/src/index.js +30 -30
- package/dist/src/interfaces/index.d.ts +4 -4
- package/dist/src/interfaces/index.js +2 -2
- package/dist/src/interfaces/rental-attr.interface.d.ts +20 -20
- package/dist/src/interfaces/rental-attr.interface.js +2 -2
- package/dist/src/interfaces/rental-find-all-search-attr.interface.d.ts +10 -10
- package/dist/src/interfaces/rental-find-all-search-attr.interface.js +2 -2
- package/dist/src/interfaces/rental-price-attr.interface.d.ts +7 -7
- package/dist/src/interfaces/rental-price-attr.interface.js +2 -2
- package/dist/src/models/index.d.ts +3 -3
- package/dist/src/models/index.js +7 -7
- package/dist/src/models/rental-price.entity.d.ts +8 -8
- package/dist/src/models/rental-price.entity.js +58 -58
- package/dist/src/models/rental.entity.d.ts +23 -23
- package/dist/src/models/rental.entity.js +140 -140
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/jest.config.js +10 -10
- package/migrations/rental-price-table-migration.js +32 -32
- package/migrations/rental-table-migrations.js +86 -86
- package/package.json +71 -71
- package/src/components/rental/rental.repository.ts +51 -51
- package/src/components/rental/rental.ts +511 -500
- package/src/components/rental-price/rental-price.repository.ts +54 -54
- package/src/components/rental-price/rental-price.ts +100 -89
- package/src/database.ts +21 -21
- package/src/enum/index.ts +3 -3
- package/src/enum/rental-status.enum.ts +29 -29
- package/src/index.ts +16 -16
- package/src/interfaces/index.ts +5 -5
- package/src/interfaces/rental-attr.interface.ts +21 -21
- package/src/interfaces/rental-find-all-search-attr.interface.ts +11 -11
- package/src/interfaces/rental-price-attr.interface.ts +7 -7
- package/src/models/index.ts +4 -4
- package/src/models/rental-price.entity.ts +38 -38
- package/src/models/rental.entity.ts +116 -116
- package/tsconfig.build.json +5 -5
- 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
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
private
|
|
28
|
-
private
|
|
29
|
-
private
|
|
30
|
-
private
|
|
31
|
-
private
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
76
|
-
this.
|
|
77
|
-
this.
|
|
78
|
-
this.
|
|
79
|
-
this.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
await
|
|
198
|
-
|
|
199
|
-
/*Part
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
'
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
'Rental
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
'Rental
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
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
|
+
}
|