@bearei/server-common 0.0.107 → 0.0.108
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.
|
@@ -7,13 +7,13 @@ export interface BaseServiceOptions<T> extends Pick<ActionLogsOptions, 'enableLo
|
|
|
7
7
|
entityOptions: DeepPartial<T>;
|
|
8
8
|
findOptions: FindOptionsWhere<T>;
|
|
9
9
|
headers: APIHeaders;
|
|
10
|
-
preValidateOptions?: Omit<PreValidateOptions<T>, 'serviceName' | '
|
|
10
|
+
preValidateOptions?: Omit<PreValidateOptions<T>, 'serviceName' | 'actionLogsOptions'>;
|
|
11
11
|
serviceName: string;
|
|
12
12
|
}
|
|
13
13
|
export type CreateOptions<T> = Omit<BaseServiceOptions<T>, 'findOptions'>;
|
|
14
14
|
export type DelOptions<T> = Omit<BaseServiceOptions<T>, 'entityOptions'>;
|
|
15
15
|
export type UpdateOptions<T> = Omit<BaseServiceOptions<T>, 'preValidateOptions'> & {
|
|
16
|
-
preValidateOptions?: Omit<PreValidateOptions<T>, 'serviceName' | '
|
|
16
|
+
preValidateOptions?: Omit<PreValidateOptions<T>, 'serviceName' | 'actionLogsOptions'> & {
|
|
17
17
|
validateKeys?: (keyof FindOptionsWhere<T>)[];
|
|
18
18
|
};
|
|
19
19
|
};
|
|
@@ -40,10 +40,10 @@ export interface UpdateAssociationOptions<T> extends Omit<BaseServiceOptions<T>,
|
|
|
40
40
|
addEntityOptions?: DeepPartial<T>;
|
|
41
41
|
}
|
|
42
42
|
export interface FindOneOptions<T> extends Omit<BaseServiceOptions<T>, 'headers' | 'entityOptions'> {
|
|
43
|
-
|
|
43
|
+
actionLogsOptions?: ActionLogsOptions;
|
|
44
44
|
}
|
|
45
45
|
export interface PreValidateOptions<T> {
|
|
46
|
-
|
|
46
|
+
actionLogsOptions?: ActionLogsOptions;
|
|
47
47
|
findOptions: FindOptionsWhere<T> & NotIdDTO;
|
|
48
48
|
orValidateKeysGroups?: (keyof FindOptionsWhere<T>)[][];
|
|
49
49
|
serviceName: string;
|
|
@@ -3,8 +3,8 @@ import { ActionLogsOptions, ActionLogsService } from '../action-logs';
|
|
|
3
3
|
import { AppEntity } from '../app.entity';
|
|
4
4
|
import { CountOptions, CountResult, CreateOptions, DelOptions, FindOneOptions, FindOptions, PreValidateOptions, ThrowErrorOptions, UpdateAssociationOptions, UpdateOptions } from './base.interface';
|
|
5
5
|
export declare class BaseService {
|
|
6
|
-
private readonly
|
|
7
|
-
constructor(
|
|
6
|
+
private readonly actionLogsService;
|
|
7
|
+
constructor(actionLogsService: ActionLogsService);
|
|
8
8
|
create<T extends AppEntity>({ enableLogging, entityOptions, headers, preValidateOptions, serviceName }: CreateOptions<T>): (repository: Repository<T>) => Promise<(entityManager?: EntityManager) => Promise<T>>;
|
|
9
9
|
del<T extends AppEntity>({ findOptions, serviceName, headers, enableLogging }: DelOptions<T>): (repository: Repository<T>) => Promise<(entityManager?: EntityManager) => Promise<void>>;
|
|
10
10
|
update<T extends AppEntity>({ enableLogging, entityOptions, findOptions, headers, preValidateOptions, serviceName }: UpdateOptions<T>): (repository: Repository<T>) => (rawEntity?: T) => Promise<(entityManager?: EntityManager) => Promise<T>>;
|
|
@@ -15,14 +15,14 @@ export declare class BaseService {
|
|
|
15
15
|
total: string;
|
|
16
16
|
}>;
|
|
17
17
|
count<T extends AppEntity>({ findOptions, findManyOptions }: CountOptions<T>): (repository: Repository<T>) => Promise<CountResult>;
|
|
18
|
-
findOne<T extends AppEntity>({ serviceName, findOptions,
|
|
18
|
+
findOne<T extends AppEntity>({ serviceName, findOptions, actionLogsOptions }: FindOneOptions<T>): (repository: Repository<T>) => Promise<T>;
|
|
19
19
|
updateAssociation<T extends AppEntity & {
|
|
20
20
|
protected?: boolean;
|
|
21
21
|
}>({ addEntityOptions, enableLogging, entityIds: rawEntityIds, findOptions, headers, serviceName }: UpdateAssociationOptions<T>): (associationKey: string) => (repository: Repository<T>) => Promise<(entityManager?: EntityManager) => Promise<T[]>>;
|
|
22
|
-
preValidate<T extends AppEntity>({
|
|
23
|
-
checkProhibited<T extends AppEntity>(
|
|
22
|
+
preValidate<T extends AppEntity>({ actionLogsOptions, findOptions, orValidateKeysGroups, serviceName, validateMethod }: PreValidateOptions<T>): Promise<(repository: Repository<T>) => Promise<void>>;
|
|
23
|
+
checkProhibited<T extends AppEntity>(actionLogsOptions?: ActionLogsOptions): (entity: T & {
|
|
24
24
|
protected?: boolean;
|
|
25
25
|
}) => Promise<void>;
|
|
26
|
-
ActionLogsSuccess<T extends AppEntity>(
|
|
27
|
-
throwError({ errorType, ...args }: ThrowErrorOptions): (
|
|
26
|
+
ActionLogsSuccess<T extends AppEntity>(actionLogsOptions: ActionLogsOptions): (entity: T | T[]) => Promise<void>;
|
|
27
|
+
throwError({ errorType, ...args }: ThrowErrorOptions): (actionLogsOptions: ActionLogsOptions) => Promise<void>;
|
|
28
28
|
}
|
|
@@ -16,12 +16,12 @@ const action_logs_1 = require("../action-logs");
|
|
|
16
16
|
const app_interface_1 = require("../app.interface");
|
|
17
17
|
const utils_1 = require("../utils");
|
|
18
18
|
let BaseService = class BaseService {
|
|
19
|
-
|
|
20
|
-
constructor(
|
|
21
|
-
this.
|
|
19
|
+
actionLogsService;
|
|
20
|
+
constructor(actionLogsService) {
|
|
21
|
+
this.actionLogsService = actionLogsService;
|
|
22
22
|
}
|
|
23
23
|
create({ enableLogging = true, entityOptions, headers, preValidateOptions, serviceName }) {
|
|
24
|
-
const
|
|
24
|
+
const actionLogsOptions = {
|
|
25
25
|
action: app_interface_1.Action.CREATE,
|
|
26
26
|
enableLogging,
|
|
27
27
|
headers,
|
|
@@ -34,18 +34,18 @@ let BaseService = class BaseService {
|
|
|
34
34
|
Object.entries(preValidateOptions.findOptions).filter(([, value]) => typeof value !== 'undefined')
|
|
35
35
|
.length;
|
|
36
36
|
if (preValidate) {
|
|
37
|
-
await (await this.preValidate({ ...preValidateOptions, serviceName,
|
|
37
|
+
await (await this.preValidate({ ...preValidateOptions, serviceName, actionLogsOptions }))(repository);
|
|
38
38
|
}
|
|
39
39
|
const createdEntity = repository.create({ ...entityOptions });
|
|
40
40
|
return async (entityManager) => {
|
|
41
41
|
const entity = await (entityManager ? entityManager.save(createdEntity) : createdEntity.save());
|
|
42
|
-
await this.ActionLogsSuccess(
|
|
42
|
+
await this.ActionLogsSuccess(actionLogsOptions)(entity);
|
|
43
43
|
return entity;
|
|
44
44
|
};
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
del({ findOptions, serviceName, headers, enableLogging = true }) {
|
|
48
|
-
const
|
|
48
|
+
const actionLogsOptions = {
|
|
49
49
|
action: app_interface_1.Action.DELETE,
|
|
50
50
|
enableLogging,
|
|
51
51
|
headers,
|
|
@@ -54,18 +54,18 @@ let BaseService = class BaseService {
|
|
|
54
54
|
serviceName
|
|
55
55
|
};
|
|
56
56
|
return async (repository) => {
|
|
57
|
-
const entity = await this.findOne({ serviceName, findOptions,
|
|
58
|
-
const raw = enableLogging ? { ...
|
|
59
|
-
await this.checkProhibited(
|
|
57
|
+
const entity = await this.findOne({ serviceName, findOptions, actionLogsOptions })(repository);
|
|
58
|
+
const raw = enableLogging ? { ...actionLogsOptions.raw, entity: { ...entity } } : undefined;
|
|
59
|
+
await this.checkProhibited(actionLogsOptions)(entity);
|
|
60
60
|
await entity.softRemove();
|
|
61
61
|
return async (entityManager) => {
|
|
62
62
|
await (entityManager ? entityManager.save(entity) : entity.save());
|
|
63
|
-
await this.ActionLogsSuccess({ ...
|
|
63
|
+
await this.ActionLogsSuccess({ ...actionLogsOptions, raw })(entity);
|
|
64
64
|
};
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
update({ enableLogging = true, entityOptions, findOptions, headers, preValidateOptions, serviceName }) {
|
|
68
|
-
const
|
|
68
|
+
const actionLogsOptions = {
|
|
69
69
|
action: app_interface_1.Action.UPDATE,
|
|
70
70
|
enableLogging,
|
|
71
71
|
headers,
|
|
@@ -80,7 +80,7 @@ let BaseService = class BaseService {
|
|
|
80
80
|
[currentValue]: rawPreValidateFindOptions[currentValue] ?? entity[currentValue]
|
|
81
81
|
}
|
|
82
82
|
: accumulator;
|
|
83
|
-
const updatePreValidate = (preValidateOptions) => (
|
|
83
|
+
const updatePreValidate = (preValidateOptions) => (actionLogsOptions) => async ({ entity, repository }) => {
|
|
84
84
|
const { validateKeys = [], findOptions: rawPreValidateFindOptions, ...otherPreValidateOptions } = preValidateOptions;
|
|
85
85
|
const preValidateKeys = [
|
|
86
86
|
...new Set([
|
|
@@ -96,15 +96,15 @@ let BaseService = class BaseService {
|
|
|
96
96
|
const preValidateFindOptions = preValidateKeys.reduce(handlePreValidateFindOptions(rawPreValidateFindOptions)(entity), {});
|
|
97
97
|
await (await this.preValidate({
|
|
98
98
|
...otherPreValidateOptions,
|
|
99
|
-
|
|
99
|
+
actionLogsOptions,
|
|
100
100
|
findOptions: { ...preValidateFindOptions, notId: entity.id },
|
|
101
101
|
serviceName
|
|
102
102
|
}))(repository);
|
|
103
103
|
};
|
|
104
104
|
return (repository) => async (rawEntity) => {
|
|
105
|
-
const entity = rawEntity ?? (await this.findOne({
|
|
106
|
-
const raw = enableLogging ? { ...
|
|
107
|
-
const nextActionLogsOptions = { ...
|
|
105
|
+
const entity = rawEntity ?? (await this.findOne({ actionLogsOptions, findOptions, serviceName })(repository));
|
|
106
|
+
const raw = enableLogging ? { ...actionLogsOptions.raw, entity: { ...entity }, entityOptions } : undefined;
|
|
107
|
+
const nextActionLogsOptions = { ...actionLogsOptions, raw };
|
|
108
108
|
await this.checkProhibited(nextActionLogsOptions)(entity);
|
|
109
109
|
if (preValidateOptions) {
|
|
110
110
|
await updatePreValidate(preValidateOptions)(nextActionLogsOptions)({ entity, repository });
|
|
@@ -112,7 +112,7 @@ let BaseService = class BaseService {
|
|
|
112
112
|
repository.merge(entity, entityOptions);
|
|
113
113
|
return async (entityManager) => {
|
|
114
114
|
await (entityManager ? entityManager.save(entity) : entity.save());
|
|
115
|
-
await this.ActionLogsSuccess({ ...
|
|
115
|
+
await this.ActionLogsSuccess({ ...actionLogsOptions, raw })(entity);
|
|
116
116
|
return entity;
|
|
117
117
|
};
|
|
118
118
|
};
|
|
@@ -184,7 +184,7 @@ let BaseService = class BaseService {
|
|
|
184
184
|
: { count: `${await queryBuilder.getCount()}` };
|
|
185
185
|
};
|
|
186
186
|
}
|
|
187
|
-
findOne({ serviceName, findOptions,
|
|
187
|
+
findOne({ serviceName, findOptions, actionLogsOptions }) {
|
|
188
188
|
return async (repository) => {
|
|
189
189
|
const entity = await repository.findOne({
|
|
190
190
|
where: Object.entries(findOptions).reduce((accumulator, [key, value]) => typeof value !== 'undefined' ? { ...accumulator, [key]: value } : accumulator, {})
|
|
@@ -194,7 +194,7 @@ let BaseService = class BaseService {
|
|
|
194
194
|
await this.throwError({
|
|
195
195
|
errorType: app_interface_1.APIError.NOT_FOUND,
|
|
196
196
|
message: `The ${serviceName} does not exist. Find by fields ${field}`
|
|
197
|
-
})(
|
|
197
|
+
})(actionLogsOptions);
|
|
198
198
|
}
|
|
199
199
|
return entity;
|
|
200
200
|
};
|
|
@@ -204,7 +204,7 @@ let BaseService = class BaseService {
|
|
|
204
204
|
const entityIds = [...new Set(rawEntityIds)];
|
|
205
205
|
const filterSoftRemoved = ({ entityStatus }) => entityStatus !== 'softRemoved';
|
|
206
206
|
return (associationKey) => {
|
|
207
|
-
const
|
|
207
|
+
const actionLogsOptions = {
|
|
208
208
|
action: app_interface_1.Action.UPDATE,
|
|
209
209
|
enableLogging,
|
|
210
210
|
headers,
|
|
@@ -219,7 +219,7 @@ let BaseService = class BaseService {
|
|
|
219
219
|
})(repository).then(({ results }) => results);
|
|
220
220
|
const raw = enableLogging ?
|
|
221
221
|
{
|
|
222
|
-
...
|
|
222
|
+
...actionLogsOptions.raw,
|
|
223
223
|
associationKey,
|
|
224
224
|
entities: entities.map(entity => ({ ...entity })),
|
|
225
225
|
entityIds
|
|
@@ -236,7 +236,7 @@ let BaseService = class BaseService {
|
|
|
236
236
|
const entitiesToRemove = await Promise.all(entities
|
|
237
237
|
.filter(entity => !entityIds.includes(entity[associationKey]))
|
|
238
238
|
.map(entity => entity.softRemove()));
|
|
239
|
-
await Promise.all(entitiesToRemove.map(this.checkProhibited(
|
|
239
|
+
await Promise.all(entitiesToRemove.map(this.checkProhibited(actionLogsOptions)));
|
|
240
240
|
const entitiesToRecover = await Promise.all(entities
|
|
241
241
|
.filter(entity => entityIds.includes(entity[associationKey]) && entity.deletedAt)
|
|
242
242
|
.map(entity => entity.recover()));
|
|
@@ -245,13 +245,13 @@ let BaseService = class BaseService {
|
|
|
245
245
|
const savedEntities = await (entityManager ?
|
|
246
246
|
entityManager.save(entityActions)
|
|
247
247
|
: repository.save(entityActions));
|
|
248
|
-
await this.ActionLogsSuccess({ ...
|
|
248
|
+
await this.ActionLogsSuccess({ ...actionLogsOptions, raw })(savedEntities);
|
|
249
249
|
return [...entities, ...entitiesToAdd].filter(filterSoftRemoved);
|
|
250
250
|
};
|
|
251
251
|
};
|
|
252
252
|
};
|
|
253
253
|
}
|
|
254
|
-
async preValidate({
|
|
254
|
+
async preValidate({ actionLogsOptions, findOptions, orValidateKeysGroups, serviceName, validateMethod = 'and' }) {
|
|
255
255
|
const handleOrKeys = ({ notId, ...args }) => (keys) => keys.reduce((accumulator, currentValue) => typeof currentValue !== 'undefined' ?
|
|
256
256
|
{ ...accumulator, [currentValue]: args[currentValue] }
|
|
257
257
|
: accumulator, (notId ? { id: (0, typeorm_1.Not)(notId) } : {}));
|
|
@@ -266,7 +266,7 @@ let BaseService = class BaseService {
|
|
|
266
266
|
code: '400001',
|
|
267
267
|
errorType: app_interface_1.APIError.BAD_REQUEST,
|
|
268
268
|
message: `When using "or" as a validation method, "orValidateKeysGroups" must be a nested array. ${orValidateKeysGroup}`
|
|
269
|
-
})(
|
|
269
|
+
})(actionLogsOptions);
|
|
270
270
|
}
|
|
271
271
|
const flattedValidateOrKeys = [...new Set(orValidateKeysGroups?.flat())];
|
|
272
272
|
const findOrOptions = orValidateKeysGroups?.map(handleOrKeys(findOptions)) ?? [];
|
|
@@ -296,33 +296,33 @@ let BaseService = class BaseService {
|
|
|
296
296
|
code: '400002',
|
|
297
297
|
errorType: app_interface_1.APIError.BAD_REQUEST,
|
|
298
298
|
message: `The ${serviceName} fields already exists. ${field}`
|
|
299
|
-
})(
|
|
299
|
+
})(actionLogsOptions);
|
|
300
300
|
}
|
|
301
301
|
};
|
|
302
302
|
}
|
|
303
|
-
checkProhibited(
|
|
303
|
+
checkProhibited(actionLogsOptions) {
|
|
304
304
|
return async (entity) => {
|
|
305
305
|
if (typeof entity.protected === 'boolean' && entity.protected) {
|
|
306
306
|
await this.throwError({
|
|
307
307
|
code: '403001',
|
|
308
308
|
errorType: app_interface_1.APIError.FORBIDDEN,
|
|
309
|
-
message: `ALL actions on default ${
|
|
310
|
-
})(
|
|
309
|
+
message: `ALL actions on default ${actionLogsOptions.serviceName} are prohibited`
|
|
310
|
+
})(actionLogsOptions);
|
|
311
311
|
}
|
|
312
312
|
};
|
|
313
313
|
}
|
|
314
|
-
ActionLogsSuccess(
|
|
315
|
-
return async (entity) => await this.
|
|
314
|
+
ActionLogsSuccess(actionLogsOptions) {
|
|
315
|
+
return async (entity) => await this.actionLogsService.success(actionLogsOptions)({ entity });
|
|
316
316
|
}
|
|
317
317
|
throwError({ errorType, ...args }) {
|
|
318
|
-
return async (
|
|
318
|
+
return async (actionLogsOptions) => {
|
|
319
319
|
try {
|
|
320
320
|
(0, utils_1.throwAPIError)(args)(errorType);
|
|
321
321
|
}
|
|
322
322
|
catch (error) {
|
|
323
|
-
if (
|
|
324
|
-
await this.
|
|
325
|
-
...
|
|
323
|
+
if (actionLogsOptions) {
|
|
324
|
+
await this.actionLogsService.failure({
|
|
325
|
+
...actionLogsOptions,
|
|
326
326
|
message: error?.message
|
|
327
327
|
})({ error });
|
|
328
328
|
}
|