116 lines
4.0 KiB
TypeScript
116 lines
4.0 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import type {
|
|
CreateWecomMessagePushRequest,
|
|
UpdateWecomMessagePushRequest,
|
|
WecomMessagePushDto,
|
|
} from '@dukang/shared-types';
|
|
import { PrismaService } from '../../common/prisma/prisma.module';
|
|
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
|
|
import { WecomMessagePushService } from '../../integrations/wecom/wecom-message-push.service';
|
|
|
|
@Injectable()
|
|
export class AdminWecomMessagePushesService {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly pushService: WecomMessagePushService,
|
|
) {}
|
|
|
|
async list(query: { name?: string; enabled?: string; page?: number; pageSize?: number }) {
|
|
const page = query.page ?? 1;
|
|
const pageSize = query.pageSize ?? 20;
|
|
const where: { name?: { contains: string }; enabled?: boolean } = {};
|
|
if (query.name?.trim()) where.name = { contains: query.name.trim() };
|
|
if (query.enabled === 'true' || query.enabled === 'false') {
|
|
where.enabled = query.enabled === 'true';
|
|
}
|
|
|
|
const [items, total] = await Promise.all([
|
|
this.prisma.wecomMessagePush.findMany({
|
|
where,
|
|
orderBy: [{ sortOrder: 'asc' }, { id: 'asc' }],
|
|
skip: (page - 1) * pageSize,
|
|
take: pageSize,
|
|
}),
|
|
this.prisma.wecomMessagePush.count({ where }),
|
|
]);
|
|
|
|
return serializeBigInt({
|
|
items: items.map((row) => this.pushService.toDto(row)),
|
|
total,
|
|
page,
|
|
pageSize,
|
|
});
|
|
}
|
|
|
|
async detail(id: bigint): Promise<WecomMessagePushDto> {
|
|
const row = await this.prisma.wecomMessagePush.findUnique({ where: { id } });
|
|
if (!row) throw new NotFoundException('消息推送不存在');
|
|
return this.pushService.toDto(row);
|
|
}
|
|
|
|
async create(dto: CreateWecomMessagePushRequest) {
|
|
const name = dto.name?.trim();
|
|
const webhookUrl = dto.webhookUrl?.trim();
|
|
if (!name) throw new BadRequestException('请填写名称');
|
|
if (!webhookUrl) throw new BadRequestException('请填写 Webhook URL');
|
|
|
|
const pushConditions = this.pushService.validatePushConditions(dto.pushConditions);
|
|
const row = await this.prisma.wecomMessagePush.create({
|
|
data: {
|
|
name,
|
|
avatarUrl: dto.avatarUrl?.trim() || null,
|
|
webhookUrl,
|
|
enabled: dto.enabled !== false,
|
|
mentionWecomUserId: dto.mentionWecomUserId?.trim() || null,
|
|
pushConditions: JSON.stringify(pushConditions),
|
|
sortOrder: dto.sortOrder ?? 0,
|
|
},
|
|
});
|
|
return this.pushService.toDto(row);
|
|
}
|
|
|
|
async update(id: bigint, dto: UpdateWecomMessagePushRequest) {
|
|
const existing = await this.prisma.wecomMessagePush.findUnique({ where: { id } });
|
|
if (!existing) throw new NotFoundException('消息推送不存在');
|
|
|
|
let pushConditionsJson = existing.pushConditions;
|
|
if (dto.pushConditions !== undefined) {
|
|
const parsed = this.pushService.validatePushConditions(dto.pushConditions);
|
|
pushConditionsJson = JSON.stringify(parsed);
|
|
}
|
|
|
|
const row = await this.prisma.wecomMessagePush.update({
|
|
where: { id },
|
|
data: {
|
|
name: dto.name !== undefined ? dto.name.trim() : undefined,
|
|
avatarUrl:
|
|
dto.avatarUrl === undefined ? undefined : dto.avatarUrl?.trim() || null,
|
|
webhookUrl: dto.webhookUrl !== undefined ? dto.webhookUrl.trim() : undefined,
|
|
enabled: dto.enabled,
|
|
mentionWecomUserId:
|
|
dto.mentionWecomUserId === undefined
|
|
? undefined
|
|
: dto.mentionWecomUserId?.trim() || null,
|
|
pushConditions: pushConditionsJson,
|
|
sortOrder: dto.sortOrder,
|
|
},
|
|
});
|
|
return this.pushService.toDto(row);
|
|
}
|
|
|
|
async remove(id: bigint) {
|
|
const existing = await this.prisma.wecomMessagePush.findUnique({ where: { id } });
|
|
if (!existing) throw new NotFoundException('消息推送不存在');
|
|
await this.prisma.wecomMessagePush.delete({ where: { id } });
|
|
return { ok: true };
|
|
}
|
|
|
|
test(id: bigint) {
|
|
return this.pushService.sendTest(id);
|
|
}
|
|
}
|