@open-mercato/shared 0.6.7-develop.6768.1.9d2c4efc43 → 0.6.7-develop.6770.1.b52298cf20

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.
@@ -38,6 +38,7 @@ import {
38
38
  isCrudCacheEnabled,
39
39
  normalizeIdentifierValue,
40
40
  normalizeTagSegment,
41
+ pickFirstIdentifier,
41
42
  resolveCrudCache
42
43
  } from "./cache.js";
43
44
  import { deriveCrudSegmentTag } from "./cache-stats.js";
@@ -560,6 +561,12 @@ function buildCrudCacheKey(resource, request, ctx, enricherSignature = "") {
560
561
  `tenant:${normalizeTagSegment(ctx.auth?.tenantId ?? null)}`,
561
562
  `selectedOrg:${normalizeTagSegment(ctx.selectedOrganizationId ?? null)}`,
562
563
  `scope:${scopeSegment}`,
564
+ // List payloads can vary per caller identity beyond tenant/org scope:
565
+ // buildFilters may narrow by ctx.auth (e.g. ?mine=true), before-interceptor
566
+ // query rewrites are feature-gated per user, and afterList/after-interceptor
567
+ // output is embedded in the stored payload — so entries MUST be partitioned
568
+ // per actor (API key or user), never shared across identities.
569
+ `user:${normalizeTagSegment(ctx.auth?.keyId ?? ctx.auth?.sub ?? null)}`,
563
570
  `query:${serializeSearchParams(url.searchParams)}`
564
571
  ];
565
572
  if (enricherSignature) {
@@ -1589,6 +1596,29 @@ function makeCrudRoute(opts) {
1589
1596
  }
1590
1597
  }
1591
1598
  }
1599
+ const createCmdUserFeatures = await resolveUserFeatures(ctx);
1600
+ const { allGuards: createCmdAllGuards } = collectAndRunGuards(ctx.container);
1601
+ let createCmdGuardAfterCallbacks = [];
1602
+ if (createCmdAllGuards.length && ctx.auth.tenantId) {
1603
+ const guardResult = await runMutationGuards(createCmdAllGuards, {
1604
+ tenantId: ctx.auth.tenantId,
1605
+ organizationId: ctx.selectedOrganizationId ?? ctx.auth.orgId ?? null,
1606
+ userId: ctx.auth.sub,
1607
+ resourceKind,
1608
+ resourceId: null,
1609
+ operation: "create",
1610
+ requestMethod: request.method,
1611
+ requestHeaders: request.headers,
1612
+ mutationPayload: input2 && typeof input2 === "object" ? input2 : null
1613
+ }, { userFeatures: createCmdUserFeatures ?? [] });
1614
+ if (!guardResult.ok) {
1615
+ return json(guardResult.errorBody ?? { error: "Operation blocked by guard" }, { status: guardResult.errorStatus ?? 422 });
1616
+ }
1617
+ if (guardResult.modifiedPayload && typeof input2 === "object" && input2) {
1618
+ input2 = { ...input2, ...guardResult.modifiedPayload };
1619
+ }
1620
+ createCmdGuardAfterCallbacks = guardResult.afterSuccessCallbacks;
1621
+ }
1592
1622
  const baseMetadata = {
1593
1623
  tenantId: ctx.auth?.tenantId ?? null,
1594
1624
  organizationId: ctx.selectedOrganizationId ?? ctx.auth.orgId ?? null,
@@ -1628,6 +1658,22 @@ function makeCrudRoute(opts) {
1628
1658
  const status = action.status ?? 201;
1629
1659
  const response = json(resolvedPayload, { status });
1630
1660
  attachOperationHeader(response, logEntry);
1661
+ const commandResultId = pickFirstIdentifier(
1662
+ result?.id,
1663
+ resolvedPayload?.id
1664
+ );
1665
+ if (createCmdGuardAfterCallbacks.length && ctx.auth.tenantId && commandResultId) {
1666
+ await runGuardAfterSuccessCallbacks(createCmdGuardAfterCallbacks, {
1667
+ tenantId: ctx.auth.tenantId,
1668
+ organizationId: ctx.selectedOrganizationId ?? ctx.auth.orgId ?? null,
1669
+ userId: ctx.auth.sub,
1670
+ resourceKind,
1671
+ resourceId: commandResultId,
1672
+ operation: "create",
1673
+ requestMethod: request.method,
1674
+ requestHeaders: request.headers
1675
+ });
1676
+ }
1631
1677
  return response;
1632
1678
  }
1633
1679
  const createConfig = opts.create;