{"version":3,"file":"permissions.mjs","names":[],"sources":["../../src/helpers/permissions/index.ts"],"sourcesContent":["import type { UserPermissions } from '~/modules/auth/types';\nimport { getCurrentPermissions } from '~/modules/auth/methods';\n\nimport { isSDKFunction, type LambdaFunction } from './decorator';\n\nlet currentUserPermissions: UserPermissions | undefined;\n\n/**\n * Refresh current user permissions\n */\nexport async function refreshPermissions(): Promise<void> {\n  currentUserPermissions = await getCurrentPermissions();\n}\n\n/**\n * Check if the user have the permission to use the function\n *\n * @param fnc The function\n * @param namespaceId The namespaces where the function can be used, check if at least one is true\n *\n * @returns If the user have the permission\n */\nexport function hasPermission(\n  fnc: LambdaFunction,\n  namespaceIds?: string[]\n): boolean {\n  if (!isSDKFunction(fnc)) {\n    // oxlint-disable-next-line no-console\n    console.warn(\n      `[ezr][hasPermission] Function ${fnc.name} is not compatible with ezrPermission decorator`\n    );\n    return true;\n  }\n\n  if (!currentUserPermissions) {\n    // oxlint-disable-next-line no-console\n    console.warn(\n      '[ezr][hasPermission] No current permissions set, please call refreshPermissions() first'\n    );\n    return false;\n  }\n\n  const { general, namespaces } = currentUserPermissions;\n  return fnc.ezrPermissions.every(({ permission, isNamespaced }) => {\n    if (!isNamespaced) {\n      return general[permission] || false;\n    }\n\n    // If no namespace provided, check if at least one is true\n    const nIds = namespaceIds ?? Object.keys(namespaces);\n\n    return nIds.some((id) => namespaces[id]?.[permission] || false);\n  });\n}\n"],"mappings":";;;;;AAKA,IAAI;;;;AAKJ,SAAsB;;;;sDAAoC;EACxD,yBAAyB,MAAM,sBAAsB;CACvD;;;;;;;;;;;AAUA,SAAgB,cACd,KACA,cACS;CACT,IAAI,CAAC,cAAc,GAAG,GAAG;EAEvB,QAAQ,KACN,iCAAiC,IAAI,KAAK,gDAC5C;EACA,OAAO;CACT;CAEA,IAAI,CAAC,wBAAwB;EAE3B,QAAQ,KACN,yFACF;EACA,OAAO;CACT;CAEA,MAAM,EAAE,SAAS,eAAe;CAChC,OAAO,IAAI,eAAe,OAAO,EAAE,YAAY,mBAAmB;EAChE,IAAI,CAAC,cACH,OAAO,QAAQ,eAAe;EAMhC,QAFa,kEAAgB,OAAO,KAAK,UAAU,EAExC,CAAC,MAAM,OAAO;;wCAAW,qEAAM,gBAAe;EAAI,CAAC;CAChE,CAAC;AACH"}