@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 2.75 kB
Source Map (JSON)
{"version":3,"file":"get-all.mjs","names":[],"sources":["../../../src/services/tenants/get-all.ts"],"sourcesContent":["import { normalizeCopy } from \"../../libs/i18n/index.js\";\nimport { UserTenantsRepository } from \"../../libs/repositories/index.js\";\nimport type { LucidAuth } from \"../../types/hono.js\";\nimport type { Tenant } from \"../../types/response.js\";\nimport { multiTenancyEnabled } from \"../../utils/helpers/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\n\n/**\n * Returns the tenants the given user has access to. Super admins have access to every\n * configured tenant, other users only the tenants they have a membership for.\n */\nconst getAll: ServiceFn<\n\t[\n\t\t{\n\t\t\tauthUser: LucidAuth;\n\t\t},\n\t],\n\tTenant[]\n> = async (context, data) => {\n\tif (!multiTenancyEnabled(context.config)) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: [],\n\t\t};\n\t}\n\n\tif (data.authUser.superAdmin) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: context.config.tenants.map((tenant) => ({\n\t\t\t\tkey: tenant.key,\n\t\t\t\tname: normalizeCopy(tenant.name),\n\t\t\t\tdefault: tenant.default ?? false,\n\t\t\t})),\n\t\t};\n\t}\n\n\tconst UserTenants = new UserTenantsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\tconst membershipsRes = await UserTenants.selectMultiple({\n\t\tselect: [\"tenant_key\"],\n\t\twhere: [\n\t\t\t{\n\t\t\t\tkey: \"user_id\",\n\t\t\t\toperator: \"=\",\n\t\t\t\tvalue: data.authUser.id,\n\t\t\t},\n\t\t],\n\t});\n\tif (membershipsRes.error) return membershipsRes;\n\n\tconst memberKeys = (membershipsRes.data ?? []).map(\n\t\t(membership) => membership.tenant_key,\n\t);\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: context.config.tenants\n\t\t\t.filter((tenant) => memberKeys.includes(tenant.key))\n\t\t\t.map((tenant) => ({\n\t\t\t\tkey: tenant.key,\n\t\t\t\tname: normalizeCopy(tenant.name),\n\t\t\t\tdefault: tenant.default ?? false,\n\t\t\t})),\n\t};\n};\n\nexport default getAll;\n"],"mappings":"uLAWA,MAAM,EAOF,MAAO,EAAS,IAAS,CAC5B,GAAI,CAAC,EAAoB,EAAQ,MAAM,EACtC,MAAO,CACN,MAAO,IAAA,GACP,KAAM,CAAC,CACR,EAGD,GAAI,EAAK,SAAS,WACjB,MAAO,CACN,MAAO,IAAA,GACP,KAAM,EAAQ,OAAO,QAAQ,IAAK,IAAY,CAC7C,IAAK,EAAO,IACZ,KAAM,EAAc,EAAO,IAAI,EAC/B,QAAS,EAAO,SAAW,EAC5B,EAAE,CACH,EAOD,IAAM,EAAiB,MAAM,IAJL,EACvB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAEuB,CAAC,CAAC,eAAe,CACvD,OAAQ,CAAC,YAAY,EACrB,MAAO,CACN,CACC,IAAK,UACL,SAAU,IACV,MAAO,EAAK,SAAS,EACtB,CACD,CACD,CAAC,EACD,GAAI,EAAe,MAAO,OAAO,EAEjC,IAAM,GAAc,EAAe,MAAQ,CAAC,EAAA,CAAG,IAC7C,GAAe,EAAW,UAC5B,EAEA,MAAO,CACN,MAAO,IAAA,GACP,KAAM,EAAQ,OAAO,QACnB,OAAQ,GAAW,EAAW,SAAS,EAAO,GAAG,CAAC,CAAC,CACnD,IAAK,IAAY,CACjB,IAAK,EAAO,IACZ,KAAM,EAAc,EAAO,IAAI,EAC/B,QAAS,EAAO,SAAW,EAC5B,EAAE,CACJ,CACD"}