@@ -210,25 +210,66 @@ export abstract class AbstractIdentitiesService extends AbstractServiceSchema {
210210 }
211211
212212 /**
213- * Check if mail and uid are unique. If mail is empty it is not checked
214- * @param data
215- * @private
213+ * Vérifie l'unicité de l'email et de l'UID d'une identité
214+ * Si l'email est vide, seul l'UID est vérifié
215+ *
216+ * @param data - Les données contenant l'ID, l'UID et optionnellement l'email à vérifier
217+ * @returns true si l'email et l'UID sont uniques, false sinon
216218 */
217- protected async checkMailAndUid ( data ) : Promise < boolean > {
218- let dataDup = [ ] ;
219- if ( data . inetOrgPerson . hasOwnProperty ( 'mail' ) && data . inetOrgPerson . mail !== '' ) {
220- const id = new Types . ObjectId ( data [ '_id' ] ) ;
221- const f : any = { '_id' : { $ne : id } , 'deletedFlag' : { $ne : true } , $or : [ { 'inetOrgPerson.uid' : data . inetOrgPerson . uid } , { 'inetOrgPerson.mail' : data . inetOrgPerson . mail } ] } ;
222- dataDup = await this . _model . find ( f ) . exec ( )
223- } else {
224- const id = new Types . ObjectId ( data [ '_id' ] ) ;
225- const f : any = { '_id' : { $ne : id } , 'deletedFlag' : { $ne : true } , 'inetOrgPerson.uid' : data . inetOrgPerson . uid } ;
226- dataDup = await this . _model . find ( f ) . exec ( )
219+ protected async checkMailAndUid ( data : IdentitiesUpsertDto | any ) : Promise < boolean > {
220+ // Validation des paramètres d'entrée
221+ if ( ! data ?. _id ) {
222+ throw new BadRequestException ( 'ID is required for mail and UID uniqueness check' ) ;
227223 }
228- if ( dataDup . length > 0 ) {
229- return false
230- } else {
231- return true
224+
225+ if ( ! data ?. inetOrgPerson ?. uid ) {
226+ throw new BadRequestException ( 'UID is required for mail and UID uniqueness check' ) ;
227+ }
228+
229+ // Validation du format de l'ID
230+ let objectId : Types . ObjectId ;
231+ try {
232+ objectId = Types . ObjectId . createFromHexString ( data . _id ) ;
233+ } catch ( error ) {
234+ throw new BadRequestException ( 'Invalid ID format' ) ;
235+ }
236+
237+ try {
238+ let duplicates : Identities [ ] = [ ] ;
239+
240+ // Vérification avec email si celui-ci est fourni et non vide
241+ if ( data . inetOrgPerson . hasOwnProperty ( 'mail' ) && data . inetOrgPerson . mail !== '' ) {
242+ // Validation du format email
243+ if ( typeof data . inetOrgPerson . mail !== 'string' ) {
244+ throw new BadRequestException ( 'Invalid email format' ) ;
245+ }
246+
247+ // Filtre pour vérifier UID ou email
248+ const filterWithMail = {
249+ '_id' : { $ne : objectId } ,
250+ 'deletedFlag' : { $ne : true } ,
251+ $or : [
252+ { 'inetOrgPerson.uid' : data . inetOrgPerson . uid } ,
253+ { 'inetOrgPerson.mail' : data . inetOrgPerson . mail }
254+ ]
255+ } ;
256+ duplicates = await this . _model . find ( filterWithMail ) . exec ( ) ;
257+ } else {
258+ // Filtre pour vérifier seulement l'UID
259+ const filterUidOnly = {
260+ '_id' : { $ne : objectId } ,
261+ 'deletedFlag' : { $ne : true } ,
262+ 'inetOrgPerson.uid' : data . inetOrgPerson . uid
263+ } ;
264+ duplicates = await this . _model . find ( filterUidOnly ) . exec ( ) ;
265+ }
266+
267+ // Retourne true si aucun doublon n'est trouvé
268+ return duplicates . length === 0 ;
269+
270+ } catch ( error ) {
271+ this . logger . error ( `Error checking mail and UID uniqueness for ID "${ data . _id } ": ${ error . message } ` ) ;
272+ throw new HttpException ( 'Failed to check mail and UID uniqueness' , 500 ) ;
232273 }
233274 }
234275
0 commit comments