@@ -232,39 +232,110 @@ export abstract class AbstractIdentitiesService extends AbstractServiceSchema {
232232 }
233233 }
234234
235- protected async checkMail ( identity , data ) : Promise < boolean > {
236- let dataDup = 0 ;
237- if ( data . inetOrgPerson . hasOwnProperty ( 'mail' ) && data . inetOrgPerson . mail !== '' ) {
235+ /**
236+ * Vérifie l'unicité de l'email d'une identité
237+ *
238+ * @param identity - L'identité existante (optionnelle pour les nouvelles identités)
239+ * @param data - Les données contenant l'email à vérifier
240+ * @returns true si l'email est unique, false sinon
241+ */
242+ protected async checkMail (
243+ identity : Identities | null ,
244+ data : IdentitiesUpsertDto | any ,
245+ ) : Promise < boolean > {
246+ // Validation des paramètres d'entrée
247+ if ( ! data ?. inetOrgPerson ) {
248+ throw new BadRequestException ( 'inetOrgPerson data is required for mail check' ) ;
249+ }
250+
251+ // Si l'email n'est pas fourni ou est vide, on considère qu'il est valide (pas de vérification d'unicité)
252+ const emailToCheck = data . inetOrgPerson . mail ;
253+ if ( ! data . inetOrgPerson . hasOwnProperty ( 'mail' ) || emailToCheck === '' ) {
254+ return true ;
255+ }
256+
257+ // Validation du format email basique
258+ if ( typeof emailToCheck !== 'string' ) {
259+ throw new BadRequestException ( 'Invalid email format' ) ;
260+ }
261+
262+ try {
263+ let duplicateCount = 0 ;
264+
238265 if ( identity ) {
239- const f : any = { '_id' : { $ne : identity . _id } , 'state' : { $ne : IdentityState . DONT_SYNC } , 'deletedFlag' : { $ne : true } , 'inetOrgPerson.mail' : identity . inetOrgPerson . mail } ;
240- dataDup = await this . _model . countDocuments ( f ) . exec ( )
266+ // Vérification pour une identité existante (mise à jour)
267+ const updateFilter = {
268+ '_id' : { $ne : identity . _id } ,
269+ 'state' : { $ne : IdentityState . DONT_SYNC } ,
270+ 'deletedFlag' : { $ne : true } ,
271+ 'inetOrgPerson.mail' : identity . inetOrgPerson . mail ,
272+ } ;
273+ duplicateCount = await this . _model . countDocuments ( updateFilter ) . exec ( ) ;
241274 } else {
242- const f : any = { 'inetOrgPerson.mail' : data . inetOrgPerson . mail } ;
243- dataDup = await this . _model . countDocuments ( f ) . exec ( )
275+ // Vérification pour une nouvelle identité (création)
276+ const createFilter = {
277+ 'inetOrgPerson.mail' : data . inetOrgPerson . mail ,
278+ } ;
279+ duplicateCount = await this . _model . countDocuments ( createFilter ) . exec ( ) ;
244280 }
245281
246- }
247- if ( dataDup > 0 ) {
248- return false
249- } else {
250- return true
282+ // Retourne true si l'email est unique (aucun doublon trouvé)
283+ return duplicateCount === 0 ;
284+
285+ } catch ( error ) {
286+ this . logger . error ( `Error checking email uniqueness for email "${ emailToCheck } ": ${ error . message } ` ) ;
287+ throw new HttpException ( 'Failed to check email uniqueness' , 500 ) ;
251288 }
252289 }
253290
254- protected async checkUid ( identity , data ) : Promise < boolean > {
255- let dataDup = 0 ;
256- if ( identity ) {
257- const f : any = { '_id' : { $ne : identity . _id } , 'state' : { $ne : IdentityState . DONT_SYNC } , 'deletedFlag' : { $ne : true } , 'inetOrgPerson.uid' : identity . inetOrgPerson . uid } ;
258- dataDup = await this . _model . countDocuments ( f ) . exec ( )
259- } else {
260- const f : any = { 'inetOrgPerson.uid' : data . inetOrgPerson . uid } ;
261- dataDup = await this . _model . countDocuments ( f ) . exec ( )
291+ /**
292+ * Vérifie l'unicité de l'UID d'une identité
293+ *
294+ * @param identity - L'identité existante (optionnelle pour les nouvelles identités)
295+ * @param data - Les données contenant l'UID à vérifier
296+ * @returns true si l'UID est unique, false sinon
297+ */
298+ protected async checkUid (
299+ identity : Identities | null ,
300+ data : IdentitiesUpsertDto | any ,
301+ ) : Promise < boolean > {
302+ // Validation des paramètres d'entrée
303+ if ( ! data ?. inetOrgPerson ?. uid ) {
304+ throw new BadRequestException ( 'UID is required for uniqueness check' ) ;
262305 }
263306
264- if ( dataDup > 0 ) {
265- return false
266- } else {
267- return true
307+ const uidToCheck = identity ?. inetOrgPerson ?. uid || data . inetOrgPerson . uid ;
308+
309+ if ( ! uidToCheck || typeof uidToCheck !== 'string' ) {
310+ throw new BadRequestException ( 'Invalid UID format' ) ;
311+ }
312+
313+ try {
314+ let duplicateCount = 0 ;
315+
316+ if ( identity ) {
317+ // Vérification pour une identité existante (mise à jour)
318+ const updateFilter = {
319+ '_id' : { $ne : identity . _id } ,
320+ 'state' : { $ne : IdentityState . DONT_SYNC } ,
321+ 'deletedFlag' : { $ne : true } ,
322+ 'inetOrgPerson.uid' : identity ?. inetOrgPerson ?. uid ,
323+ } ;
324+ duplicateCount = await this . _model . countDocuments ( updateFilter ) . exec ( ) ;
325+ } else {
326+ // Vérification pour une nouvelle identité (création)
327+ const createFilter = {
328+ 'inetOrgPerson.uid' : data . inetOrgPerson . uid ,
329+ } ;
330+ duplicateCount = await this . _model . countDocuments ( createFilter ) . exec ( ) ;
331+ }
332+
333+ // Retourne true si l'UID est unique (aucun doublon trouvé)
334+ return duplicateCount === 0 ;
335+
336+ } catch ( error ) {
337+ this . logger . error ( `Error checking UID uniqueness for UID "${ uidToCheck } ": ${ error . message } ` ) ;
338+ throw new HttpException ( 'Failed to check UID uniqueness' , 500 ) ;
268339 }
269340 }
270341}
0 commit comments