@@ -5,6 +5,7 @@ import { User } from "@TenshiJS/entity/User";
55import GenericRepository from "@TenshiJS/generics/Repository/GenericRepository" ;
66import EmailService from "@TenshiJS/services/EmailServices/EmailService" ;
77import { getEmailTemplate , getMessageEmail } from "@TenshiJS/utils/htmlTemplateUtils" ;
8+ import { config } from "@index/index" ;
89
910// Sends an email and stores a user notification
1011export async function sendEmailAndUserNotification ( userNotifications : any , variables : any , needsAddUserNotification : boolean ) : Promise < UserNotification | null > {
@@ -59,7 +60,10 @@ export async function sendEmailAndUserNotification(userNotifications: any, varia
5960 // Send the email
6061 const emailService = EmailService . getInstance ( ) ;
6162 await emailService . sendEmail ( {
62- toMail : user . email ,
63+ //localhost
64+ toMail : [ user . email ] ,
65+ //prod
66+ //toMail: [user.email, config.SUPER_ADMIN.USER_EMAIL],
6367 //toMail: "vitalinkcr2@gmail.com",
6468 subject,
6569 message : htmlBody ,
@@ -75,4 +79,154 @@ export async function sendEmailAndUserNotification(userNotifications: any, varia
7579 return null ;
7680 }
7781
78- }
82+ }
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+ export interface NotificationContext {
103+ subject : string ;
104+ title : string ;
105+ bodyContent : string ;
106+ htmlBody : string ;
107+ variables : Record < string , any > ;
108+ template : string ;
109+ user : User ;
110+ }
111+
112+ export async function buildNotificationContext (
113+ userNotifications : any ,
114+ variables : any
115+ ) : Promise < NotificationContext | null > {
116+ const repositoryNotification = new GenericRepository ( Notification ) ;
117+ const repositoryUser = new GenericRepository ( User ) ;
118+
119+ const notification : Notification = await repositoryNotification . findByCode (
120+ userNotifications . notification , true
121+ ) ;
122+
123+ if ( ! notification ) return null ;
124+
125+ const user : User = await repositoryUser . findById (
126+ userNotifications . user_receive , true
127+ ) ;
128+
129+ const useJsonTemplate = notification . text_from_email_message_json ;
130+ const template = userNotifications . override_template // <-- permite override desde el controller
131+ ?? notification . email_template
132+ ?? ConstTemplate . GENERIC_TEMPLATE_EMAIL ;
133+
134+ const subject = useJsonTemplate && userNotifications . acronymous
135+ ? getMessageEmail ( userNotifications . acronymous , user . language ! , "Subject" )
136+ : notification . subject ;
137+
138+ const bodyContent = useJsonTemplate && userNotifications . acronymous
139+ ? getMessageEmail ( userNotifications . acronymous , user . language ! , "EmailMessage" )
140+ : notification . email_message ;
141+
142+ const title = useJsonTemplate && userNotifications . acronymous
143+ ? getMessageEmail ( userNotifications . acronymous , user . language ! , "Title" )
144+ : notification . subject ;
145+
146+ const mergedVariables = {
147+ userName : user . name ,
148+ emailSubject : title ,
149+ emailContent : bodyContent ,
150+ actionTitle : notification . action_text ,
151+ actionUrl : notification . action_url ,
152+ ...variables ,
153+ } ;
154+
155+ const htmlBody = await getEmailTemplate ( template , user . language , mergedVariables ) ;
156+
157+ return {
158+ subject,
159+ title,
160+ bodyContent : bodyContent ? bodyContent : '' ,
161+ htmlBody,
162+ variables : mergedVariables ,
163+ template,
164+ user,
165+ } ;
166+ }
167+
168+
169+
170+ export async function sendEmailNotification (
171+ userNotifications : any ,
172+ variables : any ,
173+ needsAddUserNotification : boolean
174+ ) : Promise < UserNotification | null > {
175+ const repositoryNotification = new GenericRepository ( Notification ) ;
176+ const repositoryUserNotification = new GenericRepository ( UserNotification ) ;
177+
178+ const notification : Notification = await repositoryNotification . findByCode (
179+ userNotifications . notification , true
180+ ) ;
181+
182+ if ( notification ?. required_send_email ) {
183+ const ctx = await buildNotificationContext ( userNotifications , variables ) ;
184+
185+ if ( ctx ) {
186+ const emailService = EmailService . getInstance ( ) ;
187+ await emailService . sendEmail ( {
188+ toMail : [ ctx . user . email , config . SUPER_ADMIN . USER_EMAIL ] ,
189+ subject : ctx . subject ,
190+ message : ctx . htmlBody ,
191+ attachments : [ ] ,
192+ } ) ;
193+ }
194+ }
195+
196+ if ( needsAddUserNotification ) {
197+ return await repositoryUserNotification . add ( userNotifications ) ;
198+ }
199+
200+ return null ;
201+ }
202+
203+
204+
205+ /*
206+
207+ // Ejemplo desde el controller — enviar con template personalizado
208+ const ctx = await buildNotificationContext(
209+ {
210+ user_receive: appointment.customer.id,
211+ notification: "appointmentStep8",
212+ acronymous: "appointmentStep8",
213+ override_template: "credit_summary_template", // <-- tu template custom
214+ },
215+ {
216+ patientName: appointment.customer.name,
217+ procedureName: appointment.package?.procedure?.name,
218+ totalProcedure: 1500,
219+ }
220+ );
221+
222+ if (ctx) {
223+ const emailService = EmailService.getInstance();
224+ await emailService.sendEmail({
225+ toMail: [ctx.user.email],
226+ subject: ctx.subject,
227+ message: ctx.htmlBody,
228+ attachments: [],
229+ });
230+ }
231+
232+ */
0 commit comments