@escapenavigator/utils 1.8.5 → 1.8.6

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.
@@ -1,14 +1,28 @@
1
1
  import { PromocodeRO } from '@escapenavigator/types/dist/promocode/promocode.ro';
2
- export declare function validatePromocode({ promocode, players, questroomId, timeZone, orderDate, hasCertificates, hasPromocodes, isInside, }: {
3
- promocode: PromocodeRO;
4
- isInside: boolean;
2
+ declare type Cert = {
3
+ questroomId?: never;
4
+ players?: never;
5
+ hasCertificates?: never;
6
+ orderDate?: never;
7
+ timeZone: string;
8
+ type: 'certificate';
9
+ };
10
+ declare type Order = {
5
11
  questroomId: number;
6
12
  players: number;
7
13
  hasCertificates: boolean;
8
- hasPromocodes: boolean;
9
14
  orderDate: string;
10
15
  timeZone: string;
11
- }): {
16
+ type: 'order';
17
+ };
18
+ export declare function validatePromocode({ promocode, players, questroomId, timeZone, orderDate, hasCertificates, otherPromocodes, isInside, type, }: {
19
+ promocode: PromocodeRO;
20
+ isInside: boolean;
21
+ otherPromocodes: Array<{
22
+ canUseWithOtherPromocodes: boolean;
23
+ }>;
24
+ } & (Order | Cert)): {
12
25
  valid: boolean;
13
26
  errors: any[];
14
27
  };
28
+ export {};
@@ -5,15 +5,21 @@ function getWeekdayNumber(date) {
5
5
  const day = new Date(date).getDay();
6
6
  return (day === 0 ? 7 : day) - 1;
7
7
  }
8
- function validatePromocode({ promocode, players, questroomId, timeZone, orderDate, hasCertificates, hasPromocodes, isInside, }) {
8
+ function validatePromocode({ promocode, players, questroomId, timeZone, orderDate, hasCertificates, otherPromocodes, isInside, type, }) {
9
9
  const errors = [];
10
- const orderDateInTimeZone = new Date(new Date(orderDate).toLocaleString('en-US', { timeZone }));
11
- const currentDate = orderDateInTimeZone.toISOString().split('T')[0];
12
- const orderDayOfWeek = getWeekdayNumber(orderDateInTimeZone);
13
- const localOrderTime = new Date(orderDate).toLocaleTimeString('en-GB', { timeZone });
14
- const currentHour = localOrderTime;
10
+ const orderDateInTimeZone = type === 'order' && new Date(new Date(orderDate).toLocaleString('en-US', { timeZone }));
11
+ const currentDate = type === 'order' && orderDateInTimeZone.toISOString().split('T')[0];
12
+ const orderDayOfWeek = type === 'order' && getWeekdayNumber(orderDateInTimeZone);
13
+ const localOrderTime = type === 'order' && new Date(orderDate).toLocaleTimeString('en-GB', { timeZone });
14
+ const currentHour = type === 'order' && localOrderTime;
15
15
  const today = new Date(new Date().toLocaleString('en-US', { timeZone }));
16
16
  const todayDate = today.toISOString().split('T')[0];
17
+ if (type === 'certificate' && !promocode.availableForCertificates) {
18
+ errors.push('The promocode can\'t be applied for certificates.');
19
+ }
20
+ if (type === 'order' && !promocode.availableForBookings) {
21
+ errors.push('The promocode can\'t be applied for certificates.');
22
+ }
17
23
  if (promocode.inside && !isInside) {
18
24
  errors.push('The promocode can be applied by phone.');
19
25
  }
@@ -26,7 +32,7 @@ function validatePromocode({ promocode, players, questroomId, timeZone, orderDat
26
32
  else if (promocode.applyings > 0) {
27
33
  errors.push('The promocode can only be used once.');
28
34
  }
29
- if (!promocode.canUseWithOtherPromocodes && hasPromocodes) {
35
+ if (!promocode.canUseWithOtherPromocodes && otherPromocodes.length) {
30
36
  errors.push('The promocode cannot be used with other promocodes.');
31
37
  }
32
38
  if (!promocode.canUseWithOtherCertificates && hasCertificates) {
@@ -38,22 +44,22 @@ function validatePromocode({ promocode, players, questroomId, timeZone, orderDat
38
44
  if (promocode.validTo && todayDate > promocode.validTo) {
39
45
  errors.push(`The promocode has expired. It was valid until ${promocode.validTo}.`);
40
46
  }
41
- if (promocode.forOrdersFrom && currentDate < promocode.forOrdersFrom) {
47
+ if (type === 'order' && promocode.forOrdersFrom && currentDate < promocode.forOrdersFrom) {
42
48
  errors.push(`The promocode is valid for games starting from ${promocode.forOrdersFrom}.`);
43
49
  }
44
- if (promocode.forOrdersTo && currentDate > promocode.forOrdersTo) {
50
+ if (type === 'order' && promocode.forOrdersTo && currentDate > promocode.forOrdersTo) {
45
51
  errors.push(`The promocode is not valid for games after ${promocode.forOrdersTo}.`);
46
52
  }
47
53
  if (!promocode.allQuestrooms && !promocode.questroomsIds?.includes(questroomId)) {
48
54
  errors.push('The promocode is not valid for the selected escape room.');
49
55
  }
50
- if (promocode.availableFrom && currentHour < promocode.availableFrom) {
56
+ if (type === 'order' && promocode.availableFrom && currentHour < promocode.availableFrom) {
51
57
  errors.push(`The promocode is available from ${promocode.availableFrom}.`);
52
58
  }
53
- if (promocode.availableTo && currentHour > promocode.availableTo) {
59
+ if (type === 'order' && promocode.availableTo && currentHour > promocode.availableTo) {
54
60
  errors.push(`The promocode is only available until ${promocode.availableTo}.`);
55
61
  }
56
- if (promocode.availableDays && !promocode.availableDays.includes(orderDayOfWeek)) {
62
+ if (type === 'order' && promocode.availableDays && !promocode.availableDays.includes(orderDayOfWeek)) {
57
63
  errors.push('The promocode is not valid for the selected day.');
58
64
  }
59
65
  if (promocode.minPlayers && players < promocode.minPlayers) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@escapenavigator/utils",
3
- "version": "1.8.5",
3
+ "version": "1.8.6",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -14,7 +14,7 @@
14
14
  "test": "jest"
15
15
  },
16
16
  "dependencies": {
17
- "@escapenavigator/types": "^1.8.5",
17
+ "@escapenavigator/types": "^1.8.6",
18
18
  "axios": "^0.21.4",
19
19
  "class-transformer": "^0.5.1",
20
20
  "class-validator": "^0.13.2",
@@ -55,5 +55,5 @@
55
55
  "node_modules"
56
56
  ]
57
57
  },
58
- "gitHead": "f810b0697a133402f07a2c6077ef61bbb0c19ac1"
58
+ "gitHead": "ad7056985c8d13d98cde69ec89bb048bc9eb32bf"
59
59
  }