@solidstarters/solid-core 1.2.152 → 1.2.153
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.d.ts +2 -1
- package/dist/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.d.ts.map +1 -1
- package/dist/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.js +13 -5
- package/dist/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.ts +34 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solidstarters/solid-core",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.153",
|
|
4
4
|
"description": "This module is a NestJS module containing all the required core providers required by a Solid application",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
package/src/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.ts
CHANGED
|
@@ -5,15 +5,17 @@ import { CommonEntity } from 'src/entities/common.entity';
|
|
|
5
5
|
import { ComputedFieldMetadata } from 'src/helpers/solid-registry';
|
|
6
6
|
import { IEntityPreComputeFieldProvider } from 'src/interfaces';
|
|
7
7
|
import { EntityManager } from 'typeorm';
|
|
8
|
-
|
|
8
|
+
import { get } from "lodash";
|
|
9
9
|
export interface AlphaNumExternalIdContext {
|
|
10
|
-
prefix
|
|
11
|
-
length?: number;
|
|
10
|
+
prefix?: string; // alias -> staticPrefix
|
|
11
|
+
length?: number; // Optional: length of the unique code to generate, default is 5
|
|
12
|
+
dynamicFieldPrefix?: string; // NEW: field name on the entity
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
@ComputedFieldProvider()
|
|
15
16
|
@Injectable()
|
|
16
|
-
export class AlphaNumExternalIdComputationProvider<T extends CommonEntity> implements IEntityPreComputeFieldProvider<T, AlphaNumExternalIdContext>
|
|
17
|
+
export class AlphaNumExternalIdComputationProvider<T extends CommonEntity> implements IEntityPreComputeFieldProvider<T, AlphaNumExternalIdContext>
|
|
18
|
+
{
|
|
17
19
|
constructor(
|
|
18
20
|
@InjectEntityManager()
|
|
19
21
|
private readonly entityManager: EntityManager
|
|
@@ -24,14 +26,28 @@ export class AlphaNumExternalIdComputationProvider<T extends CommonEntity> imple
|
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
help(): string {
|
|
27
|
-
return 'Provider used to compute external ID for a CommonEntity.';
|
|
29
|
+
return 'Provider used to compute external ID for a CommonEntity with support for static or dynamic prefix.';
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
async preComputeValue(triggerEntity: T, computedFieldMetadata: ComputedFieldMetadata<AlphaNumExternalIdContext>
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
async preComputeValue( triggerEntity: T, computedFieldMetadata: ComputedFieldMetadata<AlphaNumExternalIdContext>
|
|
33
|
+
) {
|
|
34
|
+
const { prefix, length, dynamicFieldPrefix } =
|
|
35
|
+
computedFieldMetadata.computedFieldValueProviderCtxt;
|
|
36
|
+
|
|
37
|
+
const codeLength = length || 5;
|
|
38
|
+
|
|
39
|
+
// Determine prefix
|
|
40
|
+
let resolvedPrefix = prefix || '';
|
|
41
|
+
|
|
42
|
+
if (dynamicFieldPrefix) {
|
|
43
|
+
const dynamicValue = get(triggerEntity as any, dynamicFieldPrefix);
|
|
44
|
+
if (dynamicValue) {
|
|
45
|
+
resolvedPrefix = String(dynamicValue).trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9\-]/g, '');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const uniqueCode = await this.generateUniqueExternalId( codeLength, triggerEntity, computedFieldMetadata.fieldName );
|
|
50
|
+
triggerEntity[computedFieldMetadata.fieldName] = resolvedPrefix ? `${resolvedPrefix}-${uniqueCode}` : uniqueCode;
|
|
35
51
|
}
|
|
36
52
|
|
|
37
53
|
private generateRandomCode(length = 5): string {
|
|
@@ -43,10 +59,12 @@ export class AlphaNumExternalIdComputationProvider<T extends CommonEntity> imple
|
|
|
43
59
|
return result;
|
|
44
60
|
}
|
|
45
61
|
|
|
46
|
-
private async isExternalIdUnique(externalId: string, triggerEntity: T, fieldName: string): Promise<boolean> {
|
|
47
|
-
const count = await this.entityManager.count(triggerEntity.constructor
|
|
48
|
-
|
|
49
|
-
|
|
62
|
+
private async isExternalIdUnique( externalId: string, triggerEntity: T, fieldName: string ): Promise<boolean> {
|
|
63
|
+
const count = await this.entityManager.count(triggerEntity.constructor as any,
|
|
64
|
+
{
|
|
65
|
+
where: { [fieldName]: externalId },
|
|
66
|
+
}
|
|
67
|
+
);
|
|
50
68
|
return count === 0;
|
|
51
69
|
}
|
|
52
70
|
|
|
@@ -56,7 +74,7 @@ export class AlphaNumExternalIdComputationProvider<T extends CommonEntity> imple
|
|
|
56
74
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
57
75
|
const newId = this.generateRandomCode(codeLength);
|
|
58
76
|
|
|
59
|
-
const isUnique = await this.isExternalIdUnique(newId,
|
|
77
|
+
const isUnique = await this.isExternalIdUnique(newId,triggerEntity,fieldName);
|
|
60
78
|
|
|
61
79
|
if (isUnique) {
|
|
62
80
|
return newId;
|
|
@@ -65,4 +83,4 @@ export class AlphaNumExternalIdComputationProvider<T extends CommonEntity> imple
|
|
|
65
83
|
|
|
66
84
|
throw new Error('Failed to generate a unique external ID after multiple attempts');
|
|
67
85
|
}
|
|
68
|
-
}
|
|
86
|
+
}
|