@escapenavigator/utils 1.6.92 → 1.6.94
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.
|
@@ -0,0 +1,14 @@
|
|
|
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;
|
|
5
|
+
questroomId: number;
|
|
6
|
+
players: number;
|
|
7
|
+
hasCertificates: boolean;
|
|
8
|
+
hasPromocodes: boolean;
|
|
9
|
+
orderDate: string;
|
|
10
|
+
timeZone: string;
|
|
11
|
+
}): {
|
|
12
|
+
valid: boolean;
|
|
13
|
+
errors: any[];
|
|
14
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validatePromocode = void 0;
|
|
4
|
+
function getWeekdayNumber(date) {
|
|
5
|
+
const day = new Date(date).getDay();
|
|
6
|
+
return (day === 0 ? 7 : day) - 1;
|
|
7
|
+
}
|
|
8
|
+
function validatePromocode({ promocode, players, questroomId, timeZone, orderDate, hasCertificates, hasPromocodes, isInside, }) {
|
|
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;
|
|
15
|
+
const today = new Date(new Date().toLocaleString('en-US', { timeZone }));
|
|
16
|
+
const todayDate = today.toISOString().split('T')[0];
|
|
17
|
+
if (promocode.inside && !isInside) {
|
|
18
|
+
errors.push('The promocode can be applied by phone.');
|
|
19
|
+
}
|
|
20
|
+
if (promocode.multiple) {
|
|
21
|
+
if (promocode.availableApplyings > 0 &&
|
|
22
|
+
promocode.applyings >= promocode.availableApplyings) {
|
|
23
|
+
errors.push('The maximum number of promocode applications has been reached.');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else if (promocode.applyings > 0) {
|
|
27
|
+
errors.push('The promocode can only be used once.');
|
|
28
|
+
}
|
|
29
|
+
if (!promocode.canUseWithOtherPromocodes && hasPromocodes) {
|
|
30
|
+
errors.push('The promocode cannot be used with other promocodes.');
|
|
31
|
+
}
|
|
32
|
+
if (!promocode.canUseWithOtherCertificates && hasCertificates) {
|
|
33
|
+
errors.push('The promocode cannot be used with other certificates.');
|
|
34
|
+
}
|
|
35
|
+
if (promocode.validFrom && todayDate < promocode.validFrom) {
|
|
36
|
+
errors.push(`The promocode is valid from ${promocode.validFrom}.`);
|
|
37
|
+
}
|
|
38
|
+
if (promocode.validTo && todayDate > promocode.validTo) {
|
|
39
|
+
errors.push(`The promocode has expired. It was valid until ${promocode.validTo}.`);
|
|
40
|
+
}
|
|
41
|
+
if (promocode.forOrdersFrom && currentDate < promocode.forOrdersFrom) {
|
|
42
|
+
errors.push(`The promocode is valid for games starting from ${promocode.forOrdersFrom}.`);
|
|
43
|
+
}
|
|
44
|
+
if (promocode.forOrdersTo && currentDate > promocode.forOrdersTo) {
|
|
45
|
+
errors.push(`The promocode is not valid for games after ${promocode.forOrdersTo}.`);
|
|
46
|
+
}
|
|
47
|
+
if (!promocode.allQuestrooms && !promocode.questroomsIds?.includes(questroomId)) {
|
|
48
|
+
errors.push('The promocode is not valid for the selected escape room.');
|
|
49
|
+
}
|
|
50
|
+
if (promocode.availableFrom && currentHour < promocode.availableFrom) {
|
|
51
|
+
errors.push(`The promocode is available from ${promocode.availableFrom}.`);
|
|
52
|
+
}
|
|
53
|
+
if (promocode.availableTo && currentHour > promocode.availableTo) {
|
|
54
|
+
errors.push(`The promocode is only available until ${promocode.availableTo}.`);
|
|
55
|
+
}
|
|
56
|
+
if (promocode.availableDays && !promocode.availableDays.includes(orderDayOfWeek)) {
|
|
57
|
+
errors.push('The promocode is not valid for the selected day.');
|
|
58
|
+
}
|
|
59
|
+
if (promocode.minPlayers && players < promocode.minPlayers) {
|
|
60
|
+
errors.push(`The promocode is valid only for games with at least ${promocode.minPlayers} players.`);
|
|
61
|
+
}
|
|
62
|
+
if (promocode.maxPlayers && players > promocode.maxPlayers) {
|
|
63
|
+
errors.push(`The promocode is valid only for games with no more than ${promocode.maxPlayers} players.`);
|
|
64
|
+
}
|
|
65
|
+
if (errors.length > 0) {
|
|
66
|
+
return {
|
|
67
|
+
valid: false,
|
|
68
|
+
errors,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
valid: true,
|
|
73
|
+
errors: [],
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
exports.validatePromocode = validatePromocode;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@escapenavigator/utils",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.94",
|
|
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.6.
|
|
17
|
+
"@escapenavigator/types": "^1.6.92",
|
|
18
18
|
"axios": "^0.21.4",
|
|
19
19
|
"class-transformer": "^0.5.1",
|
|
20
20
|
"class-validator": "^0.13.1",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"node_modules"
|
|
56
56
|
]
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "f3643cd62636b0d5e30c5385f177974f60bb7875"
|
|
59
59
|
}
|