@tomei/rental 0.2.5 → 0.3.0
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 -57
- 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 +36 -35
- package/dist/src/components/rental/rental.js +238 -208
- 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 +12 -12
- package/dist/src/components/rental-price/rental-price.js +71 -71
- 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 +6 -6
- package/dist/src/enum/rental-status.enum.js +10 -10
- 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 +8 -8
- 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 -138
- package/dist/src/models/rental.entity.js.map +1 -1
- 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 -84
- package/package.json +71 -71
- package/src/components/rental/rental.repository.ts +51 -51
- package/src/components/rental/rental.ts +332 -285
- package/src/components/rental-price/rental-price.repository.ts +54 -54
- package/src/components/rental-price/rental-price.ts +89 -89
- package/src/database.ts +21 -21
- package/src/enum/index.ts +3 -3
- package/src/enum/rental-status.enum.ts +6 -6
- 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 +9 -9
- 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 -114
- package/tsconfig.build.json +5 -5
- package/tsconfig.json +23 -23
- package/dist/jest.config.d.ts +0 -2
- package/dist/jest.config.js +0 -11
- package/dist/jest.config.js.map +0 -1
- package/dist/migrations/rental-price-table-migration.d.ts +0 -2
- package/dist/migrations/rental-price-table-migration.js +0 -43
- package/dist/migrations/rental-price-table-migration.js.map +0 -1
- package/dist/migrations/rental-table-migrations.d.ts +0 -2
- package/dist/migrations/rental-table-migrations.js +0 -95
- package/dist/migrations/rental-table-migrations.js.map +0 -1
|
@@ -1,285 +1,332 @@
|
|
|
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 constructor(rentalAttr?: IRentalAttr) {
|
|
58
|
-
if (rentalAttr) {
|
|
59
|
-
this.RentalId = rentalAttr.RentalId;
|
|
60
|
-
this.CustomerId = rentalAttr.CustomerId;
|
|
61
|
-
this.CustomerType = rentalAttr.CustomerType;
|
|
62
|
-
this.ItemId = rentalAttr.ItemId;
|
|
63
|
-
this.ItemType = rentalAttr.ItemType;
|
|
64
|
-
this.PriceId = rentalAttr.PriceId;
|
|
65
|
-
this.StartDateTime = rentalAttr.StartDateTime;
|
|
66
|
-
this.EndDateTime = rentalAttr.EndDateTime;
|
|
67
|
-
this.CancelRemarks = rentalAttr.CancelRemarks;
|
|
68
|
-
this.TerminateRemarks = rentalAttr.TerminateRemarks;
|
|
69
|
-
this.AgreementNo = rentalAttr.AgreementNo;
|
|
70
|
-
this._Status = rentalAttr.Status;
|
|
71
|
-
this._EscheatmentYN = rentalAttr.EscheatmentYN;
|
|
72
|
-
this._CreatedById = rentalAttr.CreatedById;
|
|
73
|
-
this._CreatedAt = rentalAttr.CreatedAt;
|
|
74
|
-
this._UpdatedById = rentalAttr.UpdatedById;
|
|
75
|
-
this._UpdatedAt = rentalAttr.UpdatedAt;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public static async init(dbTransaction?: any, rentalId?: string) {
|
|
80
|
-
try {
|
|
81
|
-
if (rentalId) {
|
|
82
|
-
const rental = await Rental._Repo.findByPk(rentalId, dbTransaction);
|
|
83
|
-
if (rental) {
|
|
84
|
-
return new Rental(rental);
|
|
85
|
-
} else {
|
|
86
|
-
throw new ClassError('Rental', 'RentalErrMsg', 'Rental Not Found');
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return new Rental();
|
|
90
|
-
} catch (error) {
|
|
91
|
-
throw new ClassError(
|
|
92
|
-
'Rental',
|
|
93
|
-
'RentalErrMsg',
|
|
94
|
-
'Failed To Initialize Price',
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
public async create(
|
|
100
|
-
rentalPrice: RentalPrice,
|
|
101
|
-
loginUser: LoginUser,
|
|
102
|
-
dbTransaction?: any,
|
|
103
|
-
) {
|
|
104
|
-
try {
|
|
105
|
-
const systemCode =
|
|
106
|
-
ApplicationConfig.getComponentConfigValue('system-code');
|
|
107
|
-
const isPrivileged = await loginUser.checkPrivileges(
|
|
108
|
-
systemCode,
|
|
109
|
-
'Rental - Create',
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
if (!isPrivileged) {
|
|
113
|
-
throw new ClassError(
|
|
114
|
-
'RentalPrice',
|
|
115
|
-
'RentalPriceErrMsg',
|
|
116
|
-
'You do not have the privilege.',
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const isItemAvailable = await this.isItemAvailable(
|
|
121
|
-
this.ItemId,
|
|
122
|
-
this.ItemType,
|
|
123
|
-
this.StartDateTime,
|
|
124
|
-
this.EndDateTime,
|
|
125
|
-
dbTransaction,
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
if (!isItemAvailable) {
|
|
129
|
-
throw new ClassError(
|
|
130
|
-
'Rental',
|
|
131
|
-
'RentalErrMsg02',
|
|
132
|
-
'Rental Item is not available at current date.',
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
await rentalPrice.create(loginUser, dbTransaction);
|
|
137
|
-
|
|
138
|
-
this.PriceId = rentalPrice.PriceId;
|
|
139
|
-
|
|
140
|
-
this.RentalId = cuid();
|
|
141
|
-
this._Status = RentalStatusEnum.ACTIVE;
|
|
142
|
-
this._CreatedById = loginUser.ObjectId;
|
|
143
|
-
this._UpdatedById = loginUser.ObjectId;
|
|
144
|
-
this._CreatedAt = new Date();
|
|
145
|
-
this._UpdatedAt = new Date();
|
|
146
|
-
|
|
147
|
-
const data = {
|
|
148
|
-
RentalId: this.RentalId,
|
|
149
|
-
CustomerId: this.CustomerId,
|
|
150
|
-
CustomerType: this.CustomerType,
|
|
151
|
-
ItemId: this.ItemId,
|
|
152
|
-
ItemType: this.ItemType,
|
|
153
|
-
PriceId: this.PriceId,
|
|
154
|
-
StartDateTime: this.StartDateTime,
|
|
155
|
-
EndDateTime: this.EndDateTime,
|
|
156
|
-
CancelRemarks: this.CancelRemarks,
|
|
157
|
-
TerminateRemarks: this.TerminateRemarks,
|
|
158
|
-
AgreementNo: this.AgreementNo,
|
|
159
|
-
Status: this.Status,
|
|
160
|
-
EscheatmentYN: this.EscheatmentYN,
|
|
161
|
-
CreatedById: this.CreatedById,
|
|
162
|
-
CreatedAt: this.CreatedAt,
|
|
163
|
-
UpdatedById: this.UpdatedById,
|
|
164
|
-
UpdatedAt: this.UpdatedAt,
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
await Rental._Repo.create(data, dbTransaction);
|
|
168
|
-
|
|
169
|
-
const activity = new Activity();
|
|
170
|
-
activity.ActivityId = cuid();
|
|
171
|
-
activity.Action = ActionEnum.ADD;
|
|
172
|
-
activity.Description = 'Add Rental';
|
|
173
|
-
activity.EntityType = 'Rental';
|
|
174
|
-
activity.EntityId = this.RentalId;
|
|
175
|
-
activity.EntityValueBefore = JSON.stringify({});
|
|
176
|
-
activity.EntityValueAfter = JSON.stringify(data);
|
|
177
|
-
await activity.create(loginUser, dbTransaction);
|
|
178
|
-
|
|
179
|
-
return this;
|
|
180
|
-
} catch (error) {
|
|
181
|
-
throw error;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
async isItemAvailable(
|
|
186
|
-
itemId: string,
|
|
187
|
-
itemType: string,
|
|
188
|
-
startDateTime: Date,
|
|
189
|
-
endDateTime: Date,
|
|
190
|
-
dbTransaction?: any,
|
|
191
|
-
) {
|
|
192
|
-
try {
|
|
193
|
-
const item = await Rental._Repo.findOne({
|
|
194
|
-
where: {
|
|
195
|
-
ItemId: itemId,
|
|
196
|
-
ItemType: itemType,
|
|
197
|
-
StartDateTime: {
|
|
198
|
-
[Op.lte]: endDateTime,
|
|
199
|
-
},
|
|
200
|
-
EndDateTime: {
|
|
201
|
-
[Op.gte]: startDateTime,
|
|
202
|
-
},
|
|
203
|
-
},
|
|
204
|
-
transaction: dbTransaction,
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
if (item) {
|
|
208
|
-
return false;
|
|
209
|
-
} else {
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
212
|
-
} catch (error) {
|
|
213
|
-
throw error;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
public static async findAll(
|
|
218
|
-
loginUser: LoginUser,
|
|
219
|
-
dbTransaction: any,
|
|
220
|
-
page?: number,
|
|
221
|
-
row?: number,
|
|
222
|
-
search?: IRentalFindAllSearchAttr,
|
|
223
|
-
) {
|
|
224
|
-
try {
|
|
225
|
-
const systemCode = await ApplicationConfig.getComponentConfigValue(
|
|
226
|
-
'system-code',
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
const isPrivileged = await loginUser.checkPrivileges(
|
|
230
|
-
systemCode,
|
|
231
|
-
'Rental - View',
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
if (!isPrivileged) {
|
|
235
|
-
throw new ClassError(
|
|
236
|
-
'Rental',
|
|
237
|
-
'RentalErrMsg',
|
|
238
|
-
'You do not have the privilege.',
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
const queryObj: any = {};
|
|
243
|
-
|
|
244
|
-
let options: any = {
|
|
245
|
-
transaction: dbTransaction,
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
if (page && row) {
|
|
249
|
-
options = {
|
|
250
|
-
...options,
|
|
251
|
-
limit: row,
|
|
252
|
-
offset: row * (page - 1),
|
|
253
|
-
order: [['CreatedAt', 'DESC']],
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
if (search) {
|
|
258
|
-
Object.entries(search).forEach(([key, value]) => {
|
|
259
|
-
if (key === 'StartDateTime') {
|
|
260
|
-
queryObj[key] = {
|
|
261
|
-
[Op.gte]: value,
|
|
262
|
-
};
|
|
263
|
-
} else if (key === 'EndDateTime') {
|
|
264
|
-
queryObj[key] = {
|
|
265
|
-
[Op.lte]: value,
|
|
266
|
-
};
|
|
267
|
-
} else {
|
|
268
|
-
queryObj[key] = {
|
|
269
|
-
[Op.substring]: value,
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
options = {
|
|
275
|
-
...options,
|
|
276
|
-
where: queryObj,
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return await Rental._Repo.findAndCountAll(options);
|
|
281
|
-
} catch (err) {
|
|
282
|
-
throw err;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
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 constructor(rentalAttr?: IRentalAttr) {
|
|
58
|
+
if (rentalAttr) {
|
|
59
|
+
this.RentalId = rentalAttr.RentalId;
|
|
60
|
+
this.CustomerId = rentalAttr.CustomerId;
|
|
61
|
+
this.CustomerType = rentalAttr.CustomerType;
|
|
62
|
+
this.ItemId = rentalAttr.ItemId;
|
|
63
|
+
this.ItemType = rentalAttr.ItemType;
|
|
64
|
+
this.PriceId = rentalAttr.PriceId;
|
|
65
|
+
this.StartDateTime = rentalAttr.StartDateTime;
|
|
66
|
+
this.EndDateTime = rentalAttr.EndDateTime;
|
|
67
|
+
this.CancelRemarks = rentalAttr.CancelRemarks;
|
|
68
|
+
this.TerminateRemarks = rentalAttr.TerminateRemarks;
|
|
69
|
+
this.AgreementNo = rentalAttr.AgreementNo;
|
|
70
|
+
this._Status = rentalAttr.Status;
|
|
71
|
+
this._EscheatmentYN = rentalAttr.EscheatmentYN;
|
|
72
|
+
this._CreatedById = rentalAttr.CreatedById;
|
|
73
|
+
this._CreatedAt = rentalAttr.CreatedAt;
|
|
74
|
+
this._UpdatedById = rentalAttr.UpdatedById;
|
|
75
|
+
this._UpdatedAt = rentalAttr.UpdatedAt;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public static async init(dbTransaction?: any, rentalId?: string) {
|
|
80
|
+
try {
|
|
81
|
+
if (rentalId) {
|
|
82
|
+
const rental = await Rental._Repo.findByPk(rentalId, dbTransaction);
|
|
83
|
+
if (rental) {
|
|
84
|
+
return new Rental(rental);
|
|
85
|
+
} else {
|
|
86
|
+
throw new ClassError('Rental', 'RentalErrMsg', 'Rental Not Found');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return new Rental();
|
|
90
|
+
} catch (error) {
|
|
91
|
+
throw new ClassError(
|
|
92
|
+
'Rental',
|
|
93
|
+
'RentalErrMsg',
|
|
94
|
+
'Failed To Initialize Price',
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public async create(
|
|
100
|
+
rentalPrice: RentalPrice,
|
|
101
|
+
loginUser: LoginUser,
|
|
102
|
+
dbTransaction?: any,
|
|
103
|
+
) {
|
|
104
|
+
try {
|
|
105
|
+
const systemCode =
|
|
106
|
+
ApplicationConfig.getComponentConfigValue('system-code');
|
|
107
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
|
108
|
+
systemCode,
|
|
109
|
+
'Rental - Create',
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
if (!isPrivileged) {
|
|
113
|
+
throw new ClassError(
|
|
114
|
+
'RentalPrice',
|
|
115
|
+
'RentalPriceErrMsg',
|
|
116
|
+
'You do not have the privilege.',
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const isItemAvailable = await this.isItemAvailable(
|
|
121
|
+
this.ItemId,
|
|
122
|
+
this.ItemType,
|
|
123
|
+
this.StartDateTime,
|
|
124
|
+
this.EndDateTime,
|
|
125
|
+
dbTransaction,
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
if (!isItemAvailable) {
|
|
129
|
+
throw new ClassError(
|
|
130
|
+
'Rental',
|
|
131
|
+
'RentalErrMsg02',
|
|
132
|
+
'Rental Item is not available at current date.',
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
await rentalPrice.create(loginUser, dbTransaction);
|
|
137
|
+
|
|
138
|
+
this.PriceId = rentalPrice.PriceId;
|
|
139
|
+
|
|
140
|
+
this.RentalId = cuid();
|
|
141
|
+
this._Status = RentalStatusEnum.ACTIVE;
|
|
142
|
+
this._CreatedById = loginUser.ObjectId;
|
|
143
|
+
this._UpdatedById = loginUser.ObjectId;
|
|
144
|
+
this._CreatedAt = new Date();
|
|
145
|
+
this._UpdatedAt = new Date();
|
|
146
|
+
|
|
147
|
+
const data = {
|
|
148
|
+
RentalId: this.RentalId,
|
|
149
|
+
CustomerId: this.CustomerId,
|
|
150
|
+
CustomerType: this.CustomerType,
|
|
151
|
+
ItemId: this.ItemId,
|
|
152
|
+
ItemType: this.ItemType,
|
|
153
|
+
PriceId: this.PriceId,
|
|
154
|
+
StartDateTime: this.StartDateTime,
|
|
155
|
+
EndDateTime: this.EndDateTime,
|
|
156
|
+
CancelRemarks: this.CancelRemarks,
|
|
157
|
+
TerminateRemarks: this.TerminateRemarks,
|
|
158
|
+
AgreementNo: this.AgreementNo,
|
|
159
|
+
Status: this.Status,
|
|
160
|
+
EscheatmentYN: this.EscheatmentYN,
|
|
161
|
+
CreatedById: this.CreatedById,
|
|
162
|
+
CreatedAt: this.CreatedAt,
|
|
163
|
+
UpdatedById: this.UpdatedById,
|
|
164
|
+
UpdatedAt: this.UpdatedAt,
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
await Rental._Repo.create(data, dbTransaction);
|
|
168
|
+
|
|
169
|
+
const activity = new Activity();
|
|
170
|
+
activity.ActivityId = cuid();
|
|
171
|
+
activity.Action = ActionEnum.ADD;
|
|
172
|
+
activity.Description = 'Add Rental';
|
|
173
|
+
activity.EntityType = 'Rental';
|
|
174
|
+
activity.EntityId = this.RentalId;
|
|
175
|
+
activity.EntityValueBefore = JSON.stringify({});
|
|
176
|
+
activity.EntityValueAfter = JSON.stringify(data);
|
|
177
|
+
await activity.create(loginUser, dbTransaction);
|
|
178
|
+
|
|
179
|
+
return this;
|
|
180
|
+
} catch (error) {
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async isItemAvailable(
|
|
186
|
+
itemId: string,
|
|
187
|
+
itemType: string,
|
|
188
|
+
startDateTime: Date,
|
|
189
|
+
endDateTime: Date,
|
|
190
|
+
dbTransaction?: any,
|
|
191
|
+
) {
|
|
192
|
+
try {
|
|
193
|
+
const item = await Rental._Repo.findOne({
|
|
194
|
+
where: {
|
|
195
|
+
ItemId: itemId,
|
|
196
|
+
ItemType: itemType,
|
|
197
|
+
StartDateTime: {
|
|
198
|
+
[Op.lte]: endDateTime,
|
|
199
|
+
},
|
|
200
|
+
EndDateTime: {
|
|
201
|
+
[Op.gte]: startDateTime,
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
transaction: dbTransaction,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
if (item) {
|
|
208
|
+
return false;
|
|
209
|
+
} else {
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
} catch (error) {
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public static async findAll(
|
|
218
|
+
loginUser: LoginUser,
|
|
219
|
+
dbTransaction: any,
|
|
220
|
+
page?: number,
|
|
221
|
+
row?: number,
|
|
222
|
+
search?: IRentalFindAllSearchAttr,
|
|
223
|
+
) {
|
|
224
|
+
try {
|
|
225
|
+
const systemCode = await ApplicationConfig.getComponentConfigValue(
|
|
226
|
+
'system-code',
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
|
230
|
+
systemCode,
|
|
231
|
+
'Rental - View',
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
if (!isPrivileged) {
|
|
235
|
+
throw new ClassError(
|
|
236
|
+
'Rental',
|
|
237
|
+
'RentalErrMsg',
|
|
238
|
+
'You do not have the privilege.',
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const queryObj: any = {};
|
|
243
|
+
|
|
244
|
+
let options: any = {
|
|
245
|
+
transaction: dbTransaction,
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
if (page && row) {
|
|
249
|
+
options = {
|
|
250
|
+
...options,
|
|
251
|
+
limit: row,
|
|
252
|
+
offset: row * (page - 1),
|
|
253
|
+
order: [['CreatedAt', 'DESC']],
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (search) {
|
|
258
|
+
Object.entries(search).forEach(([key, value]) => {
|
|
259
|
+
if (key === 'StartDateTime') {
|
|
260
|
+
queryObj[key] = {
|
|
261
|
+
[Op.gte]: value,
|
|
262
|
+
};
|
|
263
|
+
} else if (key === 'EndDateTime') {
|
|
264
|
+
queryObj[key] = {
|
|
265
|
+
[Op.lte]: value,
|
|
266
|
+
};
|
|
267
|
+
} else {
|
|
268
|
+
queryObj[key] = {
|
|
269
|
+
[Op.substring]: value,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
options = {
|
|
275
|
+
...options,
|
|
276
|
+
where: queryObj,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return await Rental._Repo.findAndCountAll(options);
|
|
281
|
+
} catch (err) {
|
|
282
|
+
throw err;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
public static async checkActiveByItemId(
|
|
287
|
+
loginUser: LoginUser,
|
|
288
|
+
itemId: string,
|
|
289
|
+
itemType: string,
|
|
290
|
+
dbTransaction?: any,
|
|
291
|
+
) {
|
|
292
|
+
try {
|
|
293
|
+
const systemCode = await ApplicationConfig.getComponentConfigValue(
|
|
294
|
+
'system-code',
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
|
298
|
+
systemCode,
|
|
299
|
+
'Rental - View',
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
if (!isPrivileged) {
|
|
303
|
+
throw new ClassError(
|
|
304
|
+
'Rental',
|
|
305
|
+
'RentalErrMsg',
|
|
306
|
+
'You do not have the privilege.',
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const queryObj: any = {
|
|
311
|
+
ItemId: itemId,
|
|
312
|
+
ItemType: itemType,
|
|
313
|
+
Status: RentalStatusEnum.ACTIVE,
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const options: any = {
|
|
317
|
+
transaction: dbTransaction,
|
|
318
|
+
where: queryObj,
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const rental = await Rental._Repo.findOne(options);
|
|
322
|
+
|
|
323
|
+
if (rental) {
|
|
324
|
+
return true;
|
|
325
|
+
} else {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
} catch (err) {
|
|
329
|
+
throw err;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|