2025-09-11 23:47:05 +08:00
|
|
|
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
|
2025-09-11 00:19:38 +08:00
|
|
|
import {
|
2025-09-11 23:47:05 +08:00
|
|
|
AddonDefine,
|
2025-09-11 00:19:38 +08:00
|
|
|
AddonRequestHandleReq,
|
2025-09-11 23:47:05 +08:00
|
|
|
AddonService,
|
2025-09-11 00:19:38 +08:00
|
|
|
Constants,
|
|
|
|
|
CrudController,
|
|
|
|
|
newAddon,
|
|
|
|
|
ValidateException
|
|
|
|
|
} from "@certd/lib-server";
|
2025-09-11 23:47:05 +08:00
|
|
|
import { AuthService } from "../../../modules/sys/authority/service/auth-service.js";
|
|
|
|
|
import { checkPlus } from "@certd/plus-core";
|
2025-09-11 00:19:38 +08:00
|
|
|
import { http, logger, utils } from "@certd/basic";
|
2025-09-26 01:21:01 +08:00
|
|
|
import { TaskServiceBuilder } from "../../../modules/pipeline/service/getter/task-service-getter.js";
|
2026-03-15 14:01:34 +08:00
|
|
|
import { ApiTags } from "@midwayjs/swagger";
|
2025-09-11 23:47:05 +08:00
|
|
|
|
2025-09-11 00:19:38 +08:00
|
|
|
/**
|
|
|
|
|
* Addon
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
2025-09-26 01:21:01 +08:00
|
|
|
@Controller("/api/addon")
|
2026-03-15 14:01:34 +08:00
|
|
|
@ApiTags(['addon'])
|
2025-09-11 00:19:38 +08:00
|
|
|
export class AddonController extends CrudController<AddonService> {
|
|
|
|
|
@Inject()
|
|
|
|
|
service: AddonService;
|
|
|
|
|
@Inject()
|
|
|
|
|
authService: AuthService;
|
2025-09-26 01:21:01 +08:00
|
|
|
@Inject()
|
|
|
|
|
taskServiceBuilder:TaskServiceBuilder
|
2025-09-11 00:19:38 +08:00
|
|
|
|
|
|
|
|
getService(): AddonService {
|
|
|
|
|
return this.service;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/page", { description: Constants.per.authOnly })
|
2025-09-11 00:19:38 +08:00
|
|
|
async page(@Body(ALL) body) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const {projectId,userId} = await this.getProjectUserIdRead();
|
2025-09-11 00:19:38 +08:00
|
|
|
body.query = body.query ?? {};
|
|
|
|
|
delete body.query.userId;
|
2026-02-13 21:28:17 +08:00
|
|
|
body.query.projectId = projectId;
|
2025-09-11 00:19:38 +08:00
|
|
|
const buildQuery = qb => {
|
2026-02-13 21:28:17 +08:00
|
|
|
qb.andWhere("user_id = :userId", { userId });
|
2025-09-11 00:19:38 +08:00
|
|
|
};
|
|
|
|
|
const res = await this.service.page({
|
|
|
|
|
query: body.query,
|
|
|
|
|
page: body.page,
|
|
|
|
|
sort: body.sort,
|
2025-09-26 01:21:01 +08:00
|
|
|
buildQuery
|
2025-09-11 00:19:38 +08:00
|
|
|
});
|
|
|
|
|
return this.ok(res);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/list", { description: Constants.per.authOnly })
|
2025-09-11 00:19:38 +08:00
|
|
|
async list(@Body(ALL) body) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const {projectId,userId} = await this.getProjectUserIdRead();
|
2025-09-11 00:19:38 +08:00
|
|
|
body.query = body.query ?? {};
|
2026-02-13 21:28:17 +08:00
|
|
|
body.query.userId = userId;
|
|
|
|
|
body.query.projectId = projectId;
|
2025-09-11 00:19:38 +08:00
|
|
|
return super.list(body);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/add", { description: Constants.per.authOnly })
|
2025-09-11 00:19:38 +08:00
|
|
|
async add(@Body(ALL) bean) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const {userId,projectId} = await this.getProjectUserIdRead();
|
|
|
|
|
bean.userId = userId;
|
|
|
|
|
bean.projectId = projectId;
|
2025-09-11 00:19:38 +08:00
|
|
|
const type = bean.type;
|
|
|
|
|
const addonType = bean.addonType;
|
2025-09-26 01:21:01 +08:00
|
|
|
if (!type || !addonType) {
|
|
|
|
|
throw new ValidateException("请选择Addon类型");
|
2025-09-11 00:19:38 +08:00
|
|
|
}
|
2025-09-26 01:21:01 +08:00
|
|
|
const define: AddonDefine = this.service.getDefineByType(type, addonType);
|
2025-09-11 00:19:38 +08:00
|
|
|
if (!define) {
|
2025-09-26 01:21:01 +08:00
|
|
|
throw new ValidateException("Addon类型不存在");
|
2025-09-11 00:19:38 +08:00
|
|
|
}
|
|
|
|
|
if (define.needPlus) {
|
|
|
|
|
checkPlus();
|
|
|
|
|
}
|
|
|
|
|
return super.add(bean);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/update", { description: Constants.per.authOnly })
|
2025-09-11 00:19:38 +08:00
|
|
|
async update(@Body(ALL) bean) {
|
2026-02-13 21:28:17 +08:00
|
|
|
await this.checkOwner(this.getService(), bean.id, "write");
|
2025-09-11 00:19:38 +08:00
|
|
|
const old = await this.service.info(bean.id);
|
|
|
|
|
if (!old) {
|
2025-09-26 01:21:01 +08:00
|
|
|
throw new ValidateException("Addon配置不存在");
|
2025-09-11 00:19:38 +08:00
|
|
|
}
|
2025-09-26 01:21:01 +08:00
|
|
|
if (old.type !== bean.type) {
|
2025-09-11 00:19:38 +08:00
|
|
|
const addonType = old.type;
|
|
|
|
|
const type = bean.type;
|
2025-09-26 01:21:01 +08:00
|
|
|
const define: AddonDefine = this.service.getDefineByType(type, addonType);
|
2025-09-11 00:19:38 +08:00
|
|
|
if (!define) {
|
2025-09-26 01:21:01 +08:00
|
|
|
throw new ValidateException("Addon类型不存在");
|
2025-09-11 00:19:38 +08:00
|
|
|
}
|
|
|
|
|
if (define.needPlus) {
|
|
|
|
|
checkPlus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
delete bean.userId;
|
2026-02-13 21:28:17 +08:00
|
|
|
delete bean.projectId;
|
2025-09-11 00:19:38 +08:00
|
|
|
return super.update(bean);
|
|
|
|
|
}
|
2025-09-26 01:21:01 +08:00
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/info", { description: Constants.per.authOnly })
|
2025-09-26 01:21:01 +08:00
|
|
|
async info(@Query("id") id: number) {
|
2026-02-13 21:28:17 +08:00
|
|
|
await this.checkOwner(this.getService(), id, "read");
|
2025-09-11 00:19:38 +08:00
|
|
|
return super.info(id);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/delete", { description: Constants.per.authOnly })
|
2025-09-26 01:21:01 +08:00
|
|
|
async delete(@Query("id") id: number) {
|
2026-02-13 21:28:17 +08:00
|
|
|
await this.checkOwner(this.getService(), id, "write");
|
2025-09-11 00:19:38 +08:00
|
|
|
return super.delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/define", { description: Constants.per.authOnly })
|
2025-09-26 01:21:01 +08:00
|
|
|
async define(@Query("type") type: string, @Query("addonType") addonType: string) {
|
|
|
|
|
const notification = this.service.getDefineByType(type, addonType);
|
2025-09-11 00:19:38 +08:00
|
|
|
return this.ok(notification);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/getTypeDict", { description: Constants.per.authOnly })
|
2025-09-26 01:21:01 +08:00
|
|
|
async getTypeDict(@Query("addonType") addonType: string) {
|
2025-09-11 00:19:38 +08:00
|
|
|
const list: any = this.service.getDefineList(addonType);
|
|
|
|
|
let dict = [];
|
|
|
|
|
for (const item of list) {
|
|
|
|
|
dict.push({
|
|
|
|
|
value: item.name,
|
|
|
|
|
label: item.title,
|
|
|
|
|
needPlus: item.needPlus ?? false,
|
2025-09-26 01:21:01 +08:00
|
|
|
icon: item.icon
|
2025-09-11 00:19:38 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
dict = dict.sort(a => {
|
|
|
|
|
return a.needPlus ? 0 : -1;
|
|
|
|
|
});
|
|
|
|
|
return this.ok(dict);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/simpleInfo", { description: Constants.per.authOnly })
|
2025-09-26 01:21:01 +08:00
|
|
|
async simpleInfo(@Query("addonType") addonType: string, @Query("id") id: number) {
|
2025-09-11 00:19:38 +08:00
|
|
|
if (id === 0) {
|
|
|
|
|
//获取默认
|
2026-02-13 21:28:17 +08:00
|
|
|
const {projectId,userId} = await this.getProjectUserIdRead();
|
|
|
|
|
const res = await this.service.getDefault(userId, addonType,projectId);
|
2025-09-11 00:19:38 +08:00
|
|
|
if (!res) {
|
2025-09-26 01:21:01 +08:00
|
|
|
throw new ValidateException("默认Addon配置不存在");
|
2025-09-11 00:19:38 +08:00
|
|
|
}
|
|
|
|
|
const simple = await this.service.getSimpleInfo(res.id);
|
|
|
|
|
return this.ok(simple);
|
|
|
|
|
}
|
2026-02-13 21:28:17 +08:00
|
|
|
await this.checkOwner(this.getService(), id, "read",true);
|
2025-09-11 00:19:38 +08:00
|
|
|
const res = await this.service.getSimpleInfo(id);
|
|
|
|
|
return this.ok(res);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/getDefaultId", { description: Constants.per.authOnly })
|
2025-09-26 01:21:01 +08:00
|
|
|
async getDefaultId(@Query("addonType") addonType: string) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const {projectId,userId} = await this.getProjectUserIdRead();
|
|
|
|
|
const res = await this.service.getDefault(userId, addonType,projectId);
|
2025-09-11 00:19:38 +08:00
|
|
|
return this.ok(res?.id);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/setDefault", { description: Constants.per.authOnly })
|
2025-09-26 01:21:01 +08:00
|
|
|
async setDefault(@Query("addonType") addonType: string, @Query("id") id: number) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const {projectId,userId} = await this.checkOwner(this.getService(), id, "write",true);
|
|
|
|
|
const res = await this.service.setDefault(id, userId, addonType,projectId);
|
2025-09-11 00:19:38 +08:00
|
|
|
return this.ok(res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/options", { description: Constants.per.authOnly })
|
2025-09-26 01:21:01 +08:00
|
|
|
async options(@Query("addonType") addonType: string) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const {projectId,userId} = await this.getProjectUserIdRead();
|
2025-09-11 00:19:38 +08:00
|
|
|
const res = await this.service.list({
|
|
|
|
|
query: {
|
2026-02-13 21:28:17 +08:00
|
|
|
userId,
|
|
|
|
|
addonType,
|
|
|
|
|
projectId
|
2025-09-26 01:21:01 +08:00
|
|
|
}
|
2025-09-11 00:19:38 +08:00
|
|
|
});
|
|
|
|
|
for (const item of res) {
|
|
|
|
|
delete item.setting;
|
|
|
|
|
}
|
|
|
|
|
return this.ok(res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-03-15 16:20:20 +08:00
|
|
|
@Post("/handle", { description: Constants.per.authOnly })
|
2025-09-11 00:19:38 +08:00
|
|
|
async handle(@Body(ALL) body: AddonRequestHandleReq) {
|
|
|
|
|
let inputAddon = body.input.addon;
|
|
|
|
|
if (body.input.id > 0) {
|
2026-02-13 21:28:17 +08:00
|
|
|
await this.checkOwner(this.getService(), body.input.id, "write",true);
|
2025-09-11 00:19:38 +08:00
|
|
|
const oldEntity = await this.service.info(body.input.id);
|
|
|
|
|
if (oldEntity) {
|
2025-09-26 01:21:01 +08:00
|
|
|
inputAddon = JSON.parse(oldEntity.setting);
|
2025-09-11 00:19:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-13 21:28:17 +08:00
|
|
|
const {projectId,userId} = await this.getProjectUserIdRead();
|
|
|
|
|
const serviceGetter = this.taskServiceBuilder.create({ userId,projectId });
|
2025-09-26 01:21:01 +08:00
|
|
|
|
2025-09-11 00:19:38 +08:00
|
|
|
const ctx = {
|
|
|
|
|
http: http,
|
2025-09-26 01:21:01 +08:00
|
|
|
logger: logger,
|
|
|
|
|
utils: utils,
|
|
|
|
|
serviceGetter
|
|
|
|
|
};
|
|
|
|
|
const addon = await newAddon(body.addonType, body.typeName, inputAddon, ctx);
|
2025-09-11 00:19:38 +08:00
|
|
|
const res = await addon.onRequest(body);
|
|
|
|
|
return this.ok(res);
|
|
|
|
|
}
|
|
|
|
|
}
|