import{formatResponse as e}from"../utils/responseData.mjs";import{ErrorHandler as t}from"../utils/errors/ErrorHandler.mjs";import{hasPermission as n,intersectPermissions as r}from"../utils/permissions.mjs";import{sendEmail as i}from"../services/email.service.mjs";import{addNewAccessKey as a,deleteAccessKey as o,refreshAccessKey as s}from"../services/projectAccessKey.service.mjs";import{t as c}from"fastify-intlayer";const l=async(o,s)=>{let{user:l,project:u,roles:d,permissions:f}=o.locals||{},{grants:p,name:m,expiresAt:h}=o.body;if(!u)return t.handleGenericErrorResponse(s,`PROJECT_NOT_DEFINED`);if(!l)return t.handleGenericErrorResponse(s,`USER_NOT_DEFINED`);if(!n(d||[],`project:write`)({...o.locals,targetProjectIds:[u.id]}))return t.handleGenericErrorResponse(s,`PERMISSION_DENIED`);let g=r(f||[],p);try{let t=await a({name:m,expiresAt:h,grants:g},u.id,l),n=e({message:c({en:`Access key created successfully`,es:`Clave de acceso creada con éxito`,fr:`Clé d'accès créée avec succès`}),description:c({en:`The access key has been created successfully`,es:`La clave de acceso ha sido creada con éxito`,fr:`La clé d'accès a été créée avec succès`}),data:t});return i({type:`oAuthTokenCreated`,to:l.email,username:l.name,applicationName:t.name??t.clientId,scopes:t.grants,tokenDetailsUrl:`${process.env.APP_URL}/oauth2/token`,securityLogUrl:`${process.env.APP_URL}/security-log`,supportUrl:`${process.env.APP_URL}/support`}),s.send(n)}catch(e){return t.handleAppErrorResponse(s,e)}},u=async(r,i)=>{let{user:a,project:s,roles:l}=r.locals||{},{clientId:u}=r.body;if(!s)return t.handleGenericErrorResponse(i,`PROJECT_NOT_DEFINED`);if(!a)return t.handleGenericErrorResponse(i,`USER_NOT_DEFINED`);if(!u)return t.handleGenericErrorResponse(i,`CLIENT_ID_NOT_FOUND`);if(!n(l||[],`project:write`)({...r.locals,targetProjectIds:[s.id]}))return t.handleGenericErrorResponse(i,`PERMISSION_DENIED`);try{if(!await o(u,s,a.id))return t.handleGenericErrorResponse(i,`ACCESS_KEY_NOT_FOUND`,{clientId:u});let n=e({message:c({en:`Access key deleted successfully`,es:`Clave de acceso eliminada con éxito`,fr:`Clé d'accès supprimée avec succès`}),description:c({en:`The access key has been deleted successfully`,es:`La clave de acceso ha sido eliminada con éxito`,fr:`La clé d'accès a été supprimée avec succès`}),data:null});return i.send(n)}catch(e){return t.handleAppErrorResponse(i,e)}},d=async(r,i)=>{let{user:a,project:o,roles:l}=r.locals||{},{clientId:u}=r.body;if(!o)return t.handleGenericErrorResponse(i,`PROJECT_NOT_DEFINED`);if(!a)return t.handleGenericErrorResponse(i,`USER_NOT_DEFINED`);if(!u)return t.handleGenericErrorResponse(i,`CLIENT_ID_NOT_FOUND`);if(!n(l||[],`project:write`)({...r.locals,targetProjectIds:[o?.id]}))return t.handleGenericErrorResponse(i,`PERMISSION_DENIED`);try{let t=await s(u,o?.id,a?.id),n=e({message:c({en:`Access key refreshed successfully`,es:`Clave de acceso actualizada con éxito`,fr:`Clé d'accès actualisée avec succès`}),description:c({en:`The access key has been refreshed successfully`,es:`La clave de acceso ha sido actualizada con éxito`,fr:`La clé d'accès a été actualisée avec succès`}),data:t});return i.send(n)}catch(e){return t.handleAppErrorResponse(i,e)}};export{l as addNewAccessKey,u as deleteAccessKey,d as refreshAccessKey};
//# sourceMappingURL=projectAccessKey.controller.mjs.map