turf-common 1.2.1-beta.10 → 1.2.1-beta.12

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.
@@ -9,4 +9,5 @@ export interface IChargeModel {
9
9
  amountPayable: number;
10
10
  totalRefundedAmount: number;
11
11
  couponCode: string;
12
+ totalBookingHours: number;
12
13
  }
@@ -33,7 +33,7 @@ export declare class BookingCode {
33
33
  getCodeWithoutTurfCode(): string;
34
34
  static sortByCode(bookingCodes: BookingCode[]): void;
35
35
  static getOneSlotBookingCode(turfId: string, bookingDate: string, fhfm: string, thtm: string, pitchCode: string): string;
36
- static getDiscountedCost(couponCode: string, totalCost: number, validationApply?: boolean): number;
36
+ static getDiscountedCost(couponCode: string, totalCost: number, validationApply?: boolean, totalBookingHours?: number): number;
37
37
  static getDiscountAmount(couponCode: string, totalCost: number): number;
38
38
  static isValidDate(date: string): boolean;
39
39
  static parseDateCode(dateCode: string): Date;
@@ -86,4 +86,9 @@ export declare class BookingCode {
86
86
  formatDateAndTime(formateDateAndTime: IFormateDateAndTime): string;
87
87
  static timeFormatter(timeFormatter: ITimeFormatter): string;
88
88
  static dateFormatter(dateFormatter: IDateFormatter): string;
89
+ getTotalTime(unit?: "minute" | "hr"): number;
90
+ getAcrossDateCodes(): {
91
+ fromDateCode: string;
92
+ toDateCode: string;
93
+ };
89
94
  }
@@ -116,12 +116,20 @@ var BookingCode = /** @class */ (function () {
116
116
  var code = turfId + bookingDate + fhfm + thtm + pitchCode;
117
117
  return code;
118
118
  };
119
- BookingCode.getDiscountedCost = function (couponCode, totalCost, validationApply) {
119
+ BookingCode.getDiscountedCost = function (couponCode, totalCost, validationApply, totalBookingHours) {
120
120
  if (validationApply === void 0) { validationApply = true; }
121
121
  var discount = 0;
122
122
  var discountedCost = 0;
123
123
  if (couponCode) {
124
- if (couponCode.endsWith("P")) {
124
+ var code = couponCode.match(/[a-zA-Z]+/g);
125
+ var value = couponCode.match(/\d+/g);
126
+ if (validationApply && !code && !value) {
127
+ throw new app_badrequest_exception_1.AppBadRequestException({
128
+ key: "couponCode",
129
+ message: ["Coupon with code ".concat(couponCode, " is wrong ")],
130
+ });
131
+ }
132
+ if (code && (code[0] === "P" || code[0] === "PHP")) {
125
133
  var percentDiscount = parseFloat(couponCode);
126
134
  if (validationApply) {
127
135
  if (percentDiscount > 90) {
@@ -133,9 +141,20 @@ var BookingCode = /** @class */ (function () {
133
141
  }
134
142
  discount = totalCost * (percentDiscount / 100);
135
143
  }
136
- else if (couponCode.endsWith("U")) {
144
+ else if (code && code[0] === "U") {
137
145
  discount = parseFloat(couponCode);
138
146
  }
147
+ else if (code && code[0] === "PHU") {
148
+ if (!totalBookingHours) {
149
+ throw new app_badrequest_exception_1.AppBadRequestException({
150
+ key: "totalBookingHours",
151
+ message: [
152
+ "Total Booking Hours is mandatory to calculate per hour unit",
153
+ ],
154
+ });
155
+ }
156
+ discount = parseFloat(couponCode) * totalBookingHours;
157
+ }
139
158
  }
140
159
  discountedCost = totalCost - discount;
141
160
  return Math.round(discountedCost);
@@ -504,6 +523,17 @@ var BookingCode = /** @class */ (function () {
504
523
  var dateFormat = dateFormatter.dateFormat, dateCode = dateFormatter.dateCode;
505
524
  return (0, date_fns_1.format)(BookingCode.convertToDate(dateCode), dateFormat);
506
525
  };
526
+ BookingCode.prototype.getTotalTime = function (unit) {
527
+ if (unit === void 0) { unit = "minute"; }
528
+ return date_util_1.dateUtilService.calculateDateDifference(this.getAcrossDateCodes().fromDateCode, this.getAcrossDateCodes().toDateCode, unit);
529
+ };
530
+ BookingCode.prototype.getAcrossDateCodes = function () {
531
+ var fromDateCode = "".concat(this.dateCode).concat(this.fromTimeCode);
532
+ var toDateCode = "".concat(this.fromTimeCode > this.toTimeCode
533
+ ? date_util_1.dateUtilService.addNDaysToDateCode(this.dateCode, 1)
534
+ : this.dateCode).concat(this.toTimeCode);
535
+ return { fromDateCode: fromDateCode, toDateCode: toDateCode };
536
+ };
507
537
  return BookingCode;
508
538
  }());
509
539
  exports.BookingCode = BookingCode;
@@ -11,14 +11,15 @@ export declare class ChargesModel implements IChargeModel {
11
11
  amountPaid: number;
12
12
  amountPayable: number;
13
13
  totalRefundedAmount: number;
14
+ totalBookingHours: number;
14
15
  couponCode: string;
15
16
  discountPolicy: ModificationDiscountPolicy;
16
- constructor(initialGrossAmount: number, paidAmount: number, couponCode: string, discountPolicy: ModificationDiscountPolicy);
17
+ constructor(initialGrossAmount: number, paidAmount: number, couponCode: string, totalBookingHours: number, discountPolicy: ModificationDiscountPolicy);
17
18
  getGrossAmount(): number;
18
19
  getNetAmount(): number;
19
20
  getDiscountAmount(): number;
20
21
  getPayableRefundAmount(): number;
21
- static getDiscountAmount(couponCode: string, totalCost: number, validationApply?: boolean): number;
22
+ static getDiscountAmount(couponCode: string, totalCost: number, validationApply?: boolean, totalBookingHours?: number): number;
22
23
  setValues(values?: Partial<ChargesModel>): ChargesModel;
23
24
  modify(bookingModification: IBookingModification): ChargesModel | null;
24
25
  private modifyExtend;
@@ -4,7 +4,7 @@ exports.ChargesModel = void 0;
4
4
  var app_badrequest_exception_1 = require("../exceptions/app-badrequest.exception");
5
5
  var modification_discount_policy_enum_1 = require("../enums/modification-discount-policy.enum");
6
6
  var ChargesModel = /** @class */ (function () {
7
- function ChargesModel(initialGrossAmount, paidAmount, couponCode, discountPolicy) {
7
+ function ChargesModel(initialGrossAmount, paidAmount, couponCode, totalBookingHours, discountPolicy) {
8
8
  this.initialBookingGrossAmount = initialGrossAmount;
9
9
  this.amountPaid = paidAmount;
10
10
  this.couponCode = couponCode;
@@ -13,6 +13,7 @@ var ChargesModel = /** @class */ (function () {
13
13
  this.totalCancellationCharges = 0;
14
14
  this.refundPayable = 0;
15
15
  this.totalRefundedAmount = 0;
16
+ this.totalBookingHours = totalBookingHours;
16
17
  this.netTotal = this.getNetAmount();
17
18
  this.amountPayable = this.getPayableRefundAmount();
18
19
  this.discountPolicy = discountPolicy;
@@ -33,7 +34,7 @@ var ChargesModel = /** @class */ (function () {
33
34
  this.cancelledBookingGrossAmount > 0) {
34
35
  this.couponCode = "";
35
36
  }
36
- return ChargesModel.getDiscountAmount(this.couponCode, this.getGrossAmount());
37
+ return ChargesModel.getDiscountAmount(this.couponCode, this.getGrossAmount(), false, this.totalBookingHours);
37
38
  };
38
39
  ChargesModel.prototype.getPayableRefundAmount = function () {
39
40
  return (this.getNetAmount() +
@@ -46,11 +47,19 @@ var ChargesModel = /** @class */ (function () {
46
47
  // this.totalRefundAmount = this.totalRefundAmount - refundAmount;
47
48
  // this.refundedAmount += refundAmount;
48
49
  // }
49
- ChargesModel.getDiscountAmount = function (couponCode, totalCost, validationApply) {
50
+ ChargesModel.getDiscountAmount = function (couponCode, totalCost, validationApply, totalBookingHours) {
50
51
  if (validationApply === void 0) { validationApply = false; }
51
52
  var discountAmount = 0;
52
53
  if (couponCode) {
53
- if (couponCode.slice(-1) === "P") {
54
+ var code = couponCode.match(/[a-zA-Z]+/g);
55
+ var value = couponCode.match(/\d+/g);
56
+ if (validationApply && !code && !value) {
57
+ throw new app_badrequest_exception_1.AppBadRequestException({
58
+ key: "couponCode",
59
+ message: ["Coupon with code ".concat(couponCode, " is wrong ")],
60
+ });
61
+ }
62
+ if (code && (code[0] === "P" || code[0] === "PHP")) {
54
63
  var percentDiscount = parseFloat(couponCode);
55
64
  if (validationApply && percentDiscount > 90) {
56
65
  throw new app_badrequest_exception_1.AppBadRequestException({
@@ -60,9 +69,20 @@ var ChargesModel = /** @class */ (function () {
60
69
  }
61
70
  discountAmount = totalCost * (percentDiscount / 100);
62
71
  }
63
- else if (couponCode.slice(-1) === "U") {
72
+ else if (code && code[0] === "U") {
64
73
  discountAmount = parseFloat(couponCode);
65
74
  }
75
+ else if (code && code[0] === "PHU") {
76
+ if (!totalBookingHours) {
77
+ throw new app_badrequest_exception_1.AppBadRequestException({
78
+ key: "totalBookingHours",
79
+ message: [
80
+ "Total Booking Hours is mandatory to calculate per hour unit",
81
+ ],
82
+ });
83
+ }
84
+ discountAmount = parseFloat(couponCode) * totalBookingHours;
85
+ }
66
86
  }
67
87
  return Math.round(discountAmount);
68
88
  };
@@ -163,7 +183,7 @@ var ChargesModel = /** @class */ (function () {
163
183
  return newChargeModel;
164
184
  };
165
185
  ChargesModel.prototype.clone = function () {
166
- var that = new ChargesModel(this.initialBookingGrossAmount, this.amountPaid, this.couponCode, this.discountPolicy);
186
+ var that = new ChargesModel(this.initialBookingGrossAmount, this.amountPaid, this.couponCode, this.totalBookingHours, this.discountPolicy);
167
187
  Object.assign(that, this);
168
188
  return that;
169
189
  };
@@ -187,7 +207,7 @@ var ChargesModel = /** @class */ (function () {
187
207
  };
188
208
  ChargesModel.getInstance = function (chargeData, discountPolicy) {
189
209
  if (discountPolicy === void 0) { discountPolicy = modification_discount_policy_enum_1.ModificationDiscountPolicy.DISCOUNT_CONDITIONAL; }
190
- var chargesModel = new ChargesModel(chargeData.initialBookingGrossAmount, chargeData.amountPaid, chargeData.couponCode, discountPolicy);
210
+ var chargesModel = new ChargesModel(chargeData.initialBookingGrossAmount, chargeData.amountPaid, chargeData.couponCode, chargeData.totalBookingHours, discountPolicy);
191
211
  Object.assign(chargesModel, chargeData);
192
212
  return chargesModel;
193
213
  };
@@ -12,5 +12,6 @@ export declare class DateUtilService {
12
12
  formatTimeSlotCode(timeSlot: string, both?: boolean, date?: Date | null): string;
13
13
  formatDateCode(dateCode: string): string;
14
14
  getDateNDaysFromToday(n: number): Date;
15
+ calculateDateDifference(fromDateCode: string, toDateCode: string, unit?: "minute" | "hr"): number;
15
16
  }
16
17
  export declare const dateUtilService: DateUtilService;
@@ -63,6 +63,15 @@ var DateUtilService = /** @class */ (function () {
63
63
  futureDate.setDate(today.getDate() + n);
64
64
  return futureDate;
65
65
  };
66
+ DateUtilService.prototype.calculateDateDifference = function (fromDateCode, toDateCode, unit) {
67
+ if (unit === void 0) { unit = "minute"; }
68
+ var fromDate = (0, date_fns_1.parse)(fromDateCode, "yyMMddHHmm", new Date());
69
+ var toDate = (0, date_fns_1.parse)(toDateCode, "yyMMddHHmm", new Date());
70
+ var minutesDifference = (0, date_fns_1.differenceInMinutes)(toDate, fromDate);
71
+ return unit === "minute"
72
+ ? minutesDifference
73
+ : parseFloat((minutesDifference / 60).toFixed(1));
74
+ };
66
75
  return DateUtilService;
67
76
  }());
68
77
  exports.DateUtilService = DateUtilService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turf-common",
3
- "version": "1.2.1-beta.10",
3
+ "version": "1.2.1-beta.12",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",