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

164 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-11-22 17:12:39 +08:00
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { Constants, CrudController, ValidateException } from '@certd/lib-server';
2024-11-22 17:12:39 +08:00
import { NotificationService } from '../../modules/pipeline/service/notification-service.js';
import { AuthService } from '../../modules/sys/authority/service/auth-service.js';
2024-11-30 01:57:09 +08:00
import { NotificationDefine } from '@certd/pipeline';
import { checkPlus } from '@certd/plus-core';
2024-11-22 17:12:39 +08:00
/**
*
*/
@Provide()
@Controller('/api/pi/notification')
export class NotificationController extends CrudController<NotificationService> {
@Inject()
service: NotificationService;
@Inject()
authService: AuthService;
getService(): NotificationService {
return this.service;
}
@Post('/page', { summary: Constants.per.authOnly })
async page(@Body(ALL) body) {
body.query = body.query ?? {};
delete body.query.userId;
const buildQuery = qb => {
qb.andWhere('user_id = :userId', { userId: this.getUserId() });
};
const res = await this.service.page({
query: body.query,
page: body.page,
sort: body.sort,
buildQuery,
});
return this.ok(res);
}
@Post('/list', { summary: Constants.per.authOnly })
async list(@Body(ALL) body) {
body.query = body.query ?? {};
body.query.userId = this.getUserId();
2024-11-22 17:12:39 +08:00
return super.list(body);
}
@Post('/add', { summary: Constants.per.authOnly })
async add(@Body(ALL) bean) {
bean.userId = this.getUserId();
2024-11-30 01:57:09 +08:00
const type = bean.type;
const define: NotificationDefine = this.service.getDefineByType(type);
if (!define) {
throw new ValidateException('通知类型不存在');
}
if (define.needPlus) {
checkPlus();
}
2024-11-22 17:12:39 +08:00
return super.add(bean);
}
@Post('/update', { summary: Constants.per.authOnly })
async update(@Body(ALL) bean) {
await this.service.checkUserId(bean.id, this.getUserId());
2024-11-30 01:57:09 +08:00
const old = await this.service.info(bean.id);
if (!old) {
throw new ValidateException('通知配置不存在');
}
if (old.type !== bean.type) {
const type = bean.type;
const define: NotificationDefine = this.service.getDefineByType(type);
if (!define) {
throw new ValidateException('通知类型不存在');
}
if (define.needPlus) {
checkPlus();
}
}
delete bean.userId;
2024-11-22 17:12:39 +08:00
return super.update(bean);
}
@Post('/info', { summary: Constants.per.authOnly })
async info(@Query('id') id: number) {
await this.service.checkUserId(id, this.getUserId());
return super.info(id);
}
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id: number) {
await this.service.checkUserId(id, this.getUserId());
return super.delete(id);
}
@Post('/define', { summary: Constants.per.authOnly })
async define(@Query('type') type: string) {
const notification = this.service.getDefineByType(type);
return this.ok(notification);
}
@Post('/getTypeDict', { summary: Constants.per.authOnly })
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
});
}
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);
}
@Post('/simpleInfo', { summary: Constants.per.authOnly })
async simpleInfo(@Query('id') id: number) {
if (id === 0) {
//获取默认
const res = await this.service.getDefault(this.getUserId());
if (!res) {
throw new ValidateException('默认通知配置不存在');
}
const simple = await this.service.getSimpleInfo(res.id);
return this.ok(simple);
}
2024-11-22 17:12:39 +08:00
await this.authService.checkEntityUserId(this.ctx, this.service, id);
const res = await this.service.getSimpleInfo(id);
return this.ok(res);
}
@Post('/getDefaultId', { summary: Constants.per.authOnly })
async getDefaultId() {
const res = await this.service.getDefault(this.getUserId());
return this.ok(res?.id);
}
@Post('/setDefault', { summary: Constants.per.authOnly })
async setDefault(@Query('id') id: number) {
await this.service.checkUserId(id, this.getUserId());
const res = await this.service.setDefault(id, this.getUserId());
return this.ok(res);
}
2024-12-02 14:06:55 +08:00
2024-12-04 12:36:17 +08:00
@Post('/getOrCreateDefault', { summary: Constants.per.authOnly })
async getOrCreateDefault(@Body('email') email: string) {
const res = await this.service.getOrCreateDefault(email, this.getUserId());
return this.ok(res);
}
2024-12-02 14:06:55 +08:00
@Post('/options', { summary: Constants.per.authOnly })
async options() {
const res = await this.service.list({
query: {
userId: this.getUserId(),
},
});
for (const item of res) {
delete item.setting;
}
return this.ok(res);
}
2024-11-22 17:12:39 +08:00
}