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

200 lines
6.8 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
import { Constants, CrudController, ValidateException } from "@certd/lib-server";
import { NotificationService } from "../../../modules/pipeline/service/notification-service.js";
import { AuthService } from "../../../modules/sys/authority/service/auth-service.js";
import { NotificationDefine } from "@certd/pipeline";
import { checkPlus } from "@certd/plus-core";
import { ApiTags } from "@midwayjs/swagger";
2026-07-10 19:24:42 +08:00
import { AuditType, AuditAction } from "../../../modules/sys/enterprise/service/audit-constants.js";
2024-11-22 17:12:39 +08:00
/**
* 通知
*/
@Provide()
2026-05-31 01:41:33 +08:00
@Controller("/api/pi/notification")
@ApiTags(["pipeline-notification"])
2024-11-22 17:12:39 +08:00
export class NotificationController extends CrudController<NotificationService> {
@Inject()
service: NotificationService;
@Inject()
authService: AuthService;
getService(): NotificationService {
return this.service;
}
2026-05-31 01:41:33 +08:00
@Post("/page", { description: Constants.per.authOnly, summary: "查询通知配置分页列表" })
2024-11-22 17:12:39 +08:00
async page(@Body(ALL) body) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
2024-11-22 17:12:39 +08:00
body.query = body.query ?? {};
delete body.query.userId;
2026-02-13 21:28:17 +08:00
body.query.projectId = projectId;
2024-11-22 17:12:39 +08:00
const buildQuery = qb => {
2026-05-31 01:41:33 +08:00
qb.andWhere("user_id = :userId", { userId: userId });
2024-11-22 17:12:39 +08:00
};
const res = await this.service.page({
query: body.query,
page: body.page,
sort: body.sort,
buildQuery,
});
return this.ok(res);
}
2026-05-31 01:41:33 +08:00
@Post("/list", { description: Constants.per.authOnly, summary: "查询通知配置列表" })
2024-11-22 17:12:39 +08:00
async list(@Body(ALL) body) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
body.query = body.query ?? {};
2026-02-13 21:28:17 +08:00
body.query.userId = userId;
body.query.projectId = projectId;
2024-11-22 17:12:39 +08:00
return super.list(body);
}
2026-05-31 01:41:33 +08:00
@Post("/add", { description: Constants.per.authOnly, summary: "添加通知配置" })
2024-11-22 17:12:39 +08:00
async add(@Body(ALL) bean) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
2026-02-13 21:28:17 +08:00
bean.userId = userId;
bean.projectId = projectId;
2024-11-30 01:57:09 +08:00
const type = bean.type;
const define: NotificationDefine = this.service.getDefineByType(type);
if (!define) {
2026-05-31 01:41:33 +08:00
throw new ValidateException("通知类型不存在");
2024-11-30 01:57:09 +08:00
}
if (define.needPlus) {
checkPlus();
}
2026-07-10 19:24:42 +08:00
const res = await super.add(bean);
this.auditLog({
type: AuditType.notification,
action: AuditAction.add,
content: `新增了通知配置「${bean.name}」(ID:${res.data}, 类型:${bean.type})`,
});
return res;
2024-11-22 17:12:39 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/update", { description: Constants.per.authOnly, summary: "更新通知配置" })
2024-11-22 17:12:39 +08:00
async update(@Body(ALL) bean) {
2026-05-31 01:41:33 +08:00
await this.checkOwner(this.getService(), bean.id, "write");
2024-11-30 01:57:09 +08:00
const old = await this.service.info(bean.id);
if (!old) {
2026-05-31 01:41:33 +08:00
throw new ValidateException("通知配置不存在");
2024-11-30 01:57:09 +08:00
}
if (old.type !== bean.type) {
const type = bean.type;
const define: NotificationDefine = this.service.getDefineByType(type);
if (!define) {
2026-05-31 01:41:33 +08:00
throw new ValidateException("通知类型不存在");
2024-11-30 01:57:09 +08:00
}
if (define.needPlus) {
checkPlus();
}
}
delete bean.userId;
2026-02-13 21:28:17 +08:00
delete bean.projectId;
2026-07-10 19:24:42 +08:00
const res = await super.update(bean);
this.auditLog({
type: AuditType.notification,
action: AuditAction.update,
content: `修改了通知配置「${bean.name}」(ID:${bean.id})`,
});
return res;
2024-11-22 17:12:39 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/info", { description: Constants.per.authOnly, summary: "查询通知配置详情" })
async info(@Query("id") id: number) {
await this.checkOwner(this.getService(), id, "read");
2024-11-22 17:12:39 +08:00
return super.info(id);
}
2026-05-31 01:41:33 +08:00
@Post("/delete", { description: Constants.per.authOnly, summary: "删除通知配置" })
async delete(@Query("id") id: number) {
await this.checkOwner(this.getService(), id, "write");
2026-07-10 19:24:42 +08:00
const res = await super.delete(id);
this.auditLog({
type: AuditType.notification,
action: AuditAction.delete,
content: `删除了通知配置(ID:${id})`,
});
return res;
2024-11-22 17:12:39 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/define", { description: Constants.per.authOnly, summary: "查询通知插件定义" })
async define(@Query("type") type: string) {
2024-11-22 17:12:39 +08:00
const notification = this.service.getDefineByType(type);
return this.ok(notification);
}
2026-05-31 01:41:33 +08:00
@Post("/getTypeDict", { description: Constants.per.authOnly, summary: "查询通知类型字典" })
2024-11-22 17:12:39 +08:00
async getTypeDict() {
2024-11-30 01:57:09 +08:00
const list: any = this.service.getDefineList();
let dict = [];
2024-11-22 17:12:39 +08:00
for (const item of list) {
dict.push({
value: item.name,
label: item.title,
2024-11-30 01:57:09 +08:00
needPlus: item.needPlus ?? false,
icon: item.icon,
2024-11-22 17:12:39 +08:00
});
}
2025-10-25 00:09:54 +08:00
dict = dict.sort(a => {
return a.order ? 0 : -1;
});
2024-11-30 01:57:09 +08:00
dict = dict.sort(a => {
return a.needPlus ? 0 : -1;
});
2024-11-22 17:12:39 +08:00
return this.ok(dict);
}
2026-05-31 01:41:33 +08:00
@Post("/simpleInfo", { description: Constants.per.authOnly, summary: "查询通知配置简单信息" })
async simpleInfo(@Query("id") id: number) {
const { projectId, userId } = await this.getProjectUserIdRead();
if (id === 0) {
//获取默认
2026-05-31 01:41:33 +08:00
const res = await this.service.getDefault(userId, projectId);
if (!res) {
2026-05-31 01:41:33 +08:00
throw new ValidateException("默认通知配置不存在");
}
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);
2024-11-22 17:12:39 +08:00
const res = await this.service.getSimpleInfo(id);
return this.ok(res);
}
2026-05-31 01:41:33 +08:00
@Post("/getDefaultId", { description: Constants.per.authOnly, summary: "查询默认通知配置ID" })
async getDefaultId() {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
const res = await this.service.getDefault(userId, projectId);
return this.ok(res?.id);
}
2026-05-31 01:41:33 +08:00
@Post("/setDefault", { description: Constants.per.authOnly, summary: "设置默认通知配置" })
async setDefault(@Query("id") id: number) {
const { projectId, userId } = await this.getProjectUserIdRead();
await this.checkOwner(this.getService(), id, "write");
const res = await this.service.setDefault(id, userId, projectId);
return this.ok(res);
}
2024-12-02 14:06:55 +08:00
2026-05-31 01:41:33 +08:00
@Post("/getOrCreateDefault", { description: Constants.per.authOnly, summary: "获取或创建默认通知配置" })
async getOrCreateDefault(@Body("email") email: string) {
const { projectId, userId } = await this.getProjectUserIdRead();
const res = await this.service.getOrCreateDefault(email, userId, projectId);
2024-12-04 12:36:17 +08:00
return this.ok(res);
}
2026-05-31 01:41:33 +08:00
@Post("/options", { description: Constants.per.authOnly, summary: "查询通知配置选项" })
2024-12-02 14:06:55 +08:00
async options() {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
2024-12-02 14:06:55 +08:00
const res = await this.service.list({
query: {
2026-02-13 21:28:17 +08:00
userId: userId,
projectId: projectId,
2024-12-02 14:06:55 +08:00
},
});
for (const item of res) {
delete item.setting;
}
return this.ok(res);
}
2024-11-22 17:12:39 +08:00
}