1
- import { Post , Controller , Body , HttpException , HttpStatus } from '@nestjs/common'
1
+ import { Post , Controller , Body , HttpException , HttpStatus , Logger } from '@nestjs/common'
2
2
import { ApiUseTags , ApiBearerAuth } from '@nestjs/swagger'
3
3
import { set as safeSet , get as safeGet } from 'lodash'
4
4
@@ -7,25 +7,129 @@ import { jsonBodyParser } from '../../core'
7
7
import { successHandler , baseHandler , HTTP_CODE } from '../../helper'
8
8
import { JOB_TYPE } from '../../core/traverse'
9
9
10
+ function throwError ( msg : string , code : number ) {
11
+ Logger . error ( msg )
12
+ throw new HttpException ( msg , code )
13
+ }
14
+
10
15
@ApiBearerAuth ( )
11
16
@ApiUseTags ( 'apijson' )
12
17
@Controller ( 'apijson' )
13
18
export class JsonController {
14
19
constructor ( private readonly jsonService : JsonService ) { }
15
20
16
- @Post ( )
21
+ @Post ( '/update' )
22
+ async update ( @Body ( ) queryBody : object ) : Promise < any > {
23
+
24
+ const parserResult = jsonBodyParser ( queryBody )
25
+ if ( ! parserResult ) {
26
+ throwError ( 'Too many requests, please try again later' , HttpStatus . TOO_MANY_REQUESTS )
27
+ }
28
+
29
+ const { queue :jobList , errors, originalJson, RequestParser } = parserResult
30
+ console . log ( 'RELEASE requestParser instance' )
31
+ RequestParser . flushJob ( )
32
+ if ( errors . length ) {
33
+ throwError ( errors . join ( '; ' ) , HttpStatus . BAD_REQUEST )
34
+ }
35
+
36
+ if ( jobList . find ( job => {
37
+ return job . $$type === JOB_TYPE . LIST_UNION || job . $$type === JOB_TYPE . UNION
38
+ } ) ) {
39
+ throwError (
40
+ `[ERROR] update request is not support union operation "@", please check your request body` ,
41
+ HttpStatus . BAD_REQUEST )
42
+ }
43
+
44
+ if ( jobList . length > 1 ) {
45
+ throwError (
46
+ `[ERROR] update request is not support transaction insert, please request single table payload.` ,
47
+ HttpStatus . BAD_REQUEST )
48
+ }
49
+
50
+ while ( jobList . length ) {
51
+ let currentJob = jobList . pop ( )
52
+ console . log ( 'JOB ID =>' , currentJob . id )
53
+ const primaryKey = currentJob . config . primary
54
+ if ( ! currentJob . options [ primaryKey ] ) {
55
+ throwError ( `[ERROR] "${ primaryKey } " is required in update method` , HttpStatus . BAD_REQUEST )
56
+ }
57
+ try {
58
+ await this . jsonService . update (
59
+ currentJob . options [ primaryKey ] ,
60
+ currentJob . table ,
61
+ currentJob . options
62
+ )
63
+ } catch ( e ) {
64
+ throwError ( e . message , HttpStatus . BAD_REQUEST )
65
+ }
66
+ }
67
+
68
+ return successHandler ( {
69
+ message : '[INFO] update success' ,
70
+ requestBody : originalJson
71
+ } )
72
+ }
73
+
74
+ @Post ( '/add' )
75
+ async add ( @Body ( ) queryBody : object ) : Promise < any > {
76
+
77
+ const parserResult = jsonBodyParser ( queryBody )
78
+ if ( ! parserResult ) {
79
+ throwError ( 'Too many requests, please try again later' , HttpStatus . TOO_MANY_REQUESTS )
80
+ }
81
+
82
+ const { queue :jobList , errors, originalJson, RequestParser } = parserResult
83
+ console . log ( 'RELEASE requestParser instance' )
84
+ RequestParser . flushJob ( )
85
+ if ( errors . length ) {
86
+ throwError ( errors . join ( '; ' ) , HttpStatus . BAD_REQUEST )
87
+ }
88
+
89
+ if ( jobList . find ( job => {
90
+ return job . $$type === JOB_TYPE . LIST_UNION || job . $$type === JOB_TYPE . UNION
91
+ } ) ) {
92
+ throwError (
93
+ `[ERROR] add request is not support union operation "@", please check your request body` ,
94
+ HttpStatus . BAD_REQUEST )
95
+ }
96
+
97
+ if ( jobList . length > 1 ) {
98
+ throwError (
99
+ `[ERROR] add request is not support transaction insert, please request single table payload.` ,
100
+ HttpStatus . BAD_REQUEST )
101
+ }
102
+ while ( jobList . length ) {
103
+ let currentJob = jobList . pop ( )
104
+ console . log ( 'JOB ID =>' , currentJob . id )
105
+
106
+ const { ...payload } = currentJob . options // incase for functional column
107
+ try {
108
+ await this . jsonService . insert ( currentJob . table , payload )
109
+ } catch ( e ) {
110
+ throwError ( e . message , HttpStatus . BAD_REQUEST )
111
+ }
112
+ }
113
+
114
+ return successHandler ( {
115
+ message : '[INFO] insert success' ,
116
+ requestBody : originalJson
117
+ } )
118
+ }
119
+
120
+ @Post ( '/get' )
17
121
async query ( @Body ( ) queryBody : object ) : Promise < any > {
18
122
19
123
const parserResult = jsonBodyParser ( queryBody )
20
124
if ( ! parserResult ) {
21
- throw new HttpException ( 'Too many requests, please try again later' , HttpStatus . TOO_MANY_REQUESTS )
125
+ throwError ( 'Too many requests, please try again later' , HttpStatus . TOO_MANY_REQUESTS )
22
126
}
23
127
24
128
const { queue :jobList , errors, originalJson, RequestParser } = parserResult
25
129
console . log ( 'RELEASE requestParser instance' )
26
130
RequestParser . flushJob ( )
27
131
if ( errors . length ) {
28
- throw new HttpException ( errors . join ( '; ' ) , HttpStatus . BAD_REQUEST )
132
+ throwError ( errors . join ( '; ' ) , HttpStatus . BAD_REQUEST )
29
133
}
30
134
let res = JSON . parse ( JSON . stringify ( originalJson ) )
31
135
if ( ! jobList . length ) {
@@ -37,6 +141,7 @@ export class JsonController {
37
141
}
38
142
39
143
const normalizedData = data => {
144
+ if ( ! data ) return { }
40
145
Object . keys ( data ) . forEach ( key => {
41
146
const normalizedKey = key
42
147
. replace ( / \# / g, '' ) // replace "#" operation arg
@@ -76,7 +181,7 @@ export class JsonController {
76
181
const { referArgs, referVar } = currentJob . options
77
182
const referData = safeGet ( res , referArgs , undefined )
78
183
if ( ! referData ) {
79
- throw new HttpException ( `[ERROR] union job exec failed, for "${ referArgs . join ( '/' ) } " is undefined` , HttpStatus . BAD_REQUEST )
184
+ throwError ( `[ERROR] union job exec failed, for "${ referArgs . join ( '/' ) } " is undefined` , HttpStatus . BAD_REQUEST )
80
185
}
81
186
data = await this . jsonService . findOne ( currentJob . table , {
82
187
[ referVar ] : referData
@@ -89,7 +194,7 @@ export class JsonController {
89
194
90
195
const referData = safeGet ( res , referArgs , undefined )
91
196
if ( ! referData ) {
92
- throw new HttpException ( `[ERROR] union job exec failed, for "${ referArgs . join ( '/' ) } " is undefined` , HttpStatus . BAD_REQUEST )
197
+ throwError ( `[ERROR] union job exec failed, for "${ referArgs . join ( '/' ) } " is undefined` , HttpStatus . BAD_REQUEST )
93
198
}
94
199
data = await this . jsonService . find ( currentJob . table , {
95
200
[ referVar ] : referData ,
0 commit comments