@managesome/eposmatic-toolkit 0.4.9 → 0.4.11

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.
Files changed (351) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +455 -0
  2. package/esm/constants/enums/delivery.js +8 -0
  3. package/esm/helpers/index.js +3 -0
  4. package/esm/helpers/materialized-reports/index.js +32 -0
  5. package/esm/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +114 -0
  6. package/esm/helpers/materialized-reports/materialized-cash-classification-helpers.js +248 -0
  7. package/esm/helpers/materialized-reports/materialized-cash-close-day-helpers.js +396 -0
  8. package/esm/helpers/materialized-reports/materialized-cash-compile-helpers.js +245 -0
  9. package/esm/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +170 -0
  10. package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +430 -0
  11. package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +373 -0
  12. package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +216 -0
  13. package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +319 -0
  14. package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +213 -0
  15. package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +604 -0
  16. package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +186 -0
  17. package/esm/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +155 -0
  18. package/esm/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +199 -0
  19. package/esm/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +115 -0
  20. package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +89 -0
  21. package/esm/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +394 -0
  22. package/esm/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +167 -0
  23. package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +215 -0
  24. package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +477 -0
  25. package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +201 -0
  26. package/esm/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +9 -6
  27. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +11 -4
  28. package/esm/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +189 -0
  29. package/esm/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +156 -0
  30. package/esm/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +588 -0
  31. package/esm/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +175 -0
  32. package/esm/helpers/materialized-reports/tenant-cash-in-out-formatter.js +157 -0
  33. package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +230 -0
  34. package/esm/helpers/materialized-reports/tenant-discount-formatter.js +427 -0
  35. package/esm/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +238 -0
  36. package/esm/helpers/materialized-reports/tenant-payments-comparison-formatter.js +282 -0
  37. package/esm/helpers/materialized-reports/tenant-prep-time-formatter.js +228 -0
  38. package/esm/helpers/materialized-reports/tenant-sales-comparison-formatter.js +346 -0
  39. package/esm/helpers/reporting-contract/index.js +2 -0
  40. package/esm/helpers/reporting-contract/overview-contract.js +123 -0
  41. package/esm/helpers/reporting-contract/report-contract.js +228 -0
  42. package/esm/helpers/rider-earnings/index.js +1 -0
  43. package/esm/helpers/rider-earnings/rider-earnings.js +53 -0
  44. package/esm/helpers/rider-reports/index.js +2 -0
  45. package/esm/helpers/rider-reports/rider-daily-report.js +90 -0
  46. package/esm/helpers/rider-reports/rider-report.js +67 -0
  47. package/esm/helpers/store/common-order-actions.js +3 -2
  48. package/esm/schemas/models/deliveries.js +82 -1
  49. package/esm/schemas/models/index.js +15 -0
  50. package/esm/schemas/models/materialized_credit_bills.js +1 -1
  51. package/esm/schemas/models/materialized_rider_daily_reports.js +39 -0
  52. package/esm/schemas/models/materialized_tenant_cash_entries.js +95 -0
  53. package/esm/schemas/models/materialized_tenant_cash_reports.js +77 -0
  54. package/esm/schemas/models/materialized_tenant_delivery_entries.js +63 -0
  55. package/esm/schemas/models/materialized_tenant_delivery_reports.js +63 -0
  56. package/esm/schemas/models/materialized_tenant_discount_entries.js +156 -0
  57. package/esm/schemas/models/materialized_tenant_discount_reports.js +333 -0
  58. package/esm/schemas/models/materialized_tenant_hourly_comparison_entries.js +87 -0
  59. package/esm/schemas/models/materialized_tenant_hourly_comparison_reports.js +99 -0
  60. package/esm/schemas/models/materialized_tenant_payments_comparison_entries.js +71 -0
  61. package/esm/schemas/models/materialized_tenant_payments_comparison_reports.js +76 -0
  62. package/esm/schemas/models/materialized_tenant_prep_time_entries.js +130 -0
  63. package/esm/schemas/models/materialized_tenant_prep_time_reports.js +109 -0
  64. package/esm/schemas/models/materialized_tenant_sales_comparison_entries.js +130 -0
  65. package/esm/schemas/models/materialized_tenant_sales_comparison_reports.js +154 -0
  66. package/esm/schemas/models/materialized_tenant_sales_overview_reports.js +1 -1
  67. package/esm/schemas/types/index.js +15 -0
  68. package/esm/schemas/types/materialized_rider_daily_reports.js +1 -0
  69. package/esm/schemas/types/materialized_tenant_cash_entries.js +1 -0
  70. package/esm/schemas/types/materialized_tenant_cash_reports.js +1 -0
  71. package/esm/schemas/types/materialized_tenant_delivery_entries.js +1 -0
  72. package/esm/schemas/types/materialized_tenant_delivery_reports.js +1 -0
  73. package/esm/schemas/types/materialized_tenant_discount_entries.js +1 -0
  74. package/esm/schemas/types/materialized_tenant_discount_reports.js +2 -0
  75. package/esm/schemas/types/materialized_tenant_hourly_comparison_entries.js +1 -0
  76. package/esm/schemas/types/materialized_tenant_hourly_comparison_reports.js +1 -0
  77. package/esm/schemas/types/materialized_tenant_payments_comparison_entries.js +1 -0
  78. package/esm/schemas/types/materialized_tenant_payments_comparison_reports.js +11 -0
  79. package/esm/schemas/types/materialized_tenant_prep_time_entries.js +1 -0
  80. package/esm/schemas/types/materialized_tenant_prep_time_reports.js +1 -0
  81. package/esm/schemas/types/materialized_tenant_sales_comparison_entries.js +1 -0
  82. package/esm/schemas/types/materialized_tenant_sales_comparison_reports.js +14 -0
  83. package/lib/constants/MATERIALIZED_REPORTS.d.ts +345 -2
  84. package/lib/constants/MATERIALIZED_REPORTS.js +457 -1
  85. package/lib/constants/enums/delivery.d.ts +7 -0
  86. package/lib/constants/enums/delivery.js +9 -1
  87. package/lib/helpers/index.d.ts +3 -0
  88. package/lib/helpers/index.js +3 -0
  89. package/lib/helpers/materialized-reports/index.d.ts +32 -0
  90. package/lib/helpers/materialized-reports/index.js +32 -0
  91. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.d.ts +18 -0
  92. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +118 -0
  93. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.d.ts +142 -0
  94. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.js +257 -0
  95. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.d.ts +103 -0
  96. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.js +409 -0
  97. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.d.ts +73 -0
  98. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.js +252 -0
  99. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.d.ts +7 -0
  100. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +173 -0
  101. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +113 -0
  102. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +442 -0
  103. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +98 -0
  104. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +386 -0
  105. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +41 -0
  106. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +220 -0
  107. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +52 -0
  108. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +325 -0
  109. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +50 -0
  110. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +222 -0
  111. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +182 -0
  112. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +615 -0
  113. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +47 -0
  114. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +193 -0
  115. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.d.ts +64 -0
  116. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +163 -0
  117. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.d.ts +61 -0
  118. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +207 -0
  119. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.d.ts +23 -0
  120. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +120 -0
  121. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.d.ts +25 -0
  122. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +99 -0
  123. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.d.ts +79 -0
  124. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +407 -0
  125. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.d.ts +35 -0
  126. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +173 -0
  127. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +98 -0
  128. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +224 -0
  129. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +130 -0
  130. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +491 -0
  131. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.d.ts +47 -0
  132. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +208 -0
  133. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.d.ts +6 -3
  134. package/lib/helpers/materialized-reports/materialized-reports-scope-group-helpers.js +8 -5
  135. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +8 -0
  136. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +10 -3
  137. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.d.ts +48 -0
  138. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +204 -0
  139. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.d.ts +22 -0
  140. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +160 -0
  141. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.d.ts +163 -0
  142. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +603 -0
  143. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.d.ts +68 -0
  144. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +182 -0
  145. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.d.ts +98 -0
  146. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.js +160 -0
  147. package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +133 -0
  148. package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +233 -0
  149. package/lib/helpers/materialized-reports/tenant-discount-formatter.d.ts +114 -0
  150. package/lib/helpers/materialized-reports/tenant-discount-formatter.js +431 -0
  151. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.d.ts +60 -0
  152. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +244 -0
  153. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.d.ts +75 -0
  154. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.js +289 -0
  155. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.d.ts +98 -0
  156. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.js +232 -0
  157. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.d.ts +105 -0
  158. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.js +352 -0
  159. package/lib/helpers/reporting-contract/index.d.ts +2 -0
  160. package/lib/helpers/reporting-contract/index.js +18 -0
  161. package/lib/helpers/reporting-contract/overview-contract.d.ts +55 -0
  162. package/lib/helpers/reporting-contract/overview-contract.js +127 -0
  163. package/lib/helpers/reporting-contract/report-contract.d.ts +159 -0
  164. package/lib/helpers/reporting-contract/report-contract.js +239 -0
  165. package/lib/helpers/rider-earnings/index.d.ts +1 -0
  166. package/lib/helpers/rider-earnings/index.js +17 -0
  167. package/lib/helpers/rider-earnings/rider-earnings.d.ts +51 -0
  168. package/lib/helpers/rider-earnings/rider-earnings.js +57 -0
  169. package/lib/helpers/rider-reports/index.d.ts +2 -0
  170. package/lib/helpers/rider-reports/index.js +18 -0
  171. package/lib/helpers/rider-reports/rider-daily-report.d.ts +28 -0
  172. package/lib/helpers/rider-reports/rider-daily-report.js +93 -0
  173. package/lib/helpers/rider-reports/rider-report.d.ts +49 -0
  174. package/lib/helpers/rider-reports/rider-report.js +70 -0
  175. package/lib/helpers/store/common-order-actions.js +3 -2
  176. package/lib/helpers/types/materialized-reports-apis.d.ts +300 -1
  177. package/lib/schemas/models/deliveries.d.ts +58 -1
  178. package/lib/schemas/models/deliveries.js +81 -0
  179. package/lib/schemas/models/expense_details.d.ts +3 -3
  180. package/lib/schemas/models/index.d.ts +15 -0
  181. package/lib/schemas/models/index.js +15 -0
  182. package/lib/schemas/models/materialized_credit_bills.d.ts +3 -3
  183. package/lib/schemas/models/materialized_credit_bills.js +1 -1
  184. package/lib/schemas/models/materialized_rider_daily_reports.d.ts +27 -0
  185. package/lib/schemas/models/materialized_rider_daily_reports.js +42 -0
  186. package/lib/schemas/models/materialized_tenant_cash_entries.d.ts +181 -0
  187. package/lib/schemas/models/materialized_tenant_cash_entries.js +98 -0
  188. package/lib/schemas/models/materialized_tenant_cash_reports.d.ts +111 -0
  189. package/lib/schemas/models/materialized_tenant_cash_reports.js +80 -0
  190. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +98 -0
  191. package/lib/schemas/models/materialized_tenant_delivery_entries.js +66 -0
  192. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +98 -0
  193. package/lib/schemas/models/materialized_tenant_delivery_reports.js +66 -0
  194. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +199 -0
  195. package/lib/schemas/models/materialized_tenant_discount_entries.js +159 -0
  196. package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +216 -0
  197. package/lib/schemas/models/materialized_tenant_discount_reports.js +336 -0
  198. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.d.ts +95 -0
  199. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.js +82 -0
  200. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.d.ts +105 -0
  201. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.js +91 -0
  202. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.d.ts +91 -0
  203. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.js +70 -0
  204. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.d.ts +98 -0
  205. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.js +71 -0
  206. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +181 -0
  207. package/lib/schemas/models/materialized_tenant_prep_time_entries.js +133 -0
  208. package/lib/schemas/models/materialized_tenant_prep_time_reports.d.ts +118 -0
  209. package/lib/schemas/models/materialized_tenant_prep_time_reports.js +112 -0
  210. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.d.ts +139 -0
  211. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.js +120 -0
  212. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.d.ts +167 -0
  213. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.js +137 -0
  214. package/lib/schemas/models/materialized_tenant_sales_overview_reports.d.ts +3 -3
  215. package/lib/schemas/models/materialized_tenant_sales_overview_reports.js +1 -1
  216. package/lib/schemas/models/tenant_purchase_order_vendor_returns.d.ts +3 -3
  217. package/lib/schemas/models/warehouse_internal_movements.d.ts +6 -6
  218. package/lib/schemas/models/warehouse_purchase_order_receipts.d.ts +3 -3
  219. package/lib/schemas/models/warehouse_purchase_order_vendor_returns.d.ts +3 -3
  220. package/lib/schemas/models/warehouse_sale_deliveries.d.ts +6 -6
  221. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  222. package/lib/schemas/types/deliveries.d.ts +40 -1
  223. package/lib/schemas/types/index.d.ts +15 -0
  224. package/lib/schemas/types/index.js +15 -0
  225. package/lib/schemas/types/materialized_rider_daily_reports.d.ts +37 -0
  226. package/lib/schemas/types/materialized_rider_daily_reports.js +2 -0
  227. package/lib/schemas/types/materialized_tenant_cash_entries.d.ts +87 -0
  228. package/lib/schemas/types/materialized_tenant_cash_entries.js +2 -0
  229. package/lib/schemas/types/materialized_tenant_cash_reports.d.ts +144 -0
  230. package/lib/schemas/types/materialized_tenant_cash_reports.js +2 -0
  231. package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +98 -0
  232. package/lib/schemas/types/materialized_tenant_delivery_entries.js +2 -0
  233. package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +179 -0
  234. package/lib/schemas/types/materialized_tenant_delivery_reports.js +2 -0
  235. package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +81 -0
  236. package/lib/schemas/types/materialized_tenant_discount_entries.js +2 -0
  237. package/lib/schemas/types/materialized_tenant_discount_reports.d.ts +209 -0
  238. package/lib/schemas/types/materialized_tenant_discount_reports.js +3 -0
  239. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.d.ts +33 -0
  240. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.js +2 -0
  241. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.d.ts +40 -0
  242. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.js +2 -0
  243. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.d.ts +48 -0
  244. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.js +2 -0
  245. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.d.ts +51 -0
  246. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.js +14 -0
  247. package/lib/schemas/types/materialized_tenant_prep_time_entries.d.ts +88 -0
  248. package/lib/schemas/types/materialized_tenant_prep_time_entries.js +2 -0
  249. package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +132 -0
  250. package/lib/schemas/types/materialized_tenant_prep_time_reports.js +2 -0
  251. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.d.ts +43 -0
  252. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.js +2 -0
  253. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.d.ts +74 -0
  254. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.js +17 -0
  255. package/lib/schemas/types/materialized_tenant_sales_overview_reports.d.ts +7 -0
  256. package/lib/tests/deliveries-schema.test.d.ts +1 -0
  257. package/lib/tests/deliveries-schema.test.js +70 -0
  258. package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +1 -0
  259. package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +1 -0
  260. package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +1 -0
  261. package/lib/tests/materialized-reports/fixtures.d.ts +15 -1
  262. package/lib/tests/materialized-reports/fixtures.js +1 -0
  263. package/lib/tests/materialized-reports/materialized-cash-compile.test.d.ts +1 -0
  264. package/lib/tests/materialized-reports/materialized-cash-compile.test.js +197 -0
  265. package/lib/tests/materialized-reports/materialized-cash-fold.test.d.ts +1 -0
  266. package/lib/tests/materialized-reports/materialized-cash-fold.test.js +116 -0
  267. package/lib/tests/materialized-reports/materialized-cash-formatter.test.d.ts +1 -0
  268. package/lib/tests/materialized-reports/materialized-cash-formatter.test.js +153 -0
  269. package/lib/tests/materialized-reports/materialized-cash-plan.test.d.ts +1 -0
  270. package/lib/tests/materialized-reports/materialized-cash-plan.test.js +168 -0
  271. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.d.ts +1 -0
  272. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.js +267 -0
  273. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.d.ts +1 -0
  274. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +147 -0
  275. package/lib/tests/materialized-reports/materialized-delivery-compile.test.d.ts +1 -0
  276. package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +242 -0
  277. package/lib/tests/materialized-reports/materialized-delivery-fold.test.d.ts +1 -0
  278. package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +109 -0
  279. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.d.ts +1 -0
  280. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +142 -0
  281. package/lib/tests/materialized-reports/materialized-delivery-plan.test.d.ts +1 -0
  282. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +198 -0
  283. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.d.ts +1 -0
  284. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.js +173 -0
  285. package/lib/tests/materialized-reports/materialized-discount-classification.test.d.ts +1 -0
  286. package/lib/tests/materialized-reports/materialized-discount-classification.test.js +339 -0
  287. package/lib/tests/materialized-reports/materialized-discount-close-day.test.d.ts +1 -0
  288. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +506 -0
  289. package/lib/tests/materialized-reports/materialized-discount-compile.test.d.ts +1 -0
  290. package/lib/tests/materialized-reports/materialized-discount-compile.test.js +161 -0
  291. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.d.ts +1 -0
  292. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.js +107 -0
  293. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.d.ts +1 -0
  294. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +125 -0
  295. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.d.ts +1 -0
  296. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.js +146 -0
  297. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.d.ts +1 -0
  298. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.js +127 -0
  299. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.d.ts +1 -0
  300. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.js +138 -0
  301. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.d.ts +1 -0
  302. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.js +90 -0
  303. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.d.ts +1 -0
  304. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.js +164 -0
  305. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.d.ts +1 -0
  306. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.js +117 -0
  307. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.d.ts +1 -0
  308. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.js +149 -0
  309. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.d.ts +1 -0
  310. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +273 -0
  311. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.d.ts +1 -0
  312. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +271 -0
  313. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.d.ts +1 -0
  314. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +217 -0
  315. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.d.ts +1 -0
  316. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +84 -0
  317. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.d.ts +1 -0
  318. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +176 -0
  319. package/lib/tests/materialized-reports/materialized-reports-scope-group-helpers.test.js +13 -13
  320. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.d.ts +1 -0
  321. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.js +126 -0
  322. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.d.ts +1 -0
  323. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.js +207 -0
  324. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.d.ts +1 -0
  325. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.js +179 -0
  326. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.d.ts +1 -0
  327. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.js +196 -0
  328. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.d.ts +1 -0
  329. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.js +241 -0
  330. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.d.ts +1 -0
  331. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.js +96 -0
  332. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.d.ts +1 -0
  333. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.js +112 -0
  334. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.d.ts +1 -0
  335. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.js +142 -0
  336. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +8 -1
  337. package/lib/tests/materialized-reports/sales-comparison-reference.d.ts +55 -0
  338. package/lib/tests/materialized-reports/sales-comparison-reference.js +86 -0
  339. package/lib/tests/materialized-reports/tenant-discount-formatter.test.d.ts +1 -0
  340. package/lib/tests/materialized-reports/tenant-discount-formatter.test.js +289 -0
  341. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.d.ts +1 -0
  342. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.js +354 -0
  343. package/lib/tests/reporting-contract.test.d.ts +1 -0
  344. package/lib/tests/reporting-contract.test.js +267 -0
  345. package/lib/tests/rider-daily-report.test.d.ts +1 -0
  346. package/lib/tests/rider-daily-report.test.js +86 -0
  347. package/lib/tests/rider-earnings.test.d.ts +1 -0
  348. package/lib/tests/rider-earnings.test.js +65 -0
  349. package/lib/tests/rider-report.test.d.ts +1 -0
  350. package/lib/tests/rider-report.test.js +98 -0
  351. package/package.json +10 -1
@@ -0,0 +1,186 @@
1
+ import mongoose from 'mongoose';
2
+ import { MATERIALIZED_DISCOUNT_REPORT_SCOPES, MATERIALIZED_REPORT_KEY_FILTER_ENUM, MATERIALIZED_REPORT_SCOPE_ENUM, } from '../../constants/MATERIALIZED_REPORTS';
3
+ import { accumulateOrderDiscounts, toDiscountHourLabel, } from './materialized-discount-accumulator-helpers';
4
+ import { createEmptyDiscountOrderCounters } from './materialized-discount-classification-helpers';
5
+ import { buildMaterializedReportKey } from './key-helpers';
6
+ import { getDayIndexFromLocalDate, getMaterializedHourScopeField, getMaterializedPeriodScopeFields, } from './scope-helpers';
7
+ import { resolveOrderLocalHour } from './materialized-credit-bills-helpers';
8
+ /** Adds every numeric field of `source` into `target`, recursively. */
9
+ function addCounters(target, source) {
10
+ for (const [key, value] of Object.entries(source)) {
11
+ if (typeof value === 'number') {
12
+ const current = target[key];
13
+ target[key] = (typeof current === 'number' ? current : 0) + value;
14
+ continue;
15
+ }
16
+ if (value != null && typeof value === 'object' && !Array.isArray(value)) {
17
+ const nested = (target[key] ?? (target[key] = {}));
18
+ addCounters(nested, value);
19
+ }
20
+ }
21
+ }
22
+ /** A zeroed counter block used as the merge seed (deep-cloned per key). */
23
+ const EMPTY_COUNTERS_SEED = createEmptyDiscountOrderCounters();
24
+ /** A fresh, deep-cloned zeroed counter block (each key accumulates into its own). */
25
+ const freshCounters = () => JSON.parse(JSON.stringify(EMPTY_COUNTERS_SEED));
26
+ const foldWatermark = (bucket, value) => {
27
+ if (value == null)
28
+ return;
29
+ const time = new Date(value).getTime();
30
+ if (Number.isNaN(time))
31
+ return;
32
+ if (bucket.watermark == null || time > bucket.watermark.getTime())
33
+ bucket.watermark = new Date(time);
34
+ };
35
+ /**
36
+ * Compiles one tenant-day into the discount report + entries documents.
37
+ * Returns `null` on malformed input (bad date / tenant / brand); returns
38
+ * EMPTY arrays when the day has no discounts at all.
39
+ */
40
+ export function compileTenantDiscountReports(input) {
41
+ if (input == null)
42
+ return null;
43
+ const { tenantId, brandId, localDate, orders, computedAt } = input;
44
+ if (typeof tenantId !== 'string' || tenantId.length === 0)
45
+ return null;
46
+ if (typeof brandId !== 'string' || brandId.length === 0)
47
+ return null;
48
+ if (typeof localDate !== 'string' || localDate.length === 0)
49
+ return null;
50
+ if (!Array.isArray(orders))
51
+ return null;
52
+ const periodFields = getMaterializedPeriodScopeFields(localDate);
53
+ if (periodFields == null)
54
+ return null;
55
+ const dayIndex = getDayIndexFromLocalDate(localDate);
56
+ // DERIVED from the report's scope list (as prep / cash / delivery do). This used to be a
57
+ // second, hand-maintained copy of the same four scopes - so widening the report's scopes
58
+ // compiled the new ones nowhere and the documents silently never appeared.
59
+ const periodScopes = MATERIALIZED_DISCOUNT_REPORT_SCOPES.filter((scope) => scope !== MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR);
60
+ // scope_field -> key -> bucket
61
+ const byScopeField = new Map();
62
+ const scopeContextOf = new Map();
63
+ const bucketFor = (scope, scopeField, key) => {
64
+ const identity = `${scope}|${scopeField}`;
65
+ let byKey = byScopeField.get(identity);
66
+ if (byKey == null) {
67
+ byKey = new Map();
68
+ byScopeField.set(identity, byKey);
69
+ scopeContextOf.set(identity, scope);
70
+ }
71
+ let bucket = byKey.get(key);
72
+ if (bucket == null) {
73
+ bucket = { counters: freshCounters(), rows: [], watermark: null };
74
+ byKey.set(key, bucket);
75
+ }
76
+ return bucket;
77
+ };
78
+ let contributingOrders = 0;
79
+ for (const order of orders) {
80
+ if (order == null)
81
+ continue;
82
+ // Only orders of this tenant compile into this tenant's documents.
83
+ if (order.tenant_id != null && String(order.tenant_id) !== tenantId)
84
+ continue;
85
+ const hour = resolveOrderLocalHour(order, localDate, input.gmtOffset);
86
+ const context = {
87
+ local_date: localDate,
88
+ local_hour: hour,
89
+ local_hour_label: toDiscountHourLabel(localDate, hour),
90
+ };
91
+ const result = accumulateOrderDiscounts(order, context);
92
+ if (!result.has_discount)
93
+ continue;
94
+ const key = buildMaterializedReportKey([String(order.order_status ?? ''), String(order.platform ?? '')]);
95
+ // A key with a reserved character (or a missing component) cannot be a Mongo
96
+ // field name — the order is skipped rather than silently mis-keyed.
97
+ if (key == null)
98
+ continue;
99
+ contributingOrders += 1;
100
+ const watermark = order.updatedAt != null ? new Date(order.updatedAt) : null;
101
+ const targets = periodScopes.map((scope) => [scope, periodFields[scope]]);
102
+ const hourField = hour == null ? null : getMaterializedHourScopeField(localDate, hour);
103
+ if (hourField != null)
104
+ targets.push([MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, hourField]);
105
+ for (const [scope, scopeField] of targets) {
106
+ if (scopeField == null)
107
+ continue;
108
+ const bucket = bucketFor(scope, scopeField, key);
109
+ addCounters(bucket.counters, result.counters);
110
+ bucket.rows.push(...result.entries);
111
+ foldWatermark(bucket, watermark);
112
+ }
113
+ }
114
+ const sourceWatermark = new Date(computedAt);
115
+ const reports = [];
116
+ const entriesDocs = [];
117
+ for (const [identity, byKey] of byScopeField) {
118
+ const scope = scopeContextOf.get(identity);
119
+ const [scopeContextText, scopeField] = identity.split('|');
120
+ if (scopeContextText !== scope)
121
+ continue;
122
+ const counters = {};
123
+ const entrySections = {};
124
+ let watermark = null;
125
+ // The read-path rows: the SAME counters, as an array (see the type module —
126
+ // the map is right for the store/formatter, the array is what an aggregation
127
+ // can sum without dynamic-key gymnastics).
128
+ const counterRows = [];
129
+ for (const [key, bucket] of byKey) {
130
+ const bucketCounters = bucket.counters;
131
+ counters[key] = bucketCounters;
132
+ counterRows.push({ key, counters: bucketCounters });
133
+ if (bucket.rows.length > 0)
134
+ entrySections[key] = { rows: bucket.rows };
135
+ if (bucket.watermark != null && (watermark == null || bucket.watermark.getTime() > watermark.getTime())) {
136
+ watermark = bucket.watermark;
137
+ }
138
+ }
139
+ const docWatermark = watermark ?? sourceWatermark;
140
+ const isDateScoped = scope === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE || scope === MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR;
141
+ reports.push({
142
+ _id: new mongoose.Types.ObjectId().toHexString(),
143
+ tenant_id: tenantId,
144
+ brand_id: brandId,
145
+ scope_context: scope,
146
+ scope_field: scopeField,
147
+ day_index: isDateScoped ? dayIndex : null,
148
+ sale_type_key_filters: [
149
+ MATERIALIZED_REPORT_KEY_FILTER_ENUM.ORDER_STATUS,
150
+ MATERIALIZED_REPORT_KEY_FILTER_ENUM.PLATFORM,
151
+ ],
152
+ discount_counters: Object.keys(counters).length === 0 ? null : counters,
153
+ discount_counter_rows: counterRows.length === 0 ? null : counterRows,
154
+ top_sources: null,
155
+ schema_version: 1,
156
+ source_watermark: docWatermark,
157
+ computed_at: computedAt,
158
+ createdAt: computedAt,
159
+ updatedAt: computedAt,
160
+ });
161
+ // Entries documents exist only when there is at least one row.
162
+ if (Object.keys(entrySections).length > 0) {
163
+ entriesDocs.push({
164
+ _id: new mongoose.Types.ObjectId().toHexString(),
165
+ tenant_id: tenantId,
166
+ brand_id: brandId,
167
+ scope_context: scope,
168
+ scope_field: scopeField,
169
+ day_index: isDateScoped ? dayIndex : null,
170
+ sale_type_key_filters: [
171
+ MATERIALIZED_REPORT_KEY_FILTER_ENUM.ORDER_STATUS,
172
+ MATERIALIZED_REPORT_KEY_FILTER_ENUM.PLATFORM,
173
+ ],
174
+ discount_entries: entrySections,
175
+ schema_version: 1,
176
+ source_watermark: docWatermark,
177
+ computed_at: computedAt,
178
+ createdAt: computedAt,
179
+ updatedAt: computedAt,
180
+ });
181
+ }
182
+ }
183
+ return { reports, entries: entriesDocs, contributingOrders };
184
+ }
185
+ /** Scopes this report materializes — exported for the close plan + tests. */
186
+ export const MATERIALIZED_DISCOUNT_COMPILED_SCOPES = MATERIALIZED_DISCOUNT_REPORT_SCOPES;
@@ -0,0 +1,155 @@
1
+ import mongoose from 'mongoose';
2
+ import { MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_REPORT_TYPE_ENUM, MATERIALIZED_TENANT_HOURLY_COMPARISON_ENTRIES_SCHEMA_VERSION, MATERIALIZED_TENANT_HOURLY_COMPARISON_REPORTS_SCHEMA_VERSION, } from '../../constants/MATERIALIZED_REPORTS';
3
+ import { buildMaterializedTenantReportIteration } from './materialized-tenant-reports-iterations-helpers';
4
+ import { getDayIndexFromLocalDate, getMaterializedHourScopeField } from './scope-helpers';
5
+ import { needsMaterializedSalesComparisonEntriesWrite, needsMaterializedSalesComparisonReportWrite, } from './materialized-sales-comparison-close-day-helpers';
6
+ /** Every stored hour ref one settled day's plan can touch (24, never 31). */
7
+ export function getMaterializedHourlyComparisonScopeRefsForDay(localDate) {
8
+ if (getDayIndexFromLocalDate(localDate) == null)
9
+ return null;
10
+ const refs = [];
11
+ for (let hour = 0; hour < 24; hour += 1) {
12
+ const scopeField = getMaterializedHourScopeField(localDate, hour);
13
+ if (scopeField == null)
14
+ return null;
15
+ refs.push({
16
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
17
+ scope_field: scopeField,
18
+ });
19
+ }
20
+ return refs;
21
+ }
22
+ /** The `'YYYY-MM-DD'` prefix of an hour label, or null when malformed. */
23
+ const localDateOfHourLabel = (scopeField) => {
24
+ if (typeof scopeField !== 'string')
25
+ return null;
26
+ const match = /^(\d{4}-\d{2}-\d{2}) ([01]\d|2[0-3])$/.exec(scopeField);
27
+ return match == null ? null : match[1];
28
+ };
29
+ export function buildTenantHourlyComparisonCloseDayPlan(input) {
30
+ if (input == null)
31
+ return null;
32
+ const { tenantId, reason } = input;
33
+ if (typeof tenantId !== 'string' || tenantId.length === 0)
34
+ return null;
35
+ if (typeof reason !== 'string' || reason.length === 0)
36
+ return null;
37
+ const computedAt = input.computedAt ?? new Date();
38
+ if (Number.isNaN(computedAt.getTime()))
39
+ return null;
40
+ const compiledReports = (input.reports ?? []).filter((doc) => doc != null && String(doc.tenant_id) === tenantId);
41
+ const compiledEntries = (input.entries ?? []).filter((doc) => doc != null && String(doc.tenant_id) === tenantId);
42
+ const storedReports = (input.existing ?? []).filter((doc) => doc != null && String(doc.tenant_id) === tenantId);
43
+ const storedEntries = (input.existingEntries ?? []).filter((doc) => doc != null && String(doc.tenant_id) === tenantId);
44
+ const compiledDate = input.localDate ??
45
+ localDateOfHourLabel(compiledReports[0]?.scope_field) ??
46
+ localDateOfHourLabel(storedReports[0]?.scope_field);
47
+ if (compiledDate == null || getMaterializedHourlyComparisonScopeRefsForDay(compiledDate) == null) {
48
+ return null;
49
+ }
50
+ const writes = [];
51
+ const entryWrites = [];
52
+ const staleDocuments = [];
53
+ const staleEntryDocuments = [];
54
+ const skippedDocuments = [];
55
+ const skippedEntryDocuments = [];
56
+ const compiledReportsByField = new Map();
57
+ for (const doc of compiledReports) {
58
+ if (localDateOfHourLabel(doc.scope_field) !== compiledDate)
59
+ continue;
60
+ compiledReportsByField.set(String(doc.scope_field), doc);
61
+ }
62
+ const compiledEntriesByField = new Map();
63
+ for (const doc of compiledEntries) {
64
+ if (localDateOfHourLabel(doc.scope_field) !== compiledDate)
65
+ continue;
66
+ compiledEntriesByField.set(String(doc.scope_field), doc);
67
+ }
68
+ const storedReportsByField = new Map();
69
+ for (const doc of storedReports) {
70
+ if (localDateOfHourLabel(doc.scope_field) !== compiledDate)
71
+ continue;
72
+ storedReportsByField.set(String(doc.scope_field), doc);
73
+ }
74
+ const storedEntriesByField = new Map();
75
+ for (const doc of storedEntries) {
76
+ if (localDateOfHourLabel(doc.scope_field) !== compiledDate)
77
+ continue;
78
+ storedEntriesByField.set(String(doc.scope_field), doc);
79
+ }
80
+ const addWrite = (report) => {
81
+ const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
82
+ const iteration = buildMaterializedTenantReportIteration(persisted, {
83
+ reportType: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_REPORTS,
84
+ reason,
85
+ computedAt,
86
+ });
87
+ if (iteration == null)
88
+ return false;
89
+ writes.push({ report: persisted, iteration: iteration });
90
+ return true;
91
+ };
92
+ const addEntryWrite = (report) => {
93
+ const persisted = report.iteration == null ? { ...report, iteration: 1 } : report;
94
+ const iteration = buildMaterializedTenantReportIteration(persisted, {
95
+ reportType: MATERIALIZED_REPORT_TYPE_ENUM.TENANT_HOURLY_COMPARISON_ENTRIES,
96
+ reason,
97
+ computedAt,
98
+ });
99
+ if (iteration == null)
100
+ return false;
101
+ entryWrites.push({ report: persisted, iteration: iteration });
102
+ return true;
103
+ };
104
+ for (let hour = 0; hour < 24; hour += 1) {
105
+ const scopeField = getMaterializedHourScopeField(compiledDate, hour);
106
+ if (scopeField == null)
107
+ continue;
108
+ const compiled = compiledReportsByField.get(scopeField);
109
+ const stored = storedReportsByField.get(scopeField);
110
+ if (compiled != null) {
111
+ const next = {
112
+ ...compiled,
113
+ _id: stored?._id ?? compiled._id ?? new mongoose.Types.ObjectId().toHexString(),
114
+ iteration: Number(stored?.iteration ?? 0) + 1,
115
+ };
116
+ if (needsMaterializedSalesComparisonReportWrite(stored, next, input.force === true)) {
117
+ addWrite(next);
118
+ }
119
+ else if (stored != null) {
120
+ skippedDocuments.push(stored);
121
+ }
122
+ }
123
+ else if (stored != null) {
124
+ staleDocuments.push(stored);
125
+ }
126
+ const compiledEntry = compiledEntriesByField.get(scopeField);
127
+ const storedEntry = storedEntriesByField.get(scopeField);
128
+ if (compiledEntry != null) {
129
+ const next = {
130
+ ...compiledEntry,
131
+ _id: storedEntry?._id ?? compiledEntry._id ?? new mongoose.Types.ObjectId().toHexString(),
132
+ iteration: Number(storedEntry?.iteration ?? 0) + 1,
133
+ };
134
+ if (needsMaterializedSalesComparisonEntriesWrite(storedEntry, next, input.force === true)) {
135
+ addEntryWrite(next);
136
+ }
137
+ else if (storedEntry != null) {
138
+ skippedEntryDocuments.push(storedEntry);
139
+ }
140
+ }
141
+ else if (storedEntry != null) {
142
+ staleEntryDocuments.push(storedEntry);
143
+ }
144
+ }
145
+ return {
146
+ writes,
147
+ entryWrites,
148
+ staleDocuments,
149
+ staleEntryDocuments,
150
+ skippedDocuments,
151
+ skippedEntryDocuments,
152
+ };
153
+ }
154
+ /** Export the schema versions for the model contract test. */
155
+ export { MATERIALIZED_TENANT_HOURLY_COMPARISON_ENTRIES_SCHEMA_VERSION, MATERIALIZED_TENANT_HOURLY_COMPARISON_REPORTS_SCHEMA_VERSION, };
@@ -0,0 +1,199 @@
1
+ import mongoose from 'mongoose';
2
+ import { HOURLY_COMPARISON_KEY_FILTERS, MATERIALIZED_REPORT_SCOPE_ENUM, MATERIALIZED_TENANT_HOURLY_COMPARISON_ENTRIES_SCHEMA_VERSION, MATERIALIZED_TENANT_HOURLY_COMPARISON_REPORTS_SCHEMA_VERSION, } from '../../constants/MATERIALIZED_REPORTS';
3
+ import { addOrderToSalesComparisonCounters, createEmptySalesComparisonCounters, getOrderDiscountAmountCents, getOrderGrossAmountCents, getOrderItemCount, getSalesComparisonSettledLocalDate, } from './materialized-sales-comparison-accumulator-helpers';
4
+ import { buildMaterializedReportKey } from './key-helpers';
5
+ import { getDayIndexFromLocalDate, getMaterializedHourScopeField } from './scope-helpers';
6
+ /**
7
+ * The tenant-local HOUR (0–23) of an order's settlement instant.
8
+ *
9
+ * Mirrors `getSalesComparisonSettledLocalDate` (same instant shift), so the day
10
+ * and the hour can never disagree.
11
+ */
12
+ export function getHourlyComparisonSettledLocalHour(order, gmtOffset = 0) {
13
+ const raw = order?.order_settled_at;
14
+ if (raw == null)
15
+ return null;
16
+ const time = new Date(raw).getTime();
17
+ if (Number.isNaN(time))
18
+ return null;
19
+ const offset = Number.isFinite(gmtOffset) ? gmtOffset : 0;
20
+ return new Date(time + offset * 60 * 60 * 1000).getUTCHours();
21
+ }
22
+ /** `'YYYY-MM-DD HH'` of the settled day + hour. */
23
+ export function getHourlyComparisonScopeField(localDate, hour) {
24
+ return getMaterializedHourScopeField(localDate, hour);
25
+ }
26
+ const compareEntryDescending = (a, b) => {
27
+ const byTime = new Date(b.time).getTime() - new Date(a.time).getTime();
28
+ if (byTime !== 0)
29
+ return byTime;
30
+ // Same instant: `order_id` ASC, matching the page cursor.
31
+ return a.order_id < b.order_id ? -1 : a.order_id > b.order_id ? 1 : 0;
32
+ };
33
+ const watermarkOf = (order, fallback) => {
34
+ const raw = order?.updatedAt;
35
+ const time = raw == null ? NaN : new Date(raw).getTime();
36
+ return Number.isNaN(time) ? fallback : new Date(time);
37
+ };
38
+ /**
39
+ * Compiles one settled tenant-day.
40
+ *
41
+ * Returns EMPTY arrays (never null) when the day has no settled orders: an
42
+ * emptied day produces no hour document, and the close-day plan retires the
43
+ * stored ones.
44
+ */
45
+ export function compileTenantHourlyComparisonReports(input) {
46
+ if (input == null)
47
+ return null;
48
+ const { tenantId, brandId, localDate, orders, computedAt } = input;
49
+ if (typeof tenantId !== 'string' || tenantId.length === 0)
50
+ return null;
51
+ if (typeof brandId !== 'string' || brandId.length === 0)
52
+ return null;
53
+ if (typeof localDate !== 'string' || localDate.length === 0)
54
+ return null;
55
+ if (!Array.isArray(orders))
56
+ return null;
57
+ if (!(computedAt instanceof Date) || Number.isNaN(computedAt.getTime()))
58
+ return null;
59
+ const dayIndex = getDayIndexFromLocalDate(localDate);
60
+ if (dayIndex == null)
61
+ return null;
62
+ const buckets = new Map();
63
+ let contributingOrders = 0;
64
+ let skippedUnsettledOrders = 0;
65
+ let skippedForeignOrders = 0;
66
+ let skippedUnkeyableOrders = 0;
67
+ for (const order of orders) {
68
+ if (order == null)
69
+ continue;
70
+ if (order.is_deleted === true)
71
+ continue;
72
+ if (order.tenant_id != null && String(order.tenant_id) !== tenantId)
73
+ continue;
74
+ const settledDay = getSalesComparisonSettledLocalDate(order, input.gmtOffset);
75
+ if (settledDay == null) {
76
+ skippedUnsettledOrders += 1;
77
+ continue;
78
+ }
79
+ if (settledDay !== localDate) {
80
+ skippedForeignOrders += 1;
81
+ continue;
82
+ }
83
+ const hour = getHourlyComparisonSettledLocalHour(order, input.gmtOffset);
84
+ if (hour == null || hour < 0 || hour > 23) {
85
+ skippedUnsettledOrders += 1;
86
+ continue;
87
+ }
88
+ const key = buildMaterializedReportKey([String(order.order_status ?? ''), String(order.platform ?? '')]);
89
+ if (key == null) {
90
+ skippedUnkeyableOrders += 1;
91
+ continue;
92
+ }
93
+ let bucket = buckets.get(hour);
94
+ if (bucket == null) {
95
+ bucket = { counters: new Map(), rows: new Map(), watermark: null };
96
+ buckets.set(hour, bucket);
97
+ }
98
+ let counters = bucket.counters.get(key);
99
+ if (counters == null) {
100
+ counters = createEmptySalesComparisonCounters();
101
+ bucket.counters.set(key, counters);
102
+ }
103
+ addOrderToSalesComparisonCounters(counters, order);
104
+ const settledAt = new Date(order.order_settled_at);
105
+ const gross = getOrderGrossAmountCents(order);
106
+ const discount = getOrderDiscountAmountCents(order);
107
+ const row = {
108
+ order_id: String(order._id ?? ''),
109
+ order_number: order.order_number == null ? null : Number(order.order_number),
110
+ time: settledAt.toISOString(),
111
+ order_status: String(order.order_status ?? ''),
112
+ platform: String(order.platform ?? ''),
113
+ net_amount: gross - discount,
114
+ discount_amount: discount,
115
+ item_count: getOrderItemCount(order),
116
+ };
117
+ const rows = bucket.rows.get(key);
118
+ if (rows == null)
119
+ bucket.rows.set(key, [row]);
120
+ else
121
+ rows.push(row);
122
+ const orderWatermark = watermarkOf(order, computedAt);
123
+ if (bucket.watermark == null || orderWatermark.getTime() > bucket.watermark.getTime()) {
124
+ bucket.watermark = orderWatermark;
125
+ }
126
+ contributingOrders += 1;
127
+ }
128
+ if (buckets.size === 0) {
129
+ return {
130
+ reports: [],
131
+ entries: [],
132
+ contributingOrders,
133
+ skippedUnsettledOrders,
134
+ skippedForeignOrders,
135
+ skippedUnkeyableOrders,
136
+ };
137
+ }
138
+ const reports = [];
139
+ const entries = [];
140
+ for (const hour of [...buckets.keys()].sort((a, b) => a - b)) {
141
+ const bucket = buckets.get(hour);
142
+ const scopeField = getMaterializedHourScopeField(localDate, hour);
143
+ if (scopeField == null)
144
+ continue;
145
+ const salesCounters = {};
146
+ const counterRows = [];
147
+ const entrySections = {};
148
+ for (const key of [...bucket.counters.keys()].sort()) {
149
+ const counters = bucket.counters.get(key);
150
+ salesCounters[key] = counters;
151
+ counterRows.push({ key, counters });
152
+ const rows = (bucket.rows.get(key) ?? []).slice().sort(compareEntryDescending);
153
+ if (rows.length > 0)
154
+ entrySections[key] = { rows };
155
+ }
156
+ const sourceWatermark = bucket.watermark ?? computedAt;
157
+ reports.push({
158
+ _id: new mongoose.Types.ObjectId().toHexString(),
159
+ tenant_id: tenantId,
160
+ brand_id: brandId,
161
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
162
+ scope_field: scopeField,
163
+ day_index: dayIndex,
164
+ sale_type_key_filters: [...HOURLY_COMPARISON_KEY_FILTERS],
165
+ sales_counters: salesCounters,
166
+ sales_counter_rows: counterRows,
167
+ schema_version: MATERIALIZED_TENANT_HOURLY_COMPARISON_REPORTS_SCHEMA_VERSION,
168
+ source_watermark: sourceWatermark,
169
+ computed_at: computedAt,
170
+ createdAt: computedAt,
171
+ updatedAt: computedAt,
172
+ iteration: 1,
173
+ });
174
+ entries.push({
175
+ _id: new mongoose.Types.ObjectId().toHexString(),
176
+ tenant_id: tenantId,
177
+ brand_id: brandId,
178
+ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR,
179
+ scope_field: scopeField,
180
+ day_index: dayIndex,
181
+ sale_type_key_filters: [...HOURLY_COMPARISON_KEY_FILTERS],
182
+ sales_entries: entrySections,
183
+ schema_version: MATERIALIZED_TENANT_HOURLY_COMPARISON_ENTRIES_SCHEMA_VERSION,
184
+ source_watermark: sourceWatermark,
185
+ computed_at: computedAt,
186
+ createdAt: computedAt,
187
+ updatedAt: computedAt,
188
+ iteration: 1,
189
+ });
190
+ }
191
+ return {
192
+ reports,
193
+ entries,
194
+ contributingOrders,
195
+ skippedUnsettledOrders,
196
+ skippedForeignOrders,
197
+ skippedUnkeyableOrders,
198
+ };
199
+ }
@@ -0,0 +1,115 @@
1
+ import { CALENDAR_MODE_ENUM } from '../../constants/REPORT_CALENDAR';
2
+ import { MATERIALIZED_REPORT_SCOPE_ENUM, SALES_COMPARISON_BASELINE_ENUM } from '../../constants/MATERIALIZED_REPORTS';
3
+ import { getSalesComparisonBaselineRange } from './materialized-sales-comparison-baseline-helpers';
4
+ import { getDayKeysFromCalendarSelection, isValidBusinessHourWindow } from './materialized-reports-selection-helpers';
5
+ import { getMaterializedHourScopeField } from './scope-helpers';
6
+ /**
7
+ * Hourly Comparison scope groups.
8
+ *
9
+ * The report reads HOUR documents, so a calendar selection is decoded into the
10
+ * exact `localHour` labels of its EFFECTIVE days — not into day/period groups.
11
+ * The baseline window is the Sales Comparison derivation (equal-length previous
12
+ * period / same-length previous year), then the same hour labels.
13
+ *
14
+ * The time-of-day refinement NARROWS the profile (proposed decision, concept
15
+ * §11.2): `16:00 → 20:00` reads hours 16, 17, 18, 19 on every selected day,
16
+ * end-exclusive, with the same 06:00 business-morning wrap rule as every other
17
+ * hour read. An invalid/incomplete window is ignored (all 24 hours), matching
18
+ * the shared decoder's posture.
19
+ *
20
+ * Returns `null` for an unservable selection; an EMPTY array is a valid window
21
+ * with no matching day (a real zero, not an error).
22
+ */
23
+ const hourOf = (time) => {
24
+ if (time == null)
25
+ return null;
26
+ const match = /^(\d{2}):00$/.exec(time);
27
+ if (match == null)
28
+ return null;
29
+ const hour = Number(match[1]);
30
+ return hour >= 0 && hour <= 23 ? hour : null;
31
+ };
32
+ /** The hours a selection's profile covers, in business order (06…23, 00…05). */
33
+ export function getHourlyComparisonProfileHours(selection) {
34
+ const fromHour = hourOf(selection.timeStart);
35
+ const toHour = hourOf(selection.timeEnd);
36
+ if (fromHour == null || toHour == null || !isValidBusinessHourWindow(fromHour, toHour)) {
37
+ return Array.from({ length: 24 }, (_, hour) => hour);
38
+ }
39
+ const hours = [];
40
+ let cursor = fromHour;
41
+ // `isValidBusinessHourWindow` guarantees a non-empty walk within one
42
+ // business day; the bound is a belt-and-braces guard.
43
+ for (let step = 0; step < 24; step += 1) {
44
+ if (cursor === toHour)
45
+ break;
46
+ hours.push(cursor);
47
+ cursor = (cursor + 1) % 24;
48
+ }
49
+ return hours;
50
+ }
51
+ /** Exact `'YYYY-MM-DD HH'` labels for a day set × the profile's hours. */
52
+ const hourLabelsForDays = (days, hours) => {
53
+ const labels = [];
54
+ for (const day of days) {
55
+ for (const hour of hours) {
56
+ const label = getMaterializedHourScopeField(day, hour);
57
+ // A malformed day is a data issue: the caller gets null, never a
58
+ // silently smaller profile.
59
+ if (label == null)
60
+ return [];
61
+ labels.push(label);
62
+ }
63
+ }
64
+ return [...new Set(labels)].sort();
65
+ };
66
+ /**
67
+ * Current profile groups. LIVE is not served (concept §9): the close writes the
68
+ * hour documents, so a running day has none.
69
+ */
70
+ export function buildHourlyComparisonScopeGroups(selection) {
71
+ if (selection == null || typeof selection !== 'object')
72
+ return null;
73
+ if (selection.mode === CALENDAR_MODE_ENUM.LIVE)
74
+ return null;
75
+ const days = getDayKeysFromCalendarSelection(selection);
76
+ if (days.length === 0)
77
+ return null;
78
+ const hours = getHourlyComparisonProfileHours(selection);
79
+ const labels = hourLabelsForDays(days, hours);
80
+ return {
81
+ days: [...new Set(days)].sort(),
82
+ hours,
83
+ groups: labels.length === 0
84
+ ? []
85
+ : [{ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: labels }],
86
+ };
87
+ }
88
+ /**
89
+ * Baseline profile groups. `NONE` (or an unservable selection) returns `null`;
90
+ * a valid window with no matching day returns an empty group list.
91
+ */
92
+ export function buildHourlyComparisonBaselineScopeGroups(selection, baseline) {
93
+ if (selection == null || typeof selection !== 'object')
94
+ return null;
95
+ if (selection.mode === CALENDAR_MODE_ENUM.LIVE)
96
+ return null;
97
+ if (baseline === SALES_COMPARISON_BASELINE_ENUM.NONE)
98
+ return null;
99
+ if (baseline !== SALES_COMPARISON_BASELINE_ENUM.PREVIOUS_PERIOD &&
100
+ baseline !== SALES_COMPARISON_BASELINE_ENUM.PREVIOUS_YEAR) {
101
+ return null;
102
+ }
103
+ const range = getSalesComparisonBaselineRange(selection, baseline);
104
+ if (range == null)
105
+ return null;
106
+ const hours = getHourlyComparisonProfileHours(selection);
107
+ const labels = hourLabelsForDays(range.days, hours);
108
+ return {
109
+ days: [...new Set(range.days)].sort(),
110
+ hours,
111
+ groups: labels.length === 0
112
+ ? []
113
+ : [{ scope_context: MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR, scope_field: labels }],
114
+ };
115
+ }