@mbc-cqrs-serverless/master 0.1.68-beta.0 → 0.1.70-beta.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 (109) hide show
  1. package/dist/constants/index.d.ts +1 -0
  2. package/dist/constants/index.js +2 -1
  3. package/dist/constants/index.js.map +1 -1
  4. package/dist/controllers/master-data.controller.d.ts +6 -0
  5. package/dist/controllers/master-data.controller.js +74 -0
  6. package/dist/controllers/master-data.controller.js.map +1 -1
  7. package/dist/controllers/master-setting.controller.d.ts +8 -0
  8. package/dist/controllers/master-setting.controller.js +98 -0
  9. package/dist/controllers/master-setting.controller.js.map +1 -1
  10. package/dist/custom-task/custom-task.module.d.ts +2 -0
  11. package/dist/custom-task/custom-task.module.js +30 -0
  12. package/dist/custom-task/custom-task.module.js.map +1 -0
  13. package/dist/custom-task/event/task-queue-event-factory.d.ts +6 -0
  14. package/dist/custom-task/event/task-queue-event-factory.js +23 -0
  15. package/dist/custom-task/event/task-queue-event-factory.js.map +1 -0
  16. package/dist/custom-task/my-task.controller.d.ts +9 -0
  17. package/dist/custom-task/my-task.controller.js +56 -0
  18. package/dist/custom-task/my-task.controller.js.map +1 -0
  19. package/dist/custom-task/my-task.service.d.ts +12 -0
  20. package/dist/custom-task/my-task.service.js +76 -0
  21. package/dist/custom-task/my-task.service.js.map +1 -0
  22. package/dist/decorators/index.d.ts +1 -0
  23. package/dist/decorators/index.js +18 -0
  24. package/dist/decorators/index.js.map +1 -0
  25. package/dist/decorators/key.d.ts +1 -0
  26. package/dist/decorators/key.js +34 -0
  27. package/dist/decorators/key.js.map +1 -0
  28. package/dist/dto/index.d.ts +1 -0
  29. package/dist/dto/index.js +1 -0
  30. package/dist/dto/index.js.map +1 -1
  31. package/dist/dto/master-copy/index.d.ts +8 -0
  32. package/dist/dto/master-copy/index.js +25 -0
  33. package/dist/dto/master-copy/index.js.map +1 -0
  34. package/dist/dto/master-copy/master-copy.dto.d.ts +19 -0
  35. package/dist/dto/master-copy/master-copy.dto.js +92 -0
  36. package/dist/dto/master-copy/master-copy.dto.js.map +1 -0
  37. package/dist/dto/master-copy/master-data-create.dto.d.ts +7 -0
  38. package/dist/dto/master-copy/master-data-create.dto.js +43 -0
  39. package/dist/dto/master-copy/master-data-create.dto.js.map +1 -0
  40. package/dist/dto/master-copy/master-data-search.dto.d.ts +6 -0
  41. package/dist/dto/master-copy/master-data-search.dto.js +39 -0
  42. package/dist/dto/master-copy/master-data-search.dto.js.map +1 -0
  43. package/dist/dto/master-copy/master-data-update.dto.d.ts +6 -0
  44. package/dist/dto/master-copy/master-data-update.dto.js +41 -0
  45. package/dist/dto/master-copy/master-data-update.dto.js.map +1 -0
  46. package/dist/dto/master-copy/master-rds-list.entity.d.ts +6 -0
  47. package/dist/dto/master-copy/master-rds-list.entity.js +33 -0
  48. package/dist/dto/master-copy/master-rds-list.entity.js.map +1 -0
  49. package/dist/dto/master-copy/master-rds.entity.d.ts +26 -0
  50. package/dist/dto/master-copy/master-rds.entity.js +116 -0
  51. package/dist/dto/master-copy/master-rds.entity.js.map +1 -0
  52. package/dist/dto/master-copy/master-setting-search.dto.d.ts +6 -0
  53. package/dist/dto/master-copy/master-setting-search.dto.js +39 -0
  54. package/dist/dto/master-copy/master-setting-search.dto.js.map +1 -0
  55. package/dist/dto/master-copy/master-setting-update.dto.d.ts +4 -0
  56. package/dist/dto/master-copy/master-setting-update.dto.js +31 -0
  57. package/dist/dto/master-copy/master-setting-update.dto.js.map +1 -0
  58. package/dist/handler/master-sfn-task.event.d.ts +3 -0
  59. package/dist/handler/master-sfn-task.event.js +8 -0
  60. package/dist/handler/master-sfn-task.event.js.map +1 -0
  61. package/dist/handler/master-sfn-task.handler.d.ts +24 -0
  62. package/dist/handler/master-sfn-task.handler.js +242 -0
  63. package/dist/handler/master-sfn-task.handler.js.map +1 -0
  64. package/dist/helpers/index.d.ts +7 -0
  65. package/dist/helpers/index.js +15 -0
  66. package/dist/helpers/index.js.map +1 -1
  67. package/dist/helpers/rds.d.ts +4 -0
  68. package/dist/helpers/rds.js +19 -0
  69. package/dist/helpers/rds.js.map +1 -0
  70. package/dist/index.d.ts +2 -0
  71. package/dist/index.js +2 -0
  72. package/dist/index.js.map +1 -1
  73. package/dist/interfaces/master-data.interface.d.ts +2 -2
  74. package/dist/master.module-definition.d.ts +2 -0
  75. package/dist/master.module-definition.js +2 -1
  76. package/dist/master.module-definition.js.map +1 -1
  77. package/dist/master.module.js +19 -0
  78. package/dist/master.module.js.map +1 -1
  79. package/dist/services/master-data.service.d.ts +10 -9
  80. package/dist/services/master-data.service.js +112 -4
  81. package/dist/services/master-data.service.js.map +1 -1
  82. package/dist/services/master-setting.service.d.ts +16 -3
  83. package/dist/services/master-setting.service.js +126 -3
  84. package/dist/services/master-setting.service.js.map +1 -1
  85. package/dist/services/master-setting.service.spec.js +16 -0
  86. package/dist/services/master-setting.service.spec.js.map +1 -1
  87. package/dist/update-scheme.js +2 -10
  88. package/dist/update-scheme.js.map +1 -1
  89. package/package.json +6 -4
  90. package/src/templates/master/handler/master-rds.handler.ts +0 -4
  91. package/src/templates/master/master.module.ts +6 -18
  92. package/src/templates/custom-task/custom-task.module.ts +0 -18
  93. package/src/templates/custom-task/event/task-queue-event-factory.ts +0 -26
  94. package/src/templates/custom-task/my-task.controller.ts +0 -28
  95. package/src/templates/custom-task/my-task.service.ts +0 -99
  96. package/src/templates/master/dto/master-copy.dto.ts +0 -73
  97. package/src/templates/master/dto/master-data-create.dto.ts +0 -20
  98. package/src/templates/master/dto/master-data-search.dto.ts +0 -20
  99. package/src/templates/master/dto/master-data-update.dto.ts +0 -25
  100. package/src/templates/master/dto/master-rds-list.entity.ts +0 -15
  101. package/src/templates/master/dto/master-rds.entity.ts +0 -77
  102. package/src/templates/master/dto/master-setting-search.dto.ts +0 -20
  103. package/src/templates/master/dto/master-setting-update.dto.ts +0 -11
  104. package/src/templates/master/handler/master-sfn-task.event.ts +0 -3
  105. package/src/templates/master/handler/master-sfn-task.handler.ts +0 -334
  106. package/src/templates/master/master-data.controller.ts +0 -98
  107. package/src/templates/master/master-data.service.ts +0 -181
  108. package/src/templates/master/master-setting.controller.ts +0 -103
  109. package/src/templates/master/master-setting.service.ts +0 -201
@@ -1,334 +0,0 @@
1
- import {
2
- DataModel,
3
- DataService,
4
- DynamoDbService,
5
- EventHandler,
6
- extractInvokeContext,
7
- IEventHandler,
8
- IInvoke,
9
- KEY_SEPARATOR,
10
- } from '@mbc-cqrs-serverless/core'
11
- import {
12
- MasterDataService,
13
- MasterSettingService,
14
- } from '@mbc-cqrs-serverless/master'
15
- import { RotateByEnum, SequencesService } from '@mbc-cqrs-serverless/sequence'
16
- import { Logger } from '@nestjs/common'
17
- import { Prisma } from '@prisma/client'
18
- import { chunk } from 'lodash'
19
- import {
20
- DATA_SK_PREFIX,
21
- DEFAULT_TENANT_CODE,
22
- generateMasterPk,
23
- genSequenceSk,
24
- parseId,
25
- sequencePk,
26
- } from 'src/helpers/id'
27
- import { PrismaService } from 'src/prisma'
28
-
29
- import {
30
- CopyType,
31
- DataCopyMode,
32
- DataCopyOptionDto,
33
- MasterCopyDto,
34
- } from '../dto/master-copy.dto'
35
- import { MasterSfnTaskEvent } from './master-sfn-task.event'
36
-
37
- const BATCH_SIZE = 100
38
-
39
- @EventHandler(MasterSfnTaskEvent)
40
- export class MasterSfnTaskEventHandler
41
- implements IEventHandler<MasterSfnTaskEvent>
42
- {
43
- private readonly logger = new Logger(MasterSfnTaskEventHandler.name)
44
- private sequenceTableName
45
-
46
- constructor(
47
- private readonly dynamoDbService: DynamoDbService,
48
-
49
- private readonly prismaService: PrismaService,
50
- private readonly masterSettingService: MasterSettingService,
51
- private readonly masterDataService: MasterDataService,
52
- private readonly dataService: DataService,
53
- private readonly sequencesService: SequencesService,
54
- ) {
55
- this.sequenceTableName = this.dynamoDbService.getTableName('sequences')
56
- }
57
-
58
- async execute(event: MasterSfnTaskEvent): Promise<any> {
59
- const invokeContext = extractInvokeContext()
60
- const masterCopyDto = event.input?.input as unknown as MasterCopyDto
61
-
62
- this.logger.debug('sfn-event:masterCopyDto:', masterCopyDto)
63
-
64
- const { masterSettingId, targetTenants, copyType, dataCopyOption } =
65
- masterCopyDto
66
- const targetTenant = `${targetTenants[0]}`
67
-
68
- const setting = await this.fetchSetting(masterSettingId)
69
- const masterCode = this.getMasterCodeFromSetting(setting)
70
- if (copyType === CopyType.SETTING_ONLY || copyType === CopyType.BOTH) {
71
- await this.copySettingToTenant(setting, targetTenant, invokeContext)
72
- }
73
-
74
- if (copyType === CopyType.DATA_ONLY || copyType === CopyType.BOTH) {
75
- const isCopySequence = await this.shouldCopySequence(
76
- setting,
77
- targetTenant,
78
- )
79
- if (isCopySequence) {
80
- await this.copySeqToTenant(setting, targetTenant)
81
- }
82
-
83
- const dataToCopy = await this.fetchMasterData(masterCode, dataCopyOption)
84
-
85
- await this.copyDataToTenant(dataToCopy, targetTenant, invokeContext)
86
- }
87
-
88
- this.logger.debug('Completed copy process for tenant:', targetTenant)
89
- return { message: 'Copy successfully', event }
90
- }
91
-
92
- private async fetchSetting(id: string) {
93
- const setting = await this.dataService.getItem(parseId(id))
94
- this.logger.debug('sfn-event-setting', setting)
95
- return setting
96
- }
97
-
98
- private async fetchMasterData(
99
- masterCode: string,
100
- dataCopyOption?: DataCopyOptionDto,
101
- tenant: string = DEFAULT_TENANT_CODE,
102
- ): Promise<Prisma.MasterUncheckedCreateInput[]> {
103
- const where: Prisma.MasterWhereInput = {
104
- masterType: DATA_SK_PREFIX,
105
- masterTypeCode: masterCode,
106
- pk: `MASTER${KEY_SEPARATOR}${tenant}`,
107
- }
108
-
109
- if (dataCopyOption?.mode === DataCopyMode.PARTIAL) {
110
- where.id = { in: dataCopyOption.id }
111
- }
112
-
113
- this.logger.debug('sfn-event-whereCondition', where)
114
- const data = await this.prismaService.master.findMany({ where })
115
- this.logger.debug('sfn-event-dataToCopy', data.length)
116
- return data
117
- }
118
-
119
- private async copySettingToTenant(
120
- setting: DataModel,
121
- tenantCode: string,
122
- invokeContext: IInvoke,
123
- ) {
124
- const sk = setting.sk
125
- const pk = generateMasterPk(tenantCode)
126
- const masterCode = this.getMasterCodeFromSetting(setting)
127
-
128
- const tenantSetting = await this.dataService.getItem({ pk, sk })
129
-
130
- this.logger.debug(
131
- 'sfn-event-copySettingToTenant-tenantSetting',
132
- tenantSetting,
133
- )
134
-
135
- if (tenantSetting && tenantSetting.isDeleted === false) {
136
- this.logger.debug('sfn-event-copySettingToTenant-updateSetting', {
137
- pk,
138
- sk,
139
- })
140
-
141
- await this.masterSettingService.updateSetting(
142
- { pk, sk },
143
- {
144
- name: setting.name,
145
- code: masterCode,
146
- settingValue: setting.attributes as object,
147
- tenantCode,
148
- },
149
- {
150
- invokeContext,
151
- },
152
- )
153
- } else {
154
- this.logger.debug('sfn-event-copySettingToTenant-createTenantSetting', {
155
- pk,
156
- sk,
157
- })
158
-
159
- await this.masterSettingService.createTenantSetting(
160
- {
161
- name: setting.name,
162
- code: masterCode,
163
- settingValue: setting.attributes,
164
- tenantCode,
165
- },
166
- { invokeContext },
167
- )
168
- }
169
- }
170
-
171
- private async copyDataToTenant(
172
- dataToCopy: Prisma.MasterUncheckedCreateInput[],
173
- tenantCode: string,
174
- invokeContext: IInvoke,
175
- ) {
176
- const chunks = chunk(dataToCopy, BATCH_SIZE)
177
- for (const batch of chunks) {
178
- await Promise.all(
179
- batch.map(async (data) => {
180
- const parts = data.sk.split(KEY_SEPARATOR)
181
- const sk =
182
- parts.length > 1 && parts[1].trim() === ''
183
- ? `${data.sk}${data.masterCode}`
184
- : data.sk
185
- const pk = generateMasterPk(tenantCode)
186
-
187
- const tenantData = await this.dataService.getItem({ pk, sk })
188
-
189
- this.logger.debug('sfn-event-copyDataToTenant-tenantData', tenantData)
190
-
191
- // des tenant data is exist and not deleted => update des data same as src data
192
- if (tenantData && tenantData.isDeleted === false) {
193
- this.logger.debug('sfn-event-copyDataToTenant-update', { pk, sk })
194
-
195
- return this.masterDataService.update(
196
- { pk, sk },
197
- {
198
- name: data.name,
199
- attributes: data.attributes as object,
200
- isDeleted: data.isDeleted,
201
- seq: data.seq,
202
- },
203
- {
204
- invokeContext,
205
- },
206
- )
207
- }
208
-
209
- // src data is deleted => do nothing
210
- if (data.isDeleted === true) return
211
-
212
- // src data is exist => create des data
213
- return this.masterDataService.create(
214
- {
215
- code: data.masterCode,
216
- tenantCode,
217
- name: data.name,
218
- settingCode: data.masterTypeCode,
219
- attributes: data.attributes as object,
220
- seq: data.seq,
221
- },
222
- { invokeContext },
223
- )
224
- }),
225
- )
226
- }
227
- }
228
-
229
- private async shouldCopySequence(
230
- setting: DataModel,
231
- tenantCode: string,
232
- ): Promise<boolean> {
233
- const fields = setting?.attributes?.['fields'] || []
234
- const codeField = fields.find((f) => f.physicalName === 'code')
235
- if (codeField?.dataType !== 'auto_number') {
236
- this.logger.debug('Sequence not required: code field not auto_number')
237
- return false
238
- }
239
-
240
- const jcciSeqKey = this.generateSequenceKey(tenantCode, setting)
241
- const { seq: jcciSeq = 0 } =
242
- (await this.sequencesService.getCurrentSequence(jcciSeqKey)) ?? {}
243
- this.logger.debug('sfn-event-shouldCopySequence-jcciSeq', {
244
- jcciSeqKey,
245
- jcciSeq,
246
- })
247
-
248
- if (jcciSeq === 0) {
249
- this.logger.debug('Skipping sequence copy: JCCI sequence is 0')
250
- return false
251
- }
252
-
253
- const tenantSeqKey = this.generateSequenceKey(tenantCode, setting)
254
- const { seq: tenantSeq } =
255
- (await this.sequencesService.getCurrentSequence(tenantSeqKey)) ?? {}
256
- this.logger.debug('sfn-event-shouldCopySequence-tenantSeq', {
257
- tenantSeqKey,
258
- tenantSeq,
259
- })
260
-
261
- if (tenantSeq === undefined || tenantSeq === null) {
262
- this.logger.debug('Tenant sequence missing: copying sequence required')
263
- return true
264
- }
265
-
266
- if (jcciSeq > tenantSeq) {
267
- this.logger.debug('Tenant sequence is behind: copying required')
268
- return true
269
- }
270
-
271
- this.logger.debug('Tenant sequence is up to date or ahead: no copy needed')
272
- return false
273
- }
274
-
275
- private async copySeqToTenant(setting: DataModel, tenantCode: string) {
276
- const fields = setting?.attributes?.['fields'] || []
277
- const codeField = fields.find((f) => f.physicalName === 'code')
278
- const typeCode = codeField.formatCode ?? codeField.dataFormat
279
-
280
- const jcciSeqKey = this.generateSequenceKey(tenantCode, setting)
281
- const { seq: jcciSeq = 0 } =
282
- (await this.sequencesService.getCurrentSequence(jcciSeqKey)) ?? {}
283
-
284
- const tenantSeqKey = this.generateSequenceKey(tenantCode, setting)
285
- const { seq: tenantSeq = 0 } =
286
- (await this.sequencesService.getCurrentSequence(tenantSeqKey)) ?? {}
287
-
288
- const distance = jcciSeq - tenantSeq
289
- this.logger.debug('Copying sequence gap:', {
290
- jcciSeq,
291
- tenantSeq,
292
- distance,
293
- })
294
-
295
- this.logger.debug('putItem', {
296
- pk: tenantSeqKey.pk,
297
- sk: tenantSeqKey.sk,
298
- code: tenantSeqKey.sk,
299
- name: tenantSeqKey.sk.split(KEY_SEPARATOR).at(-1),
300
- seq: jcciSeq,
301
- tenantCode,
302
- type: typeCode,
303
- })
304
-
305
- this.logger.debug('this.sequenceTableName', this.sequenceTableName)
306
-
307
- await this.dynamoDbService.putItem(this.sequenceTableName, {
308
- pk: tenantSeqKey.pk,
309
- sk: tenantSeqKey.sk,
310
- code: tenantSeqKey.sk,
311
- name: tenantSeqKey.sk.split(KEY_SEPARATOR).at(-1),
312
- seq: jcciSeq,
313
- tenantCode,
314
- type: typeCode,
315
- })
316
- }
317
-
318
- private generateSequenceKey(tenantCode: string, setting: DataModel) {
319
- const fields = setting?.attributes?.['fields'] || []
320
- const codeField = fields.find((f) => f.physicalName === 'code')
321
- const seqSk = codeField.formatCode ?? codeField.dataFormat
322
-
323
- const pk = sequencePk(tenantCode)
324
- const masterCode = this.getMasterCodeFromSetting(setting)
325
- const sk = genSequenceSk(seqSk, masterCode, RotateByEnum.NONE)
326
-
327
- return { pk, sk }
328
- }
329
-
330
- private getMasterCodeFromSetting(setting: DataModel): string {
331
- const parts = setting.sk.split(KEY_SEPARATOR)
332
- return parts[1]
333
- }
334
- }
@@ -1,98 +0,0 @@
1
- import {
2
- DetailDto,
3
- getUserContext,
4
- IInvoke,
5
- INVOKE_CONTEXT,
6
- } from '@mbc-cqrs-serverless/core'
7
- import {
8
- BadRequestException,
9
- Body,
10
- Controller,
11
- Delete,
12
- Get,
13
- Injectable,
14
- Param,
15
- Post,
16
- Put,
17
- Query,
18
- } from '@nestjs/common'
19
- import { ApiTags } from '@nestjs/swagger'
20
-
21
- import { MasterDataCreateDto } from './dto/master-data-create.dto'
22
- import { MasterDataSearchDto } from './dto/master-data-search.dto'
23
- import { MasterDataUpdateDto } from './dto/master-data-update.dto'
24
- import { parsePk } from './helpers'
25
- import { CustomMasterDataService } from './master-data.service'
26
-
27
- @ApiTags('master-data')
28
- @Controller('api/master-data')
29
- @Injectable()
30
- export class MasterDataController {
31
- constructor(private readonly masterDataService: CustomMasterDataService) {}
32
-
33
- @Get('/')
34
- async list(
35
- @Query() searchDto: MasterDataSearchDto,
36
- @INVOKE_CONTEXT() invokeContext: IInvoke,
37
- ) {
38
- return this.masterDataService.list(searchDto, invokeContext)
39
- }
40
-
41
- @Get('/:pk/:sk')
42
- async getDetail(@Param() key: DetailDto) {
43
- return this.masterDataService.getDetail(key)
44
- }
45
-
46
- @Post('/')
47
- async create(
48
- @Body() createDto: MasterDataCreateDto,
49
- @INVOKE_CONTEXT() invokeContext: IInvoke,
50
- ) {
51
- return this.masterDataService.create(createDto, invokeContext)
52
- }
53
-
54
- @Put('/:pk/:sk')
55
- async update(
56
- @Param() key: DetailDto,
57
- @Body() updateDto: MasterDataUpdateDto,
58
- @INVOKE_CONTEXT() invokeContext: IInvoke,
59
- ) {
60
- const userContext = getUserContext(invokeContext)
61
-
62
- const { tenantCode } = parsePk(key.pk)
63
-
64
- if (userContext.tenantCode !== tenantCode) {
65
- throw new BadRequestException('Invalid tenant code')
66
- }
67
- return this.masterDataService.update(key, updateDto, invokeContext)
68
- }
69
-
70
- @Delete('/:pk/:sk')
71
- async delete(
72
- @Param() key: DetailDto,
73
- @INVOKE_CONTEXT() invokeContext: IInvoke,
74
- ) {
75
- const userContext = getUserContext(invokeContext)
76
-
77
- const { tenantCode } = parsePk(key.pk)
78
-
79
- if (userContext.tenantCode !== tenantCode) {
80
- throw new BadRequestException('Invalid tenant code')
81
- }
82
-
83
- return this.masterDataService.delete(key, invokeContext)
84
- }
85
-
86
- @Post('/check-exist/:settingCode/:code')
87
- async checkExistCode(
88
- @Param('settingCode') settingCode: string,
89
- @Param('code') code: string,
90
- @INVOKE_CONTEXT() invokeContext: IInvoke,
91
- ) {
92
- return this.masterDataService.checkExistCode(
93
- settingCode,
94
- code,
95
- invokeContext,
96
- )
97
- }
98
- }
@@ -1,181 +0,0 @@
1
- import {
2
- DataService,
3
- DetailDto,
4
- getUserContext,
5
- IInvoke,
6
- UserContext,
7
- } from '@mbc-cqrs-serverless/core'
8
- import { MasterDataService } from '@mbc-cqrs-serverless/master'
9
- import { Injectable, NotFoundException, Param } from '@nestjs/common'
10
- import { Prisma } from '@prisma/client'
11
- import { PrismaService } from 'src/prisma'
12
- import { ulid } from 'ulid'
13
-
14
- import { MasterDataCreateDto } from './dto/master-data-create.dto'
15
- import { MasterDataSearchDto } from './dto/master-data-search.dto'
16
- import { MasterDataUpdateDto } from './dto/master-data-update.dto'
17
- import { MasterRdsEntity } from './dto/master-rds.entity'
18
- import { MasterRdsListEntity } from './dto/master-rds-list.entity'
19
- import { DATA_SK_PREFIX, getOrderBys, SETTING_SK_PREFIX } from './helpers'
20
-
21
- @Injectable()
22
- export class CustomMasterDataService {
23
- constructor(
24
- private readonly masterDataService: MasterDataService,
25
- private readonly prismaService: PrismaService,
26
- private readonly dataService: DataService,
27
- ) {}
28
-
29
- async list(searchDto: MasterDataSearchDto, invokeContext: IInvoke) {
30
- const userContext = getUserContext(invokeContext)
31
- const deletedSettingData = await this.prismaService.master.findMany({
32
- where: {
33
- tenantCode: this.getTenantCode(userContext),
34
- isDeleted: true,
35
- masterType: SETTING_SK_PREFIX,
36
- },
37
- select: {
38
- masterCode: true,
39
- },
40
- })
41
-
42
- const deletedSettingCode = deletedSettingData.map(
43
- (setting) => setting.masterCode,
44
- )
45
-
46
- const where: Prisma.MasterWhereInput = {
47
- tenantCode: this.getTenantCode(userContext),
48
- masterType: DATA_SK_PREFIX,
49
- }
50
-
51
- if (deletedSettingCode.length > 0) {
52
- where.masterTypeCode = {
53
- notIn: deletedSettingCode,
54
- }
55
- }
56
-
57
- if (searchDto.isDeleted === false || searchDto.isDeleted === undefined) {
58
- where.isDeleted = false
59
- }
60
-
61
- const andConditions: Prisma.MasterWhereInput[] = []
62
-
63
- if (searchDto.keyword?.trim()) {
64
- andConditions.push({
65
- name: { contains: searchDto.keyword.trim() },
66
- })
67
- }
68
-
69
- if (searchDto.code?.trim()) {
70
- andConditions.push({
71
- masterCode: { contains: searchDto.code.trim() },
72
- })
73
- }
74
-
75
- if (searchDto.settingCode?.trim()) {
76
- andConditions.push({
77
- masterTypeCode: searchDto.settingCode.trim(),
78
- })
79
- }
80
-
81
- if (andConditions.length) {
82
- where.AND = andConditions
83
- }
84
-
85
- const {
86
- pageSize = 10,
87
- page = 1,
88
- orderBys = ['seq', 'masterCode'],
89
- } = searchDto
90
-
91
- const [total, items] = await Promise.all([
92
- this.prismaService.master.count({ where }),
93
- this.prismaService.master.findMany({
94
- where,
95
- take: pageSize,
96
- skip: pageSize * (page - 1),
97
- orderBy: getOrderBys<Prisma.MasterOrderByWithRelationInput>(orderBys),
98
- }),
99
- ])
100
-
101
- return new MasterRdsListEntity({
102
- total,
103
- items: items.map((item) => new MasterRdsEntity(item)),
104
- })
105
- }
106
-
107
- async getDetail(@Param() key: DetailDto) {
108
- const data = await this.dataService.getItem(key)
109
-
110
- if (!data) throw new NotFoundException()
111
-
112
- return new MasterRdsEntity(data)
113
- }
114
-
115
- async create(createDto: MasterDataCreateDto, invokeContext: IInvoke) {
116
- const userContext = getUserContext(invokeContext)
117
- let seq = createDto?.seq
118
- if (!seq) {
119
- const maxSeq = await this.prismaService.master.aggregate({
120
- _max: {
121
- seq: true,
122
- },
123
- where: {
124
- tenantCode: userContext.tenantCode,
125
- masterType: DATA_SK_PREFIX,
126
- masterTypeCode: createDto.settingCode,
127
- },
128
- })
129
- seq = (maxSeq._max.seq ?? 0) + 1
130
- createDto.attributes['seq'] = seq
131
- }
132
-
133
- return await this.masterDataService.create(
134
- {
135
- code: createDto.code ?? ulid(),
136
- tenantCode: userContext.tenantCode,
137
- name: createDto.name,
138
- settingCode: createDto.settingCode,
139
- attributes: createDto.attributes ?? {},
140
- seq,
141
- },
142
- { invokeContext },
143
- )
144
- }
145
-
146
- async update(
147
- key: DetailDto,
148
- updateDto: MasterDataUpdateDto,
149
- invokeContext: IInvoke,
150
- ) {
151
- return await this.masterDataService.update(
152
- key,
153
- {
154
- ...updateDto,
155
- },
156
- { invokeContext },
157
- )
158
- }
159
-
160
- async delete(key: DetailDto, invokeContext: IInvoke) {
161
- return this.masterDataService.delete(key, { invokeContext })
162
- }
163
-
164
- async checkExistCode(
165
- settingCode: string,
166
- code: string,
167
- invokeContext: IInvoke,
168
- ) {
169
- const userContext = getUserContext(invokeContext)
170
-
171
- return this.masterDataService.checkExistCode(
172
- userContext.tenantCode,
173
- settingCode,
174
- code,
175
- )
176
- }
177
-
178
- private getTenantCode(userContext: UserContext): string {
179
- return userContext.tenantCode
180
- }
181
- }
@@ -1,103 +0,0 @@
1
- import {
2
- DetailDto,
3
- getUserContext,
4
- IInvoke,
5
- INVOKE_CONTEXT,
6
- } from '@mbc-cqrs-serverless/core'
7
- import { CommonSettingDto } from '@mbc-cqrs-serverless/master/dist/dto'
8
- import {
9
- BadRequestException,
10
- Body,
11
- Controller,
12
- Delete,
13
- Get,
14
- Injectable,
15
- Param,
16
- Post,
17
- Put,
18
- Query,
19
- } from '@nestjs/common'
20
- import { ApiTags } from '@nestjs/swagger'
21
-
22
- import { MasterCopyDto } from './dto/master-copy.dto'
23
- import { MasterSettingSearchDto } from './dto/master-setting-search.dto'
24
- import { MasterSettingUpdateDto } from './dto/master-setting-update.dto'
25
- import { parsePk } from './helpers'
26
- import { CustomMasterSettingService } from './master-setting.service'
27
-
28
- @ApiTags('master-setting')
29
- @Controller('api/master-setting')
30
- @Injectable()
31
- export class MasterSettingController {
32
- constructor(
33
- private readonly masterSettingService: CustomMasterSettingService,
34
- ) {}
35
-
36
- @Get('/')
37
- async list(
38
- @Query() searchDto: MasterSettingSearchDto,
39
- @INVOKE_CONTEXT() invokeContext: IInvoke,
40
- ) {
41
- return this.masterSettingService.list(searchDto, invokeContext)
42
- }
43
-
44
- @Get('/:pk/:sk')
45
- async getDetail(@Param() key: DetailDto) {
46
- return this.masterSettingService.getDetail(key)
47
- }
48
-
49
- @Post('/')
50
- async create(
51
- @Body() createDto: CommonSettingDto,
52
- @INVOKE_CONTEXT() invokeContext: IInvoke,
53
- ) {
54
- return this.masterSettingService.create(createDto, invokeContext)
55
- }
56
-
57
- @Put('/:pk/:sk')
58
- async update(
59
- @Param() key: DetailDto,
60
- @Body() updateDto: MasterSettingUpdateDto,
61
- @INVOKE_CONTEXT() invokeContext: IInvoke,
62
- ) {
63
- const userContext = getUserContext(invokeContext)
64
-
65
- const { tenantCode } = parsePk(key.pk)
66
-
67
- if (userContext.tenantCode !== tenantCode) {
68
- throw new BadRequestException('Invalid tenant code')
69
- }
70
- return this.masterSettingService.update(key, updateDto, invokeContext)
71
- }
72
-
73
- @Delete('/:pk/:sk')
74
- async delete(
75
- @Param() key: DetailDto,
76
- @INVOKE_CONTEXT() invokeContext: IInvoke,
77
- ) {
78
- const userContext = getUserContext(invokeContext)
79
-
80
- const { tenantCode } = parsePk(key.pk)
81
-
82
- if (userContext.tenantCode !== tenantCode) {
83
- throw new BadRequestException('Invalid tenant code')
84
- }
85
- return this.masterSettingService.delete(key, invokeContext)
86
- }
87
-
88
- @Post('/check-exist/:code')
89
- async checkExistCode(
90
- @Param('code') code: string,
91
- @INVOKE_CONTEXT() invokeContext: IInvoke,
92
- ) {
93
- return this.masterSettingService.checkExistCode(code, invokeContext)
94
- }
95
-
96
- @Post('/copy')
97
- async copyMaster(
98
- @INVOKE_CONTEXT() invokeContext: IInvoke,
99
- @Body() masterCopyDto: MasterCopyDto,
100
- ): Promise<any> {
101
- return this.masterSettingService.copy(masterCopyDto, { invokeContext })
102
- }
103
- }