import { Injectable , InternalServerErrorException , NotAcceptableException , NotFoundException } from '@nestjs/common' ; import { MailerService } from '@nest-modules/mailer' ; import { TemplateRepository } from '../repository/template.repository' ; import { Transactional } from 'typeorm-transactional-cls-hooked' ; import { TemplateDTO } from '../dto/templates.dto' ; import { TemplateMapper } from '../mapper/template.mapper' ; import { AppConfigService as ConfigService } from '../../ConfigModule/service/app-config.service' ; import { ContactUsDTO } from '../dto/contactus.dto' ; import { EmailDTO } from '../dto/email.dto' ; @ Injectable ( ) export class MessageService { constructor ( private readonly mailerService , private readonly appConfigService , private readonly templateRepository , private readonly mapperTemplate ) { } public async sendEmail ( email ) { try { await this . mailerService . sendMail ( { to : this . appConfigService . emailContactUs , from : email . email , subject : email . title , html : email . message , } ) ; } catch ( e ) { throw new InternalServerErrorException ( e ) ; } } public async sendContactUsEmail ( contactUs ) { try { await this . mailerService . sendMail ( { to : this . appConfigService . emailContactUs , from : contactUs . email , subject : 'Fale conosco' , template : 'contact-us' , context : { name : contactUs . name , cellphone : contactUs . cellphone , message : contactUs . message , } , } ) ; } catch ( e ) { throw new InternalServerErrorException ( e ) ; } } public async sendMessage ( messageModel , templateName , contactEmail ) { try { const model = await this . templateExists ( templateName ) ; const message = this . transformObjectToArray ( messageModel ) ; const htmlMessage = this . substituteInterpolationPerValues ( model . template , message ) ; await this . mailerService . sendMail ( { to : this . appConfigService . emailContactUs , from : contactEmail , subject : model . title , html : htmlMessage , } ) ; } catch ( error ) { throw new InternalServerErrorException ( error ) ; } } public async getAllTemplates ( ) { try { return this . mapperTemplate . toDtoList ( await this . templateRepository . find ( ) ) ; } catch ( error ) { throw new InternalServerErrorException ( error ) ; } } @ Transactional ( ) public async editTemplate ( updatedTemplate ) { try { const template = await this . templateExists ( updatedTemplate . name ) ; return await this . templateRepository . save ( { ... template , ... updatedTemplate , } ) ; } catch ( error ) { throw new InternalServerErrorException ( error ) ; } } @ Transactional ( ) public async createTemplate ( template ) { try { if ( await this . templateRepository . findByName ( template . name ) ) { throw new NotAcceptableException ( 'Templates cannot have duplicate names, please rename and try again.' ) ; } return this . mapperTemplate . toDto ( await this . templateRepository . save ( template ) ) ; } catch ( error ) { throw new InternalServerErrorException ( error ) ; } } private async templateExists ( templateName ) { const template = await this . templateRepository . findByName ( templateName ) ; if ( ! template ) { throw new NotFoundException ( 'Template not found' ) ; } return template ; } private substituteInterpolationPerValues ( html , values ) { return html . replace ( /({\d})/g , ( i ) => { return values [ i . replace ( /{/ , '' ) . replace ( /}/ , '' ) ] ; } ) ; } private transformObjectToArray ( model ) { return Object . keys ( model ) . map ( ( key ) => model [ key ] ) ; } }