import{logger as e}from"../logger/index.mjs";import{formatPaginatedResponse as t,formatResponse as n}from"../utils/responseData.mjs";import{ErrorHandler as r}from"../utils/errors/ErrorHandler.mjs";import{countTags as i,createTag as a,deleteTagById as o,findTags as s,getTagById as c,updateTagById as l}from"../services/tag.service.mjs";import{hasPermission as u}from"../utils/permissions.mjs";import{getTagFiltersAndPagination as d}from"../utils/filtersAndPagination/getTagFiltersAndPagination.mjs";import{mapTagToAPI as f,mapTagsToAPI as p}from"../utils/mapper/tag.mjs";import{t as m}from"fastify-intlayer";const h=async(e,n)=>{let{user:a,organization:o,roles:c}=e.locals||{},{filters:l,sortOptions:f,pageSize:m,skip:h,page:g,getNumberOfPages:_}=d(e);if(!a)return r.handleGenericErrorResponse(n,`USER_NOT_DEFINED`);if(!o)return r.handleGenericErrorResponse(n,`ORGANIZATION_NOT_DEFINED`);try{let a=await s(l,h,m,f);if(!u(c||[],`tag:read`)({...e.locals,targetTags:a}))return r.handleGenericErrorResponse(n,`PERMISSION_DENIED`);let o=await i(l),d=t({data:p(a),page:g,pageSize:m,totalPages:_(o),totalItems:o});return n.send(d)}catch(e){return r.handleAppErrorResponse(n,e)}},g=async(e,t)=>{let{organization:i,project:o,user:s,roles:c}=e.locals||{},l=e.body;if(!s)return r.handleGenericErrorResponse(t,`USER_NOT_DEFINED`);if(!i)return r.handleGenericErrorResponse(t,`ORGANIZATION_NOT_DEFINED`);if(!o)return r.handleGenericErrorResponse(t,`PROJECT_NOT_DEFINED`);if(!l)return r.handleGenericErrorResponse(t,`PROJECT_DATA_NOT_FOUND`);let d={creatorId:s.id,organizationId:i.id,projectId:o.id,...l};if(!u(c||[],`tag:admin`)({...e.locals,targetTags:[d]}))return r.handleGenericErrorResponse(t,`PERMISSION_DENIED`);try{let e=f(await a(d)),r=n({message:m({en:`Tag created successfully`,fr:`Tag créé avec succès`,es:`Tag creado con éxito`}),description:m({en:`Your tag has been created successfully`,fr:`Votre tag a été créé avec succès`,es:`Su tag ha sido creado con éxito`}),data:e});return t.send(r)}catch(e){return r.handleAppErrorResponse(t,e)}},_=async(e,t)=>{let{tagId:i}=e.params,{organization:a,user:o,roles:s}=e.locals||{};if(!o)return r.handleGenericErrorResponse(t,`USER_NOT_DEFINED`);if(!a)return r.handleGenericErrorResponse(t,`ORGANIZATION_NOT_DEFINED`);try{let o={_id:i,name:e.body.name,key:e.body.key,description:e.body.description,instructions:e.body.instructions},d=await c(i);if(!u(s||[],`tag:write`)({...e.locals,targetTags:[d]}))return r.handleGenericErrorResponse(t,`PERMISSION_DENIED`);if(String(d.organizationId)!==String(a.id))return r.handleGenericErrorResponse(t,`TAG_NOT_IN_ORGANIZATION`);let p=f(await l(o._id,o)),h=n({message:m({en:`Tag updated successfully`,fr:`Tag mis à jour avec succès`,es:`Tag actualizado con éxito`}),description:m({en:`Your tag has been updated successfully`,fr:`Votre tag a été mis à jour avec succès`,es:`Su tag ha sido actualizado con éxito`}),data:p});return t.send(h)}catch(e){return r.handleAppErrorResponse(t,e)}},v=async(t,i)=>{let{user:a,organization:s,roles:l}=t.locals||{},{tagId:d}=t.params;if(!a)return r.handleGenericErrorResponse(i,`USER_NOT_DEFINED`);if(!s)return r.handleGenericErrorResponse(i,`ORGANIZATION_NOT_DEFINED`);if(!d)return r.handleGenericErrorResponse(i,`TAG_ID_NOT_FOUND`);try{let a=await c(d);if(!u(l||[],`tag:admin`)({...t.locals,targetTags:[a]}))return r.handleGenericErrorResponse(i,`PERMISSION_DENIED`);if(String(a.organizationId)!==String(s.id))return r.handleGenericErrorResponse(i,`TAG_NOT_IN_ORGANIZATION`);let p=await o(d);if(!p)return r.handleGenericErrorResponse(i,`TAG_NOT_FOUND`,{tagId:d});e.info(`Tag deleted: ${String(p.id)}`);let h=f(p),g=n({message:m({en:`Tag deleted successfully`,fr:`Tag supprimé avec succès`,es:`Tag eliminado con éxito`}),description:m({en:`Your tag has been deleted successfully`,fr:`Votre tag a été supprimé avec succès`,es:`Su tag ha sido eliminado con éxito`}),data:h});return i.send(g)}catch(e){return r.handleAppErrorResponse(i,e)}};export{g as addTag,v as deleteTag,h as getTags,_ as updateTag};
//# sourceMappingURL=tag.controller.mjs.map