import{logger as e}from"../logger/index.mjs";import{formatResponse as t}from"../utils/responseData.mjs";import{ErrorHandler as n}from"../utils/errors/ErrorHandler.mjs";import{hasPermission as r}from"../utils/permissions.mjs";import{createUser as i,getUserByEmail as a,getUserById as o,updateUserById as s}from"../services/user.service.mjs";import{mapUserToAPI as c}from"../utils/mapper/user.mjs";import{t as l}from"fastify-intlayer";const u=async(o,u)=>{let{roles:d}=o.locals||{},{email:f,emailList:p}=o.body;if(!f)return n.handleGenericErrorResponse(u,`USER_DATA_NOT_FOUND`);let m=Array.isArray(p)?p:[p],h=Object.fromEntries(m.map(e=>[e,!0]));try{let p=await a(f);if(!p)p=await i({email:f,emailsList:h}),e.info(`New user created and subscribed to newsletter: ${f}`);else{if(!r(d||[],`user:write`)({...o.locals,targetUsers:[p]}))return n.handleGenericErrorResponse(u,`PERMISSION_DENIED`);p=await s(p.id,{emailsList:{...p.emailsList,...h}}),e.info(`User subscribed to newsletter: ${f}`)}let m=c(p),g=t({message:l({en:`Successfully subscribed to newsletter`,fr:`Abonnement à la newsletter réussi`,es:`Suscripción al boletín exitosa`}),description:l({en:`You have been successfully subscribed to our newsletter`,fr:`Vous avez été abonné avec succès à notre newsletter`,es:`Te has suscrito exitosamente a nuestro boletín`}),data:m});return u.send(g)}catch(e){return n.handleAppErrorResponse(u,e)}},d=async(i,a)=>{let{userId:u,emailList:d}=i.body,{roles:f}=i.locals||{};if(!u)return n.handleGenericErrorResponse(a,`USER_DATA_NOT_FOUND`);try{let p=await o(u);if(!p)return n.handleGenericErrorResponse(a,`USER_NOT_FOUND`);if(!r(f||[],`user:write`)({...i.locals,targetUsers:[p]}))return n.handleGenericErrorResponse(a,`PERMISSION_DENIED`);let m=Array.isArray(d)?d:[d],h=Object.fromEntries(m.map(e=>[e,!1])),g=await s(p.id,{emailsList:{...p.emailsList,...h}});e.info(`User unsubscribed from newsletter: ${g.email}`);let _=c(g),v=t({message:l({en:`Successfully unsubscribed from newsletter`,fr:`Désabonnement de la newsletter réussi`,es:`Cancelación de suscripción al boletín exitosa`}),description:l({en:`You have been successfully unsubscribed from our newsletter`,fr:`Vous avez été désabonné avec succès de notre newsletter`,es:`Te has desuscrito exitosamente de nuestro boletín`}),data:_});return a.send(v)}catch(e){return n.handleAppErrorResponse(a,e)}},f=async(e,i)=>{let o=e.locals?.user?.email,{roles:s}=e.locals||{};if(!o)return n.handleGenericErrorResponse(i,`USER_DATA_NOT_FOUND`);try{let u=await a(o);if(!u)return n.handleGenericErrorResponse(i,`USER_NOT_FOUND`);if(!r(s||[],`user:read`)({...e.locals,targetUsers:[u]}))return n.handleGenericErrorResponse(i,`PERMISSION_DENIED`);let d=c(u),f=t({message:l({en:`Newsletter subscription status retrieved`,fr:`Statut d'abonnement à la newsletter récupéré`,es:`Estado de suscripción al boletín obtenido`}),data:d});return i.send(f)}catch(e){return n.handleAppErrorResponse(i,e)}};export{f as getNewsletterStatus,u as subscribeToNewsletter,d as unsubscribeFromNewsletter};
//# sourceMappingURL=newsletter.controller.mjs.map