Skip to content

Commit 73df13d

Browse files
Mise a jours du service et du controller identity pour y ajouter la query errorOnNotFound sur l'endpoint d'upsert
1 parent 25dbd70 commit 73df13d

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/management/identities/_dto/_parts/inetOrgPerson.dto.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import { IsString, IsEmail, IsOptional } from 'class-validator';
44
export class inetOrgPersonCreateDto {
55
@IsString()
66
@ApiProperty()
7-
cn: string;
7+
uid: string;
88

99
@IsString()
1010
@ApiProperty()
11-
sn: string;
11+
@IsOptional()
12+
cn?: string;
1213

1314
@IsString()
1415
@ApiProperty()
15-
uid: string;
16+
@IsOptional()
17+
sn?: string;
1618

1719
@IsString()
1820
@ApiProperty({ required: false })

src/management/identities/identities.controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Param,
99
Patch,
1010
Post,
11+
Query,
1112
Res,
1213
} from '@nestjs/common';
1314
import { IdentitiesDto, IdentitiesCreateDto, IdentitiesUpdateDto } from './_dto/identities.dto';
@@ -87,6 +88,7 @@ export class IdentitiesController extends AbstractController {
8788
@Res()
8889
res: Response,
8990
@Body() body: IdentitiesCreateDto,
91+
@Query('errorOnNotFound') errorOnNotFound: string = 'false',
9092
): Promise<
9193
Response<
9294
{
@@ -100,7 +102,9 @@ export class IdentitiesController extends AbstractController {
100102
> {
101103
let statusCode = HttpStatus.CREATED;
102104
let message = null;
103-
const data = await this._service.upsert<Identities>(body);
105+
const data = await this._service.upsert<Identities>(body, {
106+
errorOnNotFound: errorOnNotFound.toLowerCase() === 'true',
107+
});
104108
// If the state is TO_COMPLETE, the identity is created but additional fields are missing or invalid
105109
// Else the state is TO_VALIDATE, we return a 201 status code
106110
if ((data as unknown as Identities).state === IdentityState.TO_COMPLETE) {

src/management/identities/identities.service.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inetOrgPerson } from './_schemas/_parts/inetOrgPerson.part';
12
import { HttpException, Injectable, Logger } from '@nestjs/common';
23
import { InjectModel } from '@nestjs/mongoose';
34
import { Identities } from './_schemas/identities.schema';
@@ -32,6 +33,13 @@ export class IdentitiesService extends AbstractServiceSchema {
3233
): Promise<ModifyResult<Query<T, T, any, T>>> {
3334
Logger.log(`Upserting identity: ${JSON.stringify(data)}`);
3435
const logPrefix = `Validation [${data.inetOrgPerson.cn}]:`;
36+
console.log(options);
37+
const identity = await this._model.findOne({ 'inetOrgPerson.uid': data.inetOrgPerson.uid });
38+
console.log(identity);
39+
if (!identity && options.errorOnNotFound) {
40+
this.logger.error(`${logPrefix} Identity not found.`);
41+
throw new HttpException('Identity not found.', 404);
42+
}
3543
data.additionalFields.validations = {};
3644
try {
3745
this.logger.log(`${logPrefix} Starting additionalFields validation.`);
@@ -44,9 +52,13 @@ export class IdentitiesService extends AbstractServiceSchema {
4452
}
4553

4654
//TODO: ameliorer la logique d'upsert
47-
const identity = await this._model.findOne({ 'inetOrgPerson.uid': data.inetOrgPerson.uid });
55+
4856
if (identity) {
4957
this.logger.log(`${logPrefix} Identity already exists. Updating.`);
58+
data.inetOrgPerson = {
59+
...identity.inetOrgPerson,
60+
...data.inetOrgPerson,
61+
};
5062
data.additionalFields.objectClasses = [
5163
...new Set([...identity.additionalFields.objectClasses, ...data.additionalFields.objectClasses]),
5264
];

src/management/identities/validations/_config/inetorgperson.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@
6464
"description": "User password of the inetOrgPerson."
6565
}
6666
},
67-
"required": ["cn", "sn", "uid"]
67+
"required": ["uid"]
6868
}

0 commit comments

Comments
 (0)