1- import { BadRequestException , Injectable , Logger } from '@nestjs/common'
2- import { MailerService } from '@nestjs-modules/mailer'
3- import { get } from 'radash'
4- import { IdentitiesCrudService } from '~/management/identities/identities-crud.service'
5- import { PasswdadmService } from '~/settings/passwdadm.service'
6- import { IdentityState } from '~/management/identities/_enums/states.enum'
1+ import { BadRequestException , Injectable , Logger } from '@nestjs/common' ;
2+ import { MailerService } from '@nestjs-modules/mailer' ;
3+ import { get } from 'radash' ;
4+ import { IdentitiesCrudService } from '~/management/identities/identities-crud.service' ;
5+ import { PasswdadmService } from '~/settings/passwdadm.service' ;
6+ import { MailadmService } from '~/settings/mailadm.service' ;
7+ import { IdentityState } from '~/management/identities/_enums/states.enum' ;
78
89@Injectable ( )
910export class MailSendService {
10- private readonly logger = new Logger ( MailSendService . name )
11+ private readonly logger = new Logger ( MailSendService . name ) ;
1112
1213 public constructor (
1314 private readonly identities : IdentitiesCrudService ,
1415 private readonly passwdadmService : PasswdadmService ,
1516 private readonly mailer : MailerService ,
17+ private readonly mailadmService : MailadmService ,
1618 ) { }
1719
1820 public async sendTemplateToIdentities ( args : {
19- ids : string [ ]
20- template : string
21- variables ?: Record < string , string >
21+ ids : string [ ] ;
22+ template : string ;
23+ variables ?: Record < string , string > ;
24+ recipientAddressSource ?: 'principal' | 'personnel' ;
2225 } ) : Promise < { sent : number ; skipped : number } > {
23- const template = String ( args . template || '' ) . trim ( )
26+ const template = String ( args . template || '' ) . trim ( ) ;
2427 if ( ! template ) {
25- throw new BadRequestException ( 'Template requis' )
28+ throw new BadRequestException ( 'Template requis' ) ;
2629 }
27- const variables = ( args . variables && typeof args . variables === 'object' ? args . variables : { } ) as Record < string , any >
30+ const variables = ( args . variables && typeof args . variables === 'object' ? args . variables : { } ) as Record <
31+ string ,
32+ any
33+ > ;
2834
29- const policies : any = await this . passwdadmService . getPolicies ( )
30- const mailAttribute = String ( policies ?. emailAttribute || '' )
31- if ( ! mailAttribute ) {
32- throw new BadRequestException ( "Attribut mail alternatif non configuré (settings.passwordpolicies.emailAttribute)" )
35+ const smtp = await this . mailadmService . getParams ( ) ;
36+ const principalPath = String ( smtp ?. recipientJsonPathEmailPrincipal || '' ) . trim ( ) ;
37+ const personnelPath = String ( smtp ?. recipientJsonPathEmailPersonnel || '' ) . trim ( ) ;
38+ const policies : any = await this . passwdadmService . getPolicies ( ) ;
39+ const policyMailAttribute = String ( policies ?. emailAttribute || '' ) ;
40+
41+ const source = args . recipientAddressSource ;
42+ let mailPath : string ;
43+ if ( source === 'principal' ) {
44+ mailPath = principalPath ;
45+ if ( ! mailPath ) {
46+ throw new BadRequestException (
47+ "Chemin JSON « e-mail principal » non configuré (paramètres → Serveur SMTP → Chemin JSON de l'e-mail principal)." ,
48+ ) ;
49+ }
50+ } else if ( source === 'personnel' ) {
51+ mailPath = personnelPath ;
52+ if ( ! mailPath ) {
53+ throw new BadRequestException (
54+ "Chemin JSON « e-mail personnel » non configuré (paramètres → Serveur SMTP → Chemin JSON de l'e-mail personnel)." ,
55+ ) ;
56+ }
57+ } else {
58+ mailPath = policyMailAttribute ;
59+ if ( ! mailPath ) {
60+ throw new BadRequestException (
61+ 'Attribut mail alternatif non configuré (settings.passwordpolicies.emailAttribute)' ,
62+ ) ;
63+ }
3364 }
3465
35- const identities = await this . identities . model . find ( { _id : { $in : args . ids } , state : IdentityState . SYNCED } ) . lean ( )
66+ const identities = await this . identities . model . find ( { _id : { $in : args . ids } , state : IdentityState . SYNCED } ) . lean ( ) ;
3667 if ( ! identities ?. length ) {
37- throw new BadRequestException ( 'Aucune identité synchronisée trouvée' )
68+ throw new BadRequestException ( 'Aucune identité synchronisée trouvée' ) ;
3869 }
3970
40- let sent = 0
41- let skipped = 0
71+ let sent = 0 ;
72+ let skipped = 0 ;
4273
4374 for ( const identity of identities ) {
44- const to = get ( identity as any , mailAttribute ) as string
75+ const to = get ( identity as any , mailPath ) as string ;
4576 if ( ! to ) {
46- skipped ++
47- continue
77+ skipped ++ ;
78+ continue ;
4879 }
4980
5081 try {
@@ -56,15 +87,16 @@ export class MailSendService {
5687 identity,
5788 ...variables ,
5889 } ,
59- } )
60- sent ++
90+ } ) ;
91+ sent ++ ;
6192 } catch ( e ) {
62- this . logger . warn ( `Failed to send template <${ template } > to identity <${ ( identity as any ) ?. _id } >: ${ e ?. message || e } ` )
63- skipped ++
93+ this . logger . warn (
94+ `Failed to send template <${ template } > to identity <${ ( identity as any ) ?. _id } >: ${ e ?. message || e } ` ,
95+ ) ;
96+ skipped ++ ;
6497 }
6598 }
6699
67- return { sent, skipped }
100+ return { sent, skipped } ;
68101 }
69102}
70-
0 commit comments