@monorise/core 1.0.2 → 1.0.4-0

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.
Files changed (69) hide show
  1. package/dist/mock/monorise/chapter.d.ts +1 -1
  2. package/dist/mock/monorise/course.d.ts +1 -1
  3. package/dist/mock/monorise/learning-journey-config.d.ts +1 -1
  4. package/dist/mock/monorise/module.d.ts +1 -1
  5. package/dist/mock/monorise/organization.d.ts +1 -1
  6. package/dist/services/entity.service.d.ts +2 -2
  7. package/dist/services/entity.service.d.ts.map +1 -1
  8. package/package.json +4 -1
  9. package/configs/service.config.ts +0 -14
  10. package/constants/table.ts +0 -3
  11. package/controllers/entity/create-entity.controller.ts +0 -51
  12. package/controllers/entity/delete-entity.controller.ts +0 -35
  13. package/controllers/entity/entity.http +0 -62
  14. package/controllers/entity/get-entity.controller.ts +0 -33
  15. package/controllers/entity/list-entities.controller.ts +0 -69
  16. package/controllers/entity/update-entity.controller.ts +0 -56
  17. package/controllers/entity/upsert-entity.controller.ts +0 -97
  18. package/controllers/mutual/create-mutual.controller.ts +0 -89
  19. package/controllers/mutual/delete-mutual.controller.ts +0 -40
  20. package/controllers/mutual/get-mutual.controller.ts +0 -38
  21. package/controllers/mutual/list-entities-by-entity.controller.ts +0 -76
  22. package/controllers/mutual/mutual.http +0 -88
  23. package/controllers/mutual/update-mutual.controller.ts +0 -50
  24. package/controllers/setupRoutes.ts +0 -73
  25. package/controllers/tag/list-tags.controller.ts +0 -57
  26. package/data/DbUtils.ts +0 -40
  27. package/data/Entity.ts +0 -499
  28. package/data/EventUtils.ts +0 -47
  29. package/data/FileObject.ts +0 -16
  30. package/data/Mutual.ts +0 -779
  31. package/data/ProjectionExpression.ts +0 -8
  32. package/data/Tag.ts +0 -470
  33. package/data/abstract/Item.base.ts +0 -19
  34. package/data/abstract/Repository.base.ts +0 -92
  35. package/errors/api-error.ts +0 -39
  36. package/errors/extendable-error.ts +0 -35
  37. package/errors/standard-error.ts +0 -29
  38. package/helpers/dependencies.ts +0 -10
  39. package/helpers/event.ts +0 -85
  40. package/helpers/fromLastKeyQuery.ts +0 -11
  41. package/helpers/sleep.ts +0 -1
  42. package/helpers/toLastKeyResponse.ts +0 -11
  43. package/index.ts +0 -23
  44. package/middlewares/entity-type-check.ts +0 -20
  45. package/middlewares/mutual-type-check.ts +0 -26
  46. package/mock/entity.ts +0 -12
  47. package/mock/monorise/admin.ts +0 -35
  48. package/mock/monorise/chapter.ts +0 -94
  49. package/mock/monorise/course.ts +0 -149
  50. package/mock/monorise/index.ts +0 -143
  51. package/mock/monorise/learner.ts +0 -66
  52. package/mock/monorise/learning-activity.ts +0 -62
  53. package/mock/monorise/learning-journey-config.ts +0 -34
  54. package/mock/monorise/module.ts +0 -108
  55. package/mock/monorise/organization.ts +0 -63
  56. package/mock/monorise/reference.ts +0 -28
  57. package/mock/monorise/video.ts +0 -36
  58. package/processors/create-entity-processor.ts +0 -55
  59. package/processors/mutual-processor.ts +0 -262
  60. package/processors/prejoin-processor.ts +0 -264
  61. package/processors/replication-processor.ts +0 -261
  62. package/processors/tag-processor.ts +0 -174
  63. package/services/DependencyContainer.ts +0 -208
  64. package/services/entity-service-lifecycle.ts +0 -41
  65. package/services/entity.service.ts +0 -201
  66. package/services/mutual.service.ts +0 -285
  67. package/tsconfig.json +0 -116
  68. package/types/entity.type.ts +0 -62
  69. package/types/event.ts +0 -84
@@ -447,7 +447,7 @@ declare const config: {
447
447
  }>) | undefined;
448
448
  tags?: {
449
449
  name: string;
450
- processor: (entity: import("@monorise/react").CreatedEntity<import("@monorise/base").Entity>) => {
450
+ processor: (entity: import("@monorise/base").CreatedEntity<import("@monorise/base").Entity>) => {
451
451
  group?: string;
452
452
  sortValue?: string;
453
453
  }[];
@@ -301,7 +301,7 @@ declare const config: {
301
301
  }>) | undefined;
302
302
  tags?: {
303
303
  name: string;
304
- processor: (entity: import("@monorise/react").CreatedEntity<import("@monorise/base").Entity>) => {
304
+ processor: (entity: import("@monorise/base").CreatedEntity<import("@monorise/base").Entity>) => {
305
305
  group?: string;
306
306
  sortValue?: string;
307
307
  }[];
@@ -74,7 +74,7 @@ declare const config: {
74
74
  }>) | undefined;
75
75
  tags?: {
76
76
  name: string;
77
- processor: (entity: import("@monorise/react").CreatedEntity<import("@monorise/base").Entity>) => {
77
+ processor: (entity: import("@monorise/base").CreatedEntity<import("@monorise/base").Entity>) => {
78
78
  group?: string;
79
79
  sortValue?: string;
80
80
  }[];
@@ -201,7 +201,7 @@ declare const config: {
201
201
  }>) | undefined;
202
202
  tags?: {
203
203
  name: string;
204
- processor: (entity: import("@monorise/react").CreatedEntity<import("@monorise/base").Entity>) => {
204
+ processor: (entity: import("@monorise/base").CreatedEntity<import("@monorise/base").Entity>) => {
205
205
  group?: string;
206
206
  sortValue?: string;
207
207
  }[];
@@ -336,7 +336,7 @@ declare const config: {
336
336
  }>) | undefined;
337
337
  tags?: {
338
338
  name: string;
339
- processor: (entity: import("@monorise/react").CreatedEntity<import("@monorise/base").Entity>) => {
339
+ processor: (entity: import("@monorise/base").CreatedEntity<import("@monorise/base").Entity>) => {
340
340
  group?: string;
341
341
  sortValue?: string;
342
342
  }[];
@@ -16,13 +16,13 @@ export declare class EntityService {
16
16
  createAndUpdateDatetime?: string;
17
17
  mutualId?: string;
18
18
  };
19
- }) => Promise<import("../data/Entity").Entity<T>>;
19
+ }) => Promise<import("..").Entity<T>>;
20
20
  updateEntity: <T extends Entity>({ entityType, entityId, entityPayload, accountId, }: {
21
21
  entityType: T;
22
22
  entityId: string;
23
23
  entityPayload: Partial<EntitySchemaMap[T]>;
24
24
  accountId?: string | string[];
25
- }) => Promise<import("../data/Entity").Entity<T>>;
25
+ }) => Promise<import("..").Entity<T>>;
26
26
  deleteEntity: <T extends Entity>({ entityType, entityId, accountId, }: {
27
27
  entityType: T;
28
28
  entityId: string;
@@ -1 +1 @@
1
- {"version":3,"file":"entity.service.d.ts","sourceRoot":"","sources":["../../services/entity.service.ts"],"names":[],"mapp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AG,MAAM,EAAE,CAAC;KAC/B,mBAWC;CACH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monorise/core",
3
- "version": "1.0.2",
3
+ "version": "1.0.4-0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -8,6 +8,9 @@
8
8
  "scripts": {
9
9
  "build": "tsc"
10
10
  },
11
+ "files": [
12
+ "dist"
13
+ ],
11
14
  "keywords": [],
12
15
  "author": "",
13
16
  "license": "ISC",
@@ -1,14 +0,0 @@
1
- import {
2
- ENTITY_REPLICATION_INDEX,
3
- MUTUAL_REPLICATION_INDEX,
4
- } from '../constants/table';
5
-
6
- const CORE_TABLE = process.env.CORE_TABLE || '';
7
- const CORE_EVENT_BUS = process.env.CORE_EVENT_BUS || '';
8
-
9
- export {
10
- CORE_TABLE,
11
- CORE_EVENT_BUS,
12
- ENTITY_REPLICATION_INDEX,
13
- MUTUAL_REPLICATION_INDEX,
14
- };
@@ -1,3 +0,0 @@
1
- export const REPLICATION_INDEX = 'REPLICATION_INDEX';
2
- export const ENTITY_REPLICATION_INDEX = 'ENTITY_REPLICATION_INDEX';
3
- export const MUTUAL_REPLICATION_INDEX = 'MUTUAL_REPLICATION_INDEX';
@@ -1,51 +0,0 @@
1
- import type { Entity } from '@monorise/base';
2
- import type { Request, Response } from 'express';
3
- import httpStatus from 'http-status';
4
- import { ZodError } from 'zod';
5
- import { StandardError } from '../../errors/standard-error';
6
- import type { EntityService } from '../../services/entity.service';
7
-
8
- export class CreateEntityController {
9
- constructor(private entityService: EntityService) {}
10
-
11
- controller: (req: Request, res: Response) => void = async (req, res) => {
12
- const accountId = req.headers['account-id'];
13
- const { entityType } = req.params as unknown as {
14
- entityType: Entity;
15
- };
16
-
17
- try {
18
- const entity = await this.entityService.createEntity({
19
- entityType,
20
- entityPayload: req.body,
21
- accountId,
22
- options: {
23
- createAndUpdateDatetime: req.body.createdAt,
24
- },
25
- });
26
-
27
- return res.status(httpStatus.OK).json(entity);
28
- } catch (err) {
29
- if (err instanceof ZodError) {
30
- return res.status(httpStatus.BAD_REQUEST).json({
31
- code: 'API_VALIDATION_ERROR',
32
- message: 'API validation failed',
33
- details: err.flatten(),
34
- });
35
- }
36
-
37
- if (err instanceof StandardError && err.code === 'EMAIL_EXISTS') {
38
- return res.status(httpStatus.BAD_REQUEST).json({
39
- ...err.toJSON(),
40
- });
41
- }
42
-
43
- console.log('===create-entity error:', {
44
- err,
45
- errorContext: JSON.stringify({ body: req.body, headers: req.headers }),
46
- });
47
-
48
- throw err;
49
- }
50
- };
51
- }
@@ -1,35 +0,0 @@
1
- import type { Entity } from '@monorise/base';
2
- import type { Request, Response } from 'express';
3
- import httpStatus from 'http-status';
4
- import { StandardError } from '../../errors/standard-error';
5
- import type { EntityService } from '../../services/entity.service';
6
-
7
- export class DeleteEntityController {
8
- constructor(private readonly entityService: EntityService) {}
9
-
10
- controller: (req: Request, res: Response) => void = async (req, res) => {
11
- const accountId = req.headers['account-id'];
12
- const { entityType, entityId } = req.params as unknown as {
13
- entityType: Entity;
14
- entityId: string;
15
- };
16
-
17
- try {
18
- await this.entityService.deleteEntity({
19
- entityType,
20
- entityId,
21
- accountId,
22
- });
23
-
24
- return res.json({ message: 'entity deleted' });
25
- } catch (err) {
26
- if (err instanceof StandardError && err.code === 'ENTITY_NOT_FOUND') {
27
- return res.status(httpStatus.NOT_FOUND).json({
28
- ...err.toJSON(),
29
- });
30
- }
31
-
32
- throw err;
33
- }
34
- };
35
- }
@@ -1,62 +0,0 @@
1
- ### Entity
2
-
3
- @entityType=active-course
4
-
5
- ### List all entities
6
-
7
- GET {{BASE_URL}}/core/entity/{{entityType}}
8
- content-type: application/json
9
- x-api-key: {{X_API_KEY}}
10
-
11
- ### Search for entity with query
12
-
13
- GET {{BASE_URL}}/core/entity/{{entityType}}
14
- ?query=edit
15
- content-type: application/json
16
- x-api-key: {{X_API_KEY}}
17
-
18
- ### Create entity
19
-
20
- POST {{BASE_URL}}/core/entity/{{entityType}}
21
- content-type: application/json
22
- x-api-key: {{X_API_KEY}}
23
-
24
- {
25
- "email": "test@mail.com",
26
- "displayName": "chapter 3"
27
- }
28
-
29
- ### Get entity by entityId
30
-
31
- @entityId=config
32
-
33
- GET {{BASE_URL}}/core/entity/{{entityType}}/{{entityId}}
34
- content-type: application/json
35
- x-api-key: {{X_API_KEY}}
36
-
37
-
38
- ### Upsert entity
39
-
40
- PUT {{BASE_URL}}/core/entity/{{entityType}}/{{entityId}}
41
- content-type: application/json
42
- x-api-key: {{X_API_KEY}}
43
-
44
- {
45
- "name": "test test edit again"
46
- }
47
-
48
- ### Update entity
49
-
50
- PATCH {{BASE_URL}}/core/entity/{{entityType}}/{{entityId}}
51
- content-type: application/json
52
- x-api-key: {{X_API_KEY}}
53
-
54
- {
55
- "name": "test test edit again"
56
- }
57
-
58
- ### Delete entity
59
-
60
- DELETE {{BASE_URL}}/core/entity/{{entityType}}/{{entityId}}
61
- content-type: application/json
62
- x-api-key: {{X_API_KEY}}
@@ -1,33 +0,0 @@
1
- import type { Entity } from '@monorise/base';
2
- import type { Request, Response } from 'express';
3
- import httpStatus from 'http-status';
4
- import type { EntityRepository } from '../../data/Entity';
5
-
6
- export class GetEntityController {
7
- constructor(private entityRepository: EntityRepository) {}
8
-
9
- controller: (req: Request, res: Response) => void = async (req, res) => {
10
- const { entityType, entityId } = req.params as unknown as {
11
- entityType: Entity;
12
- entityId: string;
13
- };
14
-
15
- try {
16
- const entity = await this.entityRepository.getEntity(
17
- entityType,
18
- entityId,
19
- );
20
-
21
- return res.status(httpStatus.OK).json(entity);
22
- } catch (err) {
23
- if ((err as any).code === 'ENTITY_IS_UNDEFINED') {
24
- return res.status(httpStatus.NOT_FOUND).json({
25
- code: 'ENTITY_NOT_FOUND',
26
- message: 'Entity not found',
27
- });
28
- }
29
-
30
- throw err;
31
- }
32
- };
33
- }
@@ -1,69 +0,0 @@
1
- import type { Entity } from '@monorise/base';
2
- import type { Request, Response } from 'express';
3
- import { z } from 'zod';
4
- import type { EntityRepository } from '../../data/Entity';
5
- import { fromLastKeyQuery } from '../../helpers/fromLastKeyQuery';
6
- import { toLastKeyResponse } from '../../helpers/toLastKeyResponse';
7
-
8
- const querySchema = z.object({
9
- limit: z.string().optional(),
10
- lastKey: z.string().optional(),
11
- query: z.string().trim().optional(),
12
- start: z.string().optional(),
13
- end: z.string().optional(),
14
- });
15
-
16
- export class ListEntitiesController {
17
- constructor(private entityRepository: EntityRepository) {}
18
-
19
- controller: (req: Request, res: Response) => void = async (req, res) => {
20
- const errorContext: { [key: string]: any } = {};
21
- try {
22
- const accountId = req.headers['account-id'] || '';
23
- if (Array.isArray(accountId)) {
24
- throw new Error('multiple account-id detected');
25
- }
26
-
27
- errorContext.accountId = accountId;
28
- errorContext.params = req.params;
29
- errorContext.query = req.query;
30
-
31
- const { entityType } = req.params as unknown as {
32
- entityType: Entity;
33
- };
34
-
35
- const { lastKey, query, limit, start, end } = querySchema.parse(
36
- req.query,
37
- );
38
- if (query) {
39
- const results = await this.entityRepository.queryEntities(
40
- entityType,
41
- query,
42
- );
43
-
44
- return res.json({
45
- data: results.items.map((item) => item.toJSON()),
46
- totalCount: results.totalCount,
47
- filteredCount: results.filteredCount,
48
- });
49
- }
50
-
51
- const results = await this.entityRepository.listEntities({
52
- entityType,
53
- limit: Number(limit),
54
- options: {
55
- lastKey: fromLastKeyQuery(lastKey),
56
- },
57
- ...(start && end ? { between: { start, end } } : {}),
58
- });
59
- return res.json({
60
- data: results.items.map((item) => item.toJSON()),
61
- totalCount: results.totalCount,
62
- lastKey: toLastKeyResponse(results.lastKey),
63
- });
64
- } catch (error) {
65
- console.log({ error, errorContext });
66
- return res.status(500).json({ message: error });
67
- }
68
- };
69
- }
@@ -1,56 +0,0 @@
1
- import type { Entity } from '@monorise/base';
2
- import type { Request, Response } from 'express';
3
- import httpStatus from 'http-status';
4
- import { ZodError } from 'zod';
5
- import { StandardError } from '../../errors/standard-error';
6
- import type { EntityService } from '../../services/entity.service';
7
-
8
- export class UpdateEntityController {
9
- constructor(private entityService: EntityService) {}
10
-
11
- controller: (req: Request, res: Response) => void = async (req, res) => {
12
- const accountId = req.headers['account-id'];
13
- const { entityType, entityId } = req.params as unknown as {
14
- entityType: Entity;
15
- entityId: string;
16
- };
17
- const errorContext: any = {
18
- accountId,
19
- 'req.params': req.params,
20
- 'req.body': req.body,
21
- };
22
-
23
- try {
24
- const entity = await this.entityService.updateEntity({
25
- entityType,
26
- entityId,
27
- entityPayload: req.body,
28
- accountId,
29
- });
30
- errorContext.entity = entity;
31
-
32
- return res.status(httpStatus.OK).json(entity);
33
- } catch (err) {
34
- if (err instanceof ZodError) {
35
- return res.status(httpStatus.BAD_REQUEST).json({
36
- code: 'API_VALIDATION_ERROR',
37
- message: 'API validation failed',
38
- details: err.flatten(),
39
- });
40
- }
41
-
42
- if (err instanceof StandardError && err.code === 'ENTITY_NOT_FOUND') {
43
- return res.status(httpStatus.NOT_FOUND).json({
44
- ...err.toJSON(),
45
- });
46
- }
47
-
48
- console.log(
49
- '====UPDATE_ENTITY_CONTROLLER_ERROR',
50
- err,
51
- JSON.stringify({ errorContext }, null, 2),
52
- );
53
- throw err;
54
- }
55
- };
56
- }
@@ -1,97 +0,0 @@
1
- import type { Entity } from '@monorise/base';
2
- import type { Request, Response } from 'express';
3
- import httpStatus from 'http-status';
4
- import { ZodError } from 'zod';
5
- import { EntityConfig } from '#/lambda-layer/monorise';
6
- import type { EntityRepository } from '../../data/Entity';
7
- import { StandardError } from '../../errors/standard-error';
8
- import type { publishEvent as publishEventType } from '../../helpers/event';
9
- import { EVENT } from '../../types/event';
10
-
11
- export class UpsertEntityController {
12
- constructor(
13
- private entityRepository: EntityRepository,
14
- private publishEvent: typeof publishEventType,
15
- ) {}
16
-
17
- controller: (req: Request, res: Response) => void = async (req, res) => {
18
- const accountId = req.headers['account-id'];
19
- const { entityType, entityId } = req.params as unknown as {
20
- entityType: Entity;
21
- entityId: string;
22
- };
23
-
24
- try {
25
- const entitySchema =
26
- EntityConfig[entityType].createSchema ||
27
- EntityConfig[entityType].baseSchema;
28
- const mutualSchema = EntityConfig[entityType].mutual?.mutualSchema;
29
-
30
- if (!entitySchema || !mutualSchema) {
31
- throw new StandardError('Invalid entity type', 'INVALID_ENTITY_TYPE');
32
- }
33
-
34
- const parsedEntityPayload = entitySchema.parse(req.body);
35
- const parsedMutualPayload = mutualSchema.parse(req.body);
36
-
37
- const entity = await this.entityRepository.upsertEntity(
38
- entityType,
39
- entityId,
40
- parsedEntityPayload,
41
- );
42
-
43
- if (parsedMutualPayload) {
44
- const byEntityType = entityType;
45
- const byEntityId = entity.entityId;
46
- const publishEventPromises = [];
47
-
48
- for (const [fieldKey, config] of Object.entries(
49
- EntityConfig[entityType].mutual?.mutualFields || {},
50
- )) {
51
- publishEventPromises.push(
52
- this.publishEvent({
53
- event: EVENT.CORE.ENTITY_MUTUAL_TO_UPDATE,
54
- payload: {
55
- byEntityType,
56
- byEntityId,
57
- entityType: config.entityType,
58
- field: fieldKey,
59
- mutualIds: (parsedMutualPayload as any)[fieldKey],
60
- publishedAt: entity.updatedAt || new Date().toISOString(),
61
- },
62
- }),
63
- );
64
- }
65
- await Promise.allSettled(publishEventPromises);
66
- }
67
-
68
- await this.publishEvent({
69
- event: EVENT.CORE.ENTITY_UPSERTED,
70
- payload: {
71
- entityType,
72
- entityId: entity.entityId,
73
- payload: req.body,
74
- createdByAccountId: accountId,
75
- },
76
- });
77
-
78
- return res.status(httpStatus.OK).json(entity);
79
- } catch (err) {
80
- if (err instanceof ZodError) {
81
- return res.status(httpStatus.BAD_REQUEST).json({
82
- code: 'API_VALIDATION_ERROR',
83
- message: 'API validation failed',
84
- details: err.flatten(),
85
- });
86
- }
87
-
88
- if (err instanceof StandardError && err.code === 'EMAIL_EXISTS') {
89
- return res.status(httpStatus.BAD_REQUEST).json({
90
- ...err.toJSON(),
91
- });
92
- }
93
-
94
- throw err;
95
- }
96
- };
97
- }
@@ -1,89 +0,0 @@
1
- import type { Entity as EntityType } from '@monorise/base';
2
- import type { Request, Response } from 'express';
3
- import httpStatus from 'http-status';
4
- import { ZodError } from 'zod';
5
- import { StandardError } from '../../errors/standard-error';
6
- import type { publishEvent as publishEventType } from '../../helpers/event';
7
- import type { MutualService } from '../../services/mutual.service';
8
- // import { EVENT, type EventDetail } from '../../types/event';
9
- // import { Entity } from '#/lambda-layer/monorise';
10
-
11
- export class CreateMutualController {
12
- constructor(
13
- private mutualService: MutualService,
14
- private publishEvent: typeof publishEventType,
15
- ) {}
16
-
17
- controller: (req: Request, res: Response) => void = async (req, res) => {
18
- const accountId = req.headers['account-id'];
19
- const { byEntityType, byEntityId, entityType, entityId } =
20
- req.params as unknown as {
21
- byEntityType: EntityType;
22
- byEntityId: string;
23
- entityType: EntityType;
24
- entityId: string;
25
- };
26
-
27
- const { asEntity } = req.query;
28
-
29
- try {
30
- const { mutual, eventPayload } = await this.mutualService.createMutual({
31
- byEntityType,
32
- byEntityId,
33
- entityType,
34
- entityId,
35
- mutualPayload: req.body,
36
- accountId,
37
- options: {
38
- asEntity: asEntity as unknown as EntityType,
39
- },
40
- });
41
-
42
- /*
43
- * Add more custom event based on byEntityType and entityType
44
- */
45
-
46
- // const eventPromises = [];
47
-
48
- // const eventMaps: Record<string, EventDetail> = {
49
- // [`${Entity.LEARNER}_${Entity.LEARNING_ACTIVITY}`]:
50
- // EVENT.CORE_SERVICE.LEARNER_LEARNING_ACTIVITY_SUBMITTED,
51
- // };
52
-
53
- // if (eventMaps[`${byEntityType}_${entityType}`]) {
54
- // eventPromises.push(
55
- // this.publishEvent({
56
- // event: eventMaps[`${byEntityType}_${entityType}`],
57
- // payload: eventPayload,
58
- // }),
59
- // );
60
- // }
61
-
62
- // await Promise.all(eventPromises);
63
-
64
- return res.status(httpStatus.OK).json(mutual);
65
- } catch (err) {
66
- if (err instanceof ZodError) {
67
- return res.status(httpStatus.BAD_REQUEST).json({
68
- code: 'API_VALIDATION_ERROR',
69
- message: 'API validation failed',
70
- details: err.flatten(),
71
- });
72
- }
73
-
74
- if (err instanceof StandardError && err.code === 'MUTUAL_EXISTS') {
75
- return res.status(httpStatus.BAD_REQUEST).json({
76
- ...err.toJSON(),
77
- });
78
- }
79
-
80
- if (err instanceof StandardError && err.code === 'ENTITY_IS_UNDEFINED') {
81
- return res.status(httpStatus.BAD_REQUEST).json({
82
- ...err.toJSON(),
83
- });
84
- }
85
-
86
- throw err;
87
- }
88
- };
89
- }
@@ -1,40 +0,0 @@
1
- import type { Entity } from '@monorise/base';
2
- import type { Request, Response } from 'express';
3
- import httpStatus from 'http-status';
4
- import { StandardError } from '../../errors/standard-error';
5
- import type { MutualService } from '../../services/mutual.service';
6
-
7
- export class DeleteMutualController {
8
- constructor(private mutualService: MutualService) {}
9
-
10
- controller: (req: Request, res: Response) => void = async (req, res) => {
11
- const accountId = req.headers['account-id'];
12
- const { byEntityType, byEntityId, entityType, entityId } =
13
- req.params as unknown as {
14
- byEntityType: Entity;
15
- byEntityId: string;
16
- entityType: Entity;
17
- entityId: string;
18
- };
19
-
20
- try {
21
- const mutual = await this.mutualService.deleteMutual({
22
- byEntityType,
23
- byEntityId,
24
- entityType,
25
- entityId,
26
- accountId,
27
- });
28
-
29
- return res.json(mutual);
30
- } catch (err) {
31
- if (err instanceof StandardError && err.code === 'MUTUAL_NOT_FOUND') {
32
- return res.status(httpStatus.BAD_REQUEST).json({
33
- ...err.toJSON(),
34
- });
35
- }
36
-
37
- throw err;
38
- }
39
- };
40
- }