@konplit-services/common 1.26.0 → 1.26.1

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,4 +1,10 @@
1
+ import dayjs from "dayjs";
1
2
  import { PLAN_INTERVAL } from "./plan-types";
3
+ type Period = "day" | "week" | "month" | "year";
4
+ interface DateRange {
5
+ startOfPeriod: Date;
6
+ endOfPeriod: Date;
7
+ }
2
8
  export declare const formatDate: (dateInput: string | Date | number, // support more input types
3
9
  format?: string, tz?: string) => string;
4
10
  export declare const interswitchRefundDateFormat: (dataString: string) => string;
@@ -12,10 +18,15 @@ export declare const getCurrentDayRange: () => {
12
18
  startOfCurrentDay: Date;
13
19
  endOfCurrentDay: Date;
14
20
  };
15
- export declare const getStartAndEndDate: (date: string | Date) => {
16
- startOfDate: Date;
17
- endOfDate: Date;
18
- };
21
+ /**
22
+ * Returns the start and end of a given period for the provided date
23
+ *
24
+ * @param date - Input date (string, Date, or dayjs object)
25
+ * @param period - The time period to calculate range for ("day" | "week" | "month" | "year")
26
+ * @param timezone - Optional timezone (defaults to Africa/Lagos)
27
+ * @returns Object containing startOfPeriod and endOfPeriod as native Date objects (in UTC)
28
+ */
29
+ export declare const getStartAndEndOfPeriod: (date: string | Date | dayjs.Dayjs, period?: Period, tz?: string) => DateRange;
19
30
  export declare const isTodayOrFuture: (dateInput: string | Date) => boolean;
20
31
  export declare const convertDateToUTCRangeString: (startDateTime: string, endDateTime: string) => {
21
32
  startUTC: string;
@@ -3,18 +3,18 @@ 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.calculateDelay = exports.getFutureDateFromPlan = exports.getDuration = exports.convertDateToUTC = exports.convertDateToUTCString = exports.convertDateToUTCRange = exports.convertDateToUTCRangeString = exports.isTodayOrFuture = exports.getStartAndEndDate = exports.getCurrentDayRange = exports.getPreviousDayRange = exports.convertToDate = exports.formatDateTime = exports.interswitchRefundDateFormat = exports.formatDate = void 0;
6
+ exports.calculateDelay = exports.getFutureDateFromPlan = exports.getDuration = exports.convertDateToUTC = exports.convertDateToUTCString = exports.convertDateToUTCRange = exports.convertDateToUTCRangeString = exports.isTodayOrFuture = exports.getStartAndEndOfPeriod = exports.getCurrentDayRange = exports.getPreviousDayRange = exports.convertToDate = exports.formatDateTime = exports.interswitchRefundDateFormat = exports.formatDate = void 0;
7
7
  exports.canRefundFromSettlementLedgerByDay = canRefundFromSettlementLedgerByDay;
8
8
  exports.getSettlementDate = getSettlementDate;
9
9
  const dayjs_1 = __importDefault(require("dayjs"));
10
10
  const utc_1 = __importDefault(require("dayjs/plugin/utc"));
11
11
  const timezone_1 = __importDefault(require("dayjs/plugin/timezone"));
12
+ const plan_types_1 = require("./plan-types");
12
13
  // Extend dayjs with the UTC plugin
13
14
  dayjs_1.default.extend(utc_1.default);
14
15
  dayjs_1.default.extend(timezone_1.default);
15
16
  const DEFAULT_TZ = "Africa/Lagos";
16
17
  const DEFAULT_FORMAT = "YYYY-MM-DD";
17
- const plan_types_1 = require("./plan-types");
18
18
  const formatDate = (dateInput, // support more input types
19
19
  format = DEFAULT_FORMAT, tz = DEFAULT_TZ) => {
20
20
  if (!dateInput) {
@@ -59,15 +59,49 @@ const getCurrentDayRange = () => {
59
59
  return { startOfCurrentDay, endOfCurrentDay };
60
60
  };
61
61
  exports.getCurrentDayRange = getCurrentDayRange;
62
- const getStartAndEndDate = (date) => {
63
- const startTime = (0, dayjs_1.default)(`${date}`, "YYYY-MM-DD HH:mm");
64
- // Get the start of day
65
- const startOfDate = startTime.utc().startOf("day").toDate();
66
- // Get the end of the previous day in UTC (23:59:59.999)
67
- const endOfDate = startTime.utc().endOf("day").toDate();
68
- return { startOfDate, endOfDate };
62
+ /**
63
+ * Returns the start and end of a given period for the provided date
64
+ *
65
+ * @param date - Input date (string, Date, or dayjs object)
66
+ * @param period - The time period to calculate range for ("day" | "week" | "month" | "year")
67
+ * @param timezone - Optional timezone (defaults to Africa/Lagos)
68
+ * @returns Object containing startOfPeriod and endOfPeriod as native Date objects (in UTC)
69
+ */
70
+ const getStartAndEndOfPeriod = (date, period = "day", tz = DEFAULT_TZ) => {
71
+ // Normalize input to dayjs object in the target timezone
72
+ const inputDate = (0, dayjs_1.default)(date).tz(tz);
73
+ let start;
74
+ let end;
75
+ switch (period) {
76
+ case "day":
77
+ start = inputDate.startOf("day");
78
+ end = inputDate.endOf("day");
79
+ break;
80
+ case "week":
81
+ // Most common: week starts on Monday in many African/business contexts
82
+ // but we are using Sunday start → but can be changed to isoWeek
83
+ start = inputDate.startOf("week");
84
+ end = inputDate.endOf("week");
85
+ break;
86
+ case "month":
87
+ start = inputDate.startOf("month");
88
+ end = inputDate.endOf("month");
89
+ break;
90
+ case "year":
91
+ start = inputDate.startOf("year");
92
+ end = inputDate.endOf("year");
93
+ break;
94
+ default:
95
+ // This should never happen with TypeScript + union type, but good safety net
96
+ throw new Error(`Unsupported period: ${period}`);
97
+ }
98
+ // Return as UTC Dates (most backend systems & databases expect UTC)
99
+ return {
100
+ startOfPeriod: start.utc().toDate(),
101
+ endOfPeriod: end.utc().toDate(),
102
+ };
69
103
  };
70
- exports.getStartAndEndDate = getStartAndEndDate;
104
+ exports.getStartAndEndOfPeriod = getStartAndEndOfPeriod;
71
105
  const isTodayOrFuture = (dateInput) => {
72
106
  const input = dayjs_1.default.utc(dateInput);
73
107
  if (!input.isValid())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konplit-services/common",
3
- "version": "1.26.0",
3
+ "version": "1.26.1",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",