@loomcore/common 0.0.39 → 0.0.41
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/models/auditable.model.d.ts +7 -6
- package/dist/models/auditable.model.js +5 -4
- package/dist/models/organization.model.d.ts +3 -2
- package/dist/models/organization.model.js +4 -4
- package/dist/models/user-context.model.js +5 -3
- package/dist/utils/entity.utils.d.ts +1 -0
- package/dist/utils/entity.utils.js +5 -2
- package/dist/validation/typebox-extensions.d.ts +2 -0
- package/dist/validation/typebox-extensions.js +16 -0
- package/package.json +1 -1
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
import { AppId } from '../types/app.types.js';
|
|
1
2
|
export interface IAuditable {
|
|
2
3
|
_created: Date;
|
|
3
|
-
_createdBy:
|
|
4
|
+
_createdBy: AppId;
|
|
4
5
|
_updated: Date;
|
|
5
|
-
_updatedBy:
|
|
6
|
+
_updatedBy: AppId;
|
|
6
7
|
_deleted?: Date;
|
|
7
|
-
_deletedBy?:
|
|
8
|
+
_deletedBy?: AppId;
|
|
8
9
|
}
|
|
9
10
|
export declare const AuditableSchema: import("@sinclair/typebox").TObject<{
|
|
10
11
|
_created: import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>;
|
|
11
|
-
_createdBy: import("@sinclair/typebox").
|
|
12
|
+
_createdBy: import("@sinclair/typebox").TSchema;
|
|
12
13
|
_updated: import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>;
|
|
13
|
-
_updatedBy: import("@sinclair/typebox").
|
|
14
|
+
_updatedBy: import("@sinclair/typebox").TSchema;
|
|
14
15
|
_deleted: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>>;
|
|
15
|
-
_deletedBy: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").
|
|
16
|
+
_deletedBy: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TSchema>;
|
|
16
17
|
}>;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
|
-
import { TypeboxIsoDate } from '../validation/index.js';
|
|
2
|
+
import { getIdSchema, TypeboxIsoDate } from '../validation/index.js';
|
|
3
|
+
const idSchema = getIdSchema();
|
|
3
4
|
export const AuditableSchema = Type.Object({
|
|
4
5
|
_created: TypeboxIsoDate(),
|
|
5
|
-
_createdBy:
|
|
6
|
+
_createdBy: idSchema,
|
|
6
7
|
_updated: TypeboxIsoDate(),
|
|
7
|
-
_updatedBy:
|
|
8
|
+
_updatedBy: idSchema,
|
|
8
9
|
_deleted: Type.Optional(TypeboxIsoDate()),
|
|
9
|
-
_deletedBy: Type.Optional(
|
|
10
|
+
_deletedBy: Type.Optional(idSchema),
|
|
10
11
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { IAuditable } from './auditable.model.js';
|
|
2
|
+
import { AppId } from '../types/app.types.js';
|
|
2
3
|
export interface IOrganization extends IAuditable {
|
|
3
|
-
_id:
|
|
4
|
+
_id: AppId;
|
|
4
5
|
name: string;
|
|
5
6
|
code: string;
|
|
6
7
|
description?: string;
|
|
@@ -9,7 +10,7 @@ export interface IOrganization extends IAuditable {
|
|
|
9
10
|
authToken?: string;
|
|
10
11
|
}
|
|
11
12
|
export declare const OrganizationSchema: import("@sinclair/typebox").TObject<{
|
|
12
|
-
_id: import("@sinclair/typebox").
|
|
13
|
+
_id: import("@sinclair/typebox").TSchema;
|
|
13
14
|
name: import("@sinclair/typebox").TString;
|
|
14
15
|
code: import("@sinclair/typebox").TString;
|
|
15
16
|
description: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { entityUtils } from '../utils/index.js';
|
|
3
|
+
import { getIdSchema } from '../validation/index.js';
|
|
4
|
+
const idSchema = getIdSchema();
|
|
3
5
|
export const OrganizationSchema = Type.Object({
|
|
4
|
-
_id:
|
|
5
|
-
title: 'ID'
|
|
6
|
-
}),
|
|
6
|
+
_id: idSchema,
|
|
7
7
|
name: Type.String({
|
|
8
8
|
title: 'Name'
|
|
9
9
|
}),
|
|
@@ -24,4 +24,4 @@ export const OrganizationSchema = Type.Object({
|
|
|
24
24
|
title: 'Authentication Token'
|
|
25
25
|
}))
|
|
26
26
|
});
|
|
27
|
-
export const OrganizationSpec = entityUtils.getModelSpec(OrganizationSchema, { isAuditable: true });
|
|
27
|
+
export const OrganizationSpec = entityUtils.getModelSpec(OrganizationSchema, { isAuditable: true, isEntity: false });
|
|
@@ -21,10 +21,12 @@ export const PublicUserContextSchema = Type.Object({
|
|
|
21
21
|
organization: OrganizationSpec.fullSchema
|
|
22
22
|
});
|
|
23
23
|
export const PublicUserContextSpec = entityUtils.getModelSpec(PublicUserContextSchema);
|
|
24
|
+
import { getSystemUserId } from '../validation/index.js';
|
|
24
25
|
export function initializeSystemUserContext(systemEmail, metaOrg) {
|
|
26
|
+
const systemId = getSystemUserId();
|
|
25
27
|
_systemUserContext = {
|
|
26
28
|
user: {
|
|
27
|
-
_id:
|
|
29
|
+
_id: systemId,
|
|
28
30
|
_orgId: metaOrg?._id,
|
|
29
31
|
email: systemEmail,
|
|
30
32
|
firstName: 'System',
|
|
@@ -32,9 +34,9 @@ export function initializeSystemUserContext(systemEmail, metaOrg) {
|
|
|
32
34
|
displayName: 'System User',
|
|
33
35
|
password: 'systemPassword',
|
|
34
36
|
_created: new Date(),
|
|
35
|
-
_createdBy:
|
|
37
|
+
_createdBy: systemId,
|
|
36
38
|
_updated: new Date(),
|
|
37
|
-
_updatedBy:
|
|
39
|
+
_updatedBy: systemId,
|
|
38
40
|
},
|
|
39
41
|
authorizations: [{
|
|
40
42
|
_id: 'system-authorization',
|
|
@@ -6,6 +6,7 @@ import { IModelSpec } from '../models/model-spec.interface.js';
|
|
|
6
6
|
declare function getValidator(schema: TSchema): ReturnType<typeof TypeCompiler.Compile>;
|
|
7
7
|
declare function getModelSpec<T extends TSchema>(schema: T, options?: {
|
|
8
8
|
isAuditable?: boolean;
|
|
9
|
+
isEntity?: boolean;
|
|
9
10
|
}): IModelSpec;
|
|
10
11
|
declare function validate(validator: ReturnType<typeof TypeCompiler.Compile>, data: unknown): ValueError[] | null;
|
|
11
12
|
declare function handleValidationResult(validationErrors: ValueError[] | null, methodName: string): void;
|
|
@@ -9,11 +9,14 @@ function getValidator(schema) {
|
|
|
9
9
|
return validator;
|
|
10
10
|
}
|
|
11
11
|
function getModelSpec(schema, options = {}) {
|
|
12
|
+
const { isAuditable = false, isEntity = true } = options;
|
|
12
13
|
const partialSchema = Type.Partial(schema);
|
|
13
14
|
const schemasToIntersect = [];
|
|
14
15
|
schemasToIntersect.push(schema);
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
if (isEntity) {
|
|
17
|
+
schemasToIntersect.push(EntitySchema);
|
|
18
|
+
}
|
|
19
|
+
if (isAuditable) {
|
|
17
20
|
schemasToIntersect.push(AuditableSchema);
|
|
18
21
|
}
|
|
19
22
|
const fullSchema = Type.Intersect(schemasToIntersect);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Kind, TSchema, NumberOptions } from '@sinclair/typebox';
|
|
2
|
+
import { AppId } from '../types/app.types.js';
|
|
2
3
|
export declare const setIdSchema: (schema: TSchema) => void;
|
|
3
4
|
export declare const getIdSchema: () => TSchema;
|
|
5
|
+
export declare const getSystemUserId: () => AppId;
|
|
4
6
|
export declare function TypeboxIsoDate(options?: object): import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>;
|
|
5
7
|
export declare function TypeboxDate(options?: object): import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>;
|
|
6
8
|
export declare function TypeboxObjectId(options?: object): import("@sinclair/typebox").TString;
|
|
@@ -3,6 +3,7 @@ import { TypeRegistry } from '@sinclair/typebox';
|
|
|
3
3
|
import { Decimal as _Decimal } from 'decimal.js';
|
|
4
4
|
import { Value } from '@sinclair/typebox/value';
|
|
5
5
|
let idSchemaInstance;
|
|
6
|
+
let systemUserId;
|
|
6
7
|
export const setIdSchema = (schema) => {
|
|
7
8
|
if (idSchemaInstance) {
|
|
8
9
|
throw new Error('IdSchema has already been initialized and cannot be set again.');
|
|
@@ -11,6 +12,15 @@ export const setIdSchema = (schema) => {
|
|
|
11
12
|
throw new Error('Schema cannot be null or undefined.');
|
|
12
13
|
}
|
|
13
14
|
idSchemaInstance = schema;
|
|
15
|
+
if (schema.type === 'string') {
|
|
16
|
+
systemUserId = 'system';
|
|
17
|
+
}
|
|
18
|
+
else if (schema.type === 'number') {
|
|
19
|
+
systemUserId = 0;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
systemUserId = 'system';
|
|
23
|
+
}
|
|
14
24
|
};
|
|
15
25
|
export const getIdSchema = () => {
|
|
16
26
|
if (!idSchemaInstance) {
|
|
@@ -18,6 +28,12 @@ export const getIdSchema = () => {
|
|
|
18
28
|
}
|
|
19
29
|
return idSchemaInstance;
|
|
20
30
|
};
|
|
31
|
+
export const getSystemUserId = () => {
|
|
32
|
+
if (!systemUserId) {
|
|
33
|
+
throw new Error('SystemUser ID has not been initialized. Please call setIdSchema() at application startup.');
|
|
34
|
+
}
|
|
35
|
+
return systemUserId;
|
|
36
|
+
};
|
|
21
37
|
export function TypeboxIsoDate(options = {}) {
|
|
22
38
|
const dateTransform = Type.Transform(Type.String({ format: 'date-time', ...options }))
|
|
23
39
|
.Decode(value => new Date(value))
|
package/package.json
CHANGED