Add org-scoped reusable SMS templates in the compose modal.
Users can save, pick, and delete prepared messages shared across the organization. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "Organization" ADD COLUMN "smsTemplates" JSONB NOT NULL DEFAULT '[]';
|
||||
@@ -164,6 +164,8 @@ model Organization {
|
||||
enabledModules String[] @default([])
|
||||
/// برچسب/رنگ اولویت فعالیت و روتین (کلیدهای enum ثابت؛ فقط ظاهر)
|
||||
activityPriorities Json @default("{}")
|
||||
/// قالبهای آمادهٔ پیامک دستی ([{ id, name, body }, …])
|
||||
smsTemplates Json @default("[]")
|
||||
/// تاریخ قطع بایگانی اطلاعات قبل (شروع روز انتخابشده)؛ null = بدون فیلتر
|
||||
priorArchiveEnabledAt DateTime?
|
||||
isActive Boolean @default(true)
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { OutboundSmsLogService } from './outbound-sms-log.service';
|
||||
import { SmsTemplatesController } from './sms-templates.controller';
|
||||
import { SmsTemplatesService } from './sms-templates.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [OutboundSmsLogService],
|
||||
exports: [OutboundSmsLogService],
|
||||
controllers: [SmsTemplatesController],
|
||||
providers: [OutboundSmsLogService, SmsTemplatesService],
|
||||
exports: [OutboundSmsLogService, SmsTemplatesService],
|
||||
})
|
||||
export class OutboundSmsModule {}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import type { Actor } from '../common/record-scope';
|
||||
import {
|
||||
CreateSmsTemplateDto,
|
||||
UpdateSmsTemplateDto,
|
||||
} from './sms-templates.dto';
|
||||
import { SmsTemplatesService } from './sms-templates.service';
|
||||
|
||||
@Controller('sms-templates')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class SmsTemplatesController {
|
||||
constructor(private readonly templates: SmsTemplatesService) {}
|
||||
|
||||
@Get()
|
||||
list(@CurrentUser() user: Actor) {
|
||||
return this.templates.list(user.organizationId);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@CurrentUser() user: Actor, @Body() dto: CreateSmsTemplateDto) {
|
||||
return this.templates.create(user.organizationId, dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(
|
||||
@CurrentUser() user: Actor,
|
||||
@Param('id') id: string,
|
||||
@Body() dto: UpdateSmsTemplateDto,
|
||||
) {
|
||||
return this.templates.update(user.organizationId, id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@CurrentUser() user: Actor, @Param('id') id: string) {
|
||||
return this.templates.remove(user.organizationId, id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { IsOptional, IsString, MaxLength, MinLength } from 'class-validator';
|
||||
import { SMS_TEMPLATE_BODY_MAX, SMS_TEMPLATE_NAME_MAX } from '@mento/shared';
|
||||
|
||||
export class CreateSmsTemplateDto {
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
@MaxLength(SMS_TEMPLATE_NAME_MAX)
|
||||
name!: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(2)
|
||||
@MaxLength(SMS_TEMPLATE_BODY_MAX)
|
||||
body!: string;
|
||||
}
|
||||
|
||||
export class UpdateSmsTemplateDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
@MaxLength(SMS_TEMPLATE_NAME_MAX)
|
||||
name?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(2)
|
||||
@MaxLength(SMS_TEMPLATE_BODY_MAX)
|
||||
body?: string;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user