@managesome/eposmatic-toolkit 0.2.580 → 0.2.582

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,6 +1,24 @@
1
1
  import { ILocalTimeFields } from '../types/common';
2
2
  export declare function getTenantTime(desiredGMTOffset: number, date?: Date): Date;
3
3
  export declare function getDaysDifference(date1: Date, date2: Date, gmtOffset: number): number;
4
+ /**
5
+ * ISO 8601 week number (1-53) for a calendar date: weeks start on Monday, and
6
+ * week 1 is the week containing the year's first Thursday. Every week is
7
+ * exactly 7 days and the number can never exceed 53, so no clamp is needed.
8
+ *
9
+ * Pure UTC calendar math — independent of the runtime server's timezone.
10
+ */
11
+ export declare function getIsoWeekOfYear(localYear: number, localMonth: number, localDay: number): number;
12
+ /**
13
+ * Returns the start date ("YYYY-MM-DD") of the calendar week containing the
14
+ * given localDate. `weekStart` selects the first day of the week:
15
+ * 0 = Sunday, 1 = Monday (default, ISO-aligned).
16
+ *
17
+ * Every resulting week is exactly 7 days regardless of month/year boundaries —
18
+ * the recommended bucket for weekly analytics (same as GA's weekly granularity,
19
+ * and it never mixes partial weeks).
20
+ */
21
+ export declare function getCalendarWeekStart(localDate: string, weekStart?: number): string;
4
22
  export declare function getLocalDocumentFields(gmtOffset: number, date: Date): {
5
23
  localSecond: number;
6
24
  localMinute: number;
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getTenantTime = getTenantTime;
4
4
  exports.getDaysDifference = getDaysDifference;
5
+ exports.getIsoWeekOfYear = getIsoWeekOfYear;
6
+ exports.getCalendarWeekStart = getCalendarWeekStart;
5
7
  exports.getLocalDocumentFields = getLocalDocumentFields;
6
8
  exports.getLocalFieldsFromLocalDate = getLocalFieldsFromLocalDate;
7
9
  const constants_1 = require("../../constants");
@@ -21,6 +23,39 @@ function getDaysDifference(date1, date2, gmtOffset) {
21
23
  const millisecondsInADay = 24 * 60 * 60 * 1000;
22
24
  return Math.floor((date1Time - date2Time) / millisecondsInADay);
23
25
  }
26
+ /**
27
+ * ISO 8601 week number (1-53) for a calendar date: weeks start on Monday, and
28
+ * week 1 is the week containing the year's first Thursday. Every week is
29
+ * exactly 7 days and the number can never exceed 53, so no clamp is needed.
30
+ *
31
+ * Pure UTC calendar math — independent of the runtime server's timezone.
32
+ */
33
+ function getIsoWeekOfYear(localYear, localMonth, localDay) {
34
+ const date = new Date(Date.UTC(localYear, localMonth - 1, localDay));
35
+ const dayNum = date.getUTCDay() || 7; // Monday = 1 ... Sunday = 7
36
+ // Move to the Thursday of the current ISO week.
37
+ date.setUTCDate(date.getUTCDate() + 4 - dayNum);
38
+ const isoYearStart = new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
39
+ const diffInDays = (date.getTime() - isoYearStart.getTime()) / (24 * 60 * 60 * 1000);
40
+ return Math.ceil((diffInDays + 1) / 7);
41
+ }
42
+ /**
43
+ * Returns the start date ("YYYY-MM-DD") of the calendar week containing the
44
+ * given localDate. `weekStart` selects the first day of the week:
45
+ * 0 = Sunday, 1 = Monday (default, ISO-aligned).
46
+ *
47
+ * Every resulting week is exactly 7 days regardless of month/year boundaries —
48
+ * the recommended bucket for weekly analytics (same as GA's weekly granularity,
49
+ * and it never mixes partial weeks).
50
+ */
51
+ function getCalendarWeekStart(localDate, weekStart = 1) {
52
+ const [localYear, localMonth, localDay] = localDate.split('-').map(Number);
53
+ const date = new Date(Date.UTC(localYear, localMonth - 1, localDay));
54
+ const weekday = date.getUTCDay(); // 0 (Sun) - 6 (Sat)
55
+ const diff = (weekday - weekStart + 7) % 7;
56
+ date.setUTCDate(date.getUTCDate() - diff);
57
+ return date.toISOString().slice(0, 10);
58
+ }
24
59
  function getLocalDocumentFields(gmtOffset, date) {
25
60
  const tenantDate = getTenantTime(gmtOffset, date);
26
61
  const localSecond = tenantDate.getSeconds(); // 0 - 59
@@ -30,15 +65,18 @@ function getLocalDocumentFields(gmtOffset, date) {
30
65
  const localDate = tenantDate.getDate(); // 1 - 31
31
66
  const localMonth = tenantDate.getMonth() + 1; // 1 - 12
32
67
  const localYear = tenantDate.getFullYear(); // YYYY
33
- // Calculate week of month (1-5). Clamped to 5 because a 30/31-day month whose
34
- // 1st is a Friday/Saturday would otherwise compute to 6 (schema caps at 5).
35
- const firstDayOfMonth = new Date(tenantDate.getFullYear(), tenantDate.getMonth(), 1);
36
- const localWeek = Math.min(Math.ceil((localDate + firstDayOfMonth.getDay()) / 7), 5);
37
- // Calculate week of year (1-53). Clamped to 53 because a leap year starting
38
- // on a Saturday (e.g. 2028) would otherwise compute to 54 (schema caps at 53).
39
- const startOfYear = new Date(localYear, 0, 1);
40
- const dayOfYear = Math.floor((tenantDate.getTime() - startOfYear.getTime()) / (24 * 60 * 60 * 1000)) + 1;
41
- const localWeekOfYear = Math.min(Math.ceil((dayOfYear + startOfYear.getDay()) / 7), 53);
68
+ // Week of month (1-5): day-of-month blocks 1-7 → 1, 8-14 → 2, ..., 29-31 → 5.
69
+ // Every block is exactly 7 days except the final one, and the value can never
70
+ // exceed 5 — no clamp is required (schema caps at 5).
71
+ const localWeek = Math.ceil(localDate / 7);
72
+ // Day of year (1-366) — pure calendar math (no server timezone dependence),
73
+ // so localDayOfYear can never contradict localDate/localMonth/localYear.
74
+ const localDayOfYear = Math.floor((Date.UTC(localYear, localMonth - 1, localDate) - Date.UTC(localYear, 0, 1)) / (24 * 60 * 60 * 1000)) +
75
+ 1;
76
+ // Week of year (1-53) — ISO 8601 (Monday start, week 1 contains the first
77
+ // Thursday). ISO weeks never exceed 53, so no clamp is required (schema caps
78
+ // at 53).
79
+ const localWeekOfYear = getIsoWeekOfYear(localYear, localMonth, localDate);
42
80
  // Calculate quarter (1-4)
43
81
  const localQuarter = Math.ceil(localMonth / 3);
44
82
  // Calculate semester (1-2)
@@ -59,7 +97,7 @@ function getLocalDocumentFields(gmtOffset, date) {
59
97
  localYear,
60
98
  localDate: localDateString,
61
99
  localTime: localTimeString,
62
- localDayOfYear: dayOfYear,
100
+ localDayOfYear,
63
101
  };
64
102
  }
65
103
  /**
@@ -82,14 +120,13 @@ function getLocalFieldsFromLocalDate(localDate, operational_time_zone_offset_gmt
82
120
  // component is derived directly from the string using pure calendar math
83
121
  // (no dependence on the server's timezone).
84
122
  const weekday = new Date(Date.UTC(localYear, localMonth - 1, localDay)).getUTCDay();
85
- // Week of month (1-5) — same formula as getLocalDocumentFields, clamped to 5.
86
- const firstDayOfMonthDow = new Date(Date.UTC(localYear, localMonth - 1, 1)).getUTCDay();
87
- const localWeek = Math.min(Math.ceil((localDay + firstDayOfMonthDow) / 7), 5);
88
- // Day of year (1-366) and week of year (1-53). Clamped to 53 because a leap
89
- // year starting on a Saturday (e.g. 2028) would otherwise compute to 54.
90
- const dayOfYear = Math.floor((Date.UTC(localYear, localMonth - 1, localDay) - Date.UTC(localYear, 0, 1)) / (24 * 60 * 60 * 1000)) + 1;
91
- const startOfYearDow = new Date(Date.UTC(localYear, 0, 1)).getUTCDay();
92
- const localWeekOfYear = Math.min(Math.ceil((dayOfYear + startOfYearDow) / 7), 53);
123
+ // Week of month (1-5) — day-of-month blocks (1-7 → 1 ... 29-31 → 5). Same
124
+ // semantics as getLocalDocumentFields; never exceeds 5, no clamp required.
125
+ const localWeek = Math.ceil(localDay / 7);
126
+ // Day of year (1-366) and ISO week of year (1-53) — pure calendar math
127
+ // (no dependence on the server's timezone).
128
+ const localDayOfYear = Math.floor((Date.UTC(localYear, localMonth - 1, localDay) - Date.UTC(localYear, 0, 1)) / (24 * 60 * 60 * 1000)) + 1;
129
+ const localWeekOfYear = getIsoWeekOfYear(localYear, localMonth, localDay);
93
130
  // Time components come from the actual settle timestamp in the tenant's
94
131
  // timezone. The timestamp is normalized with new Date() since callers pass
95
132
  // the field straight from the order (which may be a Date or an ISO string).
@@ -108,6 +145,6 @@ function getLocalFieldsFromLocalDate(localDate, operational_time_zone_offset_gmt
108
145
  localQuarter: Math.ceil(localMonth / 3),
109
146
  localSemester: Math.ceil(localMonth / 6),
110
147
  localYear,
111
- localDayOfYear: dayOfYear,
148
+ localDayOfYear,
112
149
  };
113
150
  }
@@ -393,3 +393,39 @@ export declare const TenantCashDrawerSessionsSchema: Schema<any, import("mongoos
393
393
  __v: number;
394
394
  }>;
395
395
  export declare const TENANT_CASH_DRAWER_SESSIONS_COLLECTION_NAME = "tenant_cash_drawer_sessions";
396
+ /**
397
+ * Describes the indexes for `tenant_cash_drawer_sessions`. Values are Mongo key
398
+ * patterns that can be passed straight to `.hint()`.
399
+ * - `_id`: unique primary key.
400
+ * - `tenant_id__localDate`: running drawer sessions per local date.
401
+ * - `tenant_id__drawer_id__day_id`: UNIQUE per (tenant_id, drawer_id, day_id) —
402
+ * one session per drawer per day.
403
+ * - `tenant_id__drawer_id__status`: PARTIAL index — create with
404
+ * `partialFilterExpression: { status: 'IN_USE' }` (only IN_USE drawer sessions
405
+ * are indexed; every query using this hint filters status: IN_USE). Not unique
406
+ * (uniqueness is enforced at the day level above).
407
+ * - `tenant_id__day_id`: drawer sessions per business day.
408
+ */
409
+ export declare const TENANT_CASH_DRAWER_SESSIONS_COLLECTION_INDEXES: {
410
+ _id: {
411
+ _id: number;
412
+ };
413
+ tenant_id__localDate: {
414
+ tenant_id: number;
415
+ localDate: number;
416
+ };
417
+ tenant_id__drawer_id__day_id: {
418
+ tenant_id: number;
419
+ drawer_id: number;
420
+ day_id: number;
421
+ };
422
+ tenant_id__drawer_id__status: {
423
+ tenant_id: number;
424
+ drawer_id: number;
425
+ status: number;
426
+ };
427
+ tenant_id__day_id: {
428
+ tenant_id: number;
429
+ day_id: number;
430
+ };
431
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TENANT_CASH_DRAWER_SESSIONS_COLLECTION_NAME = exports.TenantCashDrawerSessionsSchema = void 0;
3
+ exports.TENANT_CASH_DRAWER_SESSIONS_COLLECTION_INDEXES = exports.TENANT_CASH_DRAWER_SESSIONS_COLLECTION_NAME = exports.TenantCashDrawerSessionsSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const constants_1 = require("../../constants");
6
6
  const common_models_1 = require("./common-models");
@@ -176,3 +176,23 @@ exports.TenantCashDrawerSessionsSchema = new mongoose_1.Schema({
176
176
  minimize: false,
177
177
  });
178
178
  exports.TENANT_CASH_DRAWER_SESSIONS_COLLECTION_NAME = 'tenant_cash_drawer_sessions';
179
+ /**
180
+ * Describes the indexes for `tenant_cash_drawer_sessions`. Values are Mongo key
181
+ * patterns that can be passed straight to `.hint()`.
182
+ * - `_id`: unique primary key.
183
+ * - `tenant_id__localDate`: running drawer sessions per local date.
184
+ * - `tenant_id__drawer_id__day_id`: UNIQUE per (tenant_id, drawer_id, day_id) —
185
+ * one session per drawer per day.
186
+ * - `tenant_id__drawer_id__status`: PARTIAL index — create with
187
+ * `partialFilterExpression: { status: 'IN_USE' }` (only IN_USE drawer sessions
188
+ * are indexed; every query using this hint filters status: IN_USE). Not unique
189
+ * (uniqueness is enforced at the day level above).
190
+ * - `tenant_id__day_id`: drawer sessions per business day.
191
+ */
192
+ exports.TENANT_CASH_DRAWER_SESSIONS_COLLECTION_INDEXES = {
193
+ _id: { _id: 1 }, // Unique Index
194
+ tenant_id__localDate: { tenant_id: 1, localDate: 1 }, // running drawer sessions per local date
195
+ tenant_id__drawer_id__day_id: { tenant_id: 1, drawer_id: 1, day_id: 1 }, // UNIQUE per (tenant, drawer, day)
196
+ tenant_id__drawer_id__status: { tenant_id: 1, drawer_id: 1, status: 1 }, // partial index (partialFilterExpression: { status: 'IN_USE' }) — open drawer lookup (not unique)
197
+ tenant_id__day_id: { tenant_id: 1, day_id: 1 }, // drawer sessions per business day
198
+ };
@@ -42,6 +42,12 @@ export declare const TenantCashDrawerSchema: Schema<any, import("mongoose").Mode
42
42
  __v: number;
43
43
  }>;
44
44
  export declare const TENANT_CASH_DRAWERS_COLLECTION_NAME = "tenant_cash_drawers";
45
+ /**
46
+ * Describes the indexes for `tenant_cash_drawers`. Values are Mongo key patterns
47
+ * that can be passed straight to `.hint()`.
48
+ * - `_id`: unique primary key.
49
+ * - `tenant_id`: per-tenant lookup (`getCashDrawers` / `deleteCashDrawer` scoping).
50
+ */
45
51
  export declare const TENANT_CASH_DRAWERS_COLLECTION_INDEXES: {
46
52
  _id: {
47
53
  _id: number;
@@ -44,6 +44,12 @@ exports.TenantCashDrawerSchema = new mongoose_1.Schema({
44
44
  minimize: false,
45
45
  });
46
46
  exports.TENANT_CASH_DRAWERS_COLLECTION_NAME = 'tenant_cash_drawers';
47
+ /**
48
+ * Describes the indexes for `tenant_cash_drawers`. Values are Mongo key patterns
49
+ * that can be passed straight to `.hint()`.
50
+ * - `_id`: unique primary key.
51
+ * - `tenant_id`: per-tenant lookup (`getCashDrawers` / `deleteCashDrawer` scoping).
52
+ */
47
53
  exports.TENANT_CASH_DRAWERS_COLLECTION_INDEXES = {
48
54
  _id: { _id: 1 }, // Unique Index
49
55
  tenant_id: {
@@ -114,3 +114,33 @@ export declare const TenantCashInOutSchema: Schema<any, import("mongoose").Model
114
114
  __v: number;
115
115
  }>;
116
116
  export declare const TENANT_CASH_IN_OUT_COLLECTION_NAME = "tenant_cash_in_out";
117
+ /**
118
+ * Describes the indexes for `tenant_cash_in_out`. Values are Mongo key patterns
119
+ * that can be passed straight to `.hint()`.
120
+ * - `_id`: unique primary key.
121
+ * - `tenant_id__localDate`: running movements per local date.
122
+ * - `tenant_id__drawer_id__createdAt`: running balance per drawer session
123
+ * lifetime (createdAt range + desc sort).
124
+ * - `tenant_id__day_id__shift_id`: movements per business day / shift (scoped by
125
+ * the shift template id); also covers (tenant_id, day_id) lookups via the
126
+ * leftmost-prefix rule.
127
+ */
128
+ export declare const TENANT_CASH_IN_OUT_COLLECTION_INDEXES: {
129
+ _id: {
130
+ _id: number;
131
+ };
132
+ tenant_id__localDate: {
133
+ tenant_id: number;
134
+ localDate: number;
135
+ };
136
+ tenant_id__drawer_id__createdAt: {
137
+ tenant_id: number;
138
+ drawer_id: number;
139
+ createdAt: number;
140
+ };
141
+ tenant_id__day_id__shift_id: {
142
+ tenant_id: number;
143
+ day_id: number;
144
+ shift_id: number;
145
+ };
146
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TENANT_CASH_IN_OUT_COLLECTION_NAME = exports.TenantCashInOutSchema = void 0;
3
+ exports.TENANT_CASH_IN_OUT_COLLECTION_INDEXES = exports.TENANT_CASH_IN_OUT_COLLECTION_NAME = exports.TenantCashInOutSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const constants_1 = require("../../constants");
6
6
  const common_models_1 = require("./common-models");
@@ -88,3 +88,20 @@ exports.TenantCashInOutSchema = new mongoose_1.Schema({
88
88
  minimize: false,
89
89
  });
90
90
  exports.TENANT_CASH_IN_OUT_COLLECTION_NAME = 'tenant_cash_in_out';
91
+ /**
92
+ * Describes the indexes for `tenant_cash_in_out`. Values are Mongo key patterns
93
+ * that can be passed straight to `.hint()`.
94
+ * - `_id`: unique primary key.
95
+ * - `tenant_id__localDate`: running movements per local date.
96
+ * - `tenant_id__drawer_id__createdAt`: running balance per drawer session
97
+ * lifetime (createdAt range + desc sort).
98
+ * - `tenant_id__day_id__shift_id`: movements per business day / shift (scoped by
99
+ * the shift template id); also covers (tenant_id, day_id) lookups via the
100
+ * leftmost-prefix rule.
101
+ */
102
+ exports.TENANT_CASH_IN_OUT_COLLECTION_INDEXES = {
103
+ _id: { _id: 1 }, // Unique Index
104
+ tenant_id__localDate: { tenant_id: 1, localDate: 1 }, // running movements per local date
105
+ tenant_id__drawer_id__createdAt: { tenant_id: 1, drawer_id: 1, createdAt: -1 }, // running balance per drawer session lifetime
106
+ tenant_id__day_id__shift_id: { tenant_id: 1, day_id: 1, shift_id: 1 }, // movements per day / shift (by shift template id)
107
+ };
@@ -327,3 +327,30 @@ export declare const TenantDaySessionSchema: Schema<any, import("mongoose").Mode
327
327
  __v: number;
328
328
  }>;
329
329
  export declare const TENANT_DAY_SESSIONS_COLLECTION_NAME = "tenant_day_sessions";
330
+ /**
331
+ * Describes the indexes for `tenant_day_sessions`. Values are Mongo key patterns
332
+ * that can be passed straight to `.hint()`.
333
+ * - `_id`: unique primary key.
334
+ * - `tenant_id__localDate`: unique per (tenant_id, localDate) — running-day lookups.
335
+ * - `tenant_id__status`: PARTIAL index — create with
336
+ * `partialFilterExpression: { status: 'OPEN' }` (only OPEN day sessions are
337
+ * indexed; every query using this hint filters status: OPEN).
338
+ * - `tenant_id__createdAt`: most recent day per tenant (sorted createdAt desc).
339
+ */
340
+ export declare const TENANT_DAY_SESSIONS_COLLECTION_INDEXES: {
341
+ _id: {
342
+ _id: number;
343
+ };
344
+ tenant_id__localDate: {
345
+ tenant_id: number;
346
+ localDate: number;
347
+ };
348
+ tenant_id__status: {
349
+ tenant_id: number;
350
+ status: number;
351
+ };
352
+ tenant_id__createdAt: {
353
+ tenant_id: number;
354
+ createdAt: number;
355
+ };
356
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TENANT_DAY_SESSIONS_COLLECTION_NAME = exports.TenantDaySessionSchema = void 0;
3
+ exports.TENANT_DAY_SESSIONS_COLLECTION_INDEXES = exports.TENANT_DAY_SESSIONS_COLLECTION_NAME = exports.TenantDaySessionSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const constants_1 = require("../../constants");
6
6
  const common_models_1 = require("./common-models");
@@ -139,3 +139,19 @@ exports.TenantDaySessionSchema = new mongoose_1.Schema({
139
139
  minimize: false,
140
140
  });
141
141
  exports.TENANT_DAY_SESSIONS_COLLECTION_NAME = 'tenant_day_sessions';
142
+ /**
143
+ * Describes the indexes for `tenant_day_sessions`. Values are Mongo key patterns
144
+ * that can be passed straight to `.hint()`.
145
+ * - `_id`: unique primary key.
146
+ * - `tenant_id__localDate`: unique per (tenant_id, localDate) — running-day lookups.
147
+ * - `tenant_id__status`: PARTIAL index — create with
148
+ * `partialFilterExpression: { status: 'OPEN' }` (only OPEN day sessions are
149
+ * indexed; every query using this hint filters status: OPEN).
150
+ * - `tenant_id__createdAt`: most recent day per tenant (sorted createdAt desc).
151
+ */
152
+ exports.TENANT_DAY_SESSIONS_COLLECTION_INDEXES = {
153
+ _id: { _id: 1 }, // Unique Index
154
+ tenant_id__localDate: { tenant_id: 1, localDate: 1 }, // unique per (tenant_id, localDate)
155
+ tenant_id__status: { tenant_id: 1, status: 1 }, // partial index (partialFilterExpression: { status: 'OPEN' }) — open-day guard
156
+ tenant_id__createdAt: { tenant_id: 1, createdAt: -1 }, // most recent day per tenant
157
+ };
@@ -357,3 +357,33 @@ export declare const TenantInstoreShiftSessionSchema: Schema<any, import("mongoo
357
357
  __v: number;
358
358
  }>;
359
359
  export declare const TENANT_INSTORE_SHIFT_SESSIONS_COLLECTION_NAME = "tenant_instore_shift_sessions";
360
+ /**
361
+ * Describes the indexes for `tenant_instore_shift_sessions`. Values are Mongo key
362
+ * patterns that can be passed straight to `.hint()`.
363
+ * - `_id`: unique primary key.
364
+ * - `tenant_id__localDate`: running shift sessions per local date.
365
+ * - `tenant_id__status`: PARTIAL index — create with
366
+ * `partialFilterExpression: { status: 'OPEN' }` (only OPEN shift sessions are
367
+ * indexed; every query using this hint filters status: OPEN).
368
+ * - `tenant_id__day_id__shift_id`: UNIQUE per (tenant_id, day_id, shift_id) —
369
+ * enforces one session per shift template per day and serves shift-count
370
+ * lookups on (tenant_id, day_id) via the leftmost-prefix rule.
371
+ */
372
+ export declare const TENANT_INSTORE_SHIFT_SESSIONS_COLLECTION_INDEXES: {
373
+ _id: {
374
+ _id: number;
375
+ };
376
+ tenant_id__localDate: {
377
+ tenant_id: number;
378
+ localDate: number;
379
+ };
380
+ tenant_id__status: {
381
+ tenant_id: number;
382
+ status: number;
383
+ };
384
+ tenant_id__day_id__shift_id: {
385
+ tenant_id: number;
386
+ day_id: number;
387
+ shift_id: number;
388
+ };
389
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TENANT_INSTORE_SHIFT_SESSIONS_COLLECTION_NAME = exports.TenantInstoreShiftSessionSchema = void 0;
3
+ exports.TENANT_INSTORE_SHIFT_SESSIONS_COLLECTION_INDEXES = exports.TENANT_INSTORE_SHIFT_SESSIONS_COLLECTION_NAME = exports.TenantInstoreShiftSessionSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const constants_1 = require("../../constants");
6
6
  const common_models_1 = require("./common-models");
@@ -174,3 +174,21 @@ exports.TenantInstoreShiftSessionSchema = new mongoose_1.Schema({
174
174
  minimize: false,
175
175
  });
176
176
  exports.TENANT_INSTORE_SHIFT_SESSIONS_COLLECTION_NAME = 'tenant_instore_shift_sessions';
177
+ /**
178
+ * Describes the indexes for `tenant_instore_shift_sessions`. Values are Mongo key
179
+ * patterns that can be passed straight to `.hint()`.
180
+ * - `_id`: unique primary key.
181
+ * - `tenant_id__localDate`: running shift sessions per local date.
182
+ * - `tenant_id__status`: PARTIAL index — create with
183
+ * `partialFilterExpression: { status: 'OPEN' }` (only OPEN shift sessions are
184
+ * indexed; every query using this hint filters status: OPEN).
185
+ * - `tenant_id__day_id__shift_id`: UNIQUE per (tenant_id, day_id, shift_id) —
186
+ * enforces one session per shift template per day and serves shift-count
187
+ * lookups on (tenant_id, day_id) via the leftmost-prefix rule.
188
+ */
189
+ exports.TENANT_INSTORE_SHIFT_SESSIONS_COLLECTION_INDEXES = {
190
+ _id: { _id: 1 }, // Unique Index
191
+ tenant_id__localDate: { tenant_id: 1, localDate: 1 }, // running shift sessions per local date
192
+ tenant_id__status: { tenant_id: 1, status: 1 }, // partial index (partialFilterExpression: { status: 'OPEN' }) — running-shift guard
193
+ tenant_id__day_id__shift_id: { tenant_id: 1, day_id: 1, shift_id: 1 }, // UNIQUE per (tenant, day, shift template)
194
+ };
@@ -48,3 +48,19 @@ export declare const TenantInstoreShiftSchema: Schema<any, import("mongoose").Mo
48
48
  __v: number;
49
49
  }>;
50
50
  export declare const TENANT_INSTORE_SHIFTS_COLLECTION_NAME = "tenant_instore_shifts";
51
+ /**
52
+ * Describes the indexes for `tenant_instore_shifts`. Values are Mongo key patterns
53
+ * that can be passed straight to `.hint()`.
54
+ * - `_id`: unique primary key.
55
+ * - `tenants__start_time`: per-tenant lookup + sort by start_time; also covers
56
+ * tenants-only lookups via the leftmost-prefix rule.
57
+ */
58
+ export declare const TENANT_INSTORE_SHIFTS_COLLECTION_INDEXES: {
59
+ _id: {
60
+ _id: number;
61
+ };
62
+ tenants__start_time: {
63
+ tenants: number;
64
+ start_time: number;
65
+ };
66
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TENANT_INSTORE_SHIFTS_COLLECTION_NAME = exports.TenantInstoreShiftSchema = void 0;
3
+ exports.TENANT_INSTORE_SHIFTS_COLLECTION_INDEXES = exports.TENANT_INSTORE_SHIFTS_COLLECTION_NAME = exports.TenantInstoreShiftSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const constants_1 = require("../../constants");
6
6
  exports.TenantInstoreShiftSchema = new mongoose_1.Schema({
@@ -53,3 +53,14 @@ exports.TenantInstoreShiftSchema = new mongoose_1.Schema({
53
53
  minimize: false,
54
54
  });
55
55
  exports.TENANT_INSTORE_SHIFTS_COLLECTION_NAME = 'tenant_instore_shifts';
56
+ /**
57
+ * Describes the indexes for `tenant_instore_shifts`. Values are Mongo key patterns
58
+ * that can be passed straight to `.hint()`.
59
+ * - `_id`: unique primary key.
60
+ * - `tenants__start_time`: per-tenant lookup + sort by start_time; also covers
61
+ * tenants-only lookups via the leftmost-prefix rule.
62
+ */
63
+ exports.TENANT_INSTORE_SHIFTS_COLLECTION_INDEXES = {
64
+ _id: { _id: 1 }, // Unique Index
65
+ tenants__start_time: { tenants: 1, start_time: 1 }, // tenant lookup (prefix) + sort by start_time
66
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const time_helpers_1 = require("../helpers/utils/time-helpers");
4
+ const addDays = (iso, days) => {
5
+ const [y, m, d] = iso.split('-').map(Number);
6
+ const dt = new Date(Date.UTC(y, m - 1, d));
7
+ dt.setUTCDate(dt.getUTCDate() + days);
8
+ return dt.toISOString().slice(0, 10);
9
+ };
10
+ describe('getIsoWeekOfYear', () => {
11
+ it('returns correct ISO 8601 week numbers (Monday start)', () => {
12
+ expect((0, time_helpers_1.getIsoWeekOfYear)(2026, 8, 23)).toBe(34); // was 35 under the old Sunday-based formula
13
+ expect((0, time_helpers_1.getIsoWeekOfYear)(2026, 8, 30)).toBe(35);
14
+ expect((0, time_helpers_1.getIsoWeekOfYear)(2026, 9, 1)).toBe(36);
15
+ expect((0, time_helpers_1.getIsoWeekOfYear)(2026, 1, 1)).toBe(1);
16
+ expect((0, time_helpers_1.getIsoWeekOfYear)(2026, 12, 31)).toBe(53);
17
+ });
18
+ it('handles year-boundary weeks (ISO week belongs to the year of its Thursday)', () => {
19
+ expect((0, time_helpers_1.getIsoWeekOfYear)(2027, 1, 1)).toBe(53); // Jan 1 2027 falls in week 53 of 2026
20
+ expect((0, time_helpers_1.getIsoWeekOfYear)(2028, 1, 1)).toBe(52); // Jan 1 2028 falls in week 52 of 2027
21
+ });
22
+ it('never exceeds 53, even for leap years starting on a Saturday', () => {
23
+ for (const week of [
24
+ (0, time_helpers_1.getIsoWeekOfYear)(2026, 12, 31),
25
+ (0, time_helpers_1.getIsoWeekOfYear)(2028, 12, 31),
26
+ (0, time_helpers_1.getIsoWeekOfYear)(2024, 12, 29),
27
+ ]) {
28
+ expect(week).toBeLessThanOrEqual(53);
29
+ }
30
+ });
31
+ });
32
+ describe('getCalendarWeekStart', () => {
33
+ it('returns the Monday (ISO) week start by default', () => {
34
+ expect((0, time_helpers_1.getCalendarWeekStart)('2026-08-23')).toBe('2026-08-17');
35
+ expect((0, time_helpers_1.getCalendarWeekStart)('2026-08-17')).toBe('2026-08-17');
36
+ expect((0, time_helpers_1.getCalendarWeekStart)('2026-01-01')).toBe('2025-12-29');
37
+ });
38
+ it('returns the Sunday week start when weekStart = 0', () => {
39
+ expect((0, time_helpers_1.getCalendarWeekStart)('2026-08-23', 0)).toBe('2026-08-23');
40
+ expect((0, time_helpers_1.getCalendarWeekStart)('2026-08-24', 0)).toBe('2026-08-23');
41
+ expect((0, time_helpers_1.getCalendarWeekStart)('2026-01-01', 0)).toBe('2025-12-28');
42
+ });
43
+ it('every calendar week in 2026 is exactly 7 days for both week starts', () => {
44
+ let day = '2026-01-01';
45
+ for (let i = 0; i < 365; i++) {
46
+ for (const weekStart of [0, 1]) {
47
+ const w = (0, time_helpers_1.getCalendarWeekStart)(day, weekStart);
48
+ expect((0, time_helpers_1.getCalendarWeekStart)(w, weekStart)).toBe(w);
49
+ expect((0, time_helpers_1.getCalendarWeekStart)(addDays(w, 6), weekStart)).toBe(w);
50
+ expect((0, time_helpers_1.getCalendarWeekStart)(addDays(w, 7), weekStart)).not.toBe(w);
51
+ }
52
+ day = addDays(day, 1);
53
+ }
54
+ });
55
+ });
56
+ describe('getLocalDocumentFields', () => {
57
+ it('computes week/day fields consistently with the tenant local date (server TZ independent)', () => {
58
+ const fields = (0, time_helpers_1.getLocalDocumentFields)(5, new Date('2026-08-23T09:26:42.353Z'));
59
+ expect(fields.localDate).toBe('2026-08-23');
60
+ expect(fields.localTime).toBe('14:26:42');
61
+ expect(fields.localDay).toBe(0); // Sunday
62
+ expect(fields.localWeek).toBe(4); // month-aligned: Aug 23 is in days 22-28
63
+ expect(fields.localWeekOfYear).toBe(34); // ISO 8601
64
+ expect(fields.localDayOfYear).toBe(235);
65
+ expect(fields.localMonth).toBe(8);
66
+ expect(fields.localYear).toBe(2026);
67
+ });
68
+ it('never produces an internal contradiction at early tenant-morning hours', () => {
69
+ // 02:00 tenant time (+5) = 21:00Z previous day; old code gave localDayOfYear 234.
70
+ const fields = (0, time_helpers_1.getLocalDocumentFields)(5, new Date('2026-08-22T21:00:00.000Z'));
71
+ expect(fields.localDate).toBe('2026-08-23');
72
+ expect(fields.localDayOfYear).toBe(235);
73
+ expect(fields.localWeekOfYear).toBe(34);
74
+ });
75
+ });
76
+ describe('getLocalFieldsFromLocalDate', () => {
77
+ it('uses month-aligned localWeek (never a 9-day week)', () => {
78
+ expect((0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-08-01', 5, new Date('2026-08-01T12:00:00Z')).localWeek).toBe(1);
79
+ expect((0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-08-07', 5, new Date('2026-08-07T12:00:00Z')).localWeek).toBe(1);
80
+ expect((0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-08-08', 5, new Date('2026-08-08T12:00:00Z')).localWeek).toBe(2);
81
+ expect((0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-08-23', 5, new Date('2026-08-23T12:00:00Z')).localWeek).toBe(4);
82
+ expect((0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-08-29', 5, new Date('2026-08-29T12:00:00Z')).localWeek).toBe(5);
83
+ expect((0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-08-30', 5, new Date('2026-08-30T12:00:00Z')).localWeek).toBe(5);
84
+ expect((0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-08-31', 5, new Date('2026-08-31T12:00:00Z')).localWeek).toBe(5);
85
+ });
86
+ it('matches getLocalDocumentFields for the same date (no week-5 clamp distortion)', () => {
87
+ const fromDate = (0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-08-23', 5, new Date('2026-08-23T09:26:42.353Z'));
88
+ const fromDoc = (0, time_helpers_1.getLocalDocumentFields)(5, new Date('2026-08-23T09:26:42.353Z'));
89
+ expect(fromDate.localWeek).toBe(fromDoc.localWeek);
90
+ expect(fromDate.localWeekOfYear).toBe(fromDoc.localWeekOfYear);
91
+ expect(fromDate.localDayOfYear).toBe(fromDoc.localDayOfYear);
92
+ expect(fromDate.localDay).toBe(fromDoc.localDay);
93
+ });
94
+ it('opens with the full month-aligned week semantics for 28-day February', () => {
95
+ expect((0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-02-01', 5, new Date('2026-02-01T12:00:00Z')).localWeek).toBe(1);
96
+ expect((0, time_helpers_1.getLocalFieldsFromLocalDate)('2026-02-28', 5, new Date('2026-02-28T12:00:00Z')).localWeek).toBe(4);
97
+ });
98
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.580",
3
+ "version": "0.2.582",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",