@payloadcms/plugin-multi-tenant 4.0.0-internal.934870d → 4.0.0-internal.94ef095

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 (42) hide show
  1. package/dist/components/GlobalViewRedirect/index.d.ts +2 -2
  2. package/dist/components/GlobalViewRedirect/index.d.ts.map +1 -1
  3. package/dist/components/GlobalViewRedirect/index.js +2 -2
  4. package/dist/components/GlobalViewRedirect/index.js.map +1 -1
  5. package/dist/filters/filterDocumentsByTenants.js.map +1 -1
  6. package/dist/hooks/afterTenantDelete.d.ts +1 -1
  7. package/dist/hooks/afterTenantDelete.d.ts.map +1 -1
  8. package/dist/hooks/afterTenantDelete.js +17 -16
  9. package/dist/hooks/afterTenantDelete.js.map +1 -1
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +286 -214
  12. package/dist/index.js.map +1 -1
  13. package/dist/providers/TenantSelectionProvider/index.client.d.ts.map +1 -1
  14. package/dist/providers/TenantSelectionProvider/index.client.js.map +1 -1
  15. package/dist/utilities/addCollectionAccess.d.ts +4 -5
  16. package/dist/utilities/addCollectionAccess.d.ts.map +1 -1
  17. package/dist/utilities/addCollectionAccess.js +36 -102
  18. package/dist/utilities/addCollectionAccess.js.map +1 -1
  19. package/dist/utilities/addFilterOptionsToFields.d.ts +2 -3
  20. package/dist/utilities/addFilterOptionsToFields.d.ts.map +1 -1
  21. package/dist/utilities/addFilterOptionsToFields.js +37 -40
  22. package/dist/utilities/addFilterOptionsToFields.js.map +1 -1
  23. package/dist/utilities/collectionAccessKeys.d.ts +3 -0
  24. package/dist/utilities/collectionAccessKeys.d.ts.map +1 -0
  25. package/dist/utilities/collectionAccessKeys.js +11 -0
  26. package/dist/utilities/collectionAccessKeys.js.map +1 -0
  27. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts +7 -0
  28. package/dist/utilities/combineBaseAccessWithTenantAccess.d.ts.map +1 -0
  29. package/dist/utilities/combineBaseAccessWithTenantAccess.js +35 -0
  30. package/dist/utilities/combineBaseAccessWithTenantAccess.js.map +1 -0
  31. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts +4 -0
  32. package/dist/utilities/combineCollectionAccessWithTenantAccess.d.ts.map +1 -0
  33. package/dist/utilities/combineCollectionAccessWithTenantAccess.js +26 -0
  34. package/dist/utilities/combineCollectionAccessWithTenantAccess.js.map +1 -0
  35. package/dist/utilities/getGlobalViewRedirect.d.ts.map +1 -1
  36. package/dist/utilities/getGlobalViewRedirect.js +24 -20
  37. package/dist/utilities/getGlobalViewRedirect.js.map +1 -1
  38. package/dist/utilities/getTenantAccessResult.d.ts +9 -0
  39. package/dist/utilities/getTenantAccessResult.d.ts.map +1 -0
  40. package/dist/utilities/getTenantAccessResult.js +46 -0
  41. package/dist/utilities/getTenantAccessResult.js.map +1 -0
  42. package/package.json +23 -26
@@ -4,9 +4,9 @@ type Args = {
4
4
  collectionSlug: CollectionSlug;
5
5
  docID?: number | string;
6
6
  globalSlugs: string[];
7
- tenantArrayFieldName: string;
8
- tenantArrayTenantFieldName: string;
9
7
  tenantFieldName: string;
8
+ tenantsArrayFieldName: string;
9
+ tenantsArrayTenantFieldName: string;
10
10
  tenantsCollectionSlug: string;
11
11
  useAsTitle: string;
12
12
  userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants'];
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/GlobalViewRedirect/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAErE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAI7D,KAAK,IAAI,GAAG;IACV,cAAc,EAAE,cAAc,CAAA;IAC9B,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACvB,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,oBAAoB,EAAE,MAAM,CAAA;IAC5B,0BAA0B,EAAE,MAAM,CAAA;IAClC,eAAe,EAAE,MAAM,CAAA;IACvB,qBAAqB,EAAE,MAAM,CAAA;IAC7B,UAAU,EAAE,MAAM,CAAA;IAClB,yBAAyB,EAAE,QAAQ,CAAC,uBAAuB,CAAC,CAAC,2BAA2B,CAAC,CAAA;IACzF,QAAQ,EAAE,SAAS,CAAA;CACpB,GAAG,WAAW,CAAA;AAEf,eAAO,MAAM,kBAAkB,SAAgB,IAAI,kBA6BlD,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/GlobalViewRedirect/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAErE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAI7D,KAAK,IAAI,GAAG;IACV,cAAc,EAAE,cAAc,CAAA;IAC9B,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACvB,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,eAAe,EAAE,MAAM,CAAA;IACvB,qBAAqB,EAAE,MAAM,CAAA;IAC7B,2BAA2B,EAAE,MAAM,CAAA;IACnC,qBAAqB,EAAE,MAAM,CAAA;IAC7B,UAAU,EAAE,MAAM,CAAA;IAClB,yBAAyB,EAAE,QAAQ,CAAC,uBAAuB,CAAC,CAAC,2BAA2B,CAAC,CAAA;IACzF,QAAQ,EAAE,SAAS,CAAA;CACpB,GAAG,WAAW,CAAA;AAEf,eAAO,MAAM,kBAAkB,SAAgB,IAAI,kBA6BlD,CAAA"}
@@ -13,8 +13,8 @@ export const GlobalViewRedirect = async (args)=>{
13
13
  payload: args.payload,
14
14
  server: args.server,
15
15
  tenantFieldName: args.tenantFieldName,
16
- tenantsArrayFieldName: args.tenantArrayFieldName,
17
- tenantsArrayTenantFieldName: args.tenantArrayTenantFieldName,
16
+ tenantsArrayFieldName: args.tenantsArrayFieldName,
17
+ tenantsArrayTenantFieldName: args.tenantsArrayTenantFieldName,
18
18
  tenantsCollectionSlug: args.tenantsCollectionSlug,
19
19
  useAsTitle: args.useAsTitle,
20
20
  user: args.user,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/components/GlobalViewRedirect/index.ts"],"sourcesContent":["import type { CollectionSlug, ServerProps, ViewTypes } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../../types.js'\n\nimport { getGlobalViewRedirect } from '../../utilities/getGlobalViewRedirect.js'\n\ntype Args = {\n collectionSlug: CollectionSlug\n docID?: number | string\n globalSlugs: string[]\n tenantArrayFieldName: string\n tenantArrayTenantFieldName: string\n tenantFieldName: string\n tenantsCollectionSlug: string\n useAsTitle: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n viewType: ViewTypes\n} & ServerProps\n\nexport const GlobalViewRedirect = async (args: Args) => {\n const collectionSlug = args?.collectionSlug\n if (collectionSlug && args.globalSlugs?.includes(collectionSlug)) {\n if (!args.server) {\n throw new Error(\n 'GlobalViewRedirect requires `server` in ServerProps. Ensure your framework adapter (e.g. @payloadcms/next) populates ServerProps.server.',\n )\n }\n const headers = await args.server.getHeaders()\n const redirectRoute = await getGlobalViewRedirect({\n slug: collectionSlug,\n docID: args.docID,\n headers,\n payload: args.payload,\n server: args.server,\n tenantFieldName: args.tenantFieldName,\n tenantsArrayFieldName: args.tenantArrayFieldName,\n tenantsArrayTenantFieldName: args.tenantArrayTenantFieldName,\n tenantsCollectionSlug: args.tenantsCollectionSlug,\n useAsTitle: args.useAsTitle,\n user: args.user,\n userHasAccessToAllTenants: args.userHasAccessToAllTenants,\n view: args.viewType,\n })\n\n if (redirectRoute) {\n args.server.redirect(redirectRoute)\n }\n }\n}\n"],"names":["getGlobalViewRedirect","GlobalViewRedirect","args","collectionSlug","globalSlugs","includes","server","Error","headers","getHeaders","redirectRoute","slug","docID","payload","tenantFieldName","tenantsArrayFieldName","tenantArrayFieldName","tenantsArrayTenantFieldName","tenantArrayTenantFieldName","tenantsCollectionSlug","useAsTitle","user","userHasAccessToAllTenants","view","viewType","redirect"],"mappings":"AAIA,SAASA,qBAAqB,QAAQ,2CAA0C;AAehF,OAAO,MAAMC,qBAAqB,OAAOC;IACvC,MAAMC,iBAAiBD,MAAMC;IAC7B,IAAIA,kBAAkBD,KAAKE,WAAW,EAAEC,SAASF,iBAAiB;QAChE,IAAI,CAACD,KAAKI,MAAM,EAAE;YAChB,MAAM,IAAIC,MACR;QAEJ;QACA,MAAMC,UAAU,MAAMN,KAAKI,MAAM,CAACG,UAAU;QAC5C,MAAMC,gBAAgB,MAAMV,sBAAsB;YAChDW,MAAMR;YACNS,OAAOV,KAAKU,KAAK;YACjBJ;YACAK,SAASX,KAAKW,OAAO;YACrBP,QAAQJ,KAAKI,MAAM;YACnBQ,iBAAiBZ,KAAKY,eAAe;YACrCC,uBAAuBb,KAAKc,oBAAoB;YAChDC,6BAA6Bf,KAAKgB,0BAA0B;YAC5DC,uBAAuBjB,KAAKiB,qBAAqB;YACjDC,YAAYlB,KAAKkB,UAAU;YAC3BC,MAAMnB,KAAKmB,IAAI;YACfC,2BAA2BpB,KAAKoB,yBAAyB;YACzDC,MAAMrB,KAAKsB,QAAQ;QACrB;QAEA,IAAId,eAAe;YACjBR,KAAKI,MAAM,CAACmB,QAAQ,CAACf;QACvB;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../../../src/components/GlobalViewRedirect/index.ts"],"sourcesContent":["import type { CollectionSlug, ServerProps, ViewTypes } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../../types.js'\n\nimport { getGlobalViewRedirect } from '../../utilities/getGlobalViewRedirect.js'\n\ntype Args = {\n collectionSlug: CollectionSlug\n docID?: number | string\n globalSlugs: string[]\n tenantFieldName: string\n tenantsArrayFieldName: string\n tenantsArrayTenantFieldName: string\n tenantsCollectionSlug: string\n useAsTitle: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n viewType: ViewTypes\n} & ServerProps\n\nexport const GlobalViewRedirect = async (args: Args) => {\n const collectionSlug = args?.collectionSlug\n if (collectionSlug && args.globalSlugs?.includes(collectionSlug)) {\n if (!args.server) {\n throw new Error(\n 'GlobalViewRedirect requires `server` in ServerProps. Ensure your framework adapter (e.g. @payloadcms/next) populates ServerProps.server.',\n )\n }\n const headers = await args.server.getHeaders()\n const redirectRoute = await getGlobalViewRedirect({\n slug: collectionSlug,\n docID: args.docID,\n headers,\n payload: args.payload,\n server: args.server,\n tenantFieldName: args.tenantFieldName,\n tenantsArrayFieldName: args.tenantsArrayFieldName,\n tenantsArrayTenantFieldName: args.tenantsArrayTenantFieldName,\n tenantsCollectionSlug: args.tenantsCollectionSlug,\n useAsTitle: args.useAsTitle,\n user: args.user,\n userHasAccessToAllTenants: args.userHasAccessToAllTenants,\n view: args.viewType,\n })\n\n if (redirectRoute) {\n args.server.redirect(redirectRoute)\n }\n }\n}\n"],"names":["getGlobalViewRedirect","GlobalViewRedirect","args","collectionSlug","globalSlugs","includes","server","Error","headers","getHeaders","redirectRoute","slug","docID","payload","tenantFieldName","tenantsArrayFieldName","tenantsArrayTenantFieldName","tenantsCollectionSlug","useAsTitle","user","userHasAccessToAllTenants","view","viewType","redirect"],"mappings":"AAIA,SAASA,qBAAqB,QAAQ,2CAA0C;AAehF,OAAO,MAAMC,qBAAqB,OAAOC;IACvC,MAAMC,iBAAiBD,MAAMC;IAC7B,IAAIA,kBAAkBD,KAAKE,WAAW,EAAEC,SAASF,iBAAiB;QAChE,IAAI,CAACD,KAAKI,MAAM,EAAE;YAChB,MAAM,IAAIC,MACR;QAEJ;QACA,MAAMC,UAAU,MAAMN,KAAKI,MAAM,CAACG,UAAU;QAC5C,MAAMC,gBAAgB,MAAMV,sBAAsB;YAChDW,MAAMR;YACNS,OAAOV,KAAKU,KAAK;YACjBJ;YACAK,SAASX,KAAKW,OAAO;YACrBP,QAAQJ,KAAKI,MAAM;YACnBQ,iBAAiBZ,KAAKY,eAAe;YACrCC,uBAAuBb,KAAKa,qBAAqB;YACjDC,6BAA6Bd,KAAKc,2BAA2B;YAC7DC,uBAAuBf,KAAKe,qBAAqB;YACjDC,YAAYhB,KAAKgB,UAAU;YAC3BC,MAAMjB,KAAKiB,IAAI;YACfC,2BAA2BlB,KAAKkB,yBAAyB;YACzDC,MAAMnB,KAAKoB,QAAQ;QACrB;QAEA,IAAIZ,eAAe;YACjBR,KAAKI,MAAM,CAACiB,QAAQ,CAACb;QACvB;IACF;AACF,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/filters/filterDocumentsByTenants.ts"],"sourcesContent":["import type { PayloadRequest, User, Where } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../types.js'\n\nimport { defaults } from '../defaults.js'\nimport { getCollectionIDType } from '../utilities/getCollectionIDType.js'\nimport { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'\nimport { getUserTenantIDs } from '../utilities/getUserTenantIDs.js'\n\ntype Args = {\n /**\n * If the document this filter is run belongs to a tenant, the tenant ID should be passed here.\n * If set, this will be used instead of the tenant cookie\n * Can be an array when hasMany is true\n */\n docTenantID?: number | number[] | string | string[]\n filterFieldName: string\n req: PayloadRequest\n tenantsArrayFieldName?: string\n tenantsArrayTenantFieldName?: string\n tenantsCollectionSlug: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n}\nexport const filterDocumentsByTenants = ({\n docTenantID,\n filterFieldName,\n req,\n tenantsArrayFieldName = defaults.tenantsArrayFieldName,\n tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n}: Args): null | Where => {\n const idType = getCollectionIDType({\n collectionSlug: tenantsCollectionSlug,\n payload: req.payload,\n })\n\n // scope results to selected tenant\n const selectedTenant = docTenantID ?? getTenantFromCookie(req.headers, idType)\n if (selectedTenant) {\n return {\n [filterFieldName]: {\n in: Array.isArray(selectedTenant) ? selectedTenant : [selectedTenant],\n },\n }\n }\n\n if (req.user && userHasAccessToAllTenants(req?.user as User)) {\n return null\n }\n\n // scope to user assigned tenants\n const userAssignedTenants = getUserTenantIDs(req.user, {\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n })\n if (userAssignedTenants.length > 0) {\n return {\n [filterFieldName]: {\n in: userAssignedTenants,\n },\n }\n }\n\n // no tenant selected and no user tenants, return null to allow access control to handle it\n return null\n}\n"],"names":["defaults","getCollectionIDType","getTenantFromCookie","getUserTenantIDs","filterDocumentsByTenants","docTenantID","filterFieldName","req","tenantsArrayFieldName","tenantsArrayTenantFieldName","tenantsCollectionSlug","userHasAccessToAllTenants","idType","collectionSlug","payload","selectedTenant","headers","in","Array","isArray","user","userAssignedTenants","length"],"mappings":"AAIA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,gBAAgB,QAAQ,mCAAkC;AAgBnE,OAAO,MAAMC,2BAA2B,CAAC,EACvCC,WAAW,EACXC,eAAe,EACfC,GAAG,EACHC,wBAAwBR,SAASQ,qBAAqB,EACtDC,8BAA8BT,SAASS,2BAA2B,EAClEC,qBAAqB,EACrBC,yBAAyB,EACpB;IACL,MAAMC,SAASX,oBAAoB;QACjCY,gBAAgBH;QAChBI,SAASP,IAAIO,OAAO;IACtB;IAEA,mCAAmC;IACnC,MAAMC,iBAAiBV,eAAeH,oBAAoBK,IAAIS,OAAO,EAAEJ;IACvE,IAAIG,gBAAgB;QAClB,OAAO;YACL,CAACT,gBAAgB,EAAE;gBACjBW,IAAIC,MAAMC,OAAO,CAACJ,kBAAkBA,iBAAiB;oBAACA;iBAAe;YACvE;QACF;IACF;IAEA,IAAIR,IAAIa,IAAI,IAAIT,0BAA0BJ,KAAKa,OAAe;QAC5D,OAAO;IACT;IAEA,iCAAiC;IACjC,MAAMC,sBAAsBlB,iBAAiBI,IAAIa,IAAI,EAAE;QACrDZ;QACAC;IACF;IACA,IAAIY,oBAAoBC,MAAM,GAAG,GAAG;QAClC,OAAO;YACL,CAAChB,gBAAgB,EAAE;gBACjBW,IAAII;YACN;QACF;IACF;IAEA,2FAA2F;IAC3F,OAAO;AACT,EAAC"}
1
+ {"version":3,"sources":["../../src/filters/filterDocumentsByTenants.ts"],"sourcesContent":["import type { PayloadRequest, User, Where } from 'payload'\n\nimport type { MultiTenantPluginConfig } from '../types.js'\n\nimport { defaults } from '../defaults.js'\nimport { getCollectionIDType } from '../utilities/getCollectionIDType.js'\nimport { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'\nimport { getUserTenantIDs } from '../utilities/getUserTenantIDs.js'\n\ntype Args = {\n /**\n * If the document this filter is run belongs to a tenant, the tenant ID should be passed here.\n * If set, this will be used instead of the tenant cookie\n * Can be an array when hasMany is true\n */\n docTenantID?: number | number[] | string | string[]\n filterFieldName: string\n req: PayloadRequest\n tenantsArrayFieldName?: string\n tenantsArrayTenantFieldName?: string\n tenantsCollectionSlug: string\n userHasAccessToAllTenants: Required<MultiTenantPluginConfig>['userHasAccessToAllTenants']\n}\nexport const filterDocumentsByTenants = ({\n docTenantID,\n filterFieldName,\n req,\n tenantsArrayFieldName = defaults.tenantsArrayFieldName,\n tenantsArrayTenantFieldName = defaults.tenantsArrayTenantFieldName,\n tenantsCollectionSlug,\n userHasAccessToAllTenants,\n}: Args): null | Where => {\n const idType = getCollectionIDType({\n collectionSlug: tenantsCollectionSlug,\n payload: req.payload,\n })\n\n // scope results to selected tenant\n const selectedTenant = docTenantID ?? getTenantFromCookie(req.headers, idType)\n if (selectedTenant) {\n return {\n [filterFieldName]: {\n in: Array.isArray(selectedTenant) ? selectedTenant : [selectedTenant],\n },\n }\n }\n\n if (req.user && userHasAccessToAllTenants(req?.user)) {\n return null\n }\n\n // scope to user assigned tenants\n const userAssignedTenants = getUserTenantIDs(req.user, {\n tenantsArrayFieldName,\n tenantsArrayTenantFieldName,\n })\n if (userAssignedTenants.length > 0) {\n return {\n [filterFieldName]: {\n in: userAssignedTenants,\n },\n }\n }\n\n // no tenant selected and no user tenants, return null to allow access control to handle it\n return null\n}\n"],"names":["defaults","getCollectionIDType","getTenantFromCookie","getUserTenantIDs","filterDocumentsByTenants","docTenantID","filterFieldName","req","tenantsArrayFieldName","tenantsArrayTenantFieldName","tenantsCollectionSlug","userHasAccessToAllTenants","idType","collectionSlug","payload","selectedTenant","headers","in","Array","isArray","user","userAssignedTenants","length"],"mappings":"AAIA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,gBAAgB,QAAQ,mCAAkC;AAgBnE,OAAO,MAAMC,2BAA2B,CAAC,EACvCC,WAAW,EACXC,eAAe,EACfC,GAAG,EACHC,wBAAwBR,SAASQ,qBAAqB,EACtDC,8BAA8BT,SAASS,2BAA2B,EAClEC,qBAAqB,EACrBC,yBAAyB,EACpB;IACL,MAAMC,SAASX,oBAAoB;QACjCY,gBAAgBH;QAChBI,SAASP,IAAIO,OAAO;IACtB;IAEA,mCAAmC;IACnC,MAAMC,iBAAiBV,eAAeH,oBAAoBK,IAAIS,OAAO,EAAEJ;IACvE,IAAIG,gBAAgB;QAClB,OAAO;YACL,CAACT,gBAAgB,EAAE;gBACjBW,IAAIC,MAAMC,OAAO,CAACJ,kBAAkBA,iBAAiB;oBAACA;iBAAe;YACvE;QACF;IACF;IAEA,IAAIR,IAAIa,IAAI,IAAIT,0BAA0BJ,KAAKa,OAAO;QACpD,OAAO;IACT;IAEA,iCAAiC;IACjC,MAAMC,sBAAsBlB,iBAAiBI,IAAIa,IAAI,EAAE;QACrDZ;QACAC;IACF;IACA,IAAIY,oBAAoBC,MAAM,GAAG,GAAG;QAClC,OAAO;YACL,CAAChB,gBAAgB,EAAE;gBACjBW,IAAII;YACN;QACF;IACF;IAEA,2FAA2F;IAC3F,OAAO;AACT,EAAC"}
@@ -13,7 +13,7 @@ type Args = {
13
13
  * - delete documents related to tenant
14
14
  * - remove tenant from users
15
15
  */
16
- export declare const addTenantCleanup: ({ collection, enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName, }: Args) => void;
16
+ export declare const addTenantCleanup: ({ collection, enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName, }: Args) => CollectionConfig;
17
17
  export declare const afterTenantDelete: ({ enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName, }: Omit<Args, 'collection'>) => CollectionAfterDeleteHook;
18
18
  export {};
19
19
  //# sourceMappingURL=afterTenantDelete.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"afterTenantDelete.d.ts","sourceRoot":"","sources":["../../src/hooks/afterTenantDelete.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,gBAAgB,EAGjB,MAAM,SAAS,CAAA;AAShB,KAAK,IAAI,GAAG;IACV,UAAU,EAAE,gBAAgB,CAAA;IAC5B,YAAY,EAAE,MAAM,EAAE,CAAA;IACtB,eAAe,EAAE,MAAM,CAAA;IACvB,qBAAqB,EAAE,MAAM,CAAA;IAC7B,SAAS,EAAE,MAAM,CAAA;IACjB,0BAA0B,EAAE,MAAM,CAAA;IAClC,gCAAgC,EAAE,MAAM,CAAA;CACzC,CAAA;AACD;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,mJAQ1B,IAAI,SAiBN,CAAA;AAED,eAAO,MAAM,iBAAiB,uIAQzB,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,KAAG,yBA+E7B,CAAA"}
1
+ {"version":3,"file":"afterTenantDelete.d.ts","sourceRoot":"","sources":["../../src/hooks/afterTenantDelete.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,gBAAgB,EAGjB,MAAM,SAAS,CAAA;AAShB,KAAK,IAAI,GAAG;IACV,UAAU,EAAE,gBAAgB,CAAA;IAC5B,YAAY,EAAE,MAAM,EAAE,CAAA;IACtB,eAAe,EAAE,MAAM,CAAA;IACvB,qBAAqB,EAAE,MAAM,CAAA;IAC7B,SAAS,EAAE,MAAM,CAAA;IACjB,0BAA0B,EAAE,MAAM,CAAA;IAClC,gCAAgC,EAAE,MAAM,CAAA;CACzC,CAAA;AACD;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,mJAQ1B,IAAI,KAAG,gBAgBR,CAAA;AAEF,eAAO,MAAM,iBAAiB,uIAQzB,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,KAAG,yBA+E7B,CAAA"}
@@ -5,22 +5,23 @@ import { getTenantFromCookie } from '../utilities/getTenantFromCookie.js';
5
5
  * Add cleanup logic when tenant is deleted
6
6
  * - delete documents related to tenant
7
7
  * - remove tenant from users
8
- */ export const addTenantCleanup = ({ collection, enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName })=>{
9
- if (!collection.hooks) {
10
- collection.hooks = {};
11
- }
12
- if (!collection.hooks?.afterDelete) {
13
- collection.hooks.afterDelete = [];
14
- }
15
- collection.hooks.afterDelete.push(afterTenantDelete({
16
- enabledSlugs,
17
- tenantFieldName,
18
- tenantsCollectionSlug,
19
- usersSlug,
20
- usersTenantsArrayFieldName,
21
- usersTenantsArrayTenantFieldName
22
- }));
23
- };
8
+ */ export const addTenantCleanup = ({ collection, enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName })=>({
9
+ ...collection,
10
+ hooks: {
11
+ ...collection.hooks,
12
+ afterDelete: [
13
+ ...collection.hooks?.afterDelete || [],
14
+ afterTenantDelete({
15
+ enabledSlugs,
16
+ tenantFieldName,
17
+ tenantsCollectionSlug,
18
+ usersSlug,
19
+ usersTenantsArrayFieldName,
20
+ usersTenantsArrayTenantFieldName
21
+ })
22
+ ]
23
+ }
24
+ });
24
25
  export const afterTenantDelete = ({ enabledSlugs, tenantFieldName, tenantsCollectionSlug, usersSlug, usersTenantsArrayFieldName, usersTenantsArrayTenantFieldName })=>async ({ id, req })=>{
25
26
  const idType = getCollectionIDType({
26
27
  collectionSlug: tenantsCollectionSlug,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/afterTenantDelete.ts"],"sourcesContent":["import type {\n CollectionAfterDeleteHook,\n CollectionConfig,\n JsonObject,\n PaginatedDocs,\n} from 'payload'\n\nimport { generateCookie, mergeHeaders } from 'payload'\n\nimport type { UserWithTenantsField } from '../types.js'\n\nimport { getCollectionIDType } from '../utilities/getCollectionIDType.js'\nimport { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'\n\ntype Args = {\n collection: CollectionConfig\n enabledSlugs: string[]\n tenantFieldName: string\n tenantsCollectionSlug: string\n usersSlug: string\n usersTenantsArrayFieldName: string\n usersTenantsArrayTenantFieldName: string\n}\n/**\n * Add cleanup logic when tenant is deleted\n * - delete documents related to tenant\n * - remove tenant from users\n */\nexport const addTenantCleanup = ({\n collection,\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n}: Args) => {\n if (!collection.hooks) {\n collection.hooks = {}\n }\n if (!collection.hooks?.afterDelete) {\n collection.hooks.afterDelete = []\n }\n collection.hooks.afterDelete.push(\n afterTenantDelete({\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n }),\n )\n}\n\nexport const afterTenantDelete =\n ({\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n }: Omit<Args, 'collection'>): CollectionAfterDeleteHook =>\n async ({ id, req }) => {\n const idType = getCollectionIDType({\n collectionSlug: tenantsCollectionSlug,\n payload: req.payload,\n })\n const currentTenantCookieID = getTenantFromCookie(req.headers, idType)\n if (currentTenantCookieID === id) {\n const newHeaders = new Headers({\n 'Set-Cookie': generateCookie<string>({\n name: 'payload-tenant',\n expires: new Date(Date.now() - 1000),\n path: '/',\n returnCookieAsObject: false,\n value: '',\n }),\n })\n\n req.responseHeaders = req.responseHeaders\n ? mergeHeaders(req.responseHeaders, newHeaders)\n : newHeaders\n }\n const cleanupPromises: Promise<JsonObject>[] = []\n enabledSlugs.forEach((slug) => {\n cleanupPromises.push(\n req.payload.delete({\n collection: slug,\n overrideAccess: true,\n req,\n where: {\n [tenantFieldName]: {\n in: [id],\n },\n },\n }),\n )\n })\n\n try {\n const usersWithTenant = (await req.payload.find({\n collection: usersSlug,\n depth: 0,\n limit: 0,\n overrideAccess: true,\n req,\n where: {\n [`${usersTenantsArrayFieldName}.${usersTenantsArrayTenantFieldName}`]: {\n in: [id],\n },\n },\n })) as PaginatedDocs<UserWithTenantsField>\n\n usersWithTenant?.docs?.forEach((user) => {\n cleanupPromises.push(\n req.payload.update({\n id: user.id,\n collection: usersSlug,\n data: {\n [usersTenantsArrayFieldName]: (\n ((user as Record<string, unknown>)[usersTenantsArrayFieldName] as Record<\n string,\n string\n >[]) || []\n ).filter((row: Record<string, string>) => {\n if (row[usersTenantsArrayTenantFieldName]) {\n return row[usersTenantsArrayTenantFieldName] !== id\n }\n }),\n },\n overrideAccess: true,\n req,\n }),\n )\n })\n } catch (e) {\n console.error('Error deleting tenants from users:', e)\n }\n\n await Promise.all(cleanupPromises)\n }\n"],"names":["generateCookie","mergeHeaders","getCollectionIDType","getTenantFromCookie","addTenantCleanup","collection","enabledSlugs","tenantFieldName","tenantsCollectionSlug","usersSlug","usersTenantsArrayFieldName","usersTenantsArrayTenantFieldName","hooks","afterDelete","push","afterTenantDelete","id","req","idType","collectionSlug","payload","currentTenantCookieID","headers","newHeaders","Headers","name","expires","Date","now","path","returnCookieAsObject","value","responseHeaders","cleanupPromises","forEach","slug","delete","overrideAccess","where","in","usersWithTenant","find","depth","limit","docs","user","update","data","filter","row","e","console","error","Promise","all"],"mappings":"AAOA,SAASA,cAAc,EAAEC,YAAY,QAAQ,UAAS;AAItD,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,mBAAmB,QAAQ,sCAAqC;AAWzE;;;;CAIC,GACD,OAAO,MAAMC,mBAAmB,CAAC,EAC/BC,UAAU,EACVC,YAAY,EACZC,eAAe,EACfC,qBAAqB,EACrBC,SAAS,EACTC,0BAA0B,EAC1BC,gCAAgC,EAC3B;IACL,IAAI,CAACN,WAAWO,KAAK,EAAE;QACrBP,WAAWO,KAAK,GAAG,CAAC;IACtB;IACA,IAAI,CAACP,WAAWO,KAAK,EAAEC,aAAa;QAClCR,WAAWO,KAAK,CAACC,WAAW,GAAG,EAAE;IACnC;IACAR,WAAWO,KAAK,CAACC,WAAW,CAACC,IAAI,CAC/BC,kBAAkB;QAChBT;QACAC;QACAC;QACAC;QACAC;QACAC;IACF;AAEJ,EAAC;AAED,OAAO,MAAMI,oBACX,CAAC,EACCT,YAAY,EACZC,eAAe,EACfC,qBAAqB,EACrBC,SAAS,EACTC,0BAA0B,EAC1BC,gCAAgC,EACP,GAC3B,OAAO,EAAEK,EAAE,EAAEC,GAAG,EAAE;QAChB,MAAMC,SAAShB,oBAAoB;YACjCiB,gBAAgBX;YAChBY,SAASH,IAAIG,OAAO;QACtB;QACA,MAAMC,wBAAwBlB,oBAAoBc,IAAIK,OAAO,EAAEJ;QAC/D,IAAIG,0BAA0BL,IAAI;YAChC,MAAMO,aAAa,IAAIC,QAAQ;gBAC7B,cAAcxB,eAAuB;oBACnCyB,MAAM;oBACNC,SAAS,IAAIC,KAAKA,KAAKC,GAAG,KAAK;oBAC/BC,MAAM;oBACNC,sBAAsB;oBACtBC,OAAO;gBACT;YACF;YAEAd,IAAIe,eAAe,GAAGf,IAAIe,eAAe,GACrC/B,aAAagB,IAAIe,eAAe,EAAET,cAClCA;QACN;QACA,MAAMU,kBAAyC,EAAE;QACjD3B,aAAa4B,OAAO,CAAC,CAACC;YACpBF,gBAAgBnB,IAAI,CAClBG,IAAIG,OAAO,CAACgB,MAAM,CAAC;gBACjB/B,YAAY8B;gBACZE,gBAAgB;gBAChBpB;gBACAqB,OAAO;oBACL,CAAC/B,gBAAgB,EAAE;wBACjBgC,IAAI;4BAACvB;yBAAG;oBACV;gBACF;YACF;QAEJ;QAEA,IAAI;YACF,MAAMwB,kBAAmB,MAAMvB,IAAIG,OAAO,CAACqB,IAAI,CAAC;gBAC9CpC,YAAYI;gBACZiC,OAAO;gBACPC,OAAO;gBACPN,gBAAgB;gBAChBpB;gBACAqB,OAAO;oBACL,CAAC,GAAG5B,2BAA2B,CAAC,EAAEC,kCAAkC,CAAC,EAAE;wBACrE4B,IAAI;4BAACvB;yBAAG;oBACV;gBACF;YACF;YAEAwB,iBAAiBI,MAAMV,QAAQ,CAACW;gBAC9BZ,gBAAgBnB,IAAI,CAClBG,IAAIG,OAAO,CAAC0B,MAAM,CAAC;oBACjB9B,IAAI6B,KAAK7B,EAAE;oBACXX,YAAYI;oBACZsC,MAAM;wBACJ,CAACrC,2BAA2B,EAAE,AAC5B,CAAA,AAAC,AAACmC,IAAgC,CAACnC,2BAA2B,IAGtD,EAAE,AAAD,EACTsC,MAAM,CAAC,CAACC;4BACR,IAAIA,GAAG,CAACtC,iCAAiC,EAAE;gCACzC,OAAOsC,GAAG,CAACtC,iCAAiC,KAAKK;4BACnD;wBACF;oBACF;oBACAqB,gBAAgB;oBAChBpB;gBACF;YAEJ;QACF,EAAE,OAAOiC,GAAG;YACVC,QAAQC,KAAK,CAAC,sCAAsCF;QACtD;QAEA,MAAMG,QAAQC,GAAG,CAACrB;IACpB,EAAC"}
1
+ {"version":3,"sources":["../../src/hooks/afterTenantDelete.ts"],"sourcesContent":["import type {\n CollectionAfterDeleteHook,\n CollectionConfig,\n JsonObject,\n PaginatedDocs,\n} from 'payload'\n\nimport { generateCookie, mergeHeaders } from 'payload'\n\nimport type { UserWithTenantsField } from '../types.js'\n\nimport { getCollectionIDType } from '../utilities/getCollectionIDType.js'\nimport { getTenantFromCookie } from '../utilities/getTenantFromCookie.js'\n\ntype Args = {\n collection: CollectionConfig\n enabledSlugs: string[]\n tenantFieldName: string\n tenantsCollectionSlug: string\n usersSlug: string\n usersTenantsArrayFieldName: string\n usersTenantsArrayTenantFieldName: string\n}\n/**\n * Add cleanup logic when tenant is deleted\n * - delete documents related to tenant\n * - remove tenant from users\n */\nexport const addTenantCleanup = ({\n collection,\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n}: Args): CollectionConfig => ({\n ...collection,\n hooks: {\n ...collection.hooks,\n afterDelete: [\n ...(collection.hooks?.afterDelete || []),\n afterTenantDelete({\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n }),\n ],\n },\n})\n\nexport const afterTenantDelete =\n ({\n enabledSlugs,\n tenantFieldName,\n tenantsCollectionSlug,\n usersSlug,\n usersTenantsArrayFieldName,\n usersTenantsArrayTenantFieldName,\n }: Omit<Args, 'collection'>): CollectionAfterDeleteHook =>\n async ({ id, req }) => {\n const idType = getCollectionIDType({\n collectionSlug: tenantsCollectionSlug,\n payload: req.payload,\n })\n const currentTenantCookieID = getTenantFromCookie(req.headers, idType)\n if (currentTenantCookieID === id) {\n const newHeaders = new Headers({\n 'Set-Cookie': generateCookie<string>({\n name: 'payload-tenant',\n expires: new Date(Date.now() - 1000),\n path: '/',\n returnCookieAsObject: false,\n value: '',\n }),\n })\n\n req.responseHeaders = req.responseHeaders\n ? mergeHeaders(req.responseHeaders, newHeaders)\n : newHeaders\n }\n const cleanupPromises: Promise<JsonObject>[] = []\n enabledSlugs.forEach((slug) => {\n cleanupPromises.push(\n req.payload.delete({\n collection: slug,\n overrideAccess: true,\n req,\n where: {\n [tenantFieldName]: {\n in: [id],\n },\n },\n }),\n )\n })\n\n try {\n const usersWithTenant = (await req.payload.find({\n collection: usersSlug,\n depth: 0,\n limit: 0,\n overrideAccess: true,\n req,\n where: {\n [`${usersTenantsArrayFieldName}.${usersTenantsArrayTenantFieldName}`]: {\n in: [id],\n },\n },\n })) as PaginatedDocs<UserWithTenantsField>\n\n usersWithTenant?.docs?.forEach((user) => {\n cleanupPromises.push(\n req.payload.update({\n id: user.id,\n collection: usersSlug,\n data: {\n [usersTenantsArrayFieldName]: (\n ((user as Record<string, unknown>)[usersTenantsArrayFieldName] as Record<\n string,\n string\n >[]) || []\n ).filter((row: Record<string, string>) => {\n if (row[usersTenantsArrayTenantFieldName]) {\n return row[usersTenantsArrayTenantFieldName] !== id\n }\n }),\n },\n overrideAccess: true,\n req,\n }),\n )\n })\n } catch (e) {\n console.error('Error deleting tenants from users:', e)\n }\n\n await Promise.all(cleanupPromises)\n }\n"],"names":["generateCookie","mergeHeaders","getCollectionIDType","getTenantFromCookie","addTenantCleanup","collection","enabledSlugs","tenantFieldName","tenantsCollectionSlug","usersSlug","usersTenantsArrayFieldName","usersTenantsArrayTenantFieldName","hooks","afterDelete","afterTenantDelete","id","req","idType","collectionSlug","payload","currentTenantCookieID","headers","newHeaders","Headers","name","expires","Date","now","path","returnCookieAsObject","value","responseHeaders","cleanupPromises","forEach","slug","push","delete","overrideAccess","where","in","usersWithTenant","find","depth","limit","docs","user","update","data","filter","row","e","console","error","Promise","all"],"mappings":"AAOA,SAASA,cAAc,EAAEC,YAAY,QAAQ,UAAS;AAItD,SAASC,mBAAmB,QAAQ,sCAAqC;AACzE,SAASC,mBAAmB,QAAQ,sCAAqC;AAWzE;;;;CAIC,GACD,OAAO,MAAMC,mBAAmB,CAAC,EAC/BC,UAAU,EACVC,YAAY,EACZC,eAAe,EACfC,qBAAqB,EACrBC,SAAS,EACTC,0BAA0B,EAC1BC,gCAAgC,EAC3B,GAAwB,CAAA;QAC7B,GAAGN,UAAU;QACbO,OAAO;YACL,GAAGP,WAAWO,KAAK;YACnBC,aAAa;mBACPR,WAAWO,KAAK,EAAEC,eAAe,EAAE;gBACvCC,kBAAkB;oBAChBR;oBACAC;oBACAC;oBACAC;oBACAC;oBACAC;gBACF;aACD;QACH;IACF,CAAA,EAAE;AAEF,OAAO,MAAMG,oBACX,CAAC,EACCR,YAAY,EACZC,eAAe,EACfC,qBAAqB,EACrBC,SAAS,EACTC,0BAA0B,EAC1BC,gCAAgC,EACP,GAC3B,OAAO,EAAEI,EAAE,EAAEC,GAAG,EAAE;QAChB,MAAMC,SAASf,oBAAoB;YACjCgB,gBAAgBV;YAChBW,SAASH,IAAIG,OAAO;QACtB;QACA,MAAMC,wBAAwBjB,oBAAoBa,IAAIK,OAAO,EAAEJ;QAC/D,IAAIG,0BAA0BL,IAAI;YAChC,MAAMO,aAAa,IAAIC,QAAQ;gBAC7B,cAAcvB,eAAuB;oBACnCwB,MAAM;oBACNC,SAAS,IAAIC,KAAKA,KAAKC,GAAG,KAAK;oBAC/BC,MAAM;oBACNC,sBAAsB;oBACtBC,OAAO;gBACT;YACF;YAEAd,IAAIe,eAAe,GAAGf,IAAIe,eAAe,GACrC9B,aAAae,IAAIe,eAAe,EAAET,cAClCA;QACN;QACA,MAAMU,kBAAyC,EAAE;QACjD1B,aAAa2B,OAAO,CAAC,CAACC;YACpBF,gBAAgBG,IAAI,CAClBnB,IAAIG,OAAO,CAACiB,MAAM,CAAC;gBACjB/B,YAAY6B;gBACZG,gBAAgB;gBAChBrB;gBACAsB,OAAO;oBACL,CAAC/B,gBAAgB,EAAE;wBACjBgC,IAAI;4BAACxB;yBAAG;oBACV;gBACF;YACF;QAEJ;QAEA,IAAI;YACF,MAAMyB,kBAAmB,MAAMxB,IAAIG,OAAO,CAACsB,IAAI,CAAC;gBAC9CpC,YAAYI;gBACZiC,OAAO;gBACPC,OAAO;gBACPN,gBAAgB;gBAChBrB;gBACAsB,OAAO;oBACL,CAAC,GAAG5B,2BAA2B,CAAC,EAAEC,kCAAkC,CAAC,EAAE;wBACrE4B,IAAI;4BAACxB;yBAAG;oBACV;gBACF;YACF;YAEAyB,iBAAiBI,MAAMX,QAAQ,CAACY;gBAC9Bb,gBAAgBG,IAAI,CAClBnB,IAAIG,OAAO,CAAC2B,MAAM,CAAC;oBACjB/B,IAAI8B,KAAK9B,EAAE;oBACXV,YAAYI;oBACZsC,MAAM;wBACJ,CAACrC,2BAA2B,EAAE,AAC5B,CAAA,AAAC,AAACmC,IAAgC,CAACnC,2BAA2B,IAGtD,EAAE,AAAD,EACTsC,MAAM,CAAC,CAACC;4BACR,IAAIA,GAAG,CAACtC,iCAAiC,EAAE;gCACzC,OAAOsC,GAAG,CAACtC,iCAAiC,KAAKI;4BACnD;wBACF;oBACF;oBACAsB,gBAAgB;oBAChBrB;gBACF;YAEJ;QACF,EAAE,OAAOkC,GAAG;YACVC,QAAQC,KAAK,CAAC,sCAAsCF;QACtD;QAEA,MAAMG,QAAQC,GAAG,CAACtB;IACpB,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAezD,eAAO,MAAM,iBAAiB,gEA6b5B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAezD,eAAO,MAAM,iBAAiB,gEAsf5B,CAAA"}