@tomei/rental 0.17.4-dev.5 → 0.17.4-dev.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/rental",
3
- "version": "0.17.4-dev.5",
3
+ "version": "0.17.4-dev.7",
4
4
  "description": "Tomei Rental Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -4,6 +4,8 @@ import { IJointHirerAttr } from '../../interfaces/joint-hirer-attr.interface';
4
4
  import { ApplicationConfig } from '@tomei/config';
5
5
  import { LoginUser } from '@tomei/sso';
6
6
  import { ActionEnum, Activity } from '@tomei/activity-history';
7
+ import { Rental } from 'components/rental/rental';
8
+ import { RentalAccountTypeEnum } from 'enum';
7
9
 
8
10
  export class JointHirer extends ObjectBase {
9
11
  ObjectId: string;
@@ -18,6 +20,7 @@ export class JointHirer extends ObjectBase {
18
20
  CreatedAt: Date;
19
21
  UpdatedById: string;
20
22
  UpdatedAt: Date;
23
+ private _Rental: Rental;
21
24
 
22
25
  get HirerId(): string {
23
26
  return this.ObjectId;
@@ -146,6 +149,16 @@ export class JointHirer extends ObjectBase {
146
149
 
147
150
  await activity.create(loginUser.ObjectId, dbTransaction);
148
151
 
152
+ //update the AccountType
153
+ const rental = await this.getRental(dbTransaction);
154
+ const totalIds = await rental.getCustomerIds(loginUser, dbTransaction);
155
+
156
+ if (totalIds.length > 1 && rental.AccountType === 'Single') {
157
+ await rental.update(loginUser, dbTransaction, {
158
+ AccountType: RentalAccountTypeEnum.JOINT,
159
+ });
160
+ }
161
+
149
162
  return this;
150
163
  } catch (error) {
151
164
  throw error;
@@ -154,6 +167,20 @@ export class JointHirer extends ObjectBase {
154
167
 
155
168
  public async remove(loginUser: LoginUser, dbTransaction?: any) {
156
169
  try {
170
+ //Make sure this.HirerId exists
171
+ if (!this.HirerId) {
172
+ throw new ClassError(
173
+ 'JointHirer',
174
+ 'JointHirerErrMsg02',
175
+ 'HirerId is required to remove Joint Hirer.',
176
+ );
177
+ }
178
+
179
+ //Make sure this.Status is not "Inactive"
180
+ if (this.Status === 'Inactive') {
181
+ return this;
182
+ }
183
+
157
184
  //Mark Joint Hirer as Inactive
158
185
  const entityValueBefore: IJointHirerAttr = this.toJSON();
159
186
  this.Status = 'Inactive'; // Set status to 'Inactive' instead of deleting the record
@@ -181,9 +208,45 @@ export class JointHirer extends ObjectBase {
181
208
 
182
209
  await activity.create(loginUser.ObjectId, dbTransaction);
183
210
 
211
+ //update the AccountType
212
+ const rental = await this.getRental(dbTransaction);
213
+ const totalIds = await rental.getCustomerIds(loginUser, dbTransaction);
214
+
215
+ if (totalIds.length === 1 && rental.AccountType === 'Joint') {
216
+ await rental.update(loginUser, dbTransaction, {
217
+ AccountType: RentalAccountTypeEnum.SINGLE,
218
+ });
219
+ }
220
+
184
221
  return this;
185
222
  } catch (error) {
186
223
  throw error;
187
224
  }
188
225
  }
226
+
227
+ private async getRental(dbTransaction?: any) {
228
+ if (this._Rental) {
229
+ return this._Rental;
230
+ }
231
+
232
+ if (!this.RentalId) {
233
+ throw new ClassError(
234
+ 'JointHirer',
235
+ 'JointHirerErrMsg04',
236
+ 'RentalId is empty.',
237
+ );
238
+ }
239
+
240
+ this._Rental = await Rental.init(this.RentalId, dbTransaction);
241
+
242
+ if (!this._Rental) {
243
+ throw new ClassError(
244
+ 'JointHirer',
245
+ 'JointHirerErrMsg05',
246
+ 'Rental not found.',
247
+ );
248
+ }
249
+
250
+ return this._Rental;
251
+ }
189
252
  }
@@ -796,6 +796,7 @@ export class Rental extends ObjectBase {
796
796
  const options: any = {
797
797
  where: {
798
798
  RentalId: this.RentalId,
799
+ Status: 'Active',
799
800
  },
800
801
  transaction: dbTransaction,
801
802
  };
@@ -810,10 +810,10 @@ export class RentalHirerChangeRequest
810
810
  await jointHirer.create(loginUser, dbTransaction);
811
811
 
812
812
  //d. Set the AccountType to "Joint" for the rental
813
- const rental = await Rental.init(dbTransaction, this.RentalId);
814
- await rental.update(loginUser, dbTransaction, {
815
- AccountType: RentalAccountTypeEnum.JOINT,
816
- });
813
+ // const rental = await Rental.init(dbTransaction, this.RentalId);
814
+ // await rental.update(loginUser, dbTransaction, {
815
+ // AccountType: RentalAccountTypeEnum.JOINT,
816
+ // });
817
817
  } else if (this.Type === HirerChangeRequestTypeEnum.REMOVE) {
818
818
  // a. Get joint hirer from remove hirer request
819
819
  const removeHirer = await this.getRemoveHirer(dbTransaction);
@@ -825,21 +825,22 @@ export class RentalHirerChangeRequest
825
825
  //Call jointHirer.remove() method by passing loginUser and dbTransaction
826
826
  await jointHirer.remove(loginUser, dbTransaction);
827
827
 
828
- //find the number of joint hirers after removal
829
- const rental = await Rental.init(dbTransaction, this.RentalId);
830
- const jointHirers = await rental.getJointHirers(dbTransaction);
828
+ // //find the number of joint hirers after removal
829
+ // const rental = await Rental.init(dbTransaction, this.RentalId);
830
+ // rental.AccountType = RentalAccountTypeEnum.JOINT;
831
+ // const jointHirers = await rental.getJointHirers(dbTransaction);
831
832
 
832
- //filter out the active joint hirers
833
- const activeJointHirers = jointHirers.filter(
834
- (hirer) => hirer.Status === 'Active',
835
- );
833
+ // //filter out the active joint hirers
834
+ // const activeJointHirers = jointHirers.filter(
835
+ // (hirer) => hirer.Status === 'Active',
836
+ // );
836
837
 
837
- //if jointHirers is empty, set rental.AccountType to "Single"
838
- if (activeJointHirers.length === 0) {
839
- await rental.update(loginUser, dbTransaction, {
840
- AccountType: RentalAccountTypeEnum.SINGLE,
841
- });
842
- }
838
+ // //if jointHirers is empty, set rental.AccountType to "Single"
839
+ // if (activeJointHirers.length === 0) {
840
+ // await rental.update(loginUser, dbTransaction, {
841
+ // AccountType: RentalAccountTypeEnum.SINGLE,
842
+ // });
843
+ // }
843
844
  }
844
845
 
845
846
  // 3. Call _Repo update method