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

212 lines
6.5 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";
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";
import { TaskServiceBuilder } from "../../../modules/pipeline/service/getter/task-service-getter.js";
2025-09-11 23:47:05 +08:00
2025-09-11 00:19:38 +08:00
/**
* Addon
*/
@Provide()
@Controller("/api/addon")
2025-09-11 00:19:38 +08:00
export class AddonController extends CrudController<AddonService> {
@Inject()
service: AddonService;
@Inject()
authService: AuthService;
@Inject()
taskServiceBuilder:TaskServiceBuilder
2025-09-11 00:19:38 +08:00
getService(): AddonService {
return this.service;
}
@Post("/page", { summary: 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,
buildQuery
2025-09-11 00:19:38 +08:00
});
return this.ok(res);
}
@Post("/list", { summary: 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);
}
@Post("/add", { summary: 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;
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);
}
@Post("/update", { summary: 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) {
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);
}
@Post("/info", { summary: Constants.per.authOnly })
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);
}
@Post("/delete", { summary: Constants.per.authOnly })
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);
}
@Post("/define", { summary: Constants.per.authOnly })
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);
}
@Post("/getTypeDict", { summary: Constants.per.authOnly })
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,
icon: item.icon
2025-09-11 00:19:38 +08:00
});
}
dict = dict.sort(a => {
return a.needPlus ? 0 : -1;
});
return this.ok(dict);
}
@Post("/simpleInfo", { summary: Constants.per.authOnly })
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) {
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);
}
@Post("/getDefaultId", { summary: Constants.per.authOnly })
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);
}
@Post("/setDefault", { summary: Constants.per.authOnly })
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);
}
@Post("/options", { summary: Constants.per.authOnly })
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-11 00:19:38 +08:00
});
for (const item of res) {
delete item.setting;
}
return this.ok(res);
}
@Post("/handle", { summary: 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) {
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-11 00:19:38 +08:00
const ctx = {
http: http,
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);
}
}