Files
certd/packages/ui/certd-server/src/controller/user/addon/addon-controller.ts
T

204 lines
7.0 KiB
TypeScript
Raw Normal View History

2025-09-11 23:47:05 +08:00
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
2026-05-31 01:41:33 +08:00
import { AddonDefine, AddonRequestHandleReq, AddonService, 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";
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()
@Controller("/api/addon")
2026-05-31 01:41:33 +08:00
@ApiTags(["addon"])
2025-09-11 00:19:38 +08:00
export class AddonController extends CrudController<AddonService> {
@Inject()
service: AddonService;
@Inject()
authService: AuthService;
@Inject()
2026-05-31 01:41:33 +08:00
taskServiceBuilder: TaskServiceBuilder;
2025-09-11 00:19:38 +08:00
getService(): AddonService {
return this.service;
}
2026-03-15 18:26:49 +08:00
@Post("/page", { description: Constants.per.authOnly, summary: "查询Addon分页列表" })
2025-09-11 00:19:38 +08:00
async page(@Body(ALL) body) {
2026-05-31 01:41:33 +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,
2026-05-31 01:41:33 +08:00
buildQuery,
2025-09-11 00:19:38 +08:00
});
return this.ok(res);
}
2026-03-15 18:26:49 +08:00
@Post("/list", { description: Constants.per.authOnly, summary: "查询Addon列表" })
2025-09-11 00:19:38 +08:00
async list(@Body(ALL) body) {
2026-05-31 01:41:33 +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 18:26:49 +08:00
@Post("/add", { description: Constants.per.authOnly, summary: "添加Addon" })
2025-09-11 00:19:38 +08:00
async add(@Body(ALL) bean) {
2026-05-31 01:41:33 +08:00
const { userId, projectId } = await this.getProjectUserIdRead();
2026-02-13 21:28:17 +08:00
bean.userId = userId;
bean.projectId = projectId;
2025-09-11 00:19:38 +08:00
const type = bean.type;
const addonType = bean.addonType;
if (!type || !addonType) {
throw new ValidateException("请选择Addon类型");
2025-09-11 00:19:38 +08:00
}
const define: AddonDefine = this.service.getDefineByType(type, addonType);
2025-09-11 00:19:38 +08:00
if (!define) {
throw new ValidateException("Addon类型不存在");
2025-09-11 00:19:38 +08:00
}
if (define.needPlus) {
checkPlus();
}
return super.add(bean);
}
2026-03-15 18:26:49 +08:00
@Post("/update", { description: Constants.per.authOnly, summary: "更新Addon" })
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) {
throw new ValidateException("Addon配置不存在");
2025-09-11 00:19:38 +08:00
}
if (old.type !== bean.type) {
2025-09-11 00:19:38 +08:00
const addonType = old.type;
const type = bean.type;
const define: AddonDefine = this.service.getDefineByType(type, addonType);
2025-09-11 00:19:38 +08:00
if (!define) {
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);
}
2026-03-15 18:26:49 +08:00
@Post("/info", { description: Constants.per.authOnly, summary: "查询Addon详情" })
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 18:26:49 +08:00
@Post("/delete", { description: Constants.per.authOnly, summary: "删除Addon" })
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 18:26:49 +08:00
@Post("/define", { description: Constants.per.authOnly, summary: "查询Addon插件定义" })
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 18:26:49 +08:00
@Post("/getTypeDict", { description: Constants.per.authOnly, summary: "查询Addon插件类型字典" })
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,
2026-05-31 01:41:33 +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 18:26:49 +08:00
@Post("/simpleInfo", { description: Constants.per.authOnly, summary: "查询Addon插件简单信息" })
async simpleInfo(@Query("addonType") addonType: string, @Query("id") id: number) {
2025-09-11 00:19:38 +08:00
if (id === 0) {
//获取默认
2026-05-31 01:41:33 +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) {
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-05-31 01:41:33 +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 18:26:49 +08:00
@Post("/getDefaultId", { description: Constants.per.authOnly, summary: "查询Addon插件默认配置ID" })
async getDefaultId(@Query("addonType") addonType: string) {
2026-05-31 01:41:33 +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 18:26:49 +08:00
@Post("/setDefault", { description: Constants.per.authOnly, summary: "设置Addon插件默认配置" })
async setDefault(@Query("addonType") addonType: string, @Query("id") id: number) {
2026-05-31 01:41:33 +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 18:26:49 +08:00
@Post("/options", { description: Constants.per.authOnly, summary: "查询Addon插件配置字典" })
async options(@Query("addonType") addonType: string) {
2026-05-31 01:41:33 +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,
2026-05-31 01:41:33 +08:00
projectId,
},
2025-09-11 00:19:38 +08:00
});
for (const item of res) {
delete item.setting;
}
return this.ok(res);
}
2026-03-15 18:26:49 +08:00
@Post("/handle", { description: Constants.per.authOnly, summary: "Addon插件处理请求" })
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-05-31 01:41:33 +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) {
inputAddon = JSON.parse(oldEntity.setting);
2025-09-11 00:19:38 +08:00
}
}
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
const serviceGetter = this.taskServiceBuilder.create({ userId, projectId });
2025-09-11 00:19:38 +08:00
const ctx = {
http: http,
logger: logger,
utils: utils,
2026-05-31 01:41:33 +08:00
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);
}
}