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{hasPermission as i}from"../utils/permissions.mjs";import{countUsers as a,createUser as o,deleteUser as s,findUsers as c,getUserByEmail as l,getUserById as u,updateUserById as d}from"../services/user.service.mjs";import{mapUserToAPI as f,mapUsersToAPI as p}from"../utils/mapper/user.mjs";import{sendEmail as m}from"../services/email.service.mjs";import{getUserFiltersAndPagination as h}from"../utils/filtersAndPagination/getUserFiltersAndPagination.mjs";import{t as g}from"fastify-intlayer";const _=async(e,t)=>{let i=e.body;if(!i)return r.handleGenericErrorResponse(t,`USER_NOT_DEFINED`);try{let e=await o(i);await m({type:`welcome`,to:e.email,username:e.name,loginLink:`${process.env.APP_URL}/auth/login`});let r=f(e),a=n({message:g({en:`User created`,fr:`Utilisateur créé`,es:`Usuario creado`}),description:g({en:`User created successfully`,fr:`Utilisateur créé avec succès`,es:`Usuario creado con éxito`}),data:r});return t.send(a)}catch(e){return r.handleAppErrorResponse(t,e)}},v=async(e,n)=>{let{user:o,roles:s}=e.locals||{};if(!o)return r.handleGenericErrorResponse(n,`USER_NOT_DEFINED`);let{filters:l,sortOptions:u,pageSize:d,skip:f,page:m,getNumberOfPages:g}=h(e);try{let o=await c(l,f,d,u);if(!i(s||[],`user:read`)({...e.locals,targetUsers:o}))return r.handleGenericErrorResponse(n,`PERMISSION_DENIED`);let h=await a(l),_=t({data:p(o),page:m,pageSize:d,totalPages:g(h),totalItems:h});return n.send(_)}catch(e){return r.handleAppErrorResponse(n,e)}},y=async(e,t)=>{let{userId:i}=e.params;try{let e=await u(i);if(!e)return r.handleGenericErrorResponse(t,`USER_NOT_DEFINED`);let a=n({data:f(e)});return t.send(a)}catch(e){return r.handleAppErrorResponse(t,e)}},b=async(e,t)=>{let{email:a}=e.params,{roles:o}=e.locals||{};try{let s=await l(a);if(!s)return r.handleGenericErrorResponse(t,`USER_NOT_DEFINED`);if(!i(o||[],`user:read`)({...e.locals,targetUsers:[s]}))return r.handleGenericErrorResponse(t,`PERMISSION_DENIED`);let c=n({data:f(s)});return t.send(c)}catch(e){return r.handleAppErrorResponse(t,e)}},x=async(t,a)=>{let o=t.body,{user:s,roles:c}=t.locals||{};if(!s)return r.handleGenericErrorResponse(a,`USER_NOT_DEFINED`);if(typeof o!=`object`)return r.handleGenericErrorResponse(a,`USER_DATA_NOT_FOUND`);if(!o.id)return r.handleGenericErrorResponse(a,`USER_INVALID_FIELDS`);let l=await u(o.id);if(!l)return r.handleGenericErrorResponse(a,`USER_NOT_FOUND`);if(!i(c||[],`user:write`)({...t.locals,targetUsers:[l]}))return r.handleGenericErrorResponse(a,`PERMISSION_DENIED`);try{let t=await d(l.id,o);e.info(`User updated: Name: ${t.name}, id: ${String(t.id)}`);let r=f(t),i=n({message:g({en:`User updated`,fr:`Utilisateur mis à jour`,es:`Usuario actualizado`}),description:g({en:`User updated successfully`,fr:`Utilisateur mis à jour avec succès`,es:`Usuario actualizado con éxito`}),data:r});return a.send(i)}catch(e){return r.handleAppErrorResponse(a,e)}},S=async(e,t)=>{let{userId:a}=e.params,{roles:o}=e.locals||{};try{let c=await u(a);if(!c)return r.handleGenericErrorResponse(t,`USER_NOT_FOUND`);if(!i(o||[],`user:admin`)({...e.locals,targetUsers:[c]}))return r.handleGenericErrorResponse(t,`PERMISSION_DENIED`);await s(a);let l=f(c),d=n({message:g({en:`User deleted`,fr:`Utilisateur supprimé`,es:`Usuario eliminado`}),description:g({en:`User deleted successfully`,fr:`Utilisateur supprimé avec succès`,es:`Usuario eliminado con éxito`}),data:l});return t.send(d)}catch(e){return r.handleAppErrorResponse(t,e)}};let C=[];const w=e=>{let t=C.filter(t=>String(t.userId)===String(e.id));for(let n of t)e.emailVerified&&n.res.raw.write(`data: ${JSON.stringify({userId:e.id,status:`verified`})}\n\n`)},T=async(t,n)=>{n.raw.setHeader(`Content-Type`,`text/event-stream;charset=utf-8`),n.raw.setHeader(`Cache-Control`,`no-cache, no-transform`),n.raw.setHeader(`Connection`,`keep-alive`),n.raw.setHeader(`X-Accel-Buffering`,`no`),n.raw.write(`:

`),n.raw.flushHeaders?.();let{userId:r}=t.params,i=Date.now(),a=await u(r);if(!a){e.error(`User not found - User ID: ${r}`),n.raw.write(`data: ${JSON.stringify({userId:r,status:`error`})}\n\n`),n.raw.end();return}let o={id:i,userId:r,res:{raw:n.raw}};C.push(o),w(a),t.raw.on(`close`,()=>{C=C.filter(e=>e.id!==i)})};export{_ as createUser,S as deleteUser,b as getUserByEmail,y as getUserById,v as getUsers,w as sendVerificationUpdate,x as updateUser,T as verifyEmailStatusSSE};
//# sourceMappingURL=user.controller.mjs.map