forked from TencentBlueKing/bk-lesscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
392 lines (379 loc) · 14.1 KB
/
Copy pathfunction.js
File metadata and controls
392 lines (379 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community Edition) available.
* Copyright (C) 2025 Tencent. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
import { LCDataService, TABLE_FILE_NAME } from '../service/common/data-service'
import {
Controller,
Get,
Post,
Put,
Delete,
BodyParams,
QueryParams,
HeaderParams,
Ctx,
ProjectAuthorization,
ProjectResAuthorization,
OutputJson
} from '../decorator'
import {
checkFuncBody,
fixFuncBody,
doubleCheckFunction,
handleRelation,
injectRelation,
handleFunctionIntoDb,
handleFunctionOutDb,
getAllGroupAndFunction
} from '../service/business/function'
import { IAM_ACTION } from '../../shared/constant.js'
import OperationLogger from '../service/common/operation-logger'
@Controller('/api/function')
export default class FunctionController {
// 获取项目下的所有函数分类
@OutputJson()
@ProjectAuthorization({})
@Get('/getAllGroupAndFunction')
getAllGroupAndFunction (
@HeaderParams({ name: 'x-project-id', require: true }) projectId,
@QueryParams({ name: 'versionId' }) versionId
) {
return getAllGroupAndFunction(projectId, versionId)
}
// 获取项目下的所有函数分类
@OutputJson()
@ProjectAuthorization({})
@Get('/getGroupList')
async getGroupList (
@HeaderParams({ name: 'x-project-id', require: true }) projectId,
@QueryParams({ name: 'versionId' }) versionId
) {
const { list } = await LCDataService.get({
tableFileName: TABLE_FILE_NAME.FUNC_GROUP,
query: {
projectId,
versionId,
deleteFlag: 0
},
order: {
order: 'ASC'
}
})
return list
}
// 获取项目下的函数
@OutputJson()
@ProjectAuthorization({})
@Get('/getFunctionList')
async getFunctionList (
@HeaderParams({ name: 'x-project-id', require: true }) projectId,
@QueryParams({ name: 'funcGroupId' }) funcGroupId,
@QueryParams({ name: 'versionId' }) versionId
) {
const query = {
versionId,
projectId,
deleteFlag: 0
}
if (funcGroupId !== undefined) {
query.funcGroupId = funcGroupId
}
const { list } = await LCDataService.get({
tableFileName: TABLE_FILE_NAME.FUNC,
query
})
const listWithRelation = await injectRelation(list, projectId, versionId)
const formattedList = handleFunctionOutDb(listWithRelation)
return formattedList
}
// 新增函数分类
@OutputJson()
@ProjectAuthorization({})
@Post('/createFunctionGroup')
async createFunctionGroup (
@HeaderParams({ name: 'x-project-id', require: true }) projectId,
@BodyParams({ name: 'groupName', require: true }) groupName,
@BodyParams({ name: 'versionId' }) versionId
) {
const {
list: funcGroupList
} = await LCDataService.get({
tableFileName: TABLE_FILE_NAME.FUNC_GROUP,
query: {
versionId,
projectId,
deleteFlag: 0
}
})
const groupNameList = groupName.split('/')
// 重复检查
const repeatGroup = funcGroupList.find(group => groupNameList.includes(group.groupName))
if (repeatGroup) {
throw new Error(global.i18n.t('分类【{{n}}】已存在,请修改后再试', { n: repeatGroup.groupName }))
}
// 新增分类
let order = funcGroupList[funcGroupList.length - 1]?.order || 0
const newGroupList = groupNameList.map((groupName) => ({
versionId,
projectId,
groupName,
order: ++order
}))
return LCDataService.bulkAdd(TABLE_FILE_NAME.FUNC_GROUP, newGroupList)
}
// 删除函数分类
@OutputJson()
@ProjectResAuthorization({ tableName: 'FUNC_GROUP', getResourceId: ctx => ctx.query.funcGroupId })
@Delete('/deleteFunctionGroup')
async deleteFunctionGroup (
@QueryParams({ name: 'funcGroupId', require: true }) funcGroupId
) {
// 判断分类下已存在函数
const isExistFunc = await LCDataService.has(TABLE_FILE_NAME.FUNC, {
funcGroupId,
deleteFlag: 0
})
if (isExistFunc) {
throw new Error(global.i18n.t('分类【ID:{{n}}】下已存在函数,无法删除,请修改后再试', { n: funcGroupId }))
}
// 判断项目下是否只有一个分类
const group = await LCDataService.findOne(TABLE_FILE_NAME.FUNC_GROUP, {
id: funcGroupId
})
const count = await LCDataService.count(TABLE_FILE_NAME.FUNC_GROUP, {
projectId: group.projectId,
deleteFlag: 0
})
if (count <= 1) {
throw new Error(global.i18n.t('应用【ID:{{n}}】下只有唯一一个分组,无法删除最后一个分组,请修改后再试', { n: group.projectId }))
}
return LCDataService.delete(TABLE_FILE_NAME.FUNC_GROUP, funcGroupId)
}
// 编辑函数分类
@OutputJson()
@ProjectAuthorization({})
@Put('/editFunctionGroups')
editFunctionGroups (
@BodyParams({ name: 'functionGroups', require: true }) functionGroups
) {
return LCDataService.bulkUpdate(TABLE_FILE_NAME.FUNC_GROUP, functionGroups)
}
// 新增函数
@ProjectAuthorization({})
@Post('/createFunction')
async createFunction (
@HeaderParams({ name: 'x-project-id', require: true }) projectId,
@BodyParams({ require: true }) functionData,
@Ctx() ctx
) {
const operationLogger = new OperationLogger(ctx)
functionData.projectId = projectId
functionData.id = null
try {
// 检查 code name 是否重复
await doubleCheckFunction(functionData, functionData.projectId, functionData.versionId)
// ESLint 检查
await checkFuncBody(functionData)
// 入库事务
await LCDataService.transaction(async (transactionalEntityHelper) => {
// 插入数据库
await transactionalEntityHelper.add(TABLE_FILE_NAME.FUNC, handleFunctionIntoDb(functionData))
// 处理关联关系
await handleRelation(functionData, functionData.projectId, functionData.versionId)
operationLogger.success({
operateTarget: global.i18n.t('函数:{{n}}', { n: functionData.funcCode })
})
})
ctx.send({
code: 0,
message: 'success',
data: functionData
})
} catch (error) {
operationLogger.error(error, {
operateTarget: global.i18n.t('函数:{{n}}', { n: functionData.funcCode })
})
const { code, message, data } = error
ctx.send({
code, message, data
})
}
}
// 编辑函数
@ProjectResAuthorization({ tableName: 'FUNC', getResourceId: ctx => ctx.request.body?.id })
@Put('/editFunction')
async editFunction (
@HeaderParams({ name: 'x-project-id', require: true }) projectId,
@BodyParams({ require: true }) functionData,
@Ctx() ctx
) {
const operationLogger = new OperationLogger(ctx)
functionData.projectId = projectId
try {
// 检查 code name 是否重复
await doubleCheckFunction(functionData, functionData.projectId, functionData.versionId)
// ESLint 检查
await checkFuncBody(functionData)
// 入库事务
await LCDataService.transaction(async (transactionalEntityHelper) => {
// 插入数据库
await transactionalEntityHelper.update(TABLE_FILE_NAME.FUNC, handleFunctionIntoDb(functionData))
// 处理关联关系
await handleRelation(functionData, functionData.projectId, functionData.versionId)
operationLogger.success({
operateTarget: global.i18n.t('函数:{{n}}', { n: functionData.funcCode })
})
})
ctx.send({
code: 0,
message: 'success',
data: functionData
})
} catch (error) {
operationLogger.error(error, {
operateTarget: global.i18n.t('函数:{{n}}', { n: functionData.funcCode })
})
const { code, message, data } = error
ctx.send({
code, message, data
})
}
}
// 批量新增函数
@OutputJson()
@ProjectAuthorization({})
@Post('/bulkCreateFunction')
async bulkCreateFunction (
@HeaderParams({ name: 'x-project-id', require: true }) projectId,
@BodyParams({ name: 'functionList', require: true }) functionList,
@BodyParams({ name: 'versionId' }) versionId
) {
// 检查 code name 是否重复
await doubleCheckFunction(functionList, projectId, versionId)
// ESLint 检查
await checkFuncBody(functionList)
// 入库事务
await LCDataService.transaction(async (transactionalEntityHelper) => {
// 插入数据库
functionList.forEach(item => {
item.id = null
})
await transactionalEntityHelper.add(TABLE_FILE_NAME.FUNC, handleFunctionIntoDb(functionList))
// 处理关联关系
await handleRelation(functionList, projectId, versionId)
})
// 返回函数列表
return functionList
}
// 删除函数
@ProjectResAuthorization({ tableName: 'FUNC', getResourceId: ctx => ctx.query.id })
@Delete('/deleteFunction')
async deleteFunction (
@QueryParams({ name: 'id', require: true }) id,
@Ctx() ctx
) {
const operationLogger = new OperationLogger(ctx)
const functionData = await LCDataService.findOne(TABLE_FILE_NAME.FUNC, { id })
try {
// 获取数据
const query = {
projectId: functionData.projectId,
versionId: functionData.versionId,
deleteFlag: 0
}
const [
{ list: funcRelateList },
{ list: varRelateList }
] = await Promise.all([
LCDataService.get({
tableFileName: TABLE_FILE_NAME.FUNC_FUNC,
query: {
...query,
parentFuncCode: functionData.funcCode
}
}),
LCDataService.get({
tableFileName: TABLE_FILE_NAME.FUNC_VARIABLE,
query: {
...query,
funcCode: functionData.funcCode
}
})
])
const {
useInfo: {
funcCodes,
pageNames,
variableCodes
}
} = await injectRelation(functionData, functionData.projectId, functionData.versionId)
if (funcCodes.length > 0) {
throw new Error(global.i18n.t('该函数被使用在函数标识为【{{n}}】的函数中,不可删除,请修改后再试', { n: funcCodes.join(',') }))
}
if (pageNames.length > 0) {
throw new Error(global.i18n.t('该函数被使用在页面名称为【{{n}}】的页面中,不可删除,请修改后再试', { n: pageNames.join(',') }))
}
if (variableCodes.length > 0) {
throw new Error(global.i18n.t('该函数被使用在变量标识为【{{n}}】的变量中,不可删除,请修改后再试', { n: variableCodes.join(',') }))
}
// 删除数据
await LCDataService.transaction(async (transactionalEntityManager) => {
await transactionalEntityManager.delete(functionData)
await transactionalEntityManager.delete(funcRelateList)
await transactionalEntityManager.delete(varRelateList)
operationLogger.success({
operateTarget: global.i18n.t('函数:{{n}}', { n: functionData.funcCode })
})
})
ctx.send({
code: 0,
message: 'success',
data: functionData
})
} catch (error) {
operationLogger.error(error, {
operateTarget: global.i18n.t('函数:{{n}}', { n: functionData.funcCode })
})
const { code, message, data } = error
ctx.send({
code, message, data
})
}
}
// 使用ESLint修复函数
@OutputJson()
@Post('/fixFunByEslint')
fixFunByEslint (
@BodyParams({ require: true }) functionData
) {
return fixFuncBody(functionData)
}
// 使用 ESLint 检查函数
@OutputJson()
@Post('/checkEslint')
async checkEslint (
@BodyParams({ require: true }) functionData
) {
// 检查结果
const lintResult = {
message: '',
data: []
}
// 执行检查
try {
await checkFuncBody(functionData)
} catch (error) {
lintResult.message = error.message
lintResult.data = error.data
}
// 抛出结果
return lintResult
}
}