Skip to content

Commit b7a38de

Browse files
committed
Add IdentitiesCommand and IdentitiesFingerprintCommand for identity management; refactor initForFingerprint and update access modifiers in AbstractIdentitiesService
1 parent e8b541c commit b7a38de

File tree

5 files changed

+91
-32
lines changed

5 files changed

+91
-32
lines changed

src/cli/cli.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { BackendsCommand } from './backends.command';
1414
import { BackendsModule } from '~/core/backends/backends.module';
1515
import { AuthModule } from '~/core/auth/auth.module';
1616
import { FactorydriveModule } from '@the-software-compagny/nestjs_module_factorydrive';
17+
import { IdentitiesCommand } from './identities.command';
18+
import { IdentitiesModule } from '~/management/identities/identities.module';
1719

1820
@Module({
1921
imports: [
@@ -65,12 +67,14 @@ import { FactorydriveModule } from '@the-software-compagny/nestjs_module_factory
6567
AgentsModule,
6668
KeyringsModule,
6769
BackendsModule,
70+
IdentitiesModule,
6871
AuthModule,
6972
],
7073
providers: [
7174
...AgentsCommand.registerWithSubCommands(),
7275
...KeyringsCommand.registerWithSubCommands(),
7376
...BackendsCommand.registerWithSubCommands(),
77+
...IdentitiesCommand.registerWithSubCommands(),
7478
AgentCreateQuestions,
7579
KeyringsCreateQuestions,
7680
],

src/cli/identities.command.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Logger } from "@nestjs/common";
2+
import { ModuleRef } from "@nestjs/core";
3+
import { Command, CommandRunner, SubCommand } from "nest-commander";
4+
import { Identities } from "~/management/identities/_schemas/identities.schema";
5+
import { IdentitiesUpsertService } from "~/management/identities/identities-upsert.service";
6+
7+
8+
@SubCommand({ name: 'fingerprint' })
9+
export class IdentitiesFingerprintCommand extends CommandRunner {
10+
private readonly logger = new Logger(IdentitiesFingerprintCommand.name);
11+
12+
public constructor(
13+
protected moduleRef: ModuleRef,
14+
private readonly identitiesUpsertService: IdentitiesUpsertService,
15+
) {
16+
super();
17+
}
18+
19+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
20+
async run(inputs: string[], options: any): Promise<void> {
21+
const total = await this.identitiesUpsertService.count();
22+
this.logger.log(`Total identities: ${total}`);
23+
24+
const identities = await this.identitiesUpsertService.find<Identities>() as unknown as Identities[];
25+
26+
for await (const identity of identities) {
27+
this.logger.log(`Processing identity: ${identity._id}`);
28+
const fingerprint = await this.identitiesUpsertService.previewFingerprint(identity.toJSON());
29+
this.logger.log(`Identity: ${identity._id}, Fingerprint: ${fingerprint}, Old: ${identity.fingerprint}`);
30+
31+
if (identity.fingerprint !== fingerprint) {
32+
this.logger.warn(`Updating fingerprint for identity: ${identity._id}`);
33+
await this.identitiesUpsertService.generateFingerprint(identity, fingerprint);
34+
} else {
35+
this.logger.log(`Fingerprint already up to date for identity: ${identity._id}`);
36+
}
37+
}
38+
}
39+
}
40+
41+
@Command({ name: 'identities', arguments: '<task>', subCommands: [IdentitiesFingerprintCommand] })
42+
export class IdentitiesCommand extends CommandRunner {
43+
public constructor(protected moduleRef: ModuleRef) {
44+
super();
45+
}
46+
47+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
48+
async run(inputs: string[], options: any): Promise<void> { }
49+
}

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,33 @@ import { IsString, IsEmail, IsOptional, IsArray, ValidateIf } from 'class-valida
44

55
export class inetOrgPersonCreateDto {
66
public static initForFingerprint(partial: Partial<inetOrgPersonCreateDto>) {
7-
return plainToInstance(inetOrgPersonCreateDto, {
8-
cn: partial.cn || null,
9-
sn: partial.sn || null,
10-
uid: partial.uid || null,
11-
employeeNumber: partial.employeeNumber || [],
12-
employeeType: partial.employeeType || null,
13-
departmentNumber: partial.departmentNumber || [],
14-
displayName: partial.displayName || null,
15-
facsimileTelephoneNumber: partial.facsimileTelephoneNumber || null,
16-
givenName: partial.givenName || null,
17-
labeledURI: partial.labeledURI || null,
18-
mail: partial.mail || null,
19-
mobile: partial.mobile || null,
20-
postalAddress: partial.postalAddress || null,
21-
preferredLanguage: partial.preferredLanguage || null,
22-
telephoneNumber: partial.telephoneNumber || null,
23-
title: partial.title || null,
24-
userCertificate: partial.userCertificate || null,
25-
jpegPhoto: partial.jpegPhoto || null,
26-
27-
...partial
28-
});
7+
try {
8+
return plainToInstance(inetOrgPersonCreateDto, {
9+
cn: partial.cn || null,
10+
sn: partial.sn || null,
11+
uid: partial.uid || null,
12+
employeeNumber: partial.employeeNumber || [],
13+
employeeType: partial.employeeType || null,
14+
departmentNumber: partial.departmentNumber || [],
15+
displayName: partial.displayName || null,
16+
facsimileTelephoneNumber: partial.facsimileTelephoneNumber || null,
17+
givenName: partial.givenName || null,
18+
labeledURI: partial.labeledURI || null,
19+
mail: partial.mail || null,
20+
mobile: partial.mobile || null,
21+
postalAddress: partial.postalAddress || null,
22+
preferredLanguage: partial.preferredLanguage || null,
23+
telephoneNumber: partial.telephoneNumber || null,
24+
title: partial.title || null,
25+
userCertificate: partial.userCertificate || null,
26+
jpegPhoto: partial.jpegPhoto || null,
27+
28+
...partial
29+
});
30+
} catch (error) {
31+
console.error('inetOrgPersonCreateDto.initForFingerprint', error);
32+
throw error;
33+
}
2934
}
3035

3136
@IsString()

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ export abstract class AbstractIdentitiesService extends AbstractServiceSchema {
9797
}
9898
}
9999
}
100-
protected async generateFingerprint<T extends AbstractSchema | Document>(
100+
101+
public async generateFingerprint<T extends AbstractSchema | Document>(
101102
identity: Identities,
102103
fingerprint?: string,
103104
): Promise<ModifyResult<Query<T, T, any, T>>> {
@@ -136,7 +137,7 @@ export abstract class AbstractIdentitiesService extends AbstractServiceSchema {
136137
).join(',')}}`;
137138
}
138139

139-
protected async previewFingerprint(identity: any): Promise<string> {
140+
public async previewFingerprint(identity: any): Promise<string> {
140141
const inetOrgPerson = inetOrgPersonDto.initForFingerprint(identity.inetOrgPerson);
141142

142143
const additionalFields = omit(identity.additionalFields, ['validations']);
@@ -230,13 +231,13 @@ export abstract class AbstractIdentitiesService extends AbstractServiceSchema {
230231
}
231232
}
232233

233-
protected async checkMail(identity,data): Promise<boolean> {
234+
protected async checkMail(identity, data): Promise<boolean> {
234235
let dataDup = 0;
235236
if (data.inetOrgPerson.hasOwnProperty('mail') && data.inetOrgPerson.mail !== '') {
236237
if (identity){
237238
const f: any = { '_id': { $ne: identity._id },'state':{$ne:IdentityState.DONT_SYNC},'deletedFlag':{$ne:true}, 'inetOrgPerson.mail': identity.inetOrgPerson.mail };
238239
dataDup = await this._model.countDocuments(f).exec()
239-
}else{
240+
} else {
240241
const f: any = { 'inetOrgPerson.mail': data.inetOrgPerson.mail };
241242
dataDup = await this._model.countDocuments(f).exec()
242243
}
@@ -249,12 +250,12 @@ export abstract class AbstractIdentitiesService extends AbstractServiceSchema {
249250
}
250251
}
251252

252-
protected async checkUid(identity,data): Promise<boolean> {
253+
protected async checkUid(identity, data): Promise<boolean> {
253254
let dataDup = 0;
254255
if (identity){
255256
const f: any = { '_id': { $ne: identity._id } ,'state':{$ne:IdentityState.DONT_SYNC},'deletedFlag':{$ne:true},'inetOrgPerson.uid': identity.inetOrgPerson.uid };
256257
dataDup = await this._model.countDocuments(f).exec()
257-
}else{
258+
} else {
258259
const f: any = { 'inetOrgPerson.uid': data.inetOrgPerson.uid };
259260
dataDup = await this._model.countDocuments(f).exec()
260261
}

src/management/identities/identities.module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import { IdentitiesPhotoController } from '~/management/identities/identities-ph
1818
import { IdentitiesActivationController } from '~/management/identities/identities-activation.controller';
1919
import { IdentitiesActivationService } from '~/management/identities/identities-activation.service';
2020
import { IdentitiesDoublonController } from '~/management/identities/identities-doublon.controller';
21-
import {IdentitiesForcePasswordController} from "~/management/identities/identities-forcepassword.controller";
22-
import {IdentitiesForcepasswordService} from "~/management/identities/identities-forcepassword.service";
23-
import {SettingsModule} from "~/settings/settings.module";
21+
import { IdentitiesForcePasswordController } from "~/management/identities/identities-forcepassword.controller";
22+
import { IdentitiesForcepasswordService } from "~/management/identities/identities-forcepassword.service";
23+
import { SettingsModule } from "~/settings/settings.module";
2424
import { EnsureIdentitiesIndexMiddleware } from './_middlewares/ensure-identities-index.middleware';
2525
import { AgentsModule } from '~/core/agents/agents.module';
2626

@@ -61,7 +61,7 @@ import { AgentsModule } from '~/core/agents/agents.module';
6161
IdentitiesActivationController,
6262
IdentitiesForcePasswordController
6363
],
64-
exports: [IdentitiesCrudService],
64+
exports: [IdentitiesCrudService, IdentitiesUpsertService],
6565
})
6666
export class IdentitiesModule implements NestModule {
6767
public configure(consumer: MiddlewareConsumer) {

0 commit comments

Comments
 (0)