@things-factory/worksheet-base 5.0.0-alpha.21 → 5.0.0-alpha.24

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.
@@ -10,383 +10,380 @@ import { Worksheet } from '../../../entities'
10
10
 
11
11
  export const worksheetsResolver = {
12
12
  async worksheets(_: any, params: ListParam, context: any) {
13
- return await worksheets(params, context)
14
- }
15
- }
13
+ try {
14
+ ////For inbound worksheet filter
15
+ const { domain, user }: { domain: Domain; user: User } = context.state
16
+ const arrivalNoticeParam: any = params.filters.find((param: any) => param.name === 'arrivalNoticeNo')
17
+ const arrivalNoticeRefNoParam = params.filters.find(param => param.name === 'arrivalNoticeRefNo')
18
+ const arrivalNoticeCrossDockingParam = params.filters.find(param => param.name === 'crossDocking')
19
+ const typeParam = params.filters.find(param => param.name === 'type')
20
+
21
+ if (arrivalNoticeParam || arrivalNoticeRefNoParam || arrivalNoticeCrossDockingParam) {
22
+ let arrFilters = []
23
+ if (arrivalNoticeParam) {
24
+ params.filters.splice(
25
+ params.filters.findIndex(item => item.name == 'arrivalNoticeNo'),
26
+ 1
27
+ )
28
+ arrFilters.push({ ...arrivalNoticeParam, name: 'name' })
29
+ }
30
+ if (arrivalNoticeRefNoParam) {
31
+ params.filters.splice(
32
+ params.filters.findIndex(item => item.name == 'arrivalNoticeRefNo'),
33
+ 1
34
+ )
35
+ arrFilters.push({ ...arrivalNoticeRefNoParam, name: 'refNo' })
36
+ }
37
+ if (arrivalNoticeCrossDockingParam) {
38
+ params.filters.splice(
39
+ params.filters.findIndex(item => item.name == 'crossDocking'),
40
+ 1
41
+ )
42
+ arrFilters.push({ ...arrivalNoticeCrossDockingParam, name: 'crossDocking' })
43
+ }
44
+ const foundArrivalNotices: ArrivalNotice[] = await getRepository(ArrivalNotice).find({
45
+ ...convertListParams({ filters: arrFilters })
46
+ })
16
47
 
17
- export async function worksheets(params: ListParam, context: any) {
18
- try {
19
- ////For inbound worksheet filter
20
- const { domain, user }: { domain: Domain; user: User } = context.state
21
- const arrivalNoticeParam: any = params.filters.find((param: any) => param.name === 'arrivalNoticeNo')
22
- const arrivalNoticeRefNoParam = params.filters.find(param => param.name === 'arrivalNoticeRefNo')
23
- const arrivalNoticeCrossDockingParam = params.filters.find(param => param.name === 'crossDocking')
24
- const typeParam = params.filters.find(param => param.name === 'type')
25
-
26
- if (arrivalNoticeParam || arrivalNoticeRefNoParam || arrivalNoticeCrossDockingParam) {
27
- let arrFilters = []
28
- if (arrivalNoticeParam) {
29
- params.filters.splice(
30
- params.filters.findIndex(item => item.name == 'arrivalNoticeNo'),
31
- 1
32
- )
33
- arrFilters.push({ ...arrivalNoticeParam, name: 'name' })
34
- }
35
- if (arrivalNoticeRefNoParam) {
36
- params.filters.splice(
37
- params.filters.findIndex(item => item.name == 'arrivalNoticeRefNo'),
38
- 1
39
- )
40
- arrFilters.push({ ...arrivalNoticeRefNoParam, name: 'refNo' })
48
+ if (foundArrivalNotices && foundArrivalNotices.length) {
49
+ params.filters.push({
50
+ name: 'arrivalNoticeId',
51
+ operator: 'in',
52
+ value: foundArrivalNotices.map((foundAN: ArrivalNotice) => foundAN.id),
53
+ relation: false
54
+ })
55
+ } else {
56
+ params.filters.push({
57
+ name: 'arrivalNoticeId',
58
+ operator: 'is_null',
59
+ relation: false
60
+ })
61
+ }
41
62
  }
42
- if (arrivalNoticeCrossDockingParam) {
43
- params.filters.splice(
44
- params.filters.findIndex(item => item.name == 'crossDocking'),
45
- 1
46
- )
47
- arrFilters.push({ ...arrivalNoticeCrossDockingParam, name: 'crossDocking' })
63
+
64
+ const tzOffsetIdx = params.filters.findIndex(param => param.name === 'tzoffset')
65
+ let tzOffset = 0
66
+ if (tzOffsetIdx >= 0) {
67
+ tzOffset = params.filters[tzOffsetIdx].value
68
+ params.filters.splice(tzOffsetIdx, 1)
48
69
  }
49
- const foundArrivalNotices: ArrivalNotice[] = await getRepository(ArrivalNotice).find({
50
- ...convertListParams({ filters: arrFilters })
51
- })
52
70
 
53
- if (foundArrivalNotices && foundArrivalNotices.length) {
71
+ const fromDateParamIdx = params.filters.findIndex(param => param.name === 'fromDate')
72
+ if (fromDateParamIdx >= 0) {
73
+ let fromDateVal = new Date(params.filters[fromDateParamIdx].value)
74
+ params.filters.splice(fromDateParamIdx, 1)
75
+
54
76
  params.filters.push({
55
- name: 'arrivalNoticeId',
56
- operator: 'in',
57
- value: foundArrivalNotices.map((foundAN: ArrivalNotice) => foundAN.id),
77
+ name: 'createdAt',
78
+ operator: 'gte',
79
+ value: fromDateVal.toISOString(),
58
80
  relation: false
59
81
  })
60
- } else {
82
+ }
83
+ const toDateParamIdx = params.filters.findIndex(param => param.name === 'toDate')
84
+ if (toDateParamIdx >= 0) {
85
+ let toDateVal = new Date(params.filters[toDateParamIdx].value)
86
+ params.filters.splice(toDateParamIdx, 1)
87
+
61
88
  params.filters.push({
62
- name: 'arrivalNoticeId',
63
- operator: 'is_null',
89
+ name: 'createdAt',
90
+ operator: 'lt',
91
+ value: new Date(toDateVal.setDate(toDateVal.getDate() + 1)).toISOString(),
64
92
  relation: false
65
93
  })
66
94
  }
67
- }
68
95
 
69
- const tzOffsetIdx = params.filters.findIndex(param => param.name === 'tzoffset')
70
- let tzOffset = 0
71
- if (tzOffsetIdx >= 0) {
72
- tzOffset = params.filters[tzOffsetIdx].value
73
- params.filters.splice(tzOffsetIdx, 1)
74
- }
96
+ ////For outbound worksheet filter
97
+ const releaseGoodParam = params.filters.find(param => param.name === 'releaseGoodNo')
98
+ const releaseGoodRefNoParam = params.filters.find(param => param.name === 'releaseGoodRefNo')
99
+ const releaseGoodCrossDockingParam = params.filters.find(param => param.name === 'crossDocking')
100
+ const releaseGoodCourierOptionParam = params.filters.find(param => param.name === 'courierOption')
101
+ const releaseGoodPackingOptionParam = params.filters.find(param => param.name === 'packingOption')
102
+ if (
103
+ releaseGoodParam ||
104
+ releaseGoodRefNoParam ||
105
+ releaseGoodCrossDockingParam ||
106
+ releaseGoodCourierOptionParam ||
107
+ releaseGoodPackingOptionParam
108
+ ) {
109
+ let arrFilters = []
110
+ if (releaseGoodParam) {
111
+ params.filters.splice(
112
+ params.filters.findIndex(item => item.name == 'releaseGoodNo'),
113
+ 1
114
+ )
115
+ arrFilters.push({ ...releaseGoodParam, name: 'name' })
116
+ }
117
+ if (releaseGoodRefNoParam) {
118
+ params.filters.splice(
119
+ params.filters.findIndex(item => item.name == 'releaseGoodRefNo'),
120
+ 1
121
+ )
122
+ arrFilters.push({ ...releaseGoodRefNoParam, name: 'refNo' })
123
+ }
75
124
 
76
- const fromDateParamIdx = params.filters.findIndex(param => param.name === 'fromDate')
77
- if (fromDateParamIdx >= 0) {
78
- let fromDateVal = new Date(params.filters[fromDateParamIdx].value)
79
- params.filters.splice(fromDateParamIdx, 1)
80
-
81
- params.filters.push({
82
- name: 'createdAt',
83
- operator: 'gte',
84
- value: fromDateVal.toISOString(),
85
- relation: false
86
- })
87
- }
88
- const toDateParamIdx = params.filters.findIndex(param => param.name === 'toDate')
89
- if (toDateParamIdx >= 0) {
90
- let toDateVal = new Date(params.filters[toDateParamIdx].value)
91
- params.filters.splice(toDateParamIdx, 1)
92
-
93
- params.filters.push({
94
- name: 'createdAt',
95
- operator: 'lt',
96
- value: new Date(toDateVal.setDate(toDateVal.getDate() + 1)).toISOString(),
97
- relation: false
98
- })
99
- }
125
+ if (releaseGoodCrossDockingParam) {
126
+ params.filters.splice(
127
+ params.filters.findIndex(item => item.name == 'crossDocking'),
128
+ 1
129
+ )
130
+ arrFilters.push({ ...releaseGoodCrossDockingParam, name: 'crossDocking' })
131
+ }
100
132
 
101
- ////For outbound worksheet filter
102
- const releaseGoodParam = params.filters.find(param => param.name === 'releaseGoodNo')
103
- const releaseGoodRefNoParam = params.filters.find(param => param.name === 'releaseGoodRefNo')
104
- const releaseGoodCrossDockingParam = params.filters.find(param => param.name === 'crossDocking')
105
- const releaseGoodCourierOptionParam = params.filters.find(param => param.name === 'courierOption')
106
- const releaseGoodPackingOptionParam = params.filters.find(param => param.name === 'packingOption')
107
- if (
108
- releaseGoodParam ||
109
- releaseGoodRefNoParam ||
110
- releaseGoodCrossDockingParam ||
111
- releaseGoodCourierOptionParam ||
112
- releaseGoodPackingOptionParam
113
- ) {
114
- let arrFilters = []
115
- if (releaseGoodParam) {
116
- params.filters.splice(
117
- params.filters.findIndex(item => item.name == 'releaseGoodNo'),
118
- 1
119
- )
120
- arrFilters.push({ ...releaseGoodParam, name: 'name' })
121
- }
122
- if (releaseGoodRefNoParam) {
123
- params.filters.splice(
124
- params.filters.findIndex(item => item.name == 'releaseGoodRefNo'),
125
- 1
126
- )
127
- arrFilters.push({ ...releaseGoodRefNoParam, name: 'refNo' })
128
- }
133
+ if (releaseGoodCourierOptionParam) {
134
+ params.filters.splice(
135
+ params.filters.findIndex(item => item.name == 'courierOption'),
136
+ 1
137
+ )
138
+ arrFilters.push({ ...releaseGoodCourierOptionParam, name: 'courierOption' })
139
+ }
129
140
 
130
- if (releaseGoodCrossDockingParam) {
131
- params.filters.splice(
132
- params.filters.findIndex(item => item.name == 'crossDocking'),
133
- 1
134
- )
135
- arrFilters.push({ ...releaseGoodCrossDockingParam, name: 'crossDocking' })
136
- }
141
+ if (releaseGoodPackingOptionParam) {
142
+ params.filters.splice(
143
+ params.filters.findIndex(item => item.name == 'packingOption'),
144
+ 1
145
+ )
146
+ arrFilters.push({ ...releaseGoodPackingOptionParam, name: 'packingOption' })
147
+ }
137
148
 
138
- if (releaseGoodCourierOptionParam) {
139
- params.filters.splice(
140
- params.filters.findIndex(item => item.name == 'courierOption'),
141
- 1
142
- )
143
- arrFilters.push({ ...releaseGoodCourierOptionParam, name: 'courierOption' })
144
- }
149
+ const foundReleaseGoods: ReleaseGood[] = await getRepository(ReleaseGood).find({
150
+ ...convertListParams({ filters: arrFilters })
151
+ })
145
152
 
146
- if (releaseGoodPackingOptionParam) {
147
- params.filters.splice(
148
- params.filters.findIndex(item => item.name == 'packingOption'),
149
- 1
150
- )
151
- arrFilters.push({ ...releaseGoodPackingOptionParam, name: 'packingOption' })
153
+ if (foundReleaseGoods && foundReleaseGoods.length) {
154
+ params.filters.push({
155
+ name: 'releaseGoodId',
156
+ operator: 'in',
157
+ value: foundReleaseGoods.map((foundRG: ReleaseGood) => foundRG.id),
158
+ relation: false
159
+ })
160
+ } else {
161
+ params.filters.push({
162
+ name: 'releaseGoodId',
163
+ operator: 'is_null',
164
+ relation: false
165
+ })
166
+ }
152
167
  }
153
168
 
154
- const foundReleaseGoods: ReleaseGood[] = await getRepository(ReleaseGood).find({
155
- ...convertListParams({ filters: arrFilters })
156
- })
157
-
158
- if (foundReleaseGoods && foundReleaseGoods.length) {
159
- params.filters.push({
160
- name: 'releaseGoodId',
161
- operator: 'in',
162
- value: foundReleaseGoods.map((foundRG: ReleaseGood) => foundRG.id),
163
- relation: false
164
- })
165
- } else {
166
- params.filters.push({
167
- name: 'releaseGoodId',
168
- operator: 'is_null',
169
- relation: false
169
+ ////For outbound return worksheet filter
170
+ const returnOrderParam = params.filters.find(param => param.name === 'returnOrderNo')
171
+ const returnOrderRefNoParam = params.filters.find(param => param.name === 'returnOrderRefNo')
172
+ if (returnOrderParam || returnOrderRefNoParam) {
173
+ let arrFilters = []
174
+ if (returnOrderParam) {
175
+ params.filters.splice(
176
+ params.filters.findIndex(item => item.name == 'returnOrderNo'),
177
+ 1
178
+ )
179
+ arrFilters.push({ ...returnOrderParam, name: 'name' })
180
+ }
181
+ if (returnOrderRefNoParam) {
182
+ params.filters.splice(
183
+ params.filters.findIndex(item => item.name == 'returnOrderRefNo'),
184
+ 1
185
+ )
186
+ arrFilters.push({ ...returnOrderRefNoParam, name: 'refNo' })
187
+ }
188
+ const foundReturnOrders: ReturnOrder[] = await getRepository(ReturnOrder).find({
189
+ ...convertListParams({ filters: arrFilters })
170
190
  })
171
- }
172
- }
173
191
 
174
- ////For outbound return worksheet filter
175
- const returnOrderParam = params.filters.find(param => param.name === 'returnOrderNo')
176
- const returnOrderRefNoParam = params.filters.find(param => param.name === 'returnOrderRefNo')
177
- if (returnOrderParam || returnOrderRefNoParam) {
178
- let arrFilters = []
179
- if (returnOrderParam) {
180
- params.filters.splice(
181
- params.filters.findIndex(item => item.name == 'returnOrderNo'),
182
- 1
183
- )
184
- arrFilters.push({ ...returnOrderParam, name: 'name' })
185
- }
186
- if (returnOrderRefNoParam) {
187
- params.filters.splice(
188
- params.filters.findIndex(item => item.name == 'returnOrderRefNo'),
189
- 1
190
- )
191
- arrFilters.push({ ...returnOrderRefNoParam, name: 'refNo' })
192
+ if (foundReturnOrders && foundReturnOrders.length) {
193
+ params.filters.push({
194
+ name: 'returnOrderId',
195
+ operator: 'in',
196
+ value: foundReturnOrders.map((foundRO: ReturnOrder) => foundRO.id),
197
+ relation: false
198
+ })
199
+ } else {
200
+ params.filters.push({
201
+ name: 'returnOrderId',
202
+ operator: 'is_null',
203
+ relation: false
204
+ })
205
+ }
192
206
  }
193
- const foundReturnOrders: ReturnOrder[] = await getRepository(ReturnOrder).find({
194
- ...convertListParams({ filters: arrFilters })
195
- })
196
207
 
197
- if (foundReturnOrders && foundReturnOrders.length) {
198
- params.filters.push({
199
- name: 'returnOrderId',
200
- operator: 'in',
201
- value: foundReturnOrders.map((foundRO: ReturnOrder) => foundRO.id),
202
- relation: false
203
- })
204
- } else {
205
- params.filters.push({
206
- name: 'returnOrderId',
207
- operator: 'is_null',
208
- relation: false
208
+ ////For inventory check worksheet filter
209
+ const inventoryCheckParam = params.filters.find(param => param.name === 'inventoryCheckNo')
210
+ const inventoryCheckStatusParam = params.filters.find(param => param.name === 'inventoryCheckStatus')
211
+ const executionDateParam = params.filters.find(param => param.name === 'executionDate')
212
+ if (inventoryCheckParam || executionDateParam || inventoryCheckStatusParam) {
213
+ let arrFilters = []
214
+ if (inventoryCheckParam) {
215
+ params.filters.splice(
216
+ params.filters.findIndex(item => item.name == 'inventoryCheckNo'),
217
+ 1
218
+ )
219
+ arrFilters.push({ ...inventoryCheckParam, name: 'name' })
220
+ }
221
+ if (inventoryCheckStatusParam) {
222
+ params.filters.splice(
223
+ params.filters.findIndex(item => item.name == 'inventoryCheckStatus'),
224
+ 1
225
+ )
226
+ arrFilters.push({ ...inventoryCheckStatusParam, name: 'status' })
227
+ }
228
+ if (executionDateParam) {
229
+ params.filters.splice(
230
+ params.filters.findIndex(item => item.name == 'executionDate'),
231
+ 1
232
+ )
233
+ arrFilters.push({ ...releaseGoodRefNoParam, name: 'refNo' })
234
+ }
235
+ const foundInventoryCheck: InventoryCheck[] = await getRepository(InventoryCheck).find({
236
+ ...convertListParams({ filters: arrFilters })
209
237
  })
210
- }
211
- }
212
238
 
213
- ////For inventory check worksheet filter
214
- const inventoryCheckParam = params.filters.find(param => param.name === 'inventoryCheckNo')
215
- const inventoryCheckStatusParam = params.filters.find(param => param.name === 'inventoryCheckStatus')
216
- const executionDateParam = params.filters.find(param => param.name === 'executionDate')
217
- if (inventoryCheckParam || executionDateParam || inventoryCheckStatusParam) {
218
- let arrFilters = []
219
- if (inventoryCheckParam) {
220
- params.filters.splice(
221
- params.filters.findIndex(item => item.name == 'inventoryCheckNo'),
222
- 1
223
- )
224
- arrFilters.push({ ...inventoryCheckParam, name: 'name' })
225
- }
226
- if (inventoryCheckStatusParam) {
227
- params.filters.splice(
228
- params.filters.findIndex(item => item.name == 'inventoryCheckStatus'),
229
- 1
230
- )
231
- arrFilters.push({ ...inventoryCheckStatusParam, name: 'status' })
232
- }
233
- if (executionDateParam) {
234
- params.filters.splice(
235
- params.filters.findIndex(item => item.name == 'executionDate'),
236
- 1
237
- )
238
- arrFilters.push({ ...releaseGoodRefNoParam, name: 'refNo' })
239
+ if (foundInventoryCheck && foundInventoryCheck.length) {
240
+ params.filters.push({
241
+ name: 'inventoryCheckId',
242
+ operator: 'in',
243
+ value: foundInventoryCheck.map((foundIC: InventoryCheck) => foundIC.id),
244
+ relation: false
245
+ })
246
+ } else {
247
+ params.filters.push({
248
+ name: 'inventoryCheckId',
249
+ operator: 'is_null',
250
+ relation: false
251
+ })
252
+ }
239
253
  }
240
- const foundInventoryCheck: InventoryCheck[] = await getRepository(InventoryCheck).find({
241
- ...convertListParams({ filters: arrFilters })
242
- })
243
254
 
244
- if (foundInventoryCheck && foundInventoryCheck.length) {
255
+ ////Set default bizplace filter
256
+ const bizplaceFilter = params.filters.find(param => param.name === 'bizplaceId')
257
+ if (!bizplaceFilter && typeParam.value[0] !== WORKSHEET_TYPE.BATCH_PICKING) {
245
258
  params.filters.push({
246
- name: 'inventoryCheckId',
259
+ name: 'bizplaceId',
247
260
  operator: 'in',
248
- value: foundInventoryCheck.map((foundIC: InventoryCheck) => foundIC.id),
249
- relation: false
250
- })
251
- } else {
252
- params.filters.push({
253
- name: 'inventoryCheckId',
254
- operator: 'is_null',
261
+ value: await getPermittedBizplaceIds(domain, user),
255
262
  relation: false
256
263
  })
257
264
  }
258
- }
259
265
 
260
- ////Set default bizplace filter
261
- const bizplaceFilter = params.filters.find(param => param.name === 'bizplaceId')
262
- if (!bizplaceFilter && typeParam.value[0] !== WORKSHEET_TYPE.BATCH_PICKING) {
263
- params.filters.push({
264
- name: 'bizplaceId',
265
- operator: 'in',
266
- value: await getPermittedBizplaceIds(domain, user),
267
- relation: false
268
- })
269
- }
266
+ ////Build and run Query
267
+ const qb: SelectQueryBuilder<Worksheet> = getRepository(Worksheet).createQueryBuilder('ws')
268
+
269
+ ////For vas worksheets filter
270
+ let orderName: string = ''
271
+ if (
272
+ params.filters.find(param => param.name === 'type' && param.value === WORKSHEET_TYPE.VAS) &&
273
+ params.filters.find(param => param.name === 'name')
274
+ ) {
275
+ orderName = params.filters.find(param => param.name === 'name').value?.toLowerCase()
276
+ params.filters = params.filters.filter(param => param.name !== 'name')
277
+ }
270
278
 
271
- ////Build and run Query
272
- const qb: SelectQueryBuilder<Worksheet> = getRepository(Worksheet).createQueryBuilder('ws')
273
-
274
- ////For vas worksheets filter
275
- let orderName: string = ''
276
- if (
277
- params.filters.find(param => param.name === 'type' && param.value === WORKSHEET_TYPE.VAS) &&
278
- params.filters.find(param => param.name === 'name')
279
- ) {
280
- orderName = params.filters.find(param => param.name === 'name').value?.toLowerCase()
281
- params.filters = params.filters.filter(param => param.name !== 'name')
282
- }
279
+ buildQuery(qb, params, context)
283
280
 
284
- buildQuery(qb, params, context)
281
+ if (orderName !== '') {
282
+ qb.andWhere(
283
+ new Brackets(qb2 => {
284
+ qb2.orWhere(`arrivalNotice.name ILIKE '${orderName}'`)
285
+ qb2.orWhere(`releaseGood.name ILIKE '${orderName}'`)
286
+ qb2.orWhere(`vasOrder.name ILIKE '${orderName}'`)
287
+ })
288
+ )
289
+ }
285
290
 
286
- if (orderName !== '') {
287
- qb.andWhere(
288
- new Brackets(qb2 => {
289
- qb2.orWhere(`arrivalNotice.name ILIKE '${orderName}'`)
290
- qb2.orWhere(`releaseGood.name ILIKE '${orderName}'`)
291
- qb2.orWhere(`vasOrder.name ILIKE '${orderName}'`)
292
- })
291
+ qb.addSelect('COALESCE("cc".rank, 99999)', 'rank')
292
+ qb.leftJoinAndSelect('ws.domain', 'domain')
293
+ qb.leftJoinAndSelect('ws.bizplace', 'bizplace')
294
+ qb.leftJoinAndSelect('ws.arrivalNotice', 'arrivalNotice')
295
+ qb.leftJoinAndSelect('ws.releaseGood', 'releaseGood')
296
+ qb.leftJoinAndSelect('ws.returnOrder', 'returnOrder')
297
+ qb.leftJoinAndSelect('ws.inventoryCheck', 'inventoryCheck')
298
+ qb.leftJoinAndSelect('ws.vasOrder', 'vasOrder')
299
+ qb.leftJoinAndSelect('ws.assignee', 'assignee')
300
+ qb.leftJoinAndSelect('ws.creator', 'creator')
301
+ qb.leftJoinAndSelect('ws.updater', 'updater')
302
+ qb.leftJoin(
303
+ subQuery => {
304
+ return subQuery
305
+ .select(`ccd.rank`, 'rank')
306
+ .addSelect(`ccd.name`, 'status')
307
+ .from(`common_code_details`, 'ccd')
308
+ .innerJoin(`ccd.commonCode`, 'cc')
309
+ .where(`ccd.domain_id = :domainId`, { domainId: domain.id })
310
+ .andWhere(`cc.name = 'WORKSHEET_STATUS'`)
311
+ },
312
+ 'cc',
313
+ 'cc.status = ws.status'
293
314
  )
294
- }
295
315
 
296
- qb.addSelect('COALESCE("cc".rank, 99999)', 'rank')
297
- qb.leftJoinAndSelect('ws.domain', 'domain')
298
- qb.leftJoinAndSelect('ws.bizplace', 'bizplace')
299
- qb.leftJoinAndSelect('ws.arrivalNotice', 'arrivalNotice')
300
- qb.leftJoinAndSelect('ws.releaseGood', 'releaseGood')
301
- qb.leftJoinAndSelect('ws.returnOrder', 'returnOrder')
302
- qb.leftJoinAndSelect('ws.inventoryCheck', 'inventoryCheck')
303
- qb.leftJoinAndSelect('ws.vasOrder', 'vasOrder')
304
- qb.leftJoinAndSelect('ws.assignee', 'assignee')
305
- qb.leftJoinAndSelect('ws.creator', 'creator')
306
- qb.leftJoinAndSelect('ws.updater', 'updater')
307
- qb.leftJoin(
308
- subQuery => {
309
- return subQuery
310
- .select(`ccd.rank`, 'rank')
311
- .addSelect(`ccd.name`, 'status')
312
- .from(`common_code_details`, 'ccd')
313
- .innerJoin(`ccd.commonCode`, 'cc')
314
- .where(`ccd.domain_id = :domainId`, { domainId: domain.id })
315
- .andWhere(`cc.name = 'WORKSHEET_STATUS'`)
316
- },
317
- 'cc',
318
- 'cc.status = ws.status'
319
- )
320
-
321
- ////Add sorting conditions
322
- const arrChildSortData = ['bizplace', 'arrivalNotice', 'releaseGood', 'returnOrder', 'inventoryCheck']
323
- let sort = (params.sortings || []).reduce(
324
- (acc, sort) => {
325
- if (
326
- sort.name != 'arrivalRefNo' &&
327
- sort.name != 'releaseRefNo' &&
328
- sort.name != 'returnOrderNo' &&
329
- sort.name != 'inventoryCheckStatus'
330
- ) {
331
- return {
332
- ...acc,
333
- [arrChildSortData.indexOf(sort.name) >= 0 ? sort.name + '.name' : 'ws.' + sort.name]: sort.desc
334
- ? 'DESC'
335
- : 'ASC'
316
+ ////Add sorting conditions
317
+ const arrChildSortData = ['bizplace', 'arrivalNotice', 'releaseGood', 'returnOrder', 'inventoryCheck']
318
+ let sort = (params.sortings || []).reduce(
319
+ (acc, sort) => {
320
+ if (
321
+ sort.name != 'arrivalRefNo' &&
322
+ sort.name != 'releaseRefNo' &&
323
+ sort.name != 'returnOrderNo' &&
324
+ sort.name != 'inventoryCheckStatus'
325
+ ) {
326
+ return {
327
+ ...acc,
328
+ [arrChildSortData.indexOf(sort.name) >= 0 ? sort.name + '.name' : 'ws.' + sort.name]: sort.desc
329
+ ? 'DESC'
330
+ : 'ASC'
331
+ }
332
+ } else {
333
+ return { ...acc }
336
334
  }
337
- } else {
338
- return { ...acc }
335
+ },
336
+ !params.sortings.some(e => e.name === 'status') ? { rank: 'ASC' } : {}
337
+ )
338
+
339
+ if (params.sortings.some(e => e.name === 'arrivalRefNo')) {
340
+ sort = {
341
+ ...sort,
342
+ 'arrivalNotice.refNo': params.sortings[params.sortings.findIndex(item => item.name == 'arrivalRefNo')].desc
343
+ ? 'DESC'
344
+ : 'ASC'
339
345
  }
340
- },
341
- !params.sortings.some(e => e.name === 'status') ? { rank: 'ASC' } : {}
342
- )
343
-
344
- if (params.sortings.some(e => e.name === 'arrivalRefNo')) {
345
- sort = {
346
- ...sort,
347
- 'arrivalNotice.refNo': params.sortings[params.sortings.findIndex(item => item.name == 'arrivalRefNo')].desc
348
- ? 'DESC'
349
- : 'ASC'
350
346
  }
351
- }
352
347
 
353
- if (params.sortings.some(e => e.name === 'releaseRefNo')) {
354
- sort = {
355
- ...sort,
356
- 'releaseGood.refNo': params.sortings[params.sortings.findIndex(item => item.name == 'releaseRefNo')].desc
357
- ? 'DESC'
358
- : 'ASC'
348
+ if (params.sortings.some(e => e.name === 'releaseRefNo')) {
349
+ sort = {
350
+ ...sort,
351
+ 'releaseGood.refNo': params.sortings[params.sortings.findIndex(item => item.name == 'releaseRefNo')].desc
352
+ ? 'DESC'
353
+ : 'ASC'
354
+ }
359
355
  }
360
- }
361
356
 
362
- if (params.sortings.some(e => e.name === 'returnOrderRefNo')) {
363
- sort = {
364
- ...sort,
365
- 'returnOrder.refNo': params.sortings[params.sortings.findIndex(item => item.name == 'returnOrderRefNo')].desc
366
- ? 'DESC'
367
- : 'ASC'
357
+ if (params.sortings.some(e => e.name === 'returnOrderRefNo')) {
358
+ sort = {
359
+ ...sort,
360
+ 'returnOrder.refNo': params.sortings[params.sortings.findIndex(item => item.name == 'returnOrderRefNo')].desc
361
+ ? 'DESC'
362
+ : 'ASC'
363
+ }
368
364
  }
369
- }
370
365
 
371
- if (params.sortings.some(e => e.name === 'inventoryCheckStatus')) {
372
- // overwrite the worksheet status sorting since inventory check status is needed
373
- sort = {
374
- 'inventoryCheck.status': params.sortings[params.sortings.findIndex(item => item.name == 'inventoryCheckStatus')]
375
- .desc
376
- ? 'DESC'
377
- : 'ASC',
378
- 'ws.endedAt': params.sortings[params.sortings.findIndex(item => item.name == 'inventoryCheckStatus')].desc
379
- ? 'DESC'
380
- : 'ASC'
366
+ if (params.sortings.some(e => e.name === 'inventoryCheckStatus')) {
367
+ // overwrite the worksheet status sorting since inventory check status is needed
368
+ sort = {
369
+ 'inventoryCheck.status': params.sortings[
370
+ params.sortings.findIndex(item => item.name == 'inventoryCheckStatus')
371
+ ].desc
372
+ ? 'DESC'
373
+ : 'ASC',
374
+ 'ws.endedAt': params.sortings[params.sortings.findIndex(item => item.name == 'inventoryCheckStatus')].desc
375
+ ? 'DESC'
376
+ : 'ASC'
377
+ }
381
378
  }
382
- }
383
379
 
384
- qb.orderBy(sort)
380
+ qb.orderBy(sort)
385
381
 
386
- const [items, total] = await qb.getManyAndCount()
382
+ const [items, total] = await qb.getManyAndCount()
387
383
 
388
- return { items, total }
389
- } catch (error) {
390
- throw error
384
+ return { items, total }
385
+ } catch (error) {
386
+ throw error
387
+ }
391
388
  }
392
389
  }