import { Injectable } from '@nestjs/common' ; import { AppConfig } from '@fc/app' ; import { validateDto } from '@fc/common' ; import { ConfigService , validationOptions } from '@fc/config' ; import { FeatureHandler , IFeatureHandler } from '@fc/feature-handler' ; import { IdentityProviderAdapterMongoService } from '@fc/identity-provider-adapter-mongo' ; import { LoggerService } from '@fc/logger' ; import { ConnectNotificationEmailParameters , MailerConfig , MailerNotificationConnectException , MailerService , MailFrom , MailTo , NoEmailException , } from '@fc/mailer' ; import { OidcSession } from '@fc/oidc' ; import { EmailsTemplates } from '../../enums' ; @ Injectable ( ) @ FeatureHandler ( 'core-fcp-send-email' ) export class CoreFcpSendEmailHandler implements IFeatureHandler < void , OidcSession > { private configMailer ; constructor ( private readonly logger , private readonly config , private readonly mailer , private readonly identityProvider ) { this . logger . setContext ( this . constructor . name ) ; this . configMailer = this . config . get < MailerConfig > ( 'Mailer' ) ; } getTodayFormattedDate ( datejs ) { const timeZone = 'Europe/Paris' ; const locateDate = datejs . toLocaleString ( [ ] , { timeZone } ) ; const date = new Date ( locateDate ) ; const day = `${ date . getDate ( ) }` . padStart ( 2 , '0' ) ; const month = `${ date . getMonth ( ) + 1 }` . padStart ( 2 , '0' ) ; const year = date . getFullYear ( ) ; const hours = `${ date . getHours ( ) }` . padStart ( 2 , '0' ) ; const minutes = `${ date . getMinutes ( ) }` . padStart ( 2 , '0' ) ; const formattedDate = `Le ${ day }/${ month }/${ year } à ${ hours }:${ minutes }` ; return formattedDate ; } private async getConnectNotificationEmailBodyContent ( session ) { const { fqdn } = this . config . get < AppConfig > ( 'App' ) ; const { idpId , spIdentity , spName } = session ; const { title : idpTitle } = await this . identityProvider . getById ( idpId ) ; const today = this . getTodayFormattedDate ( new Date ( ) ) ; const connectNotificationEmailParameters = { familyName : spIdentity . family_name , givenName : spIdentity . given_name , idpTitle , spName , today , fqdn , } ; const dtoValidationErrors = await validateDto ( connectNotificationEmailParameters , ConnectNotificationEmailParameters , validationOptions ) ; if ( dtoValidationErrors . length > 0 ) { throw new MailerNotificationConnectException ( ) ; } const fileName = EmailsTemplates . NOTIFICATION_EMAIL ; const htmlContent = this . mailer . mailToSend ( fileName , connectNotificationEmailParameters ) ; return htmlContent ; } see IVerifyFeatureHandlerHandleArgument todo author Hugues
     *  date param { OidcSession } session returns { Promise < void > } async handle ( session ) { const { from } = this . configMailer ; let errors = await validateDto ( from , MailFrom , validationOptions ) ; if ( errors . length > 0 ) { throw new NoEmailException ( ) ; } const { spName , spIdentity } = session ; const { email , given_name : givenName , family_name : familyName , } = spIdentity ; const mailTo = { email , name : `${ givenName } ${ familyName }` , } ; const to = [ mailTo ] ; errors = await validateDto ( mailTo , MailTo , validationOptions ) ; if ( errors . length > 0 ) { throw new NoEmailException ( ) ; } const body = await this . getConnectNotificationEmailBodyContent ( session ) ; this . logger . trace ( { from , to } ) ; this . mailer . send ( { from , to , subject : `Notification de connexion au service "${ spName }" grâce à FranceConnect+` , body , } ) ; } }