Files
certd/packages/ui/certd-server/src/modules/pipeline/service/notification-service.ts
T

128 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-11-22 17:12:39 +08:00
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { BaseService, ValidateException } from '@certd/lib-server';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { NotificationEntity } from '../entity/notification.js';
import { NotificationInstanceConfig, notificationRegistry } from '@certd/pipeline';
2024-11-22 17:12:39 +08:00
@Provide()
@Scope(ScopeEnum.Singleton)
export class NotificationService extends BaseService<NotificationEntity> {
@InjectEntityModel(NotificationEntity)
repository: Repository<NotificationEntity>;
//@ts-ignore
getRepository() {
return this.repository;
}
async getSimpleInfo(id: number) {
const entity = await this.info(id);
if (entity == null) {
throw new ValidateException('该通知配置不存在,请确认是否已被删除');
}
return {
id: entity.id,
name: entity.name,
userId: entity.userId,
};
}
getDefineList() {
return notificationRegistry.getDefineList();
}
getDefineByType(type: string) {
return notificationRegistry.getDefine(type);
}
async getById(id: number, userId: number): Promise<NotificationInstanceConfig> {
if (!id) {
throw new ValidateException('id不能为空');
}
if (!userId) {
throw new ValidateException('userId不能为空');
}
const res = await this.repository.findOne({
where: {
id,
userId,
},
});
if (!res) {
throw new ValidateException(`通知配置不存在<${id}>`);
}
return this.buildNotificationInstanceConfig(res);
}
private buildNotificationInstanceConfig(res: NotificationEntity) {
const setting = JSON.parse(res.setting);
return {
id: res.id,
type: res.type,
name: res.name,
userId: res.userId,
setting,
};
}
async getDefault(userId: number): Promise<NotificationInstanceConfig> {
const res = await this.repository.findOne({
where: {
userId,
},
order: {
isDefault: 'DESC',
},
});
if (!res) {
2024-12-01 03:02:59 +08:00
return null;
}
return this.buildNotificationInstanceConfig(res);
}
async setDefault(id: number, userId: number) {
if (!id) {
throw new ValidateException('id不能为空');
}
if (!userId) {
throw new ValidateException('userId不能为空');
}
await this.repository.update(
{
userId,
},
{
isDefault: false,
}
);
await this.repository.update(
{
id,
userId,
},
{
isDefault: true,
}
);
}
2024-12-04 12:36:17 +08:00
async getOrCreateDefault(email: string, userId: any) {
const defaultConfig = await this.getDefault(userId);
if (defaultConfig) {
return defaultConfig;
}
const setting = {
receivers: [email],
};
const res = await this.repository.save({
userId,
type: 'email',
name: '邮件通知',
setting: JSON.stringify(setting),
isDefault: true,
});
return this.buildNotificationInstanceConfig(res);
}
2024-11-22 17:12:39 +08:00
}