Skip to content

Commit 6d166ba

Browse files
committed
fix(json-api-nestjs): use ErrorFormatService for format error in AtomicOperationModule fix work with tmpId
1 parent 0c82168 commit 6d166ba

File tree

7 files changed

+46
-5
lines changed

7 files changed

+46
-5
lines changed

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/atomic-operation.module.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
AsyncIterate,
2020
} from './factory';
2121
import { MAP_CONTROLLER_INTERCEPTORS } from './constants';
22+
import { ErrorFormatService } from '../../modules/mixin/service';
2223

2324
@Module({})
2425
export class AtomicOperationModule implements NestModule {
@@ -48,10 +49,16 @@ export class AtomicOperationModule implements NestModule {
4849
entityModules: DynamicModule[],
4950
commonModule: DynamicModule
5051
): DynamicModule {
52+
53+
const errorFormat = (commonModule.providers || []).find(i => 'provide' in i && i.provide === ErrorFormatService )
54+
if (!errorFormat) {
55+
throw new Error('ErrorFormatService not found, should be provide in common orm module')
56+
}
5157
return {
5258
module: AtomicOperationModule,
5359
controllers: [OperationController],
5460
providers: [
61+
errorFormat,
5562
ExplorerService,
5663
ExecuteService,
5764
SwaggerService,
@@ -87,3 +94,5 @@ export class AtomicOperationModule implements NestModule {
8794
.forRoutes('{*splat}');
8895
}
8996
}
97+
98+

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/controllers/operation.controller.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { OperationMethode } from '../types';
2626
import { AsyncLocalStorage } from 'async_hooks';
2727
import { RUN_IN_TRANSACTION_FUNCTION } from '../../../constants';
28+
import { ErrorFormatService } from '@klerick/json-api-nestjs';
2829

2930
describe('OperationController', () => {
3031
let operationController: OperationController;
@@ -66,6 +67,10 @@ describe('OperationController', () => {
6667
provide: AsyncLocalStorage,
6768
useValue: new AsyncLocalStorage(),
6869
},
70+
{
71+
provide: ErrorFormatService,
72+
useValue: {}
73+
}
6974
],
7075
}).compile();
7176

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/service/execute.service.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { ParamsForExecute } from '../types';
1919
import { AsyncLocalStorage } from 'async_hooks';
2020
import { RUN_IN_TRANSACTION_FUNCTION } from '../../../constants';
2121
import { Mock } from 'vitest';
22+
import { ErrorFormatService } from '../../mixin/service';
2223

2324
describe('ExecuteService', () => {
2425
let service: ExecuteService;
@@ -54,6 +55,10 @@ describe('ExecuteService', () => {
5455
provide: AsyncLocalStorage,
5556
useValue: new AsyncLocalStorage(),
5657
},
58+
{
59+
provide: ErrorFormatService,
60+
useValue: {}
61+
}
5762
],
5863
}).compile();
5964

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/service/execute.service.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Injectable,
66
PipeTransform,
77
Type,
8+
InternalServerErrorException,
89
} from '@nestjs/common';
910
import {
1011
INTERCEPTORS_METADATA,
@@ -36,6 +37,7 @@ import { IterateFactory } from '../factory';
3637
import { TypeFromType, ValidateQueryError } from '../../../types';
3738
import { RunInTransaction } from '../../mixin/types';
3839
import { RUN_IN_TRANSACTION_FUNCTION } from '../../../constants';
40+
import { ErrorFormatService } from '../../mixin/service';
3941

4042
export function isZodError(
4143
param: string | unknown
@@ -70,6 +72,7 @@ export class ExecuteService {
7072
private mapControllerInterceptor!: MapControllerInterceptor;
7173

7274
@Inject(AsyncLocalStorage) private asyncLocalStorage!: AsyncLocalStorage<any>;
75+
@Inject(ErrorFormatService) private errorFormatService!: ErrorFormatService;
7376

7477
private _interceptorsContextCreator!: InterceptorsContextCreator;
7578

@@ -116,8 +119,8 @@ export class ExecuteService {
116119

117120
const itemReplace = this.replaceTmpIds(paramsForExecute, tmpIdsMap);
118121
const body = itemReplace.at(-1);
119-
const currentTmpId = tmpIds[i];
120-
122+
// First operation doesn't have tmpId'
123+
const currentTmpId = i !== 0 ? tmpIds[i] : undefined;
121124
if (methodName === 'postOne' && currentTmpId && body) {
122125
if (typeof body === 'object' && 'attributes' in body) {
123126
body['id'] = `${currentTmpId}`;
@@ -293,7 +296,20 @@ export class ExecuteService {
293296
}
294297
throw new HttpException(response, e.getStatus());
295298
}
296-
throw e;
299+
const formatError = this.errorFormatService.formatError(e);
300+
301+
if (formatError instanceof InternalServerErrorException) {
302+
throw formatError
303+
}
304+
const response = formatError.getResponse()
305+
if (typeof response === 'object' && 'message' in response && Array.isArray(response['message'])) {
306+
response['message'] = response['message'].map((m: any) => {
307+
m['path'] = [KEY_MAIN_INPUT_SCHEMA, `${i}`, ...m['path']];
308+
return m;
309+
});
310+
throw new HttpException(response, formatError.getStatus());
311+
}
312+
throw formatError;
297313
}
298314

299315
private async runOneOperation(

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/service/explorer.service.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ describe('ExplorerService', () => {
5757
});
5858

5959
describe('getMethodNameByParam()', () => {
60-
it('should return the correct method name for given parameters', () => {
60+
it('should return the correct method name for given parameters: postRelationship', () => {
6161
expect(service.getMethodNameByParam(Operation.add, 'id', 'rel')).toBe(
6262
'postRelationship'
6363
);
6464
});
65+
it('should return the correct method name for given parameters: postOne', () => {
66+
expect(service.getMethodNameByParam(Operation.add, 'id')).toBe(
67+
'postOne'
68+
);
69+
});
6570
});
6671

6772
describe('getParamsForMethod()', () => {

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/service/explorer.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class ExplorerService<E extends object = object> {
4848
): OperationMethode<E> {
4949
switch (operation) {
5050
case Operation.add:
51-
return id ? 'postRelationship' : 'postOne';
51+
return rel ? 'postRelationship' : 'postOne';
5252
case Operation.remove:
5353
return rel ? 'deleteRelationship' : 'deleteOne';
5454
case Operation.update:

libs/json-api/json-api-nestjs/src/lib/modules/atomic-operation/utils/zod/zod-helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const zodAdd = <T extends string>(type: T) =>
3636
.object({
3737
type: z.literal(type),
3838
tmpId: z.union([z.number(), z.string()]).optional(),
39+
id: z.string().optional(),
3940
})
4041
.strict(),
4142
data: zodGeneralData,

0 commit comments

Comments
 (0)