@managesome/eposmatic-toolkit 0.4.9 → 0.4.10

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 (333) hide show
  1. package/esm/constants/MATERIALIZED_REPORTS.js +448 -0
  2. package/esm/helpers/index.js +3 -0
  3. package/esm/helpers/materialized-reports/index.js +32 -0
  4. package/esm/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +114 -0
  5. package/esm/helpers/materialized-reports/materialized-cash-classification-helpers.js +286 -0
  6. package/esm/helpers/materialized-reports/materialized-cash-close-day-helpers.js +396 -0
  7. package/esm/helpers/materialized-reports/materialized-cash-compile-helpers.js +244 -0
  8. package/esm/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +170 -0
  9. package/esm/helpers/materialized-reports/materialized-delivery-classification-helpers.js +426 -0
  10. package/esm/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +372 -0
  11. package/esm/helpers/materialized-reports/materialized-delivery-compile-helpers.js +214 -0
  12. package/esm/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +318 -0
  13. package/esm/helpers/materialized-reports/materialized-discount-classification-helpers.js +213 -0
  14. package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +604 -0
  15. package/esm/helpers/materialized-reports/materialized-discount-compile-helpers.js +186 -0
  16. package/esm/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +155 -0
  17. package/esm/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +199 -0
  18. package/esm/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +111 -0
  19. package/esm/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +88 -0
  20. package/esm/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +394 -0
  21. package/esm/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +167 -0
  22. package/esm/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +215 -0
  23. package/esm/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +476 -0
  24. package/esm/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +201 -0
  25. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +7 -0
  26. package/esm/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +189 -0
  27. package/esm/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +157 -0
  28. package/esm/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +588 -0
  29. package/esm/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +175 -0
  30. package/esm/helpers/materialized-reports/tenant-cash-in-out-formatter.js +156 -0
  31. package/esm/helpers/materialized-reports/tenant-delivery-formatter.js +230 -0
  32. package/esm/helpers/materialized-reports/tenant-discount-formatter.js +427 -0
  33. package/esm/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +237 -0
  34. package/esm/helpers/materialized-reports/tenant-payments-comparison-formatter.js +279 -0
  35. package/esm/helpers/materialized-reports/tenant-prep-time-formatter.js +228 -0
  36. package/esm/helpers/materialized-reports/tenant-sales-comparison-formatter.js +343 -0
  37. package/esm/helpers/reporting-contract/index.js +2 -0
  38. package/esm/helpers/reporting-contract/overview-contract.js +122 -0
  39. package/esm/helpers/reporting-contract/report-contract.js +228 -0
  40. package/esm/helpers/rider-earnings/index.js +1 -0
  41. package/esm/helpers/rider-earnings/rider-earnings.js +59 -0
  42. package/esm/helpers/rider-reports/index.js +2 -0
  43. package/esm/helpers/rider-reports/rider-daily-report.js +90 -0
  44. package/esm/helpers/rider-reports/rider-report.js +67 -0
  45. package/esm/schemas/models/deliveries.js +75 -0
  46. package/esm/schemas/models/index.js +15 -0
  47. package/esm/schemas/models/materialized_rider_daily_reports.js +39 -0
  48. package/esm/schemas/models/materialized_tenant_cash_entries.js +95 -0
  49. package/esm/schemas/models/materialized_tenant_cash_reports.js +77 -0
  50. package/esm/schemas/models/materialized_tenant_delivery_entries.js +63 -0
  51. package/esm/schemas/models/materialized_tenant_delivery_reports.js +63 -0
  52. package/esm/schemas/models/materialized_tenant_discount_entries.js +156 -0
  53. package/esm/schemas/models/materialized_tenant_discount_reports.js +333 -0
  54. package/esm/schemas/models/materialized_tenant_hourly_comparison_entries.js +87 -0
  55. package/esm/schemas/models/materialized_tenant_hourly_comparison_reports.js +99 -0
  56. package/esm/schemas/models/materialized_tenant_payments_comparison_entries.js +71 -0
  57. package/esm/schemas/models/materialized_tenant_payments_comparison_reports.js +76 -0
  58. package/esm/schemas/models/materialized_tenant_prep_time_entries.js +130 -0
  59. package/esm/schemas/models/materialized_tenant_prep_time_reports.js +109 -0
  60. package/esm/schemas/models/materialized_tenant_sales_comparison_entries.js +130 -0
  61. package/esm/schemas/models/materialized_tenant_sales_comparison_reports.js +154 -0
  62. package/esm/schemas/types/index.js +15 -0
  63. package/esm/schemas/types/materialized_rider_daily_reports.js +1 -0
  64. package/esm/schemas/types/materialized_tenant_cash_entries.js +1 -0
  65. package/esm/schemas/types/materialized_tenant_cash_reports.js +1 -0
  66. package/esm/schemas/types/materialized_tenant_delivery_entries.js +1 -0
  67. package/esm/schemas/types/materialized_tenant_delivery_reports.js +1 -0
  68. package/esm/schemas/types/materialized_tenant_discount_entries.js +1 -0
  69. package/esm/schemas/types/materialized_tenant_discount_reports.js +2 -0
  70. package/esm/schemas/types/materialized_tenant_hourly_comparison_entries.js +1 -0
  71. package/esm/schemas/types/materialized_tenant_hourly_comparison_reports.js +1 -0
  72. package/esm/schemas/types/materialized_tenant_payments_comparison_entries.js +1 -0
  73. package/esm/schemas/types/materialized_tenant_payments_comparison_reports.js +11 -0
  74. package/esm/schemas/types/materialized_tenant_prep_time_entries.js +1 -0
  75. package/esm/schemas/types/materialized_tenant_prep_time_reports.js +1 -0
  76. package/esm/schemas/types/materialized_tenant_sales_comparison_entries.js +1 -0
  77. package/esm/schemas/types/materialized_tenant_sales_comparison_reports.js +14 -0
  78. package/lib/constants/MATERIALIZED_REPORTS.d.ts +339 -2
  79. package/lib/constants/MATERIALIZED_REPORTS.js +450 -1
  80. package/lib/helpers/index.d.ts +3 -0
  81. package/lib/helpers/index.js +3 -0
  82. package/lib/helpers/materialized-reports/index.d.ts +32 -0
  83. package/lib/helpers/materialized-reports/index.js +32 -0
  84. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.d.ts +18 -0
  85. package/lib/helpers/materialized-reports/materialized-cash-accumulator-helpers.js +118 -0
  86. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.d.ts +90 -0
  87. package/lib/helpers/materialized-reports/materialized-cash-classification-helpers.js +295 -0
  88. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.d.ts +102 -0
  89. package/lib/helpers/materialized-reports/materialized-cash-close-day-helpers.js +409 -0
  90. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.d.ts +72 -0
  91. package/lib/helpers/materialized-reports/materialized-cash-compile-helpers.js +251 -0
  92. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.d.ts +7 -0
  93. package/lib/helpers/materialized-reports/materialized-delivery-accumulator-helpers.js +173 -0
  94. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.d.ts +84 -0
  95. package/lib/helpers/materialized-reports/materialized-delivery-classification-helpers.js +438 -0
  96. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.d.ts +97 -0
  97. package/lib/helpers/materialized-reports/materialized-delivery-close-day-helpers.js +385 -0
  98. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.d.ts +40 -0
  99. package/lib/helpers/materialized-reports/materialized-delivery-compile-helpers.js +218 -0
  100. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.d.ts +52 -0
  101. package/lib/helpers/materialized-reports/materialized-discount-accumulator-helpers.js +324 -0
  102. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.d.ts +50 -0
  103. package/lib/helpers/materialized-reports/materialized-discount-classification-helpers.js +222 -0
  104. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +182 -0
  105. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +615 -0
  106. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.d.ts +47 -0
  107. package/lib/helpers/materialized-reports/materialized-discount-compile-helpers.js +193 -0
  108. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.d.ts +64 -0
  109. package/lib/helpers/materialized-reports/materialized-hourly-comparison-close-day-helpers.js +163 -0
  110. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.d.ts +61 -0
  111. package/lib/helpers/materialized-reports/materialized-hourly-comparison-compile-helpers.js +207 -0
  112. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.d.ts +23 -0
  113. package/lib/helpers/materialized-reports/materialized-hourly-comparison-selection-helpers.js +116 -0
  114. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.d.ts +25 -0
  115. package/lib/helpers/materialized-reports/materialized-payments-comparison-accumulator-helpers.js +98 -0
  116. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.d.ts +79 -0
  117. package/lib/helpers/materialized-reports/materialized-payments-comparison-close-day-helpers.js +407 -0
  118. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.d.ts +35 -0
  119. package/lib/helpers/materialized-reports/materialized-payments-comparison-compile-helpers.js +173 -0
  120. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.d.ts +66 -0
  121. package/lib/helpers/materialized-reports/materialized-prep-time-classification-helpers.js +224 -0
  122. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.d.ts +128 -0
  123. package/lib/helpers/materialized-reports/materialized-prep-time-close-day-helpers.js +490 -0
  124. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.d.ts +46 -0
  125. package/lib/helpers/materialized-reports/materialized-prep-time-compile-helpers.js +208 -0
  126. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.d.ts +8 -0
  127. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +8 -1
  128. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.d.ts +48 -0
  129. package/lib/helpers/materialized-reports/materialized-sales-comparison-accumulator-helpers.js +204 -0
  130. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.d.ts +22 -0
  131. package/lib/helpers/materialized-reports/materialized-sales-comparison-baseline-helpers.js +161 -0
  132. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.d.ts +163 -0
  133. package/lib/helpers/materialized-reports/materialized-sales-comparison-close-day-helpers.js +603 -0
  134. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.d.ts +68 -0
  135. package/lib/helpers/materialized-reports/materialized-sales-comparison-compile-helpers.js +182 -0
  136. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.d.ts +98 -0
  137. package/lib/helpers/materialized-reports/tenant-cash-in-out-formatter.js +159 -0
  138. package/lib/helpers/materialized-reports/tenant-delivery-formatter.d.ts +133 -0
  139. package/lib/helpers/materialized-reports/tenant-delivery-formatter.js +233 -0
  140. package/lib/helpers/materialized-reports/tenant-discount-formatter.d.ts +114 -0
  141. package/lib/helpers/materialized-reports/tenant-discount-formatter.js +431 -0
  142. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.d.ts +60 -0
  143. package/lib/helpers/materialized-reports/tenant-hourly-comparison-formatter.js +243 -0
  144. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.d.ts +75 -0
  145. package/lib/helpers/materialized-reports/tenant-payments-comparison-formatter.js +286 -0
  146. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.d.ts +97 -0
  147. package/lib/helpers/materialized-reports/tenant-prep-time-formatter.js +232 -0
  148. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.d.ts +105 -0
  149. package/lib/helpers/materialized-reports/tenant-sales-comparison-formatter.js +349 -0
  150. package/lib/helpers/reporting-contract/index.d.ts +2 -0
  151. package/lib/helpers/reporting-contract/index.js +18 -0
  152. package/lib/helpers/reporting-contract/overview-contract.d.ts +55 -0
  153. package/lib/helpers/reporting-contract/overview-contract.js +126 -0
  154. package/lib/helpers/reporting-contract/report-contract.d.ts +159 -0
  155. package/lib/helpers/reporting-contract/report-contract.js +239 -0
  156. package/lib/helpers/rider-earnings/index.d.ts +1 -0
  157. package/lib/helpers/rider-earnings/index.js +17 -0
  158. package/lib/helpers/rider-earnings/rider-earnings.d.ts +58 -0
  159. package/lib/helpers/rider-earnings/rider-earnings.js +64 -0
  160. package/lib/helpers/rider-reports/index.d.ts +2 -0
  161. package/lib/helpers/rider-reports/index.js +18 -0
  162. package/lib/helpers/rider-reports/rider-daily-report.d.ts +26 -0
  163. package/lib/helpers/rider-reports/rider-daily-report.js +93 -0
  164. package/lib/helpers/rider-reports/rider-report.d.ts +49 -0
  165. package/lib/helpers/rider-reports/rider-report.js +70 -0
  166. package/lib/helpers/types/materialized-reports-apis.d.ts +297 -1
  167. package/lib/schemas/models/deliveries.d.ts +54 -0
  168. package/lib/schemas/models/deliveries.js +75 -0
  169. package/lib/schemas/models/expense_details.d.ts +3 -3
  170. package/lib/schemas/models/index.d.ts +15 -0
  171. package/lib/schemas/models/index.js +15 -0
  172. package/lib/schemas/models/materialized_rider_daily_reports.d.ts +27 -0
  173. package/lib/schemas/models/materialized_rider_daily_reports.js +42 -0
  174. package/lib/schemas/models/materialized_tenant_cash_entries.d.ts +181 -0
  175. package/lib/schemas/models/materialized_tenant_cash_entries.js +98 -0
  176. package/lib/schemas/models/materialized_tenant_cash_reports.d.ts +111 -0
  177. package/lib/schemas/models/materialized_tenant_cash_reports.js +80 -0
  178. package/lib/schemas/models/materialized_tenant_delivery_entries.d.ts +98 -0
  179. package/lib/schemas/models/materialized_tenant_delivery_entries.js +66 -0
  180. package/lib/schemas/models/materialized_tenant_delivery_reports.d.ts +98 -0
  181. package/lib/schemas/models/materialized_tenant_delivery_reports.js +66 -0
  182. package/lib/schemas/models/materialized_tenant_discount_entries.d.ts +199 -0
  183. package/lib/schemas/models/materialized_tenant_discount_entries.js +159 -0
  184. package/lib/schemas/models/materialized_tenant_discount_reports.d.ts +216 -0
  185. package/lib/schemas/models/materialized_tenant_discount_reports.js +336 -0
  186. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.d.ts +95 -0
  187. package/lib/schemas/models/materialized_tenant_hourly_comparison_entries.js +82 -0
  188. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.d.ts +105 -0
  189. package/lib/schemas/models/materialized_tenant_hourly_comparison_reports.js +91 -0
  190. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.d.ts +91 -0
  191. package/lib/schemas/models/materialized_tenant_payments_comparison_entries.js +70 -0
  192. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.d.ts +98 -0
  193. package/lib/schemas/models/materialized_tenant_payments_comparison_reports.js +71 -0
  194. package/lib/schemas/models/materialized_tenant_prep_time_entries.d.ts +181 -0
  195. package/lib/schemas/models/materialized_tenant_prep_time_entries.js +133 -0
  196. package/lib/schemas/models/materialized_tenant_prep_time_reports.d.ts +118 -0
  197. package/lib/schemas/models/materialized_tenant_prep_time_reports.js +112 -0
  198. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.d.ts +139 -0
  199. package/lib/schemas/models/materialized_tenant_sales_comparison_entries.js +120 -0
  200. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.d.ts +167 -0
  201. package/lib/schemas/models/materialized_tenant_sales_comparison_reports.js +137 -0
  202. package/lib/schemas/models/tenant_purchase_order_vendor_returns.d.ts +3 -3
  203. package/lib/schemas/models/warehouse_internal_movements.d.ts +6 -6
  204. package/lib/schemas/models/warehouse_purchase_order_receipts.d.ts +3 -3
  205. package/lib/schemas/models/warehouse_purchase_order_vendor_returns.d.ts +3 -3
  206. package/lib/schemas/models/warehouse_sale_deliveries.d.ts +6 -6
  207. package/lib/schemas/models/warehouse_sale_invoices.d.ts +3 -3
  208. package/lib/schemas/types/deliveries.d.ts +40 -1
  209. package/lib/schemas/types/index.d.ts +15 -0
  210. package/lib/schemas/types/index.js +15 -0
  211. package/lib/schemas/types/materialized_rider_daily_reports.d.ts +37 -0
  212. package/lib/schemas/types/materialized_rider_daily_reports.js +2 -0
  213. package/lib/schemas/types/materialized_tenant_cash_entries.d.ts +87 -0
  214. package/lib/schemas/types/materialized_tenant_cash_entries.js +2 -0
  215. package/lib/schemas/types/materialized_tenant_cash_reports.d.ts +144 -0
  216. package/lib/schemas/types/materialized_tenant_cash_reports.js +2 -0
  217. package/lib/schemas/types/materialized_tenant_delivery_entries.d.ts +98 -0
  218. package/lib/schemas/types/materialized_tenant_delivery_entries.js +2 -0
  219. package/lib/schemas/types/materialized_tenant_delivery_reports.d.ts +179 -0
  220. package/lib/schemas/types/materialized_tenant_delivery_reports.js +2 -0
  221. package/lib/schemas/types/materialized_tenant_discount_entries.d.ts +81 -0
  222. package/lib/schemas/types/materialized_tenant_discount_entries.js +2 -0
  223. package/lib/schemas/types/materialized_tenant_discount_reports.d.ts +209 -0
  224. package/lib/schemas/types/materialized_tenant_discount_reports.js +3 -0
  225. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.d.ts +33 -0
  226. package/lib/schemas/types/materialized_tenant_hourly_comparison_entries.js +2 -0
  227. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.d.ts +40 -0
  228. package/lib/schemas/types/materialized_tenant_hourly_comparison_reports.js +2 -0
  229. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.d.ts +48 -0
  230. package/lib/schemas/types/materialized_tenant_payments_comparison_entries.js +2 -0
  231. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.d.ts +51 -0
  232. package/lib/schemas/types/materialized_tenant_payments_comparison_reports.js +14 -0
  233. package/lib/schemas/types/materialized_tenant_prep_time_entries.d.ts +88 -0
  234. package/lib/schemas/types/materialized_tenant_prep_time_entries.js +2 -0
  235. package/lib/schemas/types/materialized_tenant_prep_time_reports.d.ts +132 -0
  236. package/lib/schemas/types/materialized_tenant_prep_time_reports.js +2 -0
  237. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.d.ts +43 -0
  238. package/lib/schemas/types/materialized_tenant_sales_comparison_entries.js +2 -0
  239. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.d.ts +74 -0
  240. package/lib/schemas/types/materialized_tenant_sales_comparison_reports.js +17 -0
  241. package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +1 -0
  242. package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +1 -0
  243. package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +1 -0
  244. package/lib/tests/materialized-reports/fixtures.d.ts +15 -1
  245. package/lib/tests/materialized-reports/fixtures.js +1 -0
  246. package/lib/tests/materialized-reports/materialized-cash-compile.test.d.ts +1 -0
  247. package/lib/tests/materialized-reports/materialized-cash-compile.test.js +196 -0
  248. package/lib/tests/materialized-reports/materialized-cash-fold.test.d.ts +1 -0
  249. package/lib/tests/materialized-reports/materialized-cash-fold.test.js +116 -0
  250. package/lib/tests/materialized-reports/materialized-cash-formatter.test.d.ts +1 -0
  251. package/lib/tests/materialized-reports/materialized-cash-formatter.test.js +152 -0
  252. package/lib/tests/materialized-reports/materialized-cash-plan.test.d.ts +1 -0
  253. package/lib/tests/materialized-reports/materialized-cash-plan.test.js +168 -0
  254. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.d.ts +1 -0
  255. package/lib/tests/materialized-reports/materialized-cash-reconciliation.test.js +267 -0
  256. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.d.ts +1 -0
  257. package/lib/tests/materialized-reports/materialized-delivery-accumulation.test.js +147 -0
  258. package/lib/tests/materialized-reports/materialized-delivery-compile.test.d.ts +1 -0
  259. package/lib/tests/materialized-reports/materialized-delivery-compile.test.js +241 -0
  260. package/lib/tests/materialized-reports/materialized-delivery-fold.test.d.ts +1 -0
  261. package/lib/tests/materialized-reports/materialized-delivery-fold.test.js +109 -0
  262. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.d.ts +1 -0
  263. package/lib/tests/materialized-reports/materialized-delivery-formatter.test.js +141 -0
  264. package/lib/tests/materialized-reports/materialized-delivery-plan.test.d.ts +1 -0
  265. package/lib/tests/materialized-reports/materialized-delivery-plan.test.js +198 -0
  266. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.d.ts +1 -0
  267. package/lib/tests/materialized-reports/materialized-discount-aggregated-formatter.test.js +173 -0
  268. package/lib/tests/materialized-reports/materialized-discount-classification.test.d.ts +1 -0
  269. package/lib/tests/materialized-reports/materialized-discount-classification.test.js +339 -0
  270. package/lib/tests/materialized-reports/materialized-discount-close-day.test.d.ts +1 -0
  271. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +506 -0
  272. package/lib/tests/materialized-reports/materialized-discount-compile.test.d.ts +1 -0
  273. package/lib/tests/materialized-reports/materialized-discount-compile.test.js +161 -0
  274. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.d.ts +1 -0
  275. package/lib/tests/materialized-reports/materialized-discount-counter-rows.test.js +107 -0
  276. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.d.ts +1 -0
  277. package/lib/tests/materialized-reports/materialized-discount-entries-accumulation.test.js +125 -0
  278. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.d.ts +1 -0
  279. package/lib/tests/materialized-reports/materialized-hourly-comparison-compile.test.js +146 -0
  280. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.d.ts +1 -0
  281. package/lib/tests/materialized-reports/materialized-hourly-comparison-formatter.test.js +127 -0
  282. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.d.ts +1 -0
  283. package/lib/tests/materialized-reports/materialized-hourly-comparison-plan.test.js +138 -0
  284. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.d.ts +1 -0
  285. package/lib/tests/materialized-reports/materialized-hourly-comparison-selection.test.js +90 -0
  286. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.d.ts +1 -0
  287. package/lib/tests/materialized-reports/materialized-payments-comparison-compile.test.js +164 -0
  288. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.d.ts +1 -0
  289. package/lib/tests/materialized-reports/materialized-payments-comparison-formatter.test.js +117 -0
  290. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.d.ts +1 -0
  291. package/lib/tests/materialized-reports/materialized-payments-comparison-plan.test.js +149 -0
  292. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.d.ts +1 -0
  293. package/lib/tests/materialized-reports/materialized-prep-time-classification.test.js +273 -0
  294. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.d.ts +1 -0
  295. package/lib/tests/materialized-reports/materialized-prep-time-close-day.test.js +270 -0
  296. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.d.ts +1 -0
  297. package/lib/tests/materialized-reports/materialized-prep-time-compile.test.js +217 -0
  298. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.d.ts +1 -0
  299. package/lib/tests/materialized-reports/materialized-prep-time-constants.test.js +84 -0
  300. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.d.ts +1 -0
  301. package/lib/tests/materialized-reports/materialized-prep-time-plan.test.js +176 -0
  302. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.d.ts +1 -0
  303. package/lib/tests/materialized-reports/materialized-sales-comparison-baseline.test.js +126 -0
  304. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.d.ts +1 -0
  305. package/lib/tests/materialized-reports/materialized-sales-comparison-compile.test.js +206 -0
  306. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.d.ts +1 -0
  307. package/lib/tests/materialized-reports/materialized-sales-comparison-formatter.test.js +179 -0
  308. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.d.ts +1 -0
  309. package/lib/tests/materialized-reports/materialized-sales-comparison-parity.test.js +196 -0
  310. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.d.ts +1 -0
  311. package/lib/tests/materialized-reports/materialized-sales-comparison-plan.test.js +241 -0
  312. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.d.ts +1 -0
  313. package/lib/tests/materialized-reports/materialized-tenant-hourly-comparison-schema.test.js +96 -0
  314. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.d.ts +1 -0
  315. package/lib/tests/materialized-reports/materialized-tenant-payments-comparison-schema.test.js +112 -0
  316. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.d.ts +1 -0
  317. package/lib/tests/materialized-reports/materialized-tenant-sales-comparison-schema.test.js +142 -0
  318. package/lib/tests/materialized-reports/materialized-tenant-sales-overview-reports-schema.test.js +8 -1
  319. package/lib/tests/materialized-reports/sales-comparison-reference.d.ts +55 -0
  320. package/lib/tests/materialized-reports/sales-comparison-reference.js +86 -0
  321. package/lib/tests/materialized-reports/tenant-discount-formatter.test.d.ts +1 -0
  322. package/lib/tests/materialized-reports/tenant-discount-formatter.test.js +289 -0
  323. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.d.ts +1 -0
  324. package/lib/tests/materialized-reports/tenant-prep-time-formatter.test.js +354 -0
  325. package/lib/tests/reporting-contract.test.d.ts +1 -0
  326. package/lib/tests/reporting-contract.test.js +267 -0
  327. package/lib/tests/rider-daily-report.test.d.ts +1 -0
  328. package/lib/tests/rider-daily-report.test.js +86 -0
  329. package/lib/tests/rider-earnings.test.d.ts +1 -0
  330. package/lib/tests/rider-earnings.test.js +65 -0
  331. package/lib/tests/rider-report.test.d.ts +1 -0
  332. package/lib/tests/rider-report.test.js +98 -0
  333. package/package.json +10 -1
@@ -0,0 +1,506 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_discount_close_day_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-close-day-helpers");
5
+ const materialized_discount_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-compile-helpers");
6
+ const fixtures_1 = require("./fixtures");
7
+ /**
8
+ * Discounts-report CLOSE-DAY PLAN audit (M1).
9
+ *
10
+ * Proves the write plan's discipline on real fixture days:
11
+ * - first close writes the day + periods, with a history row per write;
12
+ * - a re-close with unchanged source data is IDEMPOTENT (everything skipped);
13
+ * - period documents accumulate the day's delta, and a corrected re-close
14
+ * subtracts the stored day before adding the fresh one;
15
+ * - an emptied day subtracts its delta and drops its day/hour documents;
16
+ * - stored day-scoped documents with no activity go stale (deleted by _id).
17
+ */
18
+ const TENANT = 'basha-istanbul-f7';
19
+ const BRAND = 'brand-basha';
20
+ const AT = new Date('2026-10-01T00:00:00.000Z');
21
+ /**
22
+ * Applies the DOCUMENTED fetch contract to full in-memory state.
23
+ *
24
+ * The close tests used to hand the plan EVERYTHING, which no real caller does - and the
25
+ * round-4 audit showed that over-feeding can flip `dayChanged` and mask the guard paths it
26
+ * was supposed to exercise. This selects exactly what the plan's input docs require:
27
+ *
28
+ * 1. the day's own scope refs (localDate + hours + the periods it rolls into), and
29
+ * 2. the period's DAY-scoped entry documents - the siblings a missing period document is
30
+ * rebuilt from.
31
+ */
32
+ function closeInputs(day, state) {
33
+ const refs = (0, materialized_discount_close_day_helpers_1.getMaterializedDiscountScopeRefsForDay)(day);
34
+ if (refs == null)
35
+ throw new Error('closeInputs: bad day ' + day);
36
+ const key = (doc) => `${doc.scope_context}|${doc.scope_field}`;
37
+ const refKeys = new Set(refs.map((ref) => `${ref.scope_context}|${ref.scope_field}`));
38
+ const periodKeys = new Set(refs
39
+ .filter((ref) => ref.scope_context !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE &&
40
+ ref.scope_context !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR)
41
+ .map((ref) => `${ref.scope_context}|${ref.scope_field}`));
42
+ return {
43
+ existing: state.reports.filter((doc) => refKeys.has(key(doc))),
44
+ existingEntries: state.entries.filter((doc) => {
45
+ if (refKeys.has(key(doc)))
46
+ return true;
47
+ if (doc.scope_context !== MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE)
48
+ return false;
49
+ const dayRefs = (0, materialized_discount_close_day_helpers_1.getMaterializedDiscountScopeRefsForDay)(String(doc.scope_field));
50
+ if (dayRefs == null)
51
+ return false;
52
+ return dayRefs.some((ref) => periodKeys.has(`${ref.scope_context}|${ref.scope_field}`));
53
+ }),
54
+ };
55
+ }
56
+ const ordersForDay = (localDate) => (0, fixtures_1.loadOrdersDataset)('basha').filter((order) => order.localDate === localDate);
57
+ const compileDay = (localDate) => (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
58
+ tenantId: TENANT,
59
+ brandId: BRAND,
60
+ localDate,
61
+ orders: ordersForDay(localDate),
62
+ gmtOffset: 5,
63
+ computedAt: AT,
64
+ });
65
+ /** Applies a plan to in-memory state, exactly like a real persist would. */
66
+ function applyPlan(reports, entries, plan) {
67
+ const byScope = new Map(reports.map((doc) => [`${doc.scope_context}|${doc.scope_field}`, doc]));
68
+ for (const write of plan.writes)
69
+ byScope.set(`${write.report.scope_context}|${write.report.scope_field}`, write.report);
70
+ for (const stale of plan.staleDocuments)
71
+ byScope.delete(`${stale.scope_context}|${stale.scope_field}`);
72
+ const entriesByScope = new Map(entries.map((doc) => [`${doc.scope_context}|${doc.scope_field}`, doc]));
73
+ for (const write of plan.entryWrites)
74
+ entriesByScope.set(`${write.report.scope_context}|${write.report.scope_field}`, write.report);
75
+ for (const stale of plan.staleEntryDocuments)
76
+ entriesByScope.delete(`${stale.scope_context}|${stale.scope_field}`);
77
+ return { reports: [...byScope.values()], entries: [...entriesByScope.values()] };
78
+ }
79
+ describe('materialized discounts — close-day plan', () => {
80
+ it('lists the scopes a day needs: date + 6 periods + 24 hours', () => {
81
+ const refs = (0, materialized_discount_close_day_helpers_1.getMaterializedDiscountScopeRefsForDay)('2026-09-13');
82
+ expect(refs).toHaveLength(1 + 6 + 24);
83
+ expect(refs.filter((ref) => ref.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR)).toHaveLength(24);
84
+ expect(refs[0]).toEqual({ scope_context: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE, scope_field: '2026-09-13' });
85
+ expect((0, materialized_discount_close_day_helpers_1.getMaterializedDiscountScopeRefsForDay)('nope')).toBeNull();
86
+ });
87
+ it('first close writes the day with a history row per write', () => {
88
+ const localDate = '2026-09-13';
89
+ const compiled = compileDay(localDate);
90
+ const plan = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
91
+ tenantId: TENANT,
92
+ reports: compiled.reports,
93
+ entries: compiled.entries,
94
+ reason: 'NEW_DAY',
95
+ computedAt: AT,
96
+ });
97
+ expect(plan).not.toBeNull();
98
+ expect(plan.writes.length).toBe(compiled.reports.length);
99
+ expect(plan.staleDocuments).toHaveLength(0);
100
+ expect(plan.skippedDocuments).toHaveLength(0);
101
+ for (const write of plan.writes) {
102
+ expect(write.report.iteration).toBe(1);
103
+ expect(write.iteration.report_type).toBe('TENANT_DISCOUNT_REPORTS');
104
+ expect(write.iteration.reason).toBe('NEW_DAY');
105
+ expect(write.iteration.iteration).toBe(1);
106
+ }
107
+ for (const write of plan.entryWrites) {
108
+ expect(write.iteration.report_type).toBe('TENANT_DISCOUNT_ENTRIES');
109
+ }
110
+ // A period doc created by this close (first materialization) is reported
111
+ // Six accumulating scopes: week, ISO-week, month, quarter, semester, year.
112
+ expect(plan.createdPeriodDocuments.length).toBe(6);
113
+ });
114
+ it('a re-close with unchanged source data is IDEMPOTENT', () => {
115
+ const localDate = '2026-09-13';
116
+ const compiled = compileDay(localDate);
117
+ const first = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
118
+ tenantId: TENANT,
119
+ reports: compiled.reports,
120
+ entries: compiled.entries,
121
+ reason: 'NEW_DAY',
122
+ computedAt: AT,
123
+ });
124
+ const state = applyPlan([], [], first);
125
+ const second = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
126
+ tenantId: TENANT,
127
+ reports: compiled.reports,
128
+ entries: compiled.entries,
129
+ existing: state.reports,
130
+ existingEntries: state.entries,
131
+ reason: 'RECLOSE',
132
+ computedAt: AT,
133
+ });
134
+ expect(second.writes).toHaveLength(0);
135
+ expect(second.entryWrites).toHaveLength(0);
136
+ expect(second.staleDocuments).toHaveLength(0);
137
+ expect(second.staleEntryDocuments).toHaveLength(0);
138
+ expect(second.skippedDocuments.length).toBe(state.reports.length);
139
+ expect(second.skippedEntryDocuments.length).toBe(state.entries.length);
140
+ // No iteration bump anywhere
141
+ for (const doc of state.reports)
142
+ expect(doc.iteration).toBe(1);
143
+ });
144
+ it('accumulates the day delta into period documents, and a forced re-close corrects it', () => {
145
+ const localDate = '2026-09-13';
146
+ const compiled = compileDay(localDate);
147
+ const first = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
148
+ tenantId: TENANT,
149
+ reports: compiled.reports,
150
+ entries: compiled.entries,
151
+ reason: 'NEW_DAY',
152
+ computedAt: AT,
153
+ });
154
+ const state = applyPlan([], [], first);
155
+ const monthBefore = state.reports.find((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
156
+ const dateDoc = compiled.reports.find((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
157
+ const sumOf = (doc) => Object.values(doc.discount_counters ?? {}).reduce((t, entry) => t + entry.amount, 0);
158
+ expect(sumOf(monthBefore)).toBe(sumOf(dateDoc));
159
+ // Force a re-close: the period must subtract the stored day then re-add it
160
+ const forced = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
161
+ tenantId: TENANT,
162
+ reports: compiled.reports,
163
+ entries: compiled.entries,
164
+ existing: state.reports,
165
+ existingEntries: state.entries,
166
+ reason: 'RECLOSE',
167
+ force: true,
168
+ computedAt: AT,
169
+ });
170
+ const monthWrite = forced.writes.find((write) => write.report.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
171
+ expect(monthWrite).toBeDefined();
172
+ expect(sumOf(monthWrite.report)).toBe(sumOf(dateDoc)); // subtract-then-add == unchanged
173
+ expect(monthWrite.report.iteration).toBe(2);
174
+ });
175
+ it('an emptied day subtracts its delta and drops its day documents', () => {
176
+ const localDate = '2026-09-13';
177
+ const compiled = compileDay(localDate);
178
+ const first = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
179
+ tenantId: TENANT,
180
+ reports: compiled.reports,
181
+ entries: compiled.entries,
182
+ reason: 'NEW_DAY',
183
+ computedAt: AT,
184
+ });
185
+ const state = applyPlan([], [], first);
186
+ const emptied = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
187
+ tenantId: TENANT,
188
+ localDate,
189
+ reports: [],
190
+ entries: [],
191
+ existing: state.reports,
192
+ existingEntries: state.entries,
193
+ reason: 'RECLOSE',
194
+ computedAt: AT,
195
+ });
196
+ // The day's date + hour documents are stale, and every entry doc is stale
197
+ expect(emptied.staleDocuments.length).toBeGreaterThan(0);
198
+ for (const doc of emptied.staleDocuments) {
199
+ const onDay = doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE
200
+ ? doc.scope_field === localDate
201
+ : doc.scope_field.startsWith(`${localDate} `);
202
+ expect(onDay).toBe(true);
203
+ }
204
+ expect(emptied.staleEntryDocuments.length).toBe(state.entries.length);
205
+ // Period documents are corrected by subtraction, never deleted
206
+ const after = applyPlan(state.reports, state.entries, emptied);
207
+ const monthAfter = after.reports.find((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
208
+ const sumOf = (doc) => Object.values(doc?.discount_counters ?? {}).reduce((t, entry) => t + entry.amount, 0);
209
+ expect(sumOf(monthAfter)).toBe(0);
210
+ });
211
+ it('rejects inconsistent input without throwing', () => {
212
+ const compiled = compileDay('2026-09-13');
213
+ expect((0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)(null)).toBeNull();
214
+ expect((0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
215
+ tenantId: '',
216
+ reports: compiled.reports,
217
+ entries: compiled.entries,
218
+ reason: 'NEW_DAY',
219
+ })).toBeNull();
220
+ expect((0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
221
+ tenantId: TENANT,
222
+ reports: compiled.reports,
223
+ entries: compiled.entries,
224
+ reason: '',
225
+ })).toBeNull();
226
+ // Another tenant's compiled documents
227
+ expect((0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
228
+ tenantId: 'other-tenant',
229
+ reports: compiled.reports,
230
+ entries: compiled.entries,
231
+ reason: 'NEW_DAY',
232
+ })).toBeNull();
233
+ // Duplicate compiled scope documents
234
+ expect((0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
235
+ tenantId: TENANT,
236
+ reports: [...compiled.reports, compiled.reports[0]],
237
+ entries: compiled.entries,
238
+ reason: 'NEW_DAY',
239
+ })).toBeNull();
240
+ });
241
+ it('the watermark guard decides on watermark / version / grammar', () => {
242
+ const compiled = compileDay('2026-09-13');
243
+ const doc = compiled.reports[0];
244
+ expect((0, materialized_discount_close_day_helpers_1.needsMaterializedDiscountReportWrite)(null, doc)).toBe(true);
245
+ expect((0, materialized_discount_close_day_helpers_1.needsMaterializedDiscountReportWrite)(doc, doc)).toBe(false);
246
+ expect((0, materialized_discount_close_day_helpers_1.needsMaterializedDiscountReportWrite)(doc, {
247
+ ...doc,
248
+ source_watermark: new Date(doc.source_watermark.getTime() + 1000),
249
+ })).toBe(true);
250
+ expect((0, materialized_discount_close_day_helpers_1.needsMaterializedDiscountReportWrite)(doc, { ...doc, schema_version: 2 })).toBe(true);
251
+ expect((0, materialized_discount_close_day_helpers_1.needsMaterializedDiscountReportWrite)(doc, {
252
+ ...doc,
253
+ sale_type_key_filters: [MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_KEY_FILTER_ENUM.PLATFORM],
254
+ })).toBe(true);
255
+ });
256
+ it('updateMaterializedDiscountReport prunes all-zero keys and bumps iteration', () => {
257
+ const compiled = compileDay('2026-09-13');
258
+ const dateDoc = compiled.reports.find((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
259
+ const base = { ...dateDoc, iteration: 1 };
260
+ const updated = (0, materialized_discount_close_day_helpers_1.updateMaterializedDiscountReport)(base, { subtract: [dateDoc] }, { computedAt: AT });
261
+ expect(updated).not.toBeNull();
262
+ expect(updated.discount_counters).toBeNull(); // every key returned to zero → pruned
263
+ expect(updated.iteration).toBe(2);
264
+ });
265
+ it('keeps ANOTHER day ENTRY documents when closing a later day', () => {
266
+ // Regression. A DAY-scoped entry document belonging to a DIFFERENT date used to fall
267
+ // through to the PERIOD branch, where the compile had (correctly) not emitted it, so it
268
+ // was marked STALE and DELETED. Every close therefore wiped every other day rows: in the
269
+ // demo replay the counters covered 19 discount days while only ONE day of entry rows
270
+ // survived, leaving the drill-down empty for 18 days the summary said had discounts.
271
+ //
272
+ // Two REAL fixture days, because the bug only fires when the closed day COMPILES
273
+ // something - a day that compiles nothing takes the earlier "day emptied" path.
274
+ const candidates = [...new Set((0, fixtures_1.loadOrdersDataset)('basha').map((o) => o.localDate))]
275
+ .filter((day) => typeof day === 'string')
276
+ .sort();
277
+ let first = null;
278
+ let firstDay = '';
279
+ let secondDay = '';
280
+ for (const day of candidates) {
281
+ const compiled = compileDay(day);
282
+ if (compiled.entries.length === 0)
283
+ continue;
284
+ if (first == null) {
285
+ first = applyPlan([], [], (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
286
+ tenantId: TENANT,
287
+ reports: compiled.reports,
288
+ entries: compiled.entries,
289
+ localDate: day,
290
+ existing: [],
291
+ existingEntries: [],
292
+ reason: 'NEW_DAY',
293
+ computedAt: AT,
294
+ }));
295
+ firstDay = day;
296
+ continue;
297
+ }
298
+ // A LATER day that also compiles entries. Its close must not touch the first.
299
+ if (compiled.entries.length === 0)
300
+ continue;
301
+ secondDay = day;
302
+ const second = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
303
+ tenantId: TENANT,
304
+ reports: compiled.reports,
305
+ entries: compiled.entries,
306
+ localDate: day,
307
+ existing: first.reports,
308
+ existingEntries: first.entries,
309
+ reason: 'NEW_DAY',
310
+ computedAt: AT,
311
+ });
312
+ const staleIds = new Set(second.staleEntryDocuments.map((doc) => String(doc._id)));
313
+ const firstDayEntries = first.entries.filter((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE);
314
+ expect(firstDayEntries.length).toBeGreaterThan(0);
315
+ for (const doc of firstDayEntries) {
316
+ expect(staleIds.has(String(doc._id))).toBe(false);
317
+ }
318
+ const applied = applyPlan(first.reports, first.entries, second);
319
+ expect(applied.entries.filter((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE).length).toBeGreaterThanOrEqual(firstDayEntries.length);
320
+ break;
321
+ }
322
+ expect(firstDay).not.toBe('');
323
+ expect(secondDay).not.toBe('');
324
+ });
325
+ it('AUDIT D1: emptying a day must not destroy a SIBLING day rows in the period docs', () => {
326
+ // Reproduces the defect the round-2 audit reported. The emptied-day path pushes EVERY
327
+ // entry document stale, including the PERIOD ones, and discount period entries are
328
+ // rebuilt incrementally - so the next sibling close leaves the period holding one day.
329
+ const allDays = [...new Set((0, fixtures_1.loadOrdersDataset)('basha').map((o) => o.localDate))]
330
+ .filter((d) => typeof d === 'string')
331
+ .sort();
332
+ const withDiscounts = allDays.filter((d) => compileDay(d).reports.length > 0).slice(0, 3);
333
+ expect(withDiscounts).toHaveLength(3);
334
+ const [a, b, c] = withDiscounts;
335
+ const close = (day, state, emptied = false) => {
336
+ const compiled = compileDay(day);
337
+ const plan = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
338
+ tenantId: TENANT,
339
+ reports: emptied ? [] : compiled.reports,
340
+ entries: emptied ? [] : compiled.entries,
341
+ localDate: day,
342
+ ...closeInputs(day, state),
343
+ reason: 'CORRECTION',
344
+ computedAt: AT,
345
+ });
346
+ return applyPlan(state.reports, state.entries, plan);
347
+ };
348
+ const rowsOf = (doc) => Object.values(doc?.discount_entries ?? {}).reduce((sum, section) => sum + (section?.rows ?? []).length, 0);
349
+ const monthRows = (state) => state.entries
350
+ .filter((doc) => doc.scope_context === 'localMonth')
351
+ .reduce((sum, doc) => sum + rowsOf(doc), 0);
352
+ const dayRows = (state, day) => rowsOf(state.entries.find((doc) => doc.scope_context === 'localDate' && doc.scope_field === day));
353
+ // CONTROL: close A, B, C in order.
354
+ let control = { reports: [], entries: [] };
355
+ for (const day of [a, b, c])
356
+ control = close(day, control);
357
+ const controlMonth = monthRows(control);
358
+ expect(controlMonth).toBeGreaterThan(0);
359
+ // CASE: close A and B, EMPTY A, then close C.
360
+ let state = { reports: [], entries: [] };
361
+ state = close(a, state);
362
+ state = close(b, state);
363
+ const afterB = monthRows(state);
364
+ const bContribution = dayRows(state, b);
365
+ expect(bContribution).toBeGreaterThan(0);
366
+ state = close(a, state, true);
367
+ state = close(c, state);
368
+ const caseMonth = monthRows(state);
369
+ // B and C were never touched, so their rows must still be in the month. Emptying A
370
+ // removes ONLY A. Expected 2 (B) + 2 (C) = 4; the defect yields 2.
371
+ const cContribution = dayRows(state, c);
372
+ expect({ bContribution, cContribution, caseMonth }).toEqual({
373
+ bContribution,
374
+ cContribution,
375
+ caseMonth: bContribution + cContribution,
376
+ });
377
+ });
378
+ it('AUDIT F1: re-closing a day with FEWER rows must SHRINK the period docs', () => {
379
+ // Round-4 audit finding. The normal close path MERGES period entry rows
380
+ // (add/overwrite-only), and a merge cannot remove. Only the fully-emptied day
381
+ // subtracts. So when an order is deleted or corrected after its day was closed, the
382
+ // vanished row survives in localWeek / localWeekOfYear / localMonth entry docs while
383
+ // the DAY document correctly shrinks - the period drill-down shows a phantom row.
384
+ const allDays = [...new Set((0, fixtures_1.loadOrdersDataset)('basha').map((o) => o.localDate))]
385
+ .filter((d) => typeof d === 'string')
386
+ .sort();
387
+ const rowsOf = (doc) => Object.values(doc?.discount_entries ?? {}).reduce((sum, section) => sum + (section?.rows ?? []).length, 0);
388
+ const compileWith = (day, orders) => (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
389
+ tenantId: TENANT,
390
+ brandId: BRAND,
391
+ localDate: day,
392
+ orders,
393
+ gmtOffset: 5,
394
+ computedAt: AT,
395
+ });
396
+ // A day whose full compile has rows, AND which can be recompiled with FEWER rows by
397
+ // dropping a single order - i.e. a real correction, not an emptied day.
398
+ let day = '';
399
+ let full = null;
400
+ let shrunk = null;
401
+ for (const candidate of allDays) {
402
+ const orders = ordersForDay(candidate);
403
+ const fullCompile = compileWith(candidate, orders);
404
+ const fullRows = fullCompile.entries.reduce((sum, doc) => sum + rowsOf(doc), 0);
405
+ if (fullRows === 0)
406
+ continue;
407
+ for (let drop = 0; drop < orders.length; drop += 1) {
408
+ const reducedOrders = orders.filter((_, index) => index !== drop);
409
+ const reduced = compileWith(candidate, reducedOrders);
410
+ const reducedRows = reduced.entries.reduce((sum, doc) => sum + rowsOf(doc), 0);
411
+ if (reducedRows > 0 && reducedRows < fullRows) {
412
+ day = candidate;
413
+ full = fullCompile;
414
+ shrunk = reduced;
415
+ break;
416
+ }
417
+ }
418
+ if (day !== '')
419
+ break;
420
+ }
421
+ expect(day).not.toBe('');
422
+ const close = (state, compiled, force = false) => {
423
+ const plan = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
424
+ tenantId: TENANT,
425
+ reports: compiled.reports,
426
+ entries: compiled.entries,
427
+ localDate: day,
428
+ ...closeInputs(day, state),
429
+ reason: 'CORRECTION',
430
+ computedAt: AT,
431
+ force,
432
+ });
433
+ return applyPlan(state.reports, state.entries, plan);
434
+ };
435
+ const monthRows = (state) => state.entries
436
+ .filter((doc) => doc.scope_context === 'localMonth')
437
+ .reduce((sum, doc) => sum + rowsOf(doc), 0);
438
+ const dayRows = (state) => rowsOf(state.entries.find((doc) => doc.scope_context === 'localDate' && doc.scope_field === day));
439
+ let state = close({ reports: [], entries: [] }, full);
440
+ const fullMonth = monthRows(state);
441
+ expect(fullMonth).toBeGreaterThan(0);
442
+ // FORCED: dropping an order can LOWER the compile watermark, so a natural re-close is
443
+ // skipped by the write guard and the correction never lands. force makes the shrink real.
444
+ state = close(state, shrunk, true);
445
+ const shrunkDay = dayRows(state);
446
+ const shrunkMonth = monthRows(state);
447
+ // The correction must actually have TAKEN EFFECT, or this asserts nothing: a re-close
448
+ // the watermark guard skips leaves day == month == the OLD size and passes vacuously.
449
+ expect({ fullMonth, shrunkDay }).toEqual({ fullMonth, shrunkDay: shrunkMonth });
450
+ expect(shrunkDay).toBeLessThan(fullMonth);
451
+ // The month must match the DAY: whatever the day no longer holds must be gone from
452
+ // the period too. A leak leaves the month larger.
453
+ expect({ shrunkDay, shrunkMonth, fullMonth }).toEqual({
454
+ shrunkDay,
455
+ shrunkMonth: shrunkDay,
456
+ fullMonth,
457
+ });
458
+ });
459
+ it('AUDIT F3: a MISSING period entry document is rebuilt from the DAY documents', () => {
460
+ // Round-4 review finding, third instance of one architectural cause. Discount period
461
+ // entries ACCUMULATE each day in, so a period document that was never persisted (a
462
+ // partial failure between the counter writes and the entry writes) used to be rebuilt
463
+ // from the CLOSING DAY alone - dropping every sibling day from the period drill-down
464
+ // even though their documents were right there. The counters already repaired this
465
+ // case explicitly; the entries did not.
466
+ const allDays = [...new Set((0, fixtures_1.loadOrdersDataset)('basha').map((o) => o.localDate))]
467
+ .filter((d) => typeof d === 'string')
468
+ .sort();
469
+ const rowsOf = (doc) => Object.values(doc?.discount_entries ?? {}).reduce((sum, section) => sum + (section?.rows ?? []).length, 0);
470
+ const daysWithRows = allDays
471
+ .filter((d) => compileDay(d).entries.some((entry) => rowsOf(entry) > 0))
472
+ .slice(0, 3);
473
+ expect(daysWithRows).toHaveLength(3);
474
+ const [a, b, c] = daysWithRows;
475
+ const close = (state, day) => {
476
+ const compiled = compileDay(day);
477
+ const plan = (0, materialized_discount_close_day_helpers_1.buildTenantDiscountCloseDayPlan)({
478
+ tenantId: TENANT,
479
+ reports: compiled.reports,
480
+ entries: compiled.entries,
481
+ localDate: day,
482
+ ...closeInputs(day, state),
483
+ reason: 'NEW_DAY',
484
+ computedAt: AT,
485
+ });
486
+ return applyPlan(state.reports, state.entries, plan);
487
+ };
488
+ const monthRows = (state) => rowsOf(state.entries.find((doc) => doc.scope_context === 'localMonth'));
489
+ let state = close({ reports: [], entries: [] }, a);
490
+ state = close(state, b);
491
+ expect(monthRows(state)).toBeGreaterThan(0);
492
+ // The period ENTRY document is lost; the day documents survive.
493
+ state = {
494
+ ...state,
495
+ entries: state.entries.filter((doc) => doc.scope_context !== 'localMonth'),
496
+ };
497
+ expect(monthRows(state)).toBe(0);
498
+ state = close(state, c);
499
+ const dayRows = daysWithRows.reduce((sum, day) => {
500
+ const doc = state.entries.find((entry) => entry.scope_context === 'localDate' && entry.scope_field === day);
501
+ return sum + rowsOf(doc);
502
+ }, 0);
503
+ // The rebuilt period must carry EVERY day still on hand, not just the closing one.
504
+ expect({ month: monthRows(state), dayRows }).toEqual({ month: dayRows, dayRows });
505
+ });
506
+ });
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MATERIALIZED_REPORTS_1 = require("../../constants/MATERIALIZED_REPORTS");
4
+ const materialized_discount_compile_helpers_1 = require("../../helpers/materialized-reports/materialized-discount-compile-helpers");
5
+ const fixtures_1 = require("./fixtures");
6
+ /**
7
+ * Discounts-report DAY COMPILE audit (M1).
8
+ *
9
+ * Proves the scope fan-out invariants on real fixture days:
10
+ * - one document per (scope context, scope field), hour docs only where active;
11
+ * - the entries collection mirrors the report collection's scope set;
12
+ * - the DATE document equals the day's orders, and the MONTH document equals
13
+ * the sum of its DATE documents (the accumulation identity);
14
+ * - every document carries the discount key grammar + schema version 1.
15
+ */
16
+ const TENANT = 'basha-istanbul-f7';
17
+ const BRAND = 'brand-basha';
18
+ const ordersForDay = (localDate) => (0, fixtures_1.loadOrdersDataset)('basha').filter((order) => order.localDate === localDate);
19
+ const compileDay = (localDate) => (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
20
+ tenantId: TENANT,
21
+ brandId: BRAND,
22
+ localDate,
23
+ orders: ordersForDay(localDate),
24
+ gmtOffset: 5,
25
+ computedAt: new Date('2026-10-01T00:00:00.000Z'),
26
+ });
27
+ const dateKey = (localDate) => `${MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE}|${localDate}`;
28
+ describe('materialized discounts — day compile', () => {
29
+ it('emits one document per scope field, hour docs only when active, and entries mirroring them', () => {
30
+ const result = compileDay('2026-09-13');
31
+ expect(result).not.toBeNull();
32
+ const { reports, entries } = result;
33
+ // Identity: no duplicate (scope_context, scope_field) pairs
34
+ const identities = reports.map((doc) => `${doc.scope_context}|${doc.scope_field}`);
35
+ expect(new Set(identities).size).toBe(identities.length);
36
+ // The period scopes always exist; hour docs only for active hours
37
+ for (const scope of [
38
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_DATE,
39
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK,
40
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_WEEK_OF_YEAR,
41
+ MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH,
42
+ ]) {
43
+ expect(reports.some((doc) => doc.scope_context === scope)).toBe(true);
44
+ }
45
+ const hours = reports.filter((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR);
46
+ expect(hours.length).toBeGreaterThan(0);
47
+ expect(hours.length).toBeLessThanOrEqual(24);
48
+ for (const hour of hours)
49
+ expect(hour.scope_field.startsWith('2026-09-13 ')).toBe(true);
50
+ // Every report doc carries the key grammar + version; counters are present
51
+ for (const doc of reports) {
52
+ expect(doc.sale_type_key_filters).toEqual(['ORDER_STATUS', 'PLATFORM']);
53
+ expect(doc.schema_version).toBe(1);
54
+ expect(doc.discount_counters).not.toBeNull();
55
+ expect(Object.keys(doc.discount_counters ?? {}).length).toBeGreaterThan(0);
56
+ }
57
+ // Entries exist only where rows exist, and never carry an empty section
58
+ const reportIds = new Set(identities);
59
+ for (const doc of entries) {
60
+ expect(reportIds.has(`${doc.scope_context}|${doc.scope_field}`)).toBe(true);
61
+ const sections = Object.values(doc.discount_entries ?? {});
62
+ expect(sections.length).toBeGreaterThan(0);
63
+ for (const section of sections)
64
+ expect(section.rows.length).toBeGreaterThan(0);
65
+ }
66
+ });
67
+ it('the DATE document equals the day, and hours + hour-less orders explain it', () => {
68
+ const localDate = '2026-09-13';
69
+ const result = compileDay(localDate);
70
+ expect(result).not.toBeNull();
71
+ const { reports } = result;
72
+ const dateDoc = reports.find((doc) => `${doc.scope_context}|${doc.scope_field}` === dateKey(localDate));
73
+ expect(dateDoc).toBeDefined();
74
+ const sumAcross = (docs) => docs.reduce((total, doc) => {
75
+ const counters = Object.values(doc.discount_counters ?? {});
76
+ return total + counters.reduce((sub, entry) => sub + (entry.amount ?? 0), 0);
77
+ }, 0);
78
+ const dateTotal = sumAcross([dateDoc]);
79
+ const hourTotal = sumAcross(reports.filter((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_HOUR));
80
+ // The date is authoritative for the day; hours never exceed it, and any
81
+ // difference is exactly the hour-less orders (unresolvable punch hour).
82
+ expect(dateTotal).toBeGreaterThan(0);
83
+ expect(hourTotal).toBeLessThanOrEqual(dateTotal);
84
+ });
85
+ it('the MONTH document equals the sum of its compiled DATE documents', () => {
86
+ const days = ['2026-09-04', '2026-09-13', '2026-09-20', '2026-09-23', '2026-09-24'];
87
+ const monthCounters = {};
88
+ const addMonth = (key, count, amount, resolved) => {
89
+ const current = (monthCounters[key] ?? (monthCounters[key] = { count: 0, amount: 0, resolved: 0 }));
90
+ current.count += count;
91
+ current.amount += amount;
92
+ current.resolved += resolved;
93
+ };
94
+ for (const day of days) {
95
+ const result = compileDay(day);
96
+ const monthDoc = result.reports.find((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
97
+ expect(monthDoc).toBeDefined();
98
+ for (const [key, entry] of Object.entries(monthDoc.discount_counters ?? {})) {
99
+ addMonth(key, entry.count, entry.amount, entry.order_special.resolved_amount);
100
+ }
101
+ }
102
+ // A month document built from the same days must match the per-day sums
103
+ const orders = days.flatMap((day) => ordersForDay(day));
104
+ const monthResult = (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
105
+ tenantId: TENANT,
106
+ brandId: BRAND,
107
+ localDate: '2026-09-13',
108
+ orders,
109
+ gmtOffset: 5,
110
+ computedAt: new Date('2026-10-01T00:00:00.000Z'),
111
+ });
112
+ const monthDoc = monthResult.reports.find((doc) => doc.scope_context === MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_SCOPE_ENUM.LOCAL_MONTH);
113
+ expect(monthDoc).toBeDefined();
114
+ for (const [key, entry] of Object.entries(monthDoc.discount_counters ?? {})) {
115
+ const summed = monthCounters[key];
116
+ if (summed == null)
117
+ continue;
118
+ expect(Math.round(entry.count * 100) / 100).toBe(Math.round(summed.count * 100) / 100);
119
+ expect(Math.round(entry.amount * 100) / 100).toBe(Math.round(summed.amount * 100) / 100);
120
+ }
121
+ });
122
+ it('returns empty arrays for a day with no discounts, and null on bad input', () => {
123
+ const empty = (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
124
+ tenantId: TENANT,
125
+ brandId: BRAND,
126
+ localDate: '2026-01-01',
127
+ orders: [],
128
+ gmtOffset: 5,
129
+ computedAt: new Date(),
130
+ });
131
+ expect(empty).toEqual({ reports: [], entries: [], contributingOrders: 0 });
132
+ expect((0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)(null)).toBeNull();
133
+ expect((0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
134
+ tenantId: '',
135
+ brandId: BRAND,
136
+ localDate: '2026-09-13',
137
+ orders: [],
138
+ computedAt: new Date(),
139
+ })).toBeNull();
140
+ expect((0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
141
+ tenantId: TENANT,
142
+ brandId: BRAND,
143
+ localDate: 'not-a-date',
144
+ orders: [],
145
+ computedAt: new Date(),
146
+ })).toBeNull();
147
+ });
148
+ it('ignores orders belonging to another tenant', () => {
149
+ const foreign = ordersForDay('2026-09-13').map((order) => ({ ...order, tenant_id: 'someone-else' }));
150
+ const result = (0, materialized_discount_compile_helpers_1.compileTenantDiscountReports)({
151
+ tenantId: TENANT,
152
+ brandId: BRAND,
153
+ localDate: '2026-09-13',
154
+ orders: foreign,
155
+ gmtOffset: 5,
156
+ computedAt: new Date(),
157
+ });
158
+ expect(result.contributingOrders).toBe(0);
159
+ expect(result.reports).toHaveLength(0);
160
+ });
161
+ });