@konplit-services/common 1.0.302 → 1.0.303
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.
|
@@ -5,4 +5,12 @@ import { PLAN_INTERVAL } from "./plan-types";
|
|
|
5
5
|
* @param {PLAN_INTERVAL} interval - The subscription interval.
|
|
6
6
|
* @returns {string} - The generated cron expression.
|
|
7
7
|
*/
|
|
8
|
-
export declare const generateCronExpression: (interval: PLAN_INTERVAL) => string;
|
|
8
|
+
export declare const generateCronExpression: (startDate: Date, interval: PLAN_INTERVAL) => string;
|
|
9
|
+
/**
|
|
10
|
+
* Generates a cron expression for a reminder based on the current date and selected interval.
|
|
11
|
+
*
|
|
12
|
+
* @param {Date} startDate - The start date of the subscription.
|
|
13
|
+
* @param {PLAN_INTERVAL} interval - The subscription interval.
|
|
14
|
+
* @returns {string} - The generated cron expression for the reminder.
|
|
15
|
+
*/
|
|
16
|
+
export declare const generateReminderCronEcpression: (startDate: Date, interval: PLAN_INTERVAL) => string;
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.generateCronExpression = void 0;
|
|
6
|
+
exports.generateReminderCronEcpression = exports.generateCronExpression = void 0;
|
|
7
7
|
const dayjs_1 = __importDefault(require("dayjs"));
|
|
8
8
|
const utc_1 = __importDefault(require("dayjs/plugin/utc"));
|
|
9
9
|
const plan_types_1 = require("./plan-types");
|
|
@@ -14,8 +14,8 @@ dayjs_1.default.extend(utc_1.default);
|
|
|
14
14
|
* @param {PLAN_INTERVAL} interval - The subscription interval.
|
|
15
15
|
* @returns {string} - The generated cron expression.
|
|
16
16
|
*/
|
|
17
|
-
const generateCronExpression = (interval) => {
|
|
18
|
-
const currentDate = (0, dayjs_1.default)().utc(); // Get current date in UTC
|
|
17
|
+
const generateCronExpression = (startDate, interval) => {
|
|
18
|
+
const currentDate = (0, dayjs_1.default)(startDate).utc(); // Get current date in UTC
|
|
19
19
|
let cronExpression = "";
|
|
20
20
|
switch (interval) {
|
|
21
21
|
case plan_types_1.PLAN_INTERVAL.HOURLY:
|
|
@@ -64,3 +64,80 @@ const generateCronExpression = (interval) => {
|
|
|
64
64
|
return cronExpression;
|
|
65
65
|
};
|
|
66
66
|
exports.generateCronExpression = generateCronExpression;
|
|
67
|
+
/**
|
|
68
|
+
* Generates a cron expression for a reminder based on the current date and selected interval.
|
|
69
|
+
*
|
|
70
|
+
* @param {Date} startDate - The start date of the subscription.
|
|
71
|
+
* @param {PLAN_INTERVAL} interval - The subscription interval.
|
|
72
|
+
* @returns {string} - The generated cron expression for the reminder.
|
|
73
|
+
*/
|
|
74
|
+
const generateReminderCronEcpression = (startDate, interval) => {
|
|
75
|
+
const currentDate = (0, dayjs_1.default)(startDate).utc(); // Get current date in UTC
|
|
76
|
+
let reminderDate;
|
|
77
|
+
switch (interval) {
|
|
78
|
+
case plan_types_1.PLAN_INTERVAL.HOURLY:
|
|
79
|
+
// Reminder 18 minutes before the next charge time
|
|
80
|
+
reminderDate = currentDate.add(42, "minute"); // 60 - 18 = 42 minutes from now
|
|
81
|
+
break;
|
|
82
|
+
case plan_types_1.PLAN_INTERVAL.DAILY:
|
|
83
|
+
// Reminder 3 hours before the next charge time
|
|
84
|
+
reminderDate = currentDate.add(21, "hour"); // 24 - 3 = 21 hours from now
|
|
85
|
+
break;
|
|
86
|
+
case plan_types_1.PLAN_INTERVAL.WEEKLY:
|
|
87
|
+
// Reminder 3 hours before the next charge time
|
|
88
|
+
reminderDate = currentDate.add(7, "day").subtract(3, "hour");
|
|
89
|
+
break;
|
|
90
|
+
case plan_types_1.PLAN_INTERVAL.MONTHLY:
|
|
91
|
+
// Reminder 3 hours before the next charge time
|
|
92
|
+
reminderDate = currentDate.add(1, "month").subtract(3, "hour");
|
|
93
|
+
break;
|
|
94
|
+
case plan_types_1.PLAN_INTERVAL.QUARTERLY:
|
|
95
|
+
// Reminder 3 hours before the next charge time
|
|
96
|
+
reminderDate = currentDate.add(3, "month").subtract(3, "hour");
|
|
97
|
+
break;
|
|
98
|
+
case plan_types_1.PLAN_INTERVAL["6MONTHS"]:
|
|
99
|
+
// Reminder 3 hours before the next charge time
|
|
100
|
+
reminderDate = currentDate.add(6, "month").subtract(3, "hour");
|
|
101
|
+
break;
|
|
102
|
+
case plan_types_1.PLAN_INTERVAL.YEARLY:
|
|
103
|
+
// Reminder 3 hours before the next charge time
|
|
104
|
+
reminderDate = currentDate.add(1, "year").subtract(3, "hour");
|
|
105
|
+
break;
|
|
106
|
+
default:
|
|
107
|
+
throw new Error(`Invalid interval for generating reminder: ${interval}`);
|
|
108
|
+
}
|
|
109
|
+
// Convert the reminder date to a cron expression
|
|
110
|
+
const minute = reminderDate.minute();
|
|
111
|
+
const hour = reminderDate.hour();
|
|
112
|
+
const dayOfMonth = reminderDate.date();
|
|
113
|
+
const month = reminderDate.month() + 1; // Months are 0-indexed in dayjs
|
|
114
|
+
const dayOfWeek = reminderDate.day();
|
|
115
|
+
// Handle edge case for monthly intervals (dayOfMonth > 28)
|
|
116
|
+
const adjustedDayOfMonth = dayOfMonth > 28 ? 28 : dayOfMonth;
|
|
117
|
+
switch (interval) {
|
|
118
|
+
case plan_types_1.PLAN_INTERVAL.HOURLY:
|
|
119
|
+
// Run at the calculated minute every hour
|
|
120
|
+
return `${minute} * * * *`;
|
|
121
|
+
case plan_types_1.PLAN_INTERVAL.DAILY:
|
|
122
|
+
// Run at the calculated time every day
|
|
123
|
+
return `${minute} ${hour} * * *`;
|
|
124
|
+
case plan_types_1.PLAN_INTERVAL.WEEKLY:
|
|
125
|
+
// Run at the calculated time on the same day of the week
|
|
126
|
+
return `${minute} ${hour} * * ${dayOfWeek}`;
|
|
127
|
+
case plan_types_1.PLAN_INTERVAL.MONTHLY:
|
|
128
|
+
// Run at the calculated time on the same day every month
|
|
129
|
+
return `${minute} ${hour} ${adjustedDayOfMonth} * *`;
|
|
130
|
+
case plan_types_1.PLAN_INTERVAL.QUARTERLY:
|
|
131
|
+
// Run at the calculated time every 3 months
|
|
132
|
+
return `${minute} ${hour} ${adjustedDayOfMonth} */3 *`;
|
|
133
|
+
case plan_types_1.PLAN_INTERVAL["6MONTHS"]:
|
|
134
|
+
// Run at the calculated time every 6 months
|
|
135
|
+
return `${minute} ${hour} ${adjustedDayOfMonth} */6 *`;
|
|
136
|
+
case plan_types_1.PLAN_INTERVAL.YEARLY:
|
|
137
|
+
// Run at the calculated time on the same day and month every year
|
|
138
|
+
return `${minute} ${hour} ${adjustedDayOfMonth} ${month} *`;
|
|
139
|
+
default:
|
|
140
|
+
throw new Error(`Invalid interval for generating reminder: ${interval}`);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
exports.generateReminderCronEcpression = generateReminderCronEcpression;
|