93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
import {
|
|||
|
|
BadRequestException,
|
||
|
|
Injectable,
|
||
|
|
NotFoundException,
|
||
|
|
} from '@nestjs/common';
|
||
|
|
import { randomUUID } from 'crypto';
|
||
|
|
import type { Prisma } from '@prisma/client';
|
||
|
|
import {
|
||
|
|
parseSmsTemplates,
|
||
|
|
SMS_TEMPLATE_MAX,
|
||
|
|
type SmsTemplateDto,
|
||
|
|
} from '@mento/shared';
|
||
|
|
import { PrismaService } from '../prisma/prisma.service';
|
||
|
|
import type {
|
||
|
|
CreateSmsTemplateDto,
|
||
|
|
UpdateSmsTemplateDto,
|
||
|
|
} from './sms-templates.dto';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class SmsTemplatesService {
|
||
|
|
constructor(private readonly prisma: PrismaService) {}
|
||
|
|
|
||
|
|
async list(organizationId: string): Promise<SmsTemplateDto[]> {
|
||
|
|
const row = await this.prisma.organization.findUnique({
|
||
|
|
where: { id: organizationId },
|
||
|
|
select: { smsTemplates: true },
|
||
|
|
});
|
||
|
|
return parseSmsTemplates(row?.smsTemplates);
|
||
|
|
}
|
||
|
|
|
||
|
|
async create(
|
||
|
|
organizationId: string,
|
||
|
|
dto: CreateSmsTemplateDto,
|
||
|
|
): Promise<SmsTemplateDto[]> {
|
||
|
|
const current = await this.list(organizationId);
|
||
|
|
if (current.length >= SMS_TEMPLATE_MAX) {
|
||
|
|
throw new BadRequestException(
|
||
|
|
`حداکثر ${SMS_TEMPLATE_MAX} پیامک آماده میتوانید داشته باشید.`,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
const next: SmsTemplateDto[] = [
|
||
|
|
...current,
|
||
|
|
{
|
||
|
|
id: randomUUID(),
|
||
|
|
name: dto.name.trim(),
|
||
|
|
body: dto.body.trim(),
|
||
|
|
},
|
||
|
|
];
|
||
|
|
return this.persist(organizationId, next);
|
||
|
|
}
|
||
|
|
|
||
|
|
async update(
|
||
|
|
organizationId: string,
|
||
|
|
id: string,
|
||
|
|
dto: UpdateSmsTemplateDto,
|
||
|
|
): Promise<SmsTemplateDto[]> {
|
||
|
|
const current = await this.list(organizationId);
|
||
|
|
const idx = current.findIndex((item) => item.id === id);
|
||
|
|
if (idx < 0) throw new NotFoundException('پیامک آماده پیدا نشد.');
|
||
|
|
const prev = current[idx]!;
|
||
|
|
const next = [...current];
|
||
|
|
next[idx] = {
|
||
|
|
id: prev.id,
|
||
|
|
name: dto.name !== undefined ? dto.name.trim() : prev.name,
|
||
|
|
body: dto.body !== undefined ? dto.body.trim() : prev.body,
|
||
|
|
};
|
||
|
|
return this.persist(organizationId, next);
|
||
|
|
}
|
||
|
|
|
||
|
|
async remove(organizationId: string, id: string): Promise<SmsTemplateDto[]> {
|
||
|
|
const current = await this.list(organizationId);
|
||
|
|
if (!current.some((item) => item.id === id)) {
|
||
|
|
throw new NotFoundException('پیامک آماده پیدا نشد.');
|
||
|
|
}
|
||
|
|
return this.persist(
|
||
|
|
organizationId,
|
||
|
|
current.filter((item) => item.id !== id),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
private async persist(
|
||
|
|
organizationId: string,
|
||
|
|
items: SmsTemplateDto[],
|
||
|
|
): Promise<SmsTemplateDto[]> {
|
||
|
|
const stored = parseSmsTemplates(items);
|
||
|
|
await this.prisma.organization.update({
|
||
|
|
where: { id: organizationId },
|
||
|
|
data: { smsTemplates: stored as unknown as Prisma.InputJsonValue },
|
||
|
|
});
|
||
|
|
return stored;
|
||
|
|
}
|
||
|
|
}
|