@@ -184,47 +184,59 @@ module.exports = [
184184 * @param {string } options.job.name - The name of the job.
185185 * @returns {Promise<void> }
186186 */
187- async function generateEmail ( targetPath , name , { templateEmail, job } ) {
188- try {
189- let content ;
190- const stubPath = path . join ( elementPath , 'emails/email.stub' ) ;
191187
192- if ( await fs . pathExists ( stubPath ) ) {
193- content = await fs . readFile ( stubPath , 'utf8' ) ;
194- } else {
195- console . error ( 'No template found' ) ;
188+ async function generateEmail ( targetPath , name , options = { } ) {
189+ const { templateEmail, job } = options ;
190+ const stubPath = path . join ( elementPath , "emails/email.stub" ) ; // Giả sử elementPath đã được định nghĩa
191+
192+ try {
193+ const [ stubContent , templateContent , jobResult ] = await Promise . allSettled ( [
194+ fs . readFile ( stubPath , "utf8" ) ,
195+ templateEmail && templateEmail . path && templateEmail . name
196+ ? fs . readFile ( path . join ( elementPath , "emails/template.stub" ) , "utf8" )
197+ : Promise . resolve ( "" ) ,
198+ job && job . path && job . name
199+ ? generateJob ( job . path , job . name , {
200+ importModule : `const { sendMail } = require("@iKernel/mail");` ,
201+ handle : `await sendMail(job.data);`
202+ } )
203+ : Promise . resolve ( "" )
204+ ] ) ;
205+
206+ if ( stubContent . status !== "fulfilled" ) {
207+ throw new Error ( `Failed to read email stub: ${ stubContent . reason } ` ) ;
196208 }
197- let moreReplace = 'sendMail({to: data.email,subject: "Welcome to Bamimi land", // more});'
198- let more = 'text: text'
209+ let content = stubContent . value ;
210+ let template = { key : "html" , value : `const html = 'Hello world!';` } ;
211+
199212 if ( templateEmail && templateEmail . path && templateEmail . name ) {
200- more = 'html: html'
201- const templatePath = path . join ( elementPath , 'emails/ template.stub' ) ;
202- let contentTemplate = await fs . readFile ( templatePath , 'utf8' ) ;
213+ if ( templateContent . status !== "fulfilled" ) {
214+ throw new Error ( `Failed to read email template: ${ templateContent . reason } ` ) ;
215+ }
203216
204- content = content . replace ( ' // render' , `const { renderTemplate } = require("../../utils /mail");` ) ;
205- content = content . replace ( '// content' , `const html = renderTemplate(" ${ templateEmail . name } .ejs");` ) ;
217+ content = content . replace ( " // render" , `const { renderTemplate } = require("@iKernel /mail");` ) ;
218+ await fs . outputFile ( templateEmail . path , templateContent . value ) ;
206219
207- moreReplace = moreReplace . replace ( '// more' , more ) ;
220+ template = {
221+ key : "html" ,
222+ value : `const html = renderTemplate("${ templateEmail . name } .ejs");`
223+ } ;
208224
209- await fs . outputFile ( templateEmail . path , contentTemplate ) ;
210225 console . log ( `Email template ${ templateEmail . name } created successfully at ${ templateEmail . path } ` ) ;
211226 }
212227
213228 if ( job && job . path && job . name ) {
214- await generateJob ( job . path , job . name , {
215- importModule : `const { sendMail } = require("../../utils/mail");` ,
216- handle : `await sendMail(job.data);`
217- } )
218- content = content . replace ( '// more' , `QueueManager.singleton().getQueue("${ job . name } ").add("${ job . name } ", {to: data.email, subject: "Welcome to Bamimi land", ${ more } });` ) ;
219- content = content . replace ( '// queue\n' , `const { QueueManager } = require("@knfs-tech/bamimi-schedule")` ) ;
229+ if ( jobResult . status !== "fulfilled" ) {
230+ throw new Error ( `Failed to generate job: ${ jobResult . reason } ` ) ;
231+ }
232+ content = content . replace ( "// queue" , `const QueueManager = require("@iKernel/queue")();` )
233+ . replace ( "// more" , `QueueManager.getQueue("${ job . name } ").add("${ job . name } ", {to: data.email, subject: "Welcome to Bamimi land", ${ template . key } });` ) ;
234+ } else {
235+ content = content . replace ( "// queue" , `const { sendMail } = require("@iKernel/mail");` )
236+ . replace ( "// more" , `sendMail({to: data.email, subject: "Welcome to Bamimi land", ${ template . key } });` ) ;
220237 }
221238
222- content = content . replace ( '// render\n' , `` ) ;
223- content = content . replace ( '// content' , `const text = 'Hello word!';` ) ;
224- moreReplace = moreReplace . replace ( '// more' , more ) ;
225- content = content . replace ( '// more' , moreReplace ) ;
226- content = content . replace ( '// queue\n' , `` ) ;
227- content = content . replace ( '// queueContent\n' , `` ) ;
239+ content = content . replace ( "// render" , "" ) . replace ( "// content" , template . value ) ;
228240
229241 await fs . outputFile ( targetPath , content ) ;
230242 console . log ( `Email ${ name } created successfully at ${ targetPath } ` ) ;
@@ -233,7 +245,6 @@ async function generateEmail(targetPath, name, { templateEmail, job }) {
233245 throw error ;
234246 }
235247}
236-
237248/**
238249 * Generates a job file.
239250 * @param {string } targetPath - The path where the job file will be created.
@@ -242,9 +253,10 @@ async function generateEmail(targetPath, name, { templateEmail, job }) {
242253 * @param {string } options.importModule - The module to import.
243254 * @param {string } options.handle - The handle function.
244255 * @param {boolean } options.isSchedule - create schedule
256+ * @param {string } options.queueManager - The name of queue manager
245257 * @returns {Promise<void> }
246258 */
247- async function generateJob ( targetPath , name , options = { importModule, handle, isSchedule : false } ) {
259+ async function generateJob ( targetPath , name , options = { importModule, handle, isSchedule : false , queueManager : "bullmq" } ) {
248260 try {
249261 moduleAlias . addAliases ( {
250262 '@iApp' : path . resolve ( process . cwd ( ) , 'src/app' ) ,
@@ -267,10 +279,11 @@ async function generateJob(targetPath, name, options = { importModule, handle, i
267279 console . error ( 'No template found.' ) ;
268280 }
269281
270- content = content . replace ( '// import\n' , options . importModule ?? '' ) ;
271- content = content . replace ( '// handle\n' , options . handle ?? '' ) ;
282+ content = content . replace ( '// import\n' , options . importModule || '' ) ;
283+ content = content . replace ( '// handle\n' , options . handle || '' ) ;
272284 content = content . replace ( '// jobName' , name ) ;
273285 content = content . replace ( '// queueName' , name ) ;
286+ content = content . replace ( '// queueManager' , options . queueManager || "bullmq" ) ;
274287
275288 await fs . outputFile ( targetPath , content ) ;
276289 console . log ( `Job ${ name } created successfully at ${ targetPath } ` ) ;
0 commit comments