Skip to content

Commit 65c52ac

Browse files
committed
Add force option to upsert identity and log warning when enabled
1 parent 3e1b440 commit 65c52ac

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/management/identities/identities-upsert.controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class IdentitiesUpsertController extends AbstractController {
6565
filtersQuery: {
6666
[key: string]: string;
6767
}[] = [],
68+
@Query('force') forceString: string,
6869
@Query('errorOnNotFound') errorOnNotFound: string = 'false',
6970
@Query('upsert') upsert: string = 'true',
7071
): Promise<
@@ -75,6 +76,10 @@ export class IdentitiesUpsertController extends AbstractController {
7576
validations?: MixedValue;
7677
}>
7778
> {
79+
const force = /true|on|yes|1/i.test(forceString);
80+
if (force) {
81+
this.logger.warn('Upserting with force mode enabled.');
82+
}
7883
const filters = {};
7984
if (filtersQuery.length === 0) {
8085
throw new BadRequestException('Missing filters array');
@@ -88,7 +93,7 @@ export class IdentitiesUpsertController extends AbstractController {
8893
const [code, data] = await this._service.upsertWithFingerprint<Identities>(filters, body, {
8994
errorOnNotFound: /true|on|yes|1/i.test(errorOnNotFound),
9095
upsert: /true|on|yes|1/i.test(upsert),
91-
});
96+
}, {force});
9297

9398
// If the state is TO_COMPLETE, the identity is created but additional fields are missing or invalid
9499
// Else the state is TO_VALIDATE, we return a 201 status code

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ export class IdentitiesUpsertService extends AbstractIdentitiesService {
1313
filters: FilterQuery<T>,
1414
data?: IdentitiesUpsertDto,
1515
options?: QueryOptions<T>,
16+
extra?: { force?: boolean },
1617
): Promise<[HttpStatus.OK | HttpStatus.CREATED, ModifyResult<Query<T, T, any, T>>]> {
18+
extra = {
19+
force: false,
20+
...extra,
21+
}
1722
data = this.transformNullsToString(data);
1823
const identity = await this.model.findOne<Identities>(filters).exec();
1924
this.logger.log(`Upserting identity with filters ${JSON.stringify(filters)}`);
@@ -65,7 +70,7 @@ export class IdentitiesUpsertService extends AbstractIdentitiesService {
6570
}),
6671
);
6772

68-
await this.checkFingerprint(filters, fingerprint);
73+
await this.checkFingerprint(filters, fingerprint, extra);
6974

7075
this.logger.verbose('identities upsert data: ' + JSON.stringify({
7176
$setOnInsert: {
@@ -103,7 +108,11 @@ export class IdentitiesUpsertService extends AbstractIdentitiesService {
103108
public async checkFingerprint<T extends AbstractSchema | Document>(
104109
filters: FilterQuery<T>,
105110
fingerprint: string,
111+
extra: { force?: boolean } = { force: false },
106112
): Promise<void> {
113+
if (extra.force) {
114+
return;
115+
}
107116
const identity = await this.model
108117
.findOne(
109118
{ ...filters, fingerprint },

0 commit comments

Comments
 (0)