Skip to content

Commit ef6b8d2

Browse files
committed
Refactor inetorgperson.ui.yml to add Employee Number and Employee Type fields
1 parent d377a36 commit ef6b8d2

File tree

5 files changed

+134
-34
lines changed

5 files changed

+134
-34
lines changed

src/core/backends/backends.service.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { BadRequestException, HttpStatus, Injectable, RequestTimeoutException } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
HttpStatus,
4+
Injectable,
5+
RequestTimeoutException,
6+
UnprocessableEntityException,
7+
} from '@nestjs/common';
28
import { ModuleRef } from '@nestjs/core';
39
import { Types } from 'mongoose';
410
import { AbstractQueueProcessor } from '~/_common/abstracts/abstract.queue.processor';
@@ -81,7 +87,11 @@ export class BackendsService extends AbstractQueueProcessor {
8187
$set: {
8288
state: JobState.FAILED,
8389
finishedAt: new Date(),
84-
result: payload.failedReason,
90+
result: {
91+
error: {
92+
message: payload.failedReason,
93+
},
94+
},
8595
},
8696
},
8797
{ new: true },
@@ -105,6 +115,7 @@ export class BackendsService extends AbstractQueueProcessor {
105115
},
106116
{ upsert: true, new: true },
107117
);
118+
console.log('completedJob', completedJob);
108119
await this.identitiesService.model.findByIdAndUpdate(completedJob.concernedTo.id, {
109120
$set: {
110121
state: IdentityState.SYNCED,
@@ -248,7 +259,11 @@ export class BackendsService extends AbstractQueueProcessor {
248259
$set: {
249260
state: JobState.FAILED,
250261
finishedAt: new Date(),
251-
result: error,
262+
result: {
263+
error: {
264+
message: error.message,
265+
},
266+
},
252267
},
253268
});
254269
await this.identitiesService.model.findByIdAndUpdate(concernedTo, {
@@ -265,8 +280,8 @@ export class BackendsService extends AbstractQueueProcessor {
265280
job: jobFailed as unknown as Jobs,
266281
});
267282
}
268-
throw new RequestTimeoutException({
269-
status: HttpStatus.REQUEST_TIMEOUT,
283+
throw new UnprocessableEntityException({
284+
status: HttpStatus.UNPROCESSABLE_ENTITY,
270285
message: `Job now continue to run in background ${job.id}, timeout wait until finish reached`,
271286
error,
272287
job: jobFailed as unknown as Jobs,

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export class IdentitiesCreateDto extends MetadataDto {
4444
additionalFields?: additionalFieldsPartDto;
4545
}
4646

47-
export class IdentitiesDto extends IdentitiesCreateDto {}
47+
export class IdentitiesDto extends IdentitiesCreateDto { }
4848

49-
export class IdentitiesUpdateDto extends PartialType(IdentitiesCreateDto) {}
49+
export class IdentitiesUpdateDto extends PartialType(IdentitiesCreateDto) { }
50+
51+
export class IdentitiesUpsertDto extends PartialType(IdentitiesUpdateDto) {
52+
$addToSet?: Partial<IdentitiesUpdateDto>;
53+
}

src/management/identities/identities.controller.ts

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import {
1010
Post,
1111
Query,
1212
Res,
13+
Version,
1314
} from '@nestjs/common';
14-
import { IdentitiesDto, IdentitiesCreateDto, IdentitiesUpdateDto } from './_dto/identities.dto';
15+
import { IdentitiesDto, IdentitiesCreateDto, IdentitiesUpdateDto, IdentitiesUpsertDto } from './_dto/identities.dto';
1516
import { IdentitiesService } from './identities.service';
1617
import { AbstractController } from '~/_common/abstracts/abstract.controller';
1718
import { ApiParam, ApiTags } from '@nestjs/swagger';
@@ -83,42 +84,58 @@ export class IdentitiesController extends AbstractController {
8384
}
8485

8586
@Post('upsert')
86-
@ApiCreateDecorator(IdentitiesCreateDto, IdentitiesDto)
87+
@ApiCreateDecorator(IdentitiesUpsertDto, IdentitiesDto)
8788
public async upsert(
8889
@Res()
8990
res: Response,
90-
@Body() body: IdentitiesCreateDto,
91+
@Body() body: IdentitiesUpsertDto,
9192
@Query('errorOnNotFound') errorOnNotFound: string = 'false',
92-
): Promise<
93-
Response<
94-
{
95-
statusCode: number;
96-
data?: Document<Identities, any, Identities>;
97-
message?: string;
98-
validations?: MixedValue;
99-
},
100-
any
101-
>
102-
> {
103-
let statusCode = HttpStatus.CREATED;
104-
let message = null;
93+
): Promise<Response<any>> {
10594
const data = await this._service.upsert<Identities>(body, {
106-
errorOnNotFound: errorOnNotFound.toLowerCase() === 'true',
95+
errorOnNotFound: /true|on|yes|1/i.test(errorOnNotFound),
10796
});
108-
// If the state is TO_COMPLETE, the identity is created but additional fields are missing or invalid
109-
// Else the state is TO_VALIDATE, we return a 201 status code
110-
if ((data as unknown as Identities).state === IdentityState.TO_COMPLETE) {
111-
statusCode = HttpStatus.ACCEPTED;
112-
message = 'Identitée créée avec succès, mais des champs additionnels sont manquants ou invalides.';
113-
}
114-
115-
return res.status(statusCode).json({
116-
statusCode,
97+
return res.status(200).json({
11798
data,
118-
message,
11999
});
120100
}
121101

102+
// @Post('upsert')
103+
// @ApiCreateDecorator(IdentitiesCreateDto, IdentitiesDto)
104+
// public async upsertV1(
105+
// @Res()
106+
// res: Response,
107+
// @Body() body: IdentitiesCreateDto,
108+
// @Query('errorOnNotFound') errorOnNotFound: string = 'false',
109+
// ): Promise<
110+
// Response<
111+
// {
112+
// statusCode: number;
113+
// data?: Document<Identities, any, Identities>;
114+
// message?: string;
115+
// validations?: MixedValue;
116+
// },
117+
// any
118+
// >
119+
// > {
120+
// let statusCode = HttpStatus.CREATED;
121+
// let message = null;
122+
// const data = await this._service.upsert<Identities>(body, {
123+
// errorOnNotFound: errorOnNotFound.toLowerCase() === 'true',
124+
// });
125+
// // If the state is TO_COMPLETE, the identity is created but additional fields are missing or invalid
126+
// // Else the state is TO_VALIDATE, we return a 201 status code
127+
// if ((data as unknown as Identities).state === IdentityState.TO_COMPLETE) {
128+
// statusCode = HttpStatus.ACCEPTED;
129+
// message = 'Identitée créée avec succès, mais des champs additionnels sont manquants ou invalides.';
130+
// }
131+
132+
// return res.status(statusCode).json({
133+
// statusCode,
134+
// data,
135+
// message,
136+
// });
137+
// }
138+
122139
@Get()
123140
@ApiPaginatedDecorator(PickProjectionHelper(IdentitiesDto, IdentitiesController.projection))
124141
public async search(

src/management/identities/identities.service.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,59 @@ export class IdentitiesService extends AbstractServiceSchema {
7979
//TODO: add backends service logic here
8080
}
8181

82+
// public async upsert<T extends AbstractSchema | Document>(
83+
// data?: any,
84+
// options?: QueryOptions<T>,
85+
// ): Promise<ModifyResult<Query<T, T, any, T>>> {
86+
// Logger.log(`Upserting identity: ${JSON.stringify(data)}`);
87+
// const logPrefix = `Validation [${data.inetOrgPerson.cn}]:`;
88+
// // console.log(options);
89+
// const identity = await this._model.findOne({
90+
// 'inetOrgPerson.employeeNumber': data.inetOrgPerson.employeeNumber,
91+
// 'inetOrgPerson.employeeType': data.inetOrgPerson.employeeType,
92+
// });
93+
// // console.log(identity);
94+
// if (!identity && options.errorOnNotFound) {
95+
// this.logger.error(`${logPrefix} Identity not found.`);
96+
// throw new HttpException('Identity not found.', 404);
97+
// }
98+
// data.additionalFields.validations = {};
99+
// try {
100+
// this.logger.log(`${logPrefix} Starting additionalFields validation.`);
101+
// const validations = await this._validation.validate(data.additionalFields);
102+
// this.logger.log(`${logPrefix} AdditionalFields validation successful.`);
103+
// this.logger.log(`Validations : ${validations}`);
104+
// data.state = IdentityState.TO_VALIDATE;
105+
// } catch (error) {
106+
// data = this.handleValidationError(error, data, logPrefix);
107+
// }
108+
109+
// //TODO: ameliorer la logique d'upsert
110+
// if (identity) {
111+
// this.logger.log(`${logPrefix} Identity already exists. Updating.`);
112+
// data.inetOrgPerson = {
113+
// ...identity.inetOrgPerson,
114+
// ...data.inetOrgPerson,
115+
// };
116+
// data.additionalFields.objectClasses = [
117+
// ...new Set([...identity.additionalFields.objectClasses, ...data.additionalFields.objectClasses]),
118+
// ];
119+
// data.additionalFields.attributes = {
120+
// ...identity.additionalFields.attributes,
121+
// ...data.additionalFields.attributes,
122+
// };
123+
// data.additionalFields.validations = {
124+
// ...identity.additionalFields.validations,
125+
// ...data.additionalFields.validations,
126+
// };
127+
// }
128+
129+
// //TODO: rechercher par uid ou employeeNumber + employeeType ?
130+
// const upsert = await super.upsert({ 'inetOrgPerson.uid': data.inetOrgPerson.uid }, data, options);
131+
// return upsert;
132+
// //TODO: add backends service logic here
133+
// }
134+
82135
public async update<T extends AbstractSchema | Document>(
83136
_id: Types.ObjectId | any,
84137
update: UpdateQuery<T>,

src/management/identities/jsonforms/_config/inetorgperson.ui.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ elements:
2626
- type: Control
2727
label: Prénom
2828
scope: "#/properties/givenName"
29+
- type: HorizontalLayout
30+
elements:
31+
- type: Control
32+
label: Employee Number
33+
scope: "#/properties/employeeNumber"
34+
- type: Control
35+
label: Employee Type
36+
scope: "#/properties/employeeType"
37+
options:
38+
suggestion:
39+
- TAIGA
2940
- type: HorizontalLayout
3041
elements:
3142
- type: Control

0 commit comments

Comments
 (0)