@@ -2,7 +2,10 @@ import { type Database } from '@unocha/hpc-api-core/src/db';
2
2
import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow' ;
3
3
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions' ;
4
4
import { type InstanceOfModel } from '@unocha/hpc-api-core/src/db/util/types' ;
5
- import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types' ;
5
+ import {
6
+ createBrandedValue ,
7
+ getTableColumns ,
8
+ } from '@unocha/hpc-api-core/src/util/types' ;
6
9
import { Service } from 'typedi' ;
7
10
import { FlowObjectService } from '../flow-object/flow-object-service' ;
8
11
import type {
@@ -52,11 +55,20 @@ export class FlowService {
52
55
orderBy : FlowOrderByWithSubEntity
53
56
) : Promise < UniqueFlowEntity [ ] > {
54
57
const entity = orderBy . subEntity ?? orderBy . entity ;
58
+ let columns : string [ ] = [ ] ;
59
+
55
60
// Get the entity list
56
61
// 'externalReference' is a special case
57
62
// because it does have a direct relation with flow
58
63
// and no direction
59
64
if ( entity === 'externalReference' ) {
65
+ columns = getTableColumns ( database . externalReference ) ;
66
+ if ( ! columns . includes ( orderBy . column ) ) {
67
+ throw new Error (
68
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
69
+ ) ;
70
+ }
71
+
60
72
const column = orderBy . column as keyof InstanceOfModel <
61
73
Database [ 'externalReference' ]
62
74
> ;
@@ -78,17 +90,23 @@ export class FlowService {
78
90
79
91
const refDirection = orderBy . direction ?? 'source' ;
80
92
81
- // Validate the variable using io-ts
82
-
83
93
let flowObjects = [ ] ;
84
94
let entityIDsSorted : number [ ] = [ ] ;
85
95
86
96
switch ( entity ) {
87
97
case 'emergency' : {
98
+ columns = getTableColumns ( database . emergency ) ;
99
+ if ( ! columns . includes ( orderBy . column ) ) {
100
+ throw new Error (
101
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
102
+ ) ;
103
+ }
104
+
88
105
// Get emergency entities sorted
89
106
const column = orderBy . column as keyof InstanceOfModel <
90
107
Database [ 'emergency' ]
91
108
> ;
109
+
92
110
const orderByEmergency = { column, order : orderBy . order } ;
93
111
94
112
const emergencies = await database . emergency . find ( {
@@ -102,6 +120,13 @@ export class FlowService {
102
120
break ;
103
121
}
104
122
case 'globalCluster' : {
123
+ columns = getTableColumns ( database . globalCluster ) ;
124
+
125
+ if ( ! columns . includes ( orderBy . column ) ) {
126
+ throw new Error (
127
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
128
+ ) ;
129
+ }
105
130
// Get globalCluster entities sorted
106
131
const column = orderBy . column as keyof InstanceOfModel <
107
132
Database [ 'globalCluster' ]
@@ -119,6 +144,13 @@ export class FlowService {
119
144
break ;
120
145
}
121
146
case 'governingEntity' : {
147
+ columns = getTableColumns ( database . governingEntity ) ;
148
+
149
+ if ( ! columns . includes ( orderBy . column ) ) {
150
+ throw new Error (
151
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
152
+ ) ;
153
+ }
122
154
// Get governingEntity entities sorted
123
155
const column = orderBy . column as keyof InstanceOfModel <
124
156
Database [ 'governingEntity' ]
@@ -136,6 +168,13 @@ export class FlowService {
136
168
break ;
137
169
}
138
170
case 'location' : {
171
+ columns = getTableColumns ( database . location ) ;
172
+
173
+ if ( ! columns . includes ( orderBy . column ) ) {
174
+ throw new Error (
175
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
176
+ ) ;
177
+ }
139
178
// Get location entities sorted
140
179
const column = orderBy . column as keyof InstanceOfModel <
141
180
Database [ 'location' ]
@@ -151,6 +190,13 @@ export class FlowService {
151
190
break ;
152
191
}
153
192
case 'organization' : {
193
+ columns = getTableColumns ( database . organization ) ;
194
+
195
+ if ( ! columns . includes ( orderBy . column ) ) {
196
+ throw new Error (
197
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
198
+ ) ;
199
+ }
154
200
// Get organization entities sorted
155
201
const column = orderBy . column as keyof InstanceOfModel <
156
202
Database [ 'organization' ]
@@ -168,6 +214,13 @@ export class FlowService {
168
214
break ;
169
215
}
170
216
case 'plan' : {
217
+ columns = getTableColumns ( database . plan ) ;
218
+
219
+ if ( ! columns . includes ( orderBy . column ) ) {
220
+ throw new Error (
221
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
222
+ ) ;
223
+ }
171
224
// Get plan entities sorted
172
225
const column = orderBy . column as keyof InstanceOfModel <
173
226
Database [ 'plan' ]
@@ -183,6 +236,13 @@ export class FlowService {
183
236
break ;
184
237
}
185
238
case 'project' : {
239
+ columns = getTableColumns ( database . project ) ;
240
+
241
+ if ( ! columns . includes ( orderBy . column ) ) {
242
+ throw new Error (
243
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
244
+ ) ;
245
+ }
186
246
// Get project entities sorted
187
247
const column = orderBy . column as keyof InstanceOfModel <
188
248
Database [ 'project' ]
@@ -198,6 +258,13 @@ export class FlowService {
198
258
break ;
199
259
}
200
260
case 'usageYear' : {
261
+ columns = getTableColumns ( database . usageYear ) ;
262
+
263
+ if ( ! columns . includes ( orderBy . column ) ) {
264
+ throw new Error (
265
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
266
+ ) ;
267
+ }
201
268
// Get usageYear entities sorted
202
269
const column = orderBy . column as keyof InstanceOfModel <
203
270
Database [ 'usageYear' ]
@@ -213,6 +280,13 @@ export class FlowService {
213
280
break ;
214
281
}
215
282
case 'planVersion' : {
283
+ columns = getTableColumns ( database . planVersion ) ;
284
+
285
+ if ( ! columns . includes ( orderBy . column ) ) {
286
+ throw new Error (
287
+ `Invalid column ${ orderBy . column } to sort by in ${ orderBy . entity } `
288
+ ) ;
289
+ }
216
290
// Get planVersion entities sorted
217
291
// Collect fisrt part of the entity key by the fisrt Case letter
218
292
const entityKey = `${
0 commit comments