@open-mercato/core 0.9.1-develop.7519.1.38f51f8dbb → 0.9.1-develop.7536.1.f9a5776a07

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 (105) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/modules/auth/api/logout.js +14 -6
  3. package/dist/modules/auth/api/logout.js.map +2 -2
  4. package/dist/modules/auth/api/session/refresh.js +6 -1
  5. package/dist/modules/auth/api/session/refresh.js.map +2 -2
  6. package/dist/modules/catalog/ai-tools/mutation-pack.js +10 -5
  7. package/dist/modules/catalog/ai-tools/mutation-pack.js.map +2 -2
  8. package/dist/modules/catalog/ai-tools/types.js.map +2 -2
  9. package/dist/modules/currencies/backend/currencies/[id]/page.js +5 -2
  10. package/dist/modules/currencies/backend/currencies/[id]/page.js.map +2 -2
  11. package/dist/modules/currencies/commands/currencies.js +399 -137
  12. package/dist/modules/currencies/commands/currencies.js.map +2 -2
  13. package/dist/modules/currencies/lib/seeds.js +2 -0
  14. package/dist/modules/currencies/lib/seeds.js.map +2 -2
  15. package/dist/modules/customer_accounts/api/login.js +14 -7
  16. package/dist/modules/customer_accounts/api/login.js.map +2 -2
  17. package/dist/modules/customer_accounts/services/customerUserService.js +10 -10
  18. package/dist/modules/customer_accounts/services/customerUserService.js.map +2 -2
  19. package/dist/modules/customers/commands/companies.js +10 -3
  20. package/dist/modules/customers/commands/companies.js.map +2 -2
  21. package/dist/modules/customers/commands/interactionRestoreDetails.js +75 -0
  22. package/dist/modules/customers/commands/interactionRestoreDetails.js.map +7 -0
  23. package/dist/modules/customers/commands/people.js +10 -3
  24. package/dist/modules/customers/commands/people.js.map +2 -2
  25. package/dist/modules/forms/api/distributions/[distributionId]/invitations/[invitationId]/route.js +1 -1
  26. package/dist/modules/forms/api/distributions/[distributionId]/invitations/[invitationId]/route.js.map +1 -1
  27. package/dist/modules/forms/api/form-submissions/[id]/submit/route.js +12 -2
  28. package/dist/modules/forms/api/form-submissions/[id]/submit/route.js.map +2 -2
  29. package/dist/modules/forms/api/form-submissions/route.js +16 -1
  30. package/dist/modules/forms/api/form-submissions/route.js.map +2 -2
  31. package/dist/modules/forms/api/public/submissions/[id]/submit/route.js +6 -1
  32. package/dist/modules/forms/api/public/submissions/[id]/submit/route.js.map +2 -2
  33. package/dist/modules/forms/api/runtime-helpers.js +16 -0
  34. package/dist/modules/forms/api/runtime-helpers.js.map +2 -2
  35. package/dist/modules/forms/data/validators.js.map +2 -2
  36. package/dist/modules/forms/lib/portal-subject-policy.js +17 -0
  37. package/dist/modules/forms/lib/portal-subject-policy.js.map +7 -0
  38. package/dist/modules/forms/lib/runtime-principal.js +27 -10
  39. package/dist/modules/forms/lib/runtime-principal.js.map +2 -2
  40. package/dist/modules/forms/services/distribution-service.js +35 -13
  41. package/dist/modules/forms/services/distribution-service.js.map +2 -2
  42. package/dist/modules/forms/services/submission-service.js +4 -0
  43. package/dist/modules/forms/services/submission-service.js.map +2 -2
  44. package/dist/modules/staff/commands/leave-requests.js +54 -24
  45. package/dist/modules/staff/commands/leave-requests.js.map +2 -2
  46. package/dist/modules/warranty_claims/api/portal/attachments/route.js +19 -7
  47. package/dist/modules/warranty_claims/api/portal/attachments/route.js.map +2 -2
  48. package/dist/modules/warranty_claims/commands/claims.js +1 -1
  49. package/dist/modules/warranty_claims/commands/claims.js.map +2 -2
  50. package/dist/modules/warranty_claims/subscribers/return-shipment-tracking.js +6 -6
  51. package/dist/modules/warranty_claims/subscribers/return-shipment-tracking.js.map +2 -2
  52. package/dist/modules/wms/commands/inventory-actions.js +19 -3
  53. package/dist/modules/wms/commands/inventory-actions.js.map +2 -2
  54. package/dist/modules/workflows/lib/async-activity-wait.js +43 -0
  55. package/dist/modules/workflows/lib/async-activity-wait.js.map +7 -0
  56. package/dist/modules/workflows/lib/parallel-handler.js +20 -14
  57. package/dist/modules/workflows/lib/parallel-handler.js.map +2 -2
  58. package/dist/modules/workflows/lib/workflow-executor.js +21 -23
  59. package/dist/modules/workflows/lib/workflow-executor.js.map +2 -2
  60. package/package.json +7 -7
  61. package/src/modules/auth/api/logout.ts +15 -6
  62. package/src/modules/auth/api/session/refresh.ts +6 -1
  63. package/src/modules/catalog/ai-tools/mutation-pack.ts +14 -5
  64. package/src/modules/catalog/ai-tools/types.ts +1 -0
  65. package/src/modules/currencies/backend/currencies/[id]/page.tsx +5 -1
  66. package/src/modules/currencies/commands/currencies.ts +475 -168
  67. package/src/modules/currencies/i18n/de.json +4 -0
  68. package/src/modules/currencies/i18n/en.json +4 -0
  69. package/src/modules/currencies/i18n/es.json +4 -0
  70. package/src/modules/currencies/i18n/ko.json +4 -0
  71. package/src/modules/currencies/i18n/pl.json +4 -0
  72. package/src/modules/currencies/lib/seeds.ts +2 -0
  73. package/src/modules/customer_accounts/api/login.ts +15 -7
  74. package/src/modules/customer_accounts/services/customerUserService.ts +34 -10
  75. package/src/modules/customers/commands/companies.ts +16 -4
  76. package/src/modules/customers/commands/interactionRestoreDetails.ts +110 -0
  77. package/src/modules/customers/commands/people.ts +16 -4
  78. package/src/modules/forms/api/distributions/[distributionId]/invitations/[invitationId]/route.ts +1 -1
  79. package/src/modules/forms/api/form-submissions/[id]/submit/route.ts +16 -3
  80. package/src/modules/forms/api/form-submissions/route.ts +21 -2
  81. package/src/modules/forms/api/public/submissions/[id]/submit/route.ts +7 -0
  82. package/src/modules/forms/api/runtime-helpers.ts +16 -0
  83. package/src/modules/forms/data/validators.ts +2 -1
  84. package/src/modules/forms/i18n/de.json +1 -0
  85. package/src/modules/forms/i18n/en.json +1 -0
  86. package/src/modules/forms/i18n/es.json +1 -0
  87. package/src/modules/forms/i18n/ko.json +1 -0
  88. package/src/modules/forms/i18n/pl.json +1 -0
  89. package/src/modules/forms/lib/portal-subject-policy.ts +42 -0
  90. package/src/modules/forms/lib/runtime-principal.ts +35 -11
  91. package/src/modules/forms/services/distribution-service.ts +35 -13
  92. package/src/modules/forms/services/submission-service.ts +12 -0
  93. package/src/modules/staff/commands/leave-requests.ts +54 -23
  94. package/src/modules/staff/i18n/de.json +2 -0
  95. package/src/modules/staff/i18n/en.json +2 -0
  96. package/src/modules/staff/i18n/es.json +2 -0
  97. package/src/modules/staff/i18n/ko.json +2 -0
  98. package/src/modules/staff/i18n/pl.json +2 -0
  99. package/src/modules/warranty_claims/api/portal/attachments/route.ts +20 -8
  100. package/src/modules/warranty_claims/commands/claims.ts +1 -1
  101. package/src/modules/warranty_claims/subscribers/return-shipment-tracking.ts +5 -5
  102. package/src/modules/wms/commands/inventory-actions.ts +30 -10
  103. package/src/modules/workflows/lib/async-activity-wait.ts +71 -0
  104. package/src/modules/workflows/lib/parallel-handler.ts +24 -15
  105. package/src/modules/workflows/lib/workflow-executor.ts +24 -27
@@ -8,7 +8,7 @@ import { withAtomicFlush } from "@open-mercato/shared/lib/commands/flush";
8
8
  import { invalidateCrudCache } from "@open-mercato/shared/lib/crud/cache";
9
9
  import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
10
10
  import { PlannerAvailabilityRule } from "@open-mercato/core/modules/planner/data/entities";
11
- import { StaffLeaveRequest } from "../data/entities.js";
11
+ import { StaffLeaveRequest, StaffTeamMember } from "../data/entities.js";
12
12
  import {
13
13
  staffLeaveRequestCreateSchema,
14
14
  staffLeaveRequestDecisionSchema,
@@ -20,6 +20,7 @@ import {
20
20
  commandInputScope,
21
21
  ensureOrganizationScope,
22
22
  ensureTenantScope,
23
+ explicitStaffCommandScope,
23
24
  extractUndoPayload,
24
25
  requireTeamMember,
25
26
  scopeForDecryption,
@@ -158,6 +159,34 @@ function ensurePendingStatus(request) {
158
159
  throw new CrudHttpError(400, { error: "Leave request is already finalized." });
159
160
  }
160
161
  }
162
+ async function ensureUndoablePending(request) {
163
+ if (request.status === "pending") return;
164
+ const { translate } = await resolveTranslations();
165
+ throw new CrudHttpError(409, {
166
+ error: translate(
167
+ "staff.leaveRequests.errors.undoAfterDecision",
168
+ "This leave request has already been approved or rejected and can no longer be undone. Undo the decision first."
169
+ )
170
+ });
171
+ }
172
+ async function requireUndoMember(em, snapshot) {
173
+ const scope = explicitStaffCommandScope(snapshot.tenantId, snapshot.organizationId);
174
+ const member = await findOneWithDecryption(
175
+ em,
176
+ StaffTeamMember,
177
+ applyScopeToWhere({ id: snapshot.memberId, deletedAt: null }, scope),
178
+ void 0,
179
+ scopeForDecryption(scope)
180
+ );
181
+ if (member) return member;
182
+ const { translate } = await resolveTranslations();
183
+ throw new CrudHttpError(409, {
184
+ error: translate(
185
+ "staff.leaveRequests.errors.undoMemberMissing",
186
+ "The original team member of this leave request no longer exists, so the change cannot be undone."
187
+ )
188
+ });
189
+ }
161
190
  async function createUnavailabilityRules(params) {
162
191
  const now = /* @__PURE__ */ new Date();
163
192
  const createdIds = [];
@@ -310,24 +339,24 @@ const createLeaveRequestCommand = {
310
339
  if (!after) return;
311
340
  const em = ctx.container.resolve("em").fork();
312
341
  const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(after.id, staffSnapshotScopeFromSnapshot(after)));
313
- if (request) {
314
- request.deletedAt = /* @__PURE__ */ new Date();
315
- request.updatedAt = /* @__PURE__ */ new Date();
316
- await em.flush();
317
- const de = ctx.container.resolve("dataEngine");
318
- await emitCrudUndoSideEffects({
319
- dataEngine: de,
320
- action: "deleted",
321
- entity: request,
322
- identifiers: {
323
- id: request.id,
324
- organizationId: request.organizationId,
325
- tenantId: request.tenantId
326
- },
327
- events: staffLeaveRequestCrudEvents,
328
- indexer: leaveRequestCrudIndexer
329
- });
330
- }
342
+ if (!request || request.deletedAt) return;
343
+ await ensureUndoablePending(request);
344
+ request.deletedAt = /* @__PURE__ */ new Date();
345
+ request.updatedAt = /* @__PURE__ */ new Date();
346
+ await em.flush();
347
+ const de = ctx.container.resolve("dataEngine");
348
+ await emitCrudUndoSideEffects({
349
+ dataEngine: de,
350
+ action: "deleted",
351
+ entity: request,
352
+ identifiers: {
353
+ id: request.id,
354
+ organizationId: request.organizationId,
355
+ tenantId: request.tenantId
356
+ },
357
+ events: staffLeaveRequestCrudEvents,
358
+ indexer: leaveRequestCrudIndexer
359
+ });
331
360
  },
332
361
  redo: makeCreateRedo({
333
362
  entityClass: StaffLeaveRequest,
@@ -431,17 +460,18 @@ const updateLeaveRequestCommand = {
431
460
  if (!before || !after) return;
432
461
  const em = ctx.container.resolve("em").fork();
433
462
  const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(after.id, staffSnapshotScopeFromSnapshot(after)));
434
- if (!request) return;
463
+ if (!request || request.deletedAt) return;
464
+ await ensureUndoablePending(request);
465
+ const currentMemberId = typeof request.member === "string" ? request.member : request.member.id;
466
+ if (before.memberId !== currentMemberId) {
467
+ request.member = await requireUndoMember(em, before);
468
+ }
435
469
  request.startDate = new Date(before.startDate);
436
470
  request.endDate = new Date(before.endDate);
437
471
  request.timezone = before.timezone;
438
- request.status = before.status;
439
472
  request.unavailabilityReasonEntryId = before.unavailabilityReasonEntryId;
440
473
  request.unavailabilityReasonValue = before.unavailabilityReasonValue;
441
474
  request.note = before.note;
442
- request.decisionComment = before.decisionComment;
443
- request.decidedByUserId = before.decidedByUserId;
444
- request.decidedAt = before.decidedAt ? new Date(before.decidedAt) : null;
445
475
  request.updatedAt = /* @__PURE__ */ new Date();
446
476
  await em.flush();
447
477
  const de = ctx.container.resolve("dataEngine");
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/staff/commands/leave-requests.ts"],
4
- "sourcesContent": ["import { randomUUID } from 'crypto'\nimport type { CommandHandler, CommandRuntimeContext } from '@open-mercato/shared/lib/commands'\nimport { registerCommand } from '@open-mercato/shared/lib/commands'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { emitCrudSideEffects, emitCrudUndoSideEffects, buildChanges } from '@open-mercato/shared/lib/commands/helpers'\nimport { makeCreateRedo } from '@open-mercato/shared/lib/commands/redo'\nimport { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'\nimport type { CrudIndexerConfig } from '@open-mercato/shared/lib/crud/types'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { invalidateCrudCache } from '@open-mercato/shared/lib/crud/cache'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { PlannerAvailabilityRule } from '@open-mercato/core/modules/planner/data/entities'\nimport { StaffLeaveRequest, type StaffLeaveRequestStatus } from '../data/entities'\nimport {\n staffLeaveRequestCreateSchema,\n staffLeaveRequestDecisionSchema,\n staffLeaveRequestUpdateSchema,\n type StaffLeaveRequestCreateInput,\n type StaffLeaveRequestDecisionInput,\n type StaffLeaveRequestUpdateInput,\n} from '../data/validators'\nimport {\n applyScopeToWhere,\n commandActorScope,\n commandInputScope,\n ensureOrganizationScope,\n ensureTenantScope,\n explicitStaffCommandScope,\n extractUndoPayload,\n requireTeamMember,\n scopeForDecryption,\n scopedStaffSnapshotWhere,\n staffSnapshotDecryptionScope,\n staffSnapshotScopeFromContext,\n staffSnapshotScopeFromSnapshot,\n type StaffSnapshotScope,\n} from './shared'\nimport { E } from '#generated/entities.ids.generated'\nimport { resolveNotificationService } from '../../notifications/lib/notificationService'\nimport { buildFeatureNotificationFromType, buildNotificationFromType } from '../../notifications/lib/notificationBuilder'\nimport { notificationTypes } from '../notifications'\nimport { staffLeaveRequestCrudEvents } from '../lib/crud'\nimport { plannerAvailabilityRuleCrudEvents } from '../../planner/lib/crud'\n\nconst leaveRequestCrudIndexer: CrudIndexerConfig<StaffLeaveRequest> = {\n entityType: E.staff.staff_leave_request,\n}\n\nconst availabilityRuleCrudIndexer: CrudIndexerConfig<PlannerAvailabilityRule> = {\n entityType: E.planner.planner_availability_rule,\n cacheAliases: ['planner.availability'],\n}\n\ntype LeaveRequestSnapshot = {\n id: string\n tenantId: string\n organizationId: string\n memberId: string\n startDate: string\n endDate: string\n timezone: string\n status: StaffLeaveRequestStatus\n unavailabilityReasonEntryId: string | null\n unavailabilityReasonValue: string | null\n note: string | null\n decisionComment: string | null\n submittedByUserId: string | null\n decidedByUserId: string | null\n decidedAt: string | null\n deletedAt: string | null\n createdAt: string | null\n updatedAt: string | null\n}\n\ntype LeaveRequestUndoPayload = {\n before?: LeaveRequestSnapshot | null\n after?: LeaveRequestSnapshot | null\n availabilityRuleIds?: string[]\n}\n\nfunction parseUuidCandidate(value: string | null | undefined): string | null {\n if (!value) return null\n const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/\n return uuidRegex.test(value) ? value : null\n}\n\nfunction resolveAuthUserId(ctx: { auth?: { sub?: string | null; isApiKey?: boolean } | null }): string | null {\n if (!ctx.auth || ctx.auth.isApiKey) return null\n return parseUuidCandidate(ctx.auth.sub ?? null)\n}\n\nfunction formatDateKey(value: Date): string {\n const year = value.getUTCFullYear()\n const month = String(value.getUTCMonth() + 1).padStart(2, '0')\n const day = String(value.getUTCDate()).padStart(2, '0')\n return `${year}-${month}-${day}`\n}\n\nfunction listDateKeysInRange(start: Date, end: Date): string[] {\n const dates: string[] = []\n const current = new Date(Date.UTC(start.getUTCFullYear(), start.getUTCMonth(), start.getUTCDate()))\n const last = new Date(Date.UTC(end.getUTCFullYear(), end.getUTCMonth(), end.getUTCDate()))\n while (current <= last) {\n dates.push(formatDateKey(current))\n current.setUTCDate(current.getUTCDate() + 1)\n }\n return dates\n}\n\nfunction formatDuration(minutes: number): string {\n const clamped = Math.max(1, minutes)\n const hours = Math.floor(clamped / 60)\n const mins = clamped % 60\n if (hours > 0 && mins > 0) return `PT${hours}H${mins}M`\n if (hours > 0) return `PT${hours}H`\n return `PT${mins}M`\n}\n\nfunction buildAvailabilityRrule(start: Date, end: Date): string {\n const dtStart = start.toISOString().replace(/[-:]/g, '').split('.')[0] + 'Z'\n const durationMinutes = Math.max(1, Math.round((end.getTime() - start.getTime()) / 60000))\n const duration = formatDuration(durationMinutes)\n return `DTSTART:${dtStart}\\nDURATION:${duration}\\nRRULE:FREQ=DAILY;COUNT=1`\n}\n\nfunction buildFullDayRrule(date: string): string | null {\n const [year, month, day] = date.split('-').map((part) => Number(part))\n if (!Number.isFinite(year) || !Number.isFinite(month) || !Number.isFinite(day)) return null\n const start = new Date(Date.UTC(year, month - 1, day, 0, 0, 0))\n const end = new Date(start.getTime() + 24 * 60 * 60 * 1000)\n return buildAvailabilityRrule(start, end)\n}\n\nasync function loadLeaveRequestSnapshot(em: EntityManager, id: string, scope?: StaffSnapshotScope | null): Promise<LeaveRequestSnapshot | null> {\n const request = await findOneWithDecryption(\n em,\n StaffLeaveRequest,\n scopedStaffSnapshotWhere(id, scope),\n undefined,\n staffSnapshotDecryptionScope(scope),\n )\n if (!request) return null\n const memberId = typeof request.member === 'string' ? request.member : request.member.id\n return {\n id: request.id,\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n memberId,\n startDate: request.startDate.toISOString(),\n endDate: request.endDate.toISOString(),\n timezone: request.timezone,\n status: request.status,\n unavailabilityReasonEntryId: request.unavailabilityReasonEntryId ?? null,\n unavailabilityReasonValue: request.unavailabilityReasonValue ?? null,\n note: request.note ?? null,\n decisionComment: request.decisionComment ?? null,\n submittedByUserId: request.submittedByUserId ?? null,\n decidedByUserId: request.decidedByUserId ?? null,\n decidedAt: request.decidedAt ? request.decidedAt.toISOString() : null,\n deletedAt: request.deletedAt ? request.deletedAt.toISOString() : null,\n createdAt: request.createdAt ? request.createdAt.toISOString() : null,\n updatedAt: request.updatedAt ? request.updatedAt.toISOString() : null,\n }\n}\n\nfunction leaveRequestSeedFromSnapshot(snapshot: LeaveRequestSnapshot): Record<string, unknown> {\n return {\n id: snapshot.id,\n tenantId: snapshot.tenantId,\n organizationId: snapshot.organizationId,\n member: snapshot.memberId,\n startDate: new Date(snapshot.startDate),\n endDate: new Date(snapshot.endDate),\n timezone: snapshot.timezone,\n status: snapshot.status,\n unavailabilityReasonEntryId: snapshot.unavailabilityReasonEntryId ?? null,\n unavailabilityReasonValue: snapshot.unavailabilityReasonValue ?? null,\n note: snapshot.note ?? null,\n decisionComment: snapshot.decisionComment ?? null,\n submittedByUserId: snapshot.submittedByUserId ?? null,\n decidedByUserId: snapshot.decidedByUserId ?? null,\n decidedAt: snapshot.decidedAt ? new Date(snapshot.decidedAt) : null,\n createdAt: snapshot.createdAt ? new Date(snapshot.createdAt) : new Date(),\n updatedAt: new Date(),\n deletedAt: null,\n }\n}\n\nasync function requireLeaveRequest(\n em: EntityManager,\n id: string,\n scope: ReturnType<typeof explicitStaffCommandScope>,\n): Promise<StaffLeaveRequest> {\n const request = await findOneWithDecryption(\n em,\n StaffLeaveRequest,\n applyScopeToWhere<StaffLeaveRequest>({ id, deletedAt: null }, scope),\n undefined,\n scopeForDecryption(scope),\n )\n if (!request) throw new CrudHttpError(404, { error: 'Leave request not found.' })\n return request\n}\n\nfunction ensurePendingStatus(request: StaffLeaveRequest): void {\n if (request.status !== 'pending') {\n throw new CrudHttpError(400, { error: 'Leave request is already finalized.' })\n }\n}\n\nasync function createUnavailabilityRules(params: {\n em: EntityManager\n tenantId: string\n organizationId: string\n memberId: string\n timezone: string\n dates: string[]\n note: string | null\n reasonEntryId: string | null\n reasonValue: string | null\n}): Promise<string[]> {\n const now = new Date()\n const createdIds: string[] = []\n params.dates.forEach((date) => {\n const rrule = buildFullDayRrule(date)\n if (!rrule) return\n const ruleId = randomUUID()\n const rule = params.em.create(PlannerAvailabilityRule, {\n id: ruleId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n subjectType: 'member',\n subjectId: params.memberId,\n timezone: params.timezone,\n rrule,\n exdates: [],\n kind: 'unavailability',\n note: params.note,\n unavailabilityReasonEntryId: params.reasonEntryId,\n unavailabilityReasonValue: params.reasonValue,\n createdAt: now,\n updatedAt: now,\n deletedAt: null,\n })\n params.em.persist(rule)\n createdIds.push(ruleId)\n })\n return createdIds\n}\n\nasync function invalidateAvailabilityCache(params: {\n container: CommandRuntimeContext['container']\n tenantId: string | null\n organizationId: string | null\n ruleIds: string[]\n}) {\n if (!params.ruleIds.length) return\n const resource = 'planner.availability'\n const fallbackTenant = params.tenantId ?? null\n for (const ruleId of params.ruleIds) {\n await invalidateCrudCache(\n params.container,\n resource,\n { id: ruleId, organizationId: params.organizationId, tenantId: params.tenantId },\n fallbackTenant,\n 'updated',\n )\n }\n}\n\nconst createLeaveRequestCommand: CommandHandler<StaffLeaveRequestCreateInput, { requestId: string }> = {\n id: 'staff.leave-requests.create',\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestCreateSchema.parse(rawInput)\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n const scope = commandInputScope(ctx, parsed.tenantId, parsed.organizationId)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const member = await requireTeamMember(\n em,\n parsed.memberId,\n scope,\n 'Team member not found',\n )\n ensureTenantScope(ctx, member.tenantId)\n ensureOrganizationScope(ctx, member.organizationId)\n\n const submittedByUserId = parsed.submittedByUserId ?? resolveAuthUserId(ctx)\n const now = new Date()\n const request = em.create(StaffLeaveRequest, {\n tenantId: parsed.tenantId,\n organizationId: parsed.organizationId,\n member,\n startDate: parsed.startDate,\n endDate: parsed.endDate,\n timezone: parsed.timezone,\n status: 'pending',\n unavailabilityReasonEntryId: parsed.unavailabilityReasonEntryId ?? null,\n unavailabilityReasonValue: parsed.unavailabilityReasonValue ?? null,\n note: parsed.note ?? null,\n decisionComment: null,\n submittedByUserId,\n decidedByUserId: null,\n decidedAt: null,\n createdAt: now,\n updatedAt: now,\n deletedAt: null,\n })\n em.persist(request)\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n // Create notification for users who can approve/reject leave requests\n try {\n const notificationService = resolveNotificationService(ctx.container)\n const typeDef = notificationTypes.find((type) => type.type === 'staff.leave_request.pending')\n if (typeDef) {\n const memberName = member.displayName || 'Team member'\n const startDateStr = request.startDate.toLocaleDateString()\n const endDateStr = request.endDate.toLocaleDateString()\n\n const notificationInput = buildFeatureNotificationFromType(typeDef, {\n requiredFeature: 'staff.leave_requests.manage',\n bodyVariables: {\n memberName,\n startDate: startDateStr,\n endDate: endDateStr,\n },\n sourceEntityType: 'staff:leave_request',\n sourceEntityId: request.id,\n linkHref: `/backend/staff/leave-requests/${request.id}`,\n })\n\n await notificationService.createForFeature(notificationInput, {\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n })\n }\n } catch {\n // Notification creation is non-critical, don't fail the command\n }\n\n return { requestId: request.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return await loadLeaveRequestSnapshot(em, result.requestId, staffSnapshotScopeFromContext(ctx))\n },\n buildLog: async ({ result, ctx }) => {\n const { translate } = await resolveTranslations()\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const snapshot = await loadLeaveRequestSnapshot(em, result.requestId, staffSnapshotScopeFromContext(ctx))\n return {\n actionLabel: translate('staff.audit.leaveRequests.create', 'Create leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: result.requestId,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: snapshot?.memberId ?? null,\n tenantId: snapshot?.tenantId ?? null,\n organizationId: snapshot?.organizationId ?? null,\n snapshotAfter: snapshot ?? null,\n payload: {\n undo: {\n after: snapshot ?? null,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(after.id, staffSnapshotScopeFromSnapshot(after)))\n if (request) {\n request.deletedAt = new Date()\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n }\n },\n redo: makeCreateRedo<StaffLeaveRequest, LeaveRequestSnapshot, StaffLeaveRequestCreateInput, { requestId: string }>({\n entityClass: StaffLeaveRequest,\n getSnapshotId: (snapshot) => snapshot.id,\n seedFromSnapshot: leaveRequestSeedFromSnapshot,\n findRow: ({ em, id, snapshot }) =>\n findOneWithDecryption(\n em,\n StaffLeaveRequest,\n scopedStaffSnapshotWhere(id, staffSnapshotScopeFromSnapshot(snapshot)),\n undefined,\n { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId },\n ),\n buildResult: (entity) => ({ requestId: entity.id }),\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n }),\n}\n\nconst updateLeaveRequestCommand: CommandHandler<StaffLeaveRequestUpdateInput, { requestId: string }> = {\n id: 'staff.leave-requests.update',\n async prepare(rawInput, ctx) {\n const parsed = staffLeaveRequestUpdateSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadLeaveRequestSnapshot(em, parsed.id, staffSnapshotScopeFromContext(ctx))\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestUpdateSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const scope = commandActorScope(ctx)\n const request = await requireLeaveRequest(em, parsed.id, scope)\n ensureTenantScope(ctx, request.tenantId)\n ensureOrganizationScope(ctx, request.organizationId)\n ensurePendingStatus(request)\n\n if (parsed.memberId !== undefined) {\n const member = await requireTeamMember(em, parsed.memberId, scope, 'Team member not found')\n ensureTenantScope(ctx, member.tenantId)\n ensureOrganizationScope(ctx, member.organizationId)\n request.member = member\n }\n if (parsed.startDate !== undefined) request.startDate = parsed.startDate\n if (parsed.endDate !== undefined) request.endDate = parsed.endDate\n if (parsed.timezone !== undefined) request.timezone = parsed.timezone\n if (parsed.unavailabilityReasonEntryId !== undefined) request.unavailabilityReasonEntryId = parsed.unavailabilityReasonEntryId ?? null\n if (parsed.unavailabilityReasonValue !== undefined) request.unavailabilityReasonValue = parsed.unavailabilityReasonValue ?? null\n if (parsed.note !== undefined) request.note = parsed.note ?? null\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n return { requestId: request.id }\n },\n buildLog: async ({ snapshots, ctx }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as LeaveRequestSnapshot | undefined\n if (!before) return null\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const after = await loadLeaveRequestSnapshot(em, before.id, staffSnapshotScopeFromSnapshot(before))\n const changes = after\n ? buildChanges(before as unknown as Record<string, unknown>, after as unknown as Record<string, unknown>, [\n 'memberId',\n 'startDate',\n 'endDate',\n 'timezone',\n 'unavailabilityReasonEntryId',\n 'unavailabilityReasonValue',\n 'note',\n ])\n : {}\n return {\n actionLabel: translate('staff.audit.leaveRequests.update', 'Update leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: before.id,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: before.memberId ?? null,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n snapshotAfter: after ?? null,\n changes,\n payload: {\n undo: {\n before,\n after: after ?? null,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const before = payload?.before\n const after = payload?.after\n if (!before || !after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(after.id, staffSnapshotScopeFromSnapshot(after)))\n if (!request) return\n request.startDate = new Date(before.startDate)\n request.endDate = new Date(before.endDate)\n request.timezone = before.timezone\n request.status = before.status\n request.unavailabilityReasonEntryId = before.unavailabilityReasonEntryId\n request.unavailabilityReasonValue = before.unavailabilityReasonValue\n request.note = before.note\n request.decisionComment = before.decisionComment\n request.decidedByUserId = before.decidedByUserId\n request.decidedAt = before.decidedAt ? new Date(before.decidedAt) : null\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n },\n}\n\nconst deleteLeaveRequestCommand: CommandHandler<{ id: string }, { requestId: string }> = {\n id: 'staff.leave-requests.delete',\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.pick({ id: true }).parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await requireLeaveRequest(em, parsed.id, commandActorScope(ctx))\n ensureTenantScope(ctx, request.tenantId)\n ensureOrganizationScope(ctx, request.organizationId)\n ensurePendingStatus(request)\n\n request.deletedAt = new Date()\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n return { requestId: request.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return await loadLeaveRequestSnapshot(em, result.requestId, staffSnapshotScopeFromContext(ctx))\n },\n buildLog: async ({ result, ctx }) => {\n const { translate } = await resolveTranslations()\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const snapshot = await loadLeaveRequestSnapshot(em, result.requestId, staffSnapshotScopeFromContext(ctx))\n return {\n actionLabel: translate('staff.audit.leaveRequests.delete', 'Delete leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: result.requestId,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: snapshot?.memberId ?? null,\n tenantId: snapshot?.tenantId ?? null,\n organizationId: snapshot?.organizationId ?? null,\n snapshotAfter: snapshot ?? null,\n payload: {\n undo: {\n after: snapshot ?? null,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(after.id, staffSnapshotScopeFromSnapshot(after)))\n if (!request) return\n request.deletedAt = null\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'created',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n },\n}\n\nconst acceptLeaveRequestCommand: CommandHandler<StaffLeaveRequestDecisionInput, { requestId: string; ruleIds: string[] }> = {\n id: 'staff.leave-requests.accept',\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await requireLeaveRequest(em, parsed.id, commandActorScope(ctx))\n ensureTenantScope(ctx, request.tenantId)\n ensureOrganizationScope(ctx, request.organizationId)\n ensurePendingStatus(request)\n\n const memberId = typeof request.member === 'string' ? request.member : request.member.id\n const decidedByUserId = parsed.decidedByUserId ?? resolveAuthUserId(ctx)\n const now = new Date()\n const dates = listDateKeysInRange(request.startDate, request.endDate)\n let createdRuleIds: string[] = []\n\n await em.transactional(async (trx) => {\n request.status = 'approved'\n request.decisionComment = parsed.decisionComment ?? null\n request.decidedByUserId = decidedByUserId\n request.decidedAt = now\n request.updatedAt = now\n trx.persist(request)\n\n createdRuleIds = await createUnavailabilityRules({\n em: trx,\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n memberId,\n timezone: request.timezone,\n dates,\n note: request.note ?? null,\n reasonEntryId: request.unavailabilityReasonEntryId ?? null,\n reasonValue: request.unavailabilityReasonValue ?? null,\n })\n await trx.flush()\n })\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n if (createdRuleIds.length) {\n const rules = await em.find(PlannerAvailabilityRule, { id: { $in: createdRuleIds } })\n for (const rule of rules) {\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: rule,\n identifiers: {\n id: rule.id,\n organizationId: rule.organizationId,\n tenantId: rule.tenantId,\n },\n events: plannerAvailabilityRuleCrudEvents,\n indexer: availabilityRuleCrudIndexer,\n })\n }\n }\n await invalidateAvailabilityCache({\n container: ctx.container,\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n ruleIds: createdRuleIds,\n })\n\n // Send notification to the requester\n if (request.submittedByUserId) {\n try {\n const notificationService = resolveNotificationService(ctx.container)\n const typeDef = notificationTypes.find((type) => type.type === 'staff.leave_request.approved')\n if (typeDef) {\n const startDateStr = request.startDate.toLocaleDateString()\n const endDateStr = request.endDate.toLocaleDateString()\n\n const notificationInput = buildNotificationFromType(typeDef, {\n recipientUserId: request.submittedByUserId,\n bodyVariables: {\n startDate: startDateStr,\n endDate: endDateStr,\n },\n sourceEntityType: 'staff:leave_request',\n sourceEntityId: request.id,\n linkHref: `/backend/staff/leave-requests/${request.id}`,\n })\n\n await notificationService.create(notificationInput, {\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n })\n }\n } catch {\n // Notification creation is non-critical, don't fail the command\n }\n }\n\n return { requestId: request.id, ruleIds: createdRuleIds }\n },\n buildLog: async ({ result, ctx, snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as LeaveRequestSnapshot | undefined\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const after = await loadLeaveRequestSnapshot(\n em,\n result.requestId,\n staffSnapshotScopeFromSnapshot(before) ?? staffSnapshotScopeFromContext(ctx),\n )\n return {\n actionLabel: translate('staff.audit.leaveRequests.accept', 'Approve leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: result.requestId,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: after?.memberId ?? before?.memberId ?? null,\n tenantId: after?.tenantId ?? before?.tenantId ?? null,\n organizationId: after?.organizationId ?? before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n snapshotAfter: after ?? null,\n payload: {\n undo: {\n before: before ?? null,\n after: after ?? null,\n availabilityRuleIds: result.ruleIds,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n async prepare(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadLeaveRequestSnapshot(em, parsed.id, staffSnapshotScopeFromContext(ctx))\n return snapshot ? { before: snapshot } : {}\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const before = payload?.before\n const availabilityRuleIds = payload?.availabilityRuleIds ?? []\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(before.id, staffSnapshotScopeFromSnapshot(before)))\n if (!request) return\n\n // Fetch rules before mutating so the query cannot reset unit-of-work tracking\n // between the request scalar mutations and the flush (SPEC-018 Problem 1).\n const rules = availabilityRuleIds.length\n ? await em.find(PlannerAvailabilityRule, { id: { $in: availabilityRuleIds } })\n : []\n\n await withAtomicFlush(em, [\n () => {\n request.status = before.status\n request.decisionComment = before.decisionComment\n request.decidedByUserId = before.decidedByUserId\n request.decidedAt = before.decidedAt ? new Date(before.decidedAt) : null\n request.updatedAt = new Date()\n\n if (rules.length) {\n const now = new Date()\n rules.forEach((rule) => {\n rule.deletedAt = now\n rule.updatedAt = now\n })\n }\n },\n ], { transaction: true })\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n for (const rule of rules) {\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: rule,\n identifiers: {\n id: rule.id,\n organizationId: rule.organizationId,\n tenantId: rule.tenantId,\n },\n events: plannerAvailabilityRuleCrudEvents,\n indexer: availabilityRuleCrudIndexer,\n })\n }\n await invalidateAvailabilityCache({\n container: ctx.container,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n ruleIds: availabilityRuleIds,\n })\n },\n}\n\nconst rejectLeaveRequestCommand: CommandHandler<StaffLeaveRequestDecisionInput, { requestId: string }> = {\n id: 'staff.leave-requests.reject',\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await requireLeaveRequest(em, parsed.id, commandActorScope(ctx))\n ensureTenantScope(ctx, request.tenantId)\n ensureOrganizationScope(ctx, request.organizationId)\n ensurePendingStatus(request)\n\n request.status = 'rejected'\n request.decisionComment = parsed.decisionComment ?? null\n request.decidedByUserId = parsed.decidedByUserId ?? resolveAuthUserId(ctx)\n request.decidedAt = new Date()\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n // Send notification to the requester\n if (request.submittedByUserId) {\n try {\n const notificationService = resolveNotificationService(ctx.container)\n const typeDef = notificationTypes.find((type) => type.type === 'staff.leave_request.rejected')\n if (typeDef) {\n const startDateStr = request.startDate.toLocaleDateString()\n const endDateStr = request.endDate.toLocaleDateString()\n\n const notificationInput = buildNotificationFromType(typeDef, {\n recipientUserId: request.submittedByUserId,\n bodyVariables: {\n startDate: startDateStr,\n endDate: endDateStr,\n reason: request.decisionComment ?? '',\n },\n sourceEntityType: 'staff:leave_request',\n sourceEntityId: request.id,\n linkHref: `/backend/staff/leave-requests/${request.id}`,\n })\n\n await notificationService.create(notificationInput, {\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n })\n }\n } catch {\n // Notification creation is non-critical, don't fail the command\n }\n }\n\n return { requestId: request.id }\n },\n async prepare(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadLeaveRequestSnapshot(em, parsed.id, staffSnapshotScopeFromContext(ctx))\n return snapshot ? { before: snapshot } : {}\n },\n buildLog: async ({ result, ctx, snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as LeaveRequestSnapshot | undefined\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const after = await loadLeaveRequestSnapshot(\n em,\n result.requestId,\n staffSnapshotScopeFromSnapshot(before) ?? staffSnapshotScopeFromContext(ctx),\n )\n return {\n actionLabel: translate('staff.audit.leaveRequests.reject', 'Reject leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: result.requestId,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: after?.memberId ?? before?.memberId ?? null,\n tenantId: after?.tenantId ?? before?.tenantId ?? null,\n organizationId: after?.organizationId ?? before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n snapshotAfter: after ?? null,\n payload: {\n undo: {\n before: before ?? null,\n after: after ?? null,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(before.id, staffSnapshotScopeFromSnapshot(before)))\n if (!request) return\n request.status = before.status\n request.decisionComment = before.decisionComment\n request.decidedByUserId = before.decidedByUserId\n request.decidedAt = before.decidedAt ? new Date(before.decidedAt) : null\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n },\n}\n\nregisterCommand(createLeaveRequestCommand)\nregisterCommand(updateLeaveRequestCommand)\nregisterCommand(deleteLeaveRequestCommand)\nregisterCommand(acceptLeaveRequestCommand)\nregisterCommand(rejectLeaveRequestCommand)\n"],
5
- "mappings": "AAAA,SAAS,kBAAkB;AAE3B,SAAS,uBAAuB;AAEhC,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AACpC,SAAS,qBAAqB,yBAAyB,oBAAoB;AAC3E,SAAS,sBAAsB;AAC/B,SAAS,uBAAuB;AAGhC,SAAS,2BAA2B;AAC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B,yBAAyB;AACzC,gBAAgB,yBAAyB;AACzC,gBAAgB,yBAAyB;",
4
+ "sourcesContent": ["import { randomUUID } from 'crypto'\nimport type { CommandHandler, CommandRuntimeContext } from '@open-mercato/shared/lib/commands'\nimport { registerCommand } from '@open-mercato/shared/lib/commands'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { emitCrudSideEffects, emitCrudUndoSideEffects, buildChanges } from '@open-mercato/shared/lib/commands/helpers'\nimport { makeCreateRedo } from '@open-mercato/shared/lib/commands/redo'\nimport { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'\nimport type { CrudIndexerConfig } from '@open-mercato/shared/lib/crud/types'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { invalidateCrudCache } from '@open-mercato/shared/lib/crud/cache'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { PlannerAvailabilityRule } from '@open-mercato/core/modules/planner/data/entities'\nimport { StaffLeaveRequest, StaffTeamMember, type StaffLeaveRequestStatus } from '../data/entities'\nimport {\n staffLeaveRequestCreateSchema,\n staffLeaveRequestDecisionSchema,\n staffLeaveRequestUpdateSchema,\n type StaffLeaveRequestCreateInput,\n type StaffLeaveRequestDecisionInput,\n type StaffLeaveRequestUpdateInput,\n} from '../data/validators'\nimport {\n applyScopeToWhere,\n commandActorScope,\n commandInputScope,\n ensureOrganizationScope,\n ensureTenantScope,\n explicitStaffCommandScope,\n extractUndoPayload,\n requireTeamMember,\n scopeForDecryption,\n scopedStaffSnapshotWhere,\n staffSnapshotDecryptionScope,\n staffSnapshotScopeFromContext,\n staffSnapshotScopeFromSnapshot,\n type StaffSnapshotScope,\n} from './shared'\nimport { E } from '#generated/entities.ids.generated'\nimport { resolveNotificationService } from '../../notifications/lib/notificationService'\nimport { buildFeatureNotificationFromType, buildNotificationFromType } from '../../notifications/lib/notificationBuilder'\nimport { notificationTypes } from '../notifications'\nimport { staffLeaveRequestCrudEvents } from '../lib/crud'\nimport { plannerAvailabilityRuleCrudEvents } from '../../planner/lib/crud'\n\nconst leaveRequestCrudIndexer: CrudIndexerConfig<StaffLeaveRequest> = {\n entityType: E.staff.staff_leave_request,\n}\n\nconst availabilityRuleCrudIndexer: CrudIndexerConfig<PlannerAvailabilityRule> = {\n entityType: E.planner.planner_availability_rule,\n cacheAliases: ['planner.availability'],\n}\n\ntype LeaveRequestSnapshot = {\n id: string\n tenantId: string\n organizationId: string\n memberId: string\n startDate: string\n endDate: string\n timezone: string\n status: StaffLeaveRequestStatus\n unavailabilityReasonEntryId: string | null\n unavailabilityReasonValue: string | null\n note: string | null\n decisionComment: string | null\n submittedByUserId: string | null\n decidedByUserId: string | null\n decidedAt: string | null\n deletedAt: string | null\n createdAt: string | null\n updatedAt: string | null\n}\n\ntype LeaveRequestUndoPayload = {\n before?: LeaveRequestSnapshot | null\n after?: LeaveRequestSnapshot | null\n availabilityRuleIds?: string[]\n}\n\nfunction parseUuidCandidate(value: string | null | undefined): string | null {\n if (!value) return null\n const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/\n return uuidRegex.test(value) ? value : null\n}\n\nfunction resolveAuthUserId(ctx: { auth?: { sub?: string | null; isApiKey?: boolean } | null }): string | null {\n if (!ctx.auth || ctx.auth.isApiKey) return null\n return parseUuidCandidate(ctx.auth.sub ?? null)\n}\n\nfunction formatDateKey(value: Date): string {\n const year = value.getUTCFullYear()\n const month = String(value.getUTCMonth() + 1).padStart(2, '0')\n const day = String(value.getUTCDate()).padStart(2, '0')\n return `${year}-${month}-${day}`\n}\n\nfunction listDateKeysInRange(start: Date, end: Date): string[] {\n const dates: string[] = []\n const current = new Date(Date.UTC(start.getUTCFullYear(), start.getUTCMonth(), start.getUTCDate()))\n const last = new Date(Date.UTC(end.getUTCFullYear(), end.getUTCMonth(), end.getUTCDate()))\n while (current <= last) {\n dates.push(formatDateKey(current))\n current.setUTCDate(current.getUTCDate() + 1)\n }\n return dates\n}\n\nfunction formatDuration(minutes: number): string {\n const clamped = Math.max(1, minutes)\n const hours = Math.floor(clamped / 60)\n const mins = clamped % 60\n if (hours > 0 && mins > 0) return `PT${hours}H${mins}M`\n if (hours > 0) return `PT${hours}H`\n return `PT${mins}M`\n}\n\nfunction buildAvailabilityRrule(start: Date, end: Date): string {\n const dtStart = start.toISOString().replace(/[-:]/g, '').split('.')[0] + 'Z'\n const durationMinutes = Math.max(1, Math.round((end.getTime() - start.getTime()) / 60000))\n const duration = formatDuration(durationMinutes)\n return `DTSTART:${dtStart}\\nDURATION:${duration}\\nRRULE:FREQ=DAILY;COUNT=1`\n}\n\nfunction buildFullDayRrule(date: string): string | null {\n const [year, month, day] = date.split('-').map((part) => Number(part))\n if (!Number.isFinite(year) || !Number.isFinite(month) || !Number.isFinite(day)) return null\n const start = new Date(Date.UTC(year, month - 1, day, 0, 0, 0))\n const end = new Date(start.getTime() + 24 * 60 * 60 * 1000)\n return buildAvailabilityRrule(start, end)\n}\n\nasync function loadLeaveRequestSnapshot(em: EntityManager, id: string, scope?: StaffSnapshotScope | null): Promise<LeaveRequestSnapshot | null> {\n const request = await findOneWithDecryption(\n em,\n StaffLeaveRequest,\n scopedStaffSnapshotWhere(id, scope),\n undefined,\n staffSnapshotDecryptionScope(scope),\n )\n if (!request) return null\n const memberId = typeof request.member === 'string' ? request.member : request.member.id\n return {\n id: request.id,\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n memberId,\n startDate: request.startDate.toISOString(),\n endDate: request.endDate.toISOString(),\n timezone: request.timezone,\n status: request.status,\n unavailabilityReasonEntryId: request.unavailabilityReasonEntryId ?? null,\n unavailabilityReasonValue: request.unavailabilityReasonValue ?? null,\n note: request.note ?? null,\n decisionComment: request.decisionComment ?? null,\n submittedByUserId: request.submittedByUserId ?? null,\n decidedByUserId: request.decidedByUserId ?? null,\n decidedAt: request.decidedAt ? request.decidedAt.toISOString() : null,\n deletedAt: request.deletedAt ? request.deletedAt.toISOString() : null,\n createdAt: request.createdAt ? request.createdAt.toISOString() : null,\n updatedAt: request.updatedAt ? request.updatedAt.toISOString() : null,\n }\n}\n\nfunction leaveRequestSeedFromSnapshot(snapshot: LeaveRequestSnapshot): Record<string, unknown> {\n return {\n id: snapshot.id,\n tenantId: snapshot.tenantId,\n organizationId: snapshot.organizationId,\n member: snapshot.memberId,\n startDate: new Date(snapshot.startDate),\n endDate: new Date(snapshot.endDate),\n timezone: snapshot.timezone,\n status: snapshot.status,\n unavailabilityReasonEntryId: snapshot.unavailabilityReasonEntryId ?? null,\n unavailabilityReasonValue: snapshot.unavailabilityReasonValue ?? null,\n note: snapshot.note ?? null,\n decisionComment: snapshot.decisionComment ?? null,\n submittedByUserId: snapshot.submittedByUserId ?? null,\n decidedByUserId: snapshot.decidedByUserId ?? null,\n decidedAt: snapshot.decidedAt ? new Date(snapshot.decidedAt) : null,\n createdAt: snapshot.createdAt ? new Date(snapshot.createdAt) : new Date(),\n updatedAt: new Date(),\n deletedAt: null,\n }\n}\n\nasync function requireLeaveRequest(\n em: EntityManager,\n id: string,\n scope: ReturnType<typeof explicitStaffCommandScope>,\n): Promise<StaffLeaveRequest> {\n const request = await findOneWithDecryption(\n em,\n StaffLeaveRequest,\n applyScopeToWhere<StaffLeaveRequest>({ id, deletedAt: null }, scope),\n undefined,\n scopeForDecryption(scope),\n )\n if (!request) throw new CrudHttpError(404, { error: 'Leave request not found.' })\n return request\n}\n\nfunction ensurePendingStatus(request: StaffLeaveRequest): void {\n if (request.status !== 'pending') {\n throw new CrudHttpError(400, { error: 'Leave request is already finalized.' })\n }\n}\n\nasync function ensureUndoablePending(request: StaffLeaveRequest): Promise<void> {\n if (request.status === 'pending') return\n const { translate } = await resolveTranslations()\n throw new CrudHttpError(409, {\n error: translate(\n 'staff.leaveRequests.errors.undoAfterDecision',\n 'This leave request has already been approved or rejected and can no longer be undone. Undo the decision first.',\n ),\n })\n}\n\nasync function requireUndoMember(em: EntityManager, snapshot: LeaveRequestSnapshot): Promise<StaffTeamMember> {\n const scope = explicitStaffCommandScope(snapshot.tenantId, snapshot.organizationId)\n const member = await findOneWithDecryption(\n em,\n StaffTeamMember,\n applyScopeToWhere<StaffTeamMember>({ id: snapshot.memberId, deletedAt: null }, scope),\n undefined,\n scopeForDecryption(scope),\n )\n if (member) return member\n const { translate } = await resolveTranslations()\n throw new CrudHttpError(409, {\n error: translate(\n 'staff.leaveRequests.errors.undoMemberMissing',\n 'The original team member of this leave request no longer exists, so the change cannot be undone.',\n ),\n })\n}\n\nasync function createUnavailabilityRules(params: {\n em: EntityManager\n tenantId: string\n organizationId: string\n memberId: string\n timezone: string\n dates: string[]\n note: string | null\n reasonEntryId: string | null\n reasonValue: string | null\n}): Promise<string[]> {\n const now = new Date()\n const createdIds: string[] = []\n params.dates.forEach((date) => {\n const rrule = buildFullDayRrule(date)\n if (!rrule) return\n const ruleId = randomUUID()\n const rule = params.em.create(PlannerAvailabilityRule, {\n id: ruleId,\n tenantId: params.tenantId,\n organizationId: params.organizationId,\n subjectType: 'member',\n subjectId: params.memberId,\n timezone: params.timezone,\n rrule,\n exdates: [],\n kind: 'unavailability',\n note: params.note,\n unavailabilityReasonEntryId: params.reasonEntryId,\n unavailabilityReasonValue: params.reasonValue,\n createdAt: now,\n updatedAt: now,\n deletedAt: null,\n })\n params.em.persist(rule)\n createdIds.push(ruleId)\n })\n return createdIds\n}\n\nasync function invalidateAvailabilityCache(params: {\n container: CommandRuntimeContext['container']\n tenantId: string | null\n organizationId: string | null\n ruleIds: string[]\n}) {\n if (!params.ruleIds.length) return\n const resource = 'planner.availability'\n const fallbackTenant = params.tenantId ?? null\n for (const ruleId of params.ruleIds) {\n await invalidateCrudCache(\n params.container,\n resource,\n { id: ruleId, organizationId: params.organizationId, tenantId: params.tenantId },\n fallbackTenant,\n 'updated',\n )\n }\n}\n\nconst createLeaveRequestCommand: CommandHandler<StaffLeaveRequestCreateInput, { requestId: string }> = {\n id: 'staff.leave-requests.create',\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestCreateSchema.parse(rawInput)\n ensureTenantScope(ctx, parsed.tenantId)\n ensureOrganizationScope(ctx, parsed.organizationId)\n const scope = commandInputScope(ctx, parsed.tenantId, parsed.organizationId)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const member = await requireTeamMember(\n em,\n parsed.memberId,\n scope,\n 'Team member not found',\n )\n ensureTenantScope(ctx, member.tenantId)\n ensureOrganizationScope(ctx, member.organizationId)\n\n const submittedByUserId = parsed.submittedByUserId ?? resolveAuthUserId(ctx)\n const now = new Date()\n const request = em.create(StaffLeaveRequest, {\n tenantId: parsed.tenantId,\n organizationId: parsed.organizationId,\n member,\n startDate: parsed.startDate,\n endDate: parsed.endDate,\n timezone: parsed.timezone,\n status: 'pending',\n unavailabilityReasonEntryId: parsed.unavailabilityReasonEntryId ?? null,\n unavailabilityReasonValue: parsed.unavailabilityReasonValue ?? null,\n note: parsed.note ?? null,\n decisionComment: null,\n submittedByUserId,\n decidedByUserId: null,\n decidedAt: null,\n createdAt: now,\n updatedAt: now,\n deletedAt: null,\n })\n em.persist(request)\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n // Create notification for users who can approve/reject leave requests\n try {\n const notificationService = resolveNotificationService(ctx.container)\n const typeDef = notificationTypes.find((type) => type.type === 'staff.leave_request.pending')\n if (typeDef) {\n const memberName = member.displayName || 'Team member'\n const startDateStr = request.startDate.toLocaleDateString()\n const endDateStr = request.endDate.toLocaleDateString()\n\n const notificationInput = buildFeatureNotificationFromType(typeDef, {\n requiredFeature: 'staff.leave_requests.manage',\n bodyVariables: {\n memberName,\n startDate: startDateStr,\n endDate: endDateStr,\n },\n sourceEntityType: 'staff:leave_request',\n sourceEntityId: request.id,\n linkHref: `/backend/staff/leave-requests/${request.id}`,\n })\n\n await notificationService.createForFeature(notificationInput, {\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n })\n }\n } catch {\n // Notification creation is non-critical, don't fail the command\n }\n\n return { requestId: request.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return await loadLeaveRequestSnapshot(em, result.requestId, staffSnapshotScopeFromContext(ctx))\n },\n buildLog: async ({ result, ctx }) => {\n const { translate } = await resolveTranslations()\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const snapshot = await loadLeaveRequestSnapshot(em, result.requestId, staffSnapshotScopeFromContext(ctx))\n return {\n actionLabel: translate('staff.audit.leaveRequests.create', 'Create leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: result.requestId,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: snapshot?.memberId ?? null,\n tenantId: snapshot?.tenantId ?? null,\n organizationId: snapshot?.organizationId ?? null,\n snapshotAfter: snapshot ?? null,\n payload: {\n undo: {\n after: snapshot ?? null,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(after.id, staffSnapshotScopeFromSnapshot(after)))\n if (!request || request.deletedAt) return\n await ensureUndoablePending(request)\n request.deletedAt = new Date()\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n },\n redo: makeCreateRedo<StaffLeaveRequest, LeaveRequestSnapshot, StaffLeaveRequestCreateInput, { requestId: string }>({\n entityClass: StaffLeaveRequest,\n getSnapshotId: (snapshot) => snapshot.id,\n seedFromSnapshot: leaveRequestSeedFromSnapshot,\n findRow: ({ em, id, snapshot }) =>\n findOneWithDecryption(\n em,\n StaffLeaveRequest,\n scopedStaffSnapshotWhere(id, staffSnapshotScopeFromSnapshot(snapshot)),\n undefined,\n { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId },\n ),\n buildResult: (entity) => ({ requestId: entity.id }),\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n }),\n}\n\nconst updateLeaveRequestCommand: CommandHandler<StaffLeaveRequestUpdateInput, { requestId: string }> = {\n id: 'staff.leave-requests.update',\n async prepare(rawInput, ctx) {\n const parsed = staffLeaveRequestUpdateSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadLeaveRequestSnapshot(em, parsed.id, staffSnapshotScopeFromContext(ctx))\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestUpdateSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const scope = commandActorScope(ctx)\n const request = await requireLeaveRequest(em, parsed.id, scope)\n ensureTenantScope(ctx, request.tenantId)\n ensureOrganizationScope(ctx, request.organizationId)\n ensurePendingStatus(request)\n\n if (parsed.memberId !== undefined) {\n const member = await requireTeamMember(em, parsed.memberId, scope, 'Team member not found')\n ensureTenantScope(ctx, member.tenantId)\n ensureOrganizationScope(ctx, member.organizationId)\n request.member = member\n }\n if (parsed.startDate !== undefined) request.startDate = parsed.startDate\n if (parsed.endDate !== undefined) request.endDate = parsed.endDate\n if (parsed.timezone !== undefined) request.timezone = parsed.timezone\n if (parsed.unavailabilityReasonEntryId !== undefined) request.unavailabilityReasonEntryId = parsed.unavailabilityReasonEntryId ?? null\n if (parsed.unavailabilityReasonValue !== undefined) request.unavailabilityReasonValue = parsed.unavailabilityReasonValue ?? null\n if (parsed.note !== undefined) request.note = parsed.note ?? null\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n return { requestId: request.id }\n },\n buildLog: async ({ snapshots, ctx }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as LeaveRequestSnapshot | undefined\n if (!before) return null\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const after = await loadLeaveRequestSnapshot(em, before.id, staffSnapshotScopeFromSnapshot(before))\n const changes = after\n ? buildChanges(before as unknown as Record<string, unknown>, after as unknown as Record<string, unknown>, [\n 'memberId',\n 'startDate',\n 'endDate',\n 'timezone',\n 'unavailabilityReasonEntryId',\n 'unavailabilityReasonValue',\n 'note',\n ])\n : {}\n return {\n actionLabel: translate('staff.audit.leaveRequests.update', 'Update leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: before.id,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: before.memberId ?? null,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n snapshotAfter: after ?? null,\n changes,\n payload: {\n undo: {\n before,\n after: after ?? null,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const before = payload?.before\n const after = payload?.after\n if (!before || !after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(after.id, staffSnapshotScopeFromSnapshot(after)))\n if (!request || request.deletedAt) return\n await ensureUndoablePending(request)\n const currentMemberId = typeof request.member === 'string' ? request.member : request.member.id\n if (before.memberId !== currentMemberId) {\n request.member = await requireUndoMember(em, before)\n }\n request.startDate = new Date(before.startDate)\n request.endDate = new Date(before.endDate)\n request.timezone = before.timezone\n request.unavailabilityReasonEntryId = before.unavailabilityReasonEntryId\n request.unavailabilityReasonValue = before.unavailabilityReasonValue\n request.note = before.note\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n },\n}\n\nconst deleteLeaveRequestCommand: CommandHandler<{ id: string }, { requestId: string }> = {\n id: 'staff.leave-requests.delete',\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.pick({ id: true }).parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await requireLeaveRequest(em, parsed.id, commandActorScope(ctx))\n ensureTenantScope(ctx, request.tenantId)\n ensureOrganizationScope(ctx, request.organizationId)\n ensurePendingStatus(request)\n\n request.deletedAt = new Date()\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n return { requestId: request.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return await loadLeaveRequestSnapshot(em, result.requestId, staffSnapshotScopeFromContext(ctx))\n },\n buildLog: async ({ result, ctx }) => {\n const { translate } = await resolveTranslations()\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const snapshot = await loadLeaveRequestSnapshot(em, result.requestId, staffSnapshotScopeFromContext(ctx))\n return {\n actionLabel: translate('staff.audit.leaveRequests.delete', 'Delete leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: result.requestId,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: snapshot?.memberId ?? null,\n tenantId: snapshot?.tenantId ?? null,\n organizationId: snapshot?.organizationId ?? null,\n snapshotAfter: snapshot ?? null,\n payload: {\n undo: {\n after: snapshot ?? null,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const after = payload?.after\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(after.id, staffSnapshotScopeFromSnapshot(after)))\n if (!request) return\n request.deletedAt = null\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'created',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n },\n}\n\nconst acceptLeaveRequestCommand: CommandHandler<StaffLeaveRequestDecisionInput, { requestId: string; ruleIds: string[] }> = {\n id: 'staff.leave-requests.accept',\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await requireLeaveRequest(em, parsed.id, commandActorScope(ctx))\n ensureTenantScope(ctx, request.tenantId)\n ensureOrganizationScope(ctx, request.organizationId)\n ensurePendingStatus(request)\n\n const memberId = typeof request.member === 'string' ? request.member : request.member.id\n const decidedByUserId = parsed.decidedByUserId ?? resolveAuthUserId(ctx)\n const now = new Date()\n const dates = listDateKeysInRange(request.startDate, request.endDate)\n let createdRuleIds: string[] = []\n\n await em.transactional(async (trx) => {\n request.status = 'approved'\n request.decisionComment = parsed.decisionComment ?? null\n request.decidedByUserId = decidedByUserId\n request.decidedAt = now\n request.updatedAt = now\n trx.persist(request)\n\n createdRuleIds = await createUnavailabilityRules({\n em: trx,\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n memberId,\n timezone: request.timezone,\n dates,\n note: request.note ?? null,\n reasonEntryId: request.unavailabilityReasonEntryId ?? null,\n reasonValue: request.unavailabilityReasonValue ?? null,\n })\n await trx.flush()\n })\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n if (createdRuleIds.length) {\n const rules = await em.find(PlannerAvailabilityRule, { id: { $in: createdRuleIds } })\n for (const rule of rules) {\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'created',\n entity: rule,\n identifiers: {\n id: rule.id,\n organizationId: rule.organizationId,\n tenantId: rule.tenantId,\n },\n events: plannerAvailabilityRuleCrudEvents,\n indexer: availabilityRuleCrudIndexer,\n })\n }\n }\n await invalidateAvailabilityCache({\n container: ctx.container,\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n ruleIds: createdRuleIds,\n })\n\n // Send notification to the requester\n if (request.submittedByUserId) {\n try {\n const notificationService = resolveNotificationService(ctx.container)\n const typeDef = notificationTypes.find((type) => type.type === 'staff.leave_request.approved')\n if (typeDef) {\n const startDateStr = request.startDate.toLocaleDateString()\n const endDateStr = request.endDate.toLocaleDateString()\n\n const notificationInput = buildNotificationFromType(typeDef, {\n recipientUserId: request.submittedByUserId,\n bodyVariables: {\n startDate: startDateStr,\n endDate: endDateStr,\n },\n sourceEntityType: 'staff:leave_request',\n sourceEntityId: request.id,\n linkHref: `/backend/staff/leave-requests/${request.id}`,\n })\n\n await notificationService.create(notificationInput, {\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n })\n }\n } catch {\n // Notification creation is non-critical, don't fail the command\n }\n }\n\n return { requestId: request.id, ruleIds: createdRuleIds }\n },\n buildLog: async ({ result, ctx, snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as LeaveRequestSnapshot | undefined\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const after = await loadLeaveRequestSnapshot(\n em,\n result.requestId,\n staffSnapshotScopeFromSnapshot(before) ?? staffSnapshotScopeFromContext(ctx),\n )\n return {\n actionLabel: translate('staff.audit.leaveRequests.accept', 'Approve leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: result.requestId,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: after?.memberId ?? before?.memberId ?? null,\n tenantId: after?.tenantId ?? before?.tenantId ?? null,\n organizationId: after?.organizationId ?? before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n snapshotAfter: after ?? null,\n payload: {\n undo: {\n before: before ?? null,\n after: after ?? null,\n availabilityRuleIds: result.ruleIds,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n async prepare(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadLeaveRequestSnapshot(em, parsed.id, staffSnapshotScopeFromContext(ctx))\n return snapshot ? { before: snapshot } : {}\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const before = payload?.before\n const availabilityRuleIds = payload?.availabilityRuleIds ?? []\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(before.id, staffSnapshotScopeFromSnapshot(before)))\n if (!request) return\n\n // Fetch rules before mutating so the query cannot reset unit-of-work tracking\n // between the request scalar mutations and the flush (SPEC-018 Problem 1).\n const rules = availabilityRuleIds.length\n ? await em.find(PlannerAvailabilityRule, { id: { $in: availabilityRuleIds } })\n : []\n\n await withAtomicFlush(em, [\n () => {\n request.status = before.status\n request.decisionComment = before.decisionComment\n request.decidedByUserId = before.decidedByUserId\n request.decidedAt = before.decidedAt ? new Date(before.decidedAt) : null\n request.updatedAt = new Date()\n\n if (rules.length) {\n const now = new Date()\n rules.forEach((rule) => {\n rule.deletedAt = now\n rule.updatedAt = now\n })\n }\n },\n ], { transaction: true })\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n for (const rule of rules) {\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'deleted',\n entity: rule,\n identifiers: {\n id: rule.id,\n organizationId: rule.organizationId,\n tenantId: rule.tenantId,\n },\n events: plannerAvailabilityRuleCrudEvents,\n indexer: availabilityRuleCrudIndexer,\n })\n }\n await invalidateAvailabilityCache({\n container: ctx.container,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n ruleIds: availabilityRuleIds,\n })\n },\n}\n\nconst rejectLeaveRequestCommand: CommandHandler<StaffLeaveRequestDecisionInput, { requestId: string }> = {\n id: 'staff.leave-requests.reject',\n async execute(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await requireLeaveRequest(em, parsed.id, commandActorScope(ctx))\n ensureTenantScope(ctx, request.tenantId)\n ensureOrganizationScope(ctx, request.organizationId)\n ensurePendingStatus(request)\n\n request.status = 'rejected'\n request.decisionComment = parsed.decisionComment ?? null\n request.decidedByUserId = parsed.decidedByUserId ?? resolveAuthUserId(ctx)\n request.decidedAt = new Date()\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n\n // Send notification to the requester\n if (request.submittedByUserId) {\n try {\n const notificationService = resolveNotificationService(ctx.container)\n const typeDef = notificationTypes.find((type) => type.type === 'staff.leave_request.rejected')\n if (typeDef) {\n const startDateStr = request.startDate.toLocaleDateString()\n const endDateStr = request.endDate.toLocaleDateString()\n\n const notificationInput = buildNotificationFromType(typeDef, {\n recipientUserId: request.submittedByUserId,\n bodyVariables: {\n startDate: startDateStr,\n endDate: endDateStr,\n reason: request.decisionComment ?? '',\n },\n sourceEntityType: 'staff:leave_request',\n sourceEntityId: request.id,\n linkHref: `/backend/staff/leave-requests/${request.id}`,\n })\n\n await notificationService.create(notificationInput, {\n tenantId: request.tenantId,\n organizationId: request.organizationId,\n })\n }\n } catch {\n // Notification creation is non-critical, don't fail the command\n }\n }\n\n return { requestId: request.id }\n },\n async prepare(rawInput, ctx) {\n const parsed = staffLeaveRequestDecisionSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadLeaveRequestSnapshot(em, parsed.id, staffSnapshotScopeFromContext(ctx))\n return snapshot ? { before: snapshot } : {}\n },\n buildLog: async ({ result, ctx, snapshots }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as LeaveRequestSnapshot | undefined\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const after = await loadLeaveRequestSnapshot(\n em,\n result.requestId,\n staffSnapshotScopeFromSnapshot(before) ?? staffSnapshotScopeFromContext(ctx),\n )\n return {\n actionLabel: translate('staff.audit.leaveRequests.reject', 'Reject leave request'),\n resourceKind: 'staff.leave_request',\n resourceId: result.requestId,\n parentResourceKind: 'staff.teamMember',\n parentResourceId: after?.memberId ?? before?.memberId ?? null,\n tenantId: after?.tenantId ?? before?.tenantId ?? null,\n organizationId: after?.organizationId ?? before?.organizationId ?? null,\n snapshotBefore: before ?? null,\n snapshotAfter: after ?? null,\n payload: {\n undo: {\n before: before ?? null,\n after: after ?? null,\n } satisfies LeaveRequestUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const payload = extractUndoPayload<LeaveRequestUndoPayload>(logEntry)\n const before = payload?.before\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const request = await em.findOne(StaffLeaveRequest, scopedStaffSnapshotWhere(before.id, staffSnapshotScopeFromSnapshot(before)))\n if (!request) return\n request.status = before.status\n request.decisionComment = before.decisionComment\n request.decidedByUserId = before.decidedByUserId\n request.decidedAt = before.decidedAt ? new Date(before.decidedAt) : null\n request.updatedAt = new Date()\n await em.flush()\n\n const de = (ctx.container.resolve('dataEngine') as DataEngine)\n await emitCrudUndoSideEffects({\n dataEngine: de,\n action: 'updated',\n entity: request,\n identifiers: {\n id: request.id,\n organizationId: request.organizationId,\n tenantId: request.tenantId,\n },\n events: staffLeaveRequestCrudEvents,\n indexer: leaveRequestCrudIndexer,\n })\n },\n}\n\nregisterCommand(createLeaveRequestCommand)\nregisterCommand(updateLeaveRequestCommand)\nregisterCommand(deleteLeaveRequestCommand)\nregisterCommand(acceptLeaveRequestCommand)\nregisterCommand(rejectLeaveRequestCommand)\n"],
5
+ "mappings": "AAAA,SAAS,kBAAkB;AAE3B,SAAS,uBAAuB;AAEhC,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AACpC,SAAS,qBAAqB,yBAAyB,oBAAoB;AAC3E,SAAS,sBAAsB;AAC/B,SAAS,uBAAuB;AAGhC,SAAS,2BAA2B;AACpC,SAAS,6BAA6B;AACtC,SAAS,+BAA+B;AACxC,SAAS,mBAAmB,uBAAqD;AACjF;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,E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YAAQ,kBAAkB,OAAO;AACjC,YAAQ,kBAAkB,OAAO;AACjC,YAAQ,YAAY,OAAO,YAAY,IAAI,KAAK,OAAO,SAAS,IAAI;AACpE,YAAQ,YAAY,oBAAI,KAAK;AAC7B,UAAM,GAAG,MAAM;AAEf,UAAM,KAAM,IAAI,UAAU,QAAQ,YAAY;AAC9C,UAAM,wBAAwB;AAAA,MAC5B,YAAY;AAAA,MACZ,QAAQ;AAAA,MACR,QAAQ;AAAA,MACR,aAAa;AAAA,QACX,IAAI,QAAQ;AAAA,QACZ,gBAAgB,QAAQ;AAAA,QACxB,UAAU,QAAQ;AAAA,MACpB;AAAA,MACA,QAAQ;AAAA,MACR,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AACF;AAEA,gBAAgB,yBAAyB;AACzC,gBAAgB,yBAAyB;AACzC,gBAAgB,yBAAyB;AACzC,gBAAgB,yBAAyB;AACzC,gBAAgB,yBAAyB;",
6
6
  "names": []
7
7
  }
@@ -13,7 +13,11 @@ import { checkAttachmentAccess } from "@open-mercato/core/modules/attachments/li
13
13
  import { attachmentCrudEvents, attachmentCrudIndexer } from "@open-mercato/core/modules/attachments/lib/crud";
14
14
  import { readAttachmentMetadata } from "@open-mercato/core/modules/attachments/lib/metadata";
15
15
  import { clearAttachmentThumbnailCache } from "@open-mercato/core/modules/attachments/lib/thumbnailCache";
16
- import { isMultipartRequestWithinUploadLimit } from "@open-mercato/core/modules/attachments/lib/upload-limits";
16
+ import {
17
+ isMultipartUploadLimitError,
18
+ parseMultipartFormDataWithinUploadLimit,
19
+ resolveDefaultAttachmentMaxUploadBytes
20
+ } from "@open-mercato/core/modules/attachments/lib/upload-limits";
17
21
  import {
18
22
  isScopedAttachmentUploadError
19
23
  } from "@open-mercato/core/modules/attachments/lib/scoped-upload-service";
@@ -289,6 +293,13 @@ async function GET(req) {
289
293
  totalPages: Math.max(1, Math.ceil(visible.length / pageSize))
290
294
  });
291
295
  }
296
+ function uploadTooLargeResponse(t) {
297
+ return NextResponse.json({
298
+ ok: false,
299
+ error: t("attachments.errors.maxUploadSize", "Attachment exceeds the maximum upload size."),
300
+ code: "max_upload_size"
301
+ }, { status: 413 });
302
+ }
292
303
  async function handleUpload(req, replacement) {
293
304
  const contextOrResponse = await resolvePortalContext(req);
294
305
  if (contextOrResponse instanceof Response) return contextOrResponse;
@@ -298,13 +309,13 @@ async function handleUpload(req, replacement) {
298
309
  if (!contentType.toLowerCase().includes("multipart/form-data")) {
299
310
  return NextResponse.json({ ok: false, error: translate("warranty_claims.errors.invalidInput", "Invalid input") }, { status: 400 });
300
311
  }
301
- if (!isMultipartRequestWithinUploadLimit(req.headers.get("content-length"))) {
302
- return NextResponse.json({
303
- ok: false,
304
- error: t("attachments.errors.maxUploadSize", "Attachment exceeds the maximum upload size.")
305
- }, { status: 413 });
312
+ let form;
313
+ try {
314
+ form = await parseMultipartFormDataWithinUploadLimit(req);
315
+ } catch (error) {
316
+ if (isMultipartUploadLimitError(error)) return uploadTooLargeResponse(t);
317
+ throw error;
306
318
  }
307
- const form = await req.formData();
308
319
  const parsed = uploadBodySchema.safeParse({
309
320
  claimId: form.get("claimId") ?? void 0,
310
321
  file: form.get("file") instanceof File ? form.get("file").name : ""
@@ -321,6 +332,7 @@ async function handleUpload(req, replacement) {
321
332
  if (!(file instanceof File)) {
322
333
  return NextResponse.json({ ok: false, error: translate("warranty_claims.portal.detail.fileRequired", "Choose a file before uploading.") }, { status: 400 });
323
334
  }
335
+ if (file.size > resolveDefaultAttachmentMaxUploadBytes()) return uploadTooLargeResponse(t);
324
336
  const buffer = Buffer.from(await file.arrayBuffer());
325
337
  const attachmentId = replacement && parsedAttachmentId?.success ? parsedAttachmentId.data : null;
326
338
  const replacementTarget = attachmentId ? await loadOwnedVisibleAttachment(context, attachmentId) : null;