Skip to content

Commit d319b57

Browse files
committed
Refactor identity service schema and CRUD operations; omit unnecessary fields in updates
1 parent a914bc6 commit d319b57

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/_common/abstracts/abstract.service.schema.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { AbstractService, AbstractServiceContext } from './abstract.service';
1717
import { ServiceSchemaInterface } from './interfaces/service.schema.interface';
1818
import { AbstractSchema } from './schemas/abstract.schema';
1919
import mongodb from 'mongodb';
20+
import { omit } from 'radash';
2021

2122
@Injectable()
2223
export abstract class AbstractServiceSchema extends AbstractService implements ServiceSchemaInterface {
@@ -227,17 +228,18 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
227228
if (beforeEvent?.options) options = { ...options, ...beforeEvent.options }
228229
}
229230
}
231+
console.log('_id', update)
230232
let updated = await this._model
231233
.findByIdAndUpdate<Query<T | null, T, any, T>>(
232-
{ _id },
234+
_id,
233235
{
234-
...update,
236+
...omit(update, ['_id']),
235237
$setOnInsert: {
236238
'metadata.createdBy': this.request?.user?.username || 'anonymous',
237239
'metadata.createdAt': new Date(),
238240
},
239241
$set: {
240-
...(update?.$set || {}),
242+
...omit(update?.$set || {}, ['_id']),
241243
'metadata.lastUpdatedBy': this.request?.user?.username || 'anonymous',
242244
'metadata.lastUpdatedAt': new Date(),
243245
}

src/management/identities/_schemas/_parts/inetOrgPerson.part.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class inetOrgPerson extends Document {
6060
})
6161
public employeeNumber: string[];
6262

63-
@Prop({ type: String, required: true })
63+
@Prop({ type: String })
6464
public employeeType: string;
6565

6666
@Prop({ type: Array, of: String, required: true, default: [] })

src/management/identities/identities-crud.service.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ValidationConfigException, ValidationSchemaException } from '~/_common/
55
import { IdentityState } from '~/management/identities/_enums/states.enum';
66
import { Identities } from '~/management/identities/_schemas/identities.schema';
77
import { HttpException } from '@nestjs/common';
8+
import { omit } from 'radash';
89

910
export class IdentitiesCrudService extends AbstractIdentitiesService {
1011
public async create<T extends AbstractSchema | Document>(
@@ -14,9 +15,9 @@ export class IdentitiesCrudService extends AbstractIdentitiesService {
1415
data = this.transformNullsToString(data);
1516
await this.checkInetOrgPersonJpegPhoto(data);
1617
//recherche si email oy uid deja present
17-
const f:any = { $or: [{ 'inetOrgPerson.uid' : data.inetOrgPerson.uid}, {'inetOrgPerson.mail': data.inetOrgPerson.mail }]};
18+
const f: any = { $or: [{ 'inetOrgPerson.uid': data.inetOrgPerson.uid }, { 'inetOrgPerson.mail': data.inetOrgPerson.mail }] };
1819
let dataDup = await this._model.countDocuments(f).exec()
19-
if (dataDup > 0){
20+
if (dataDup > 0) {
2021
this.logger.error('Identité existante');
2122
throw new HttpException("Uid ou mail déjà présent dans une autre identité", 400);
2223
}
@@ -53,16 +54,22 @@ export class IdentitiesCrudService extends AbstractIdentitiesService {
5354
}
5455

5556
// if (update.state === IdentityState.TO_COMPLETE) {
56-
update = { ...update, state: IdentityState.TO_VALIDATE };
57+
update = {
58+
...omit(update, ['inetOrgPerson']),
59+
inetOrgPerson: omit(update.inetOrgPerson, ['employeeNumber', 'employeeType']),
60+
state: IdentityState.TO_VALIDATE,
61+
};
5762

5863
await this.checkInetOrgPersonJpegPhoto(update);
5964

65+
console.log('update', update, _id, options)
66+
6067
// }
6168
// if (update.state === IdentityState.SYNCED) {
6269
// update = { ...update, state: IdentityState.TO_VALIDATE };
6370
// }
6471
//update.state = IdentityState.TO_VALIDATE;
65-
const updated = await super.update(_id, update, options);
72+
const updated = await super.update(_id, { $set: update }, options);
6673
//TODO: add backends service logic here (TO_SYNC)
6774
return await this.generateFingerprint(updated as unknown as Identities);
6875
}

0 commit comments

Comments
 (0)