import type {Template} from '@plunk/db'; import {Prisma} from '@plunk/db'; import type {PaginatedResponse} from '@plunk/types'; import {prisma} from '../database/prisma.js'; import {HttpException} from '../exceptions/index.js'; import {buildEmailFieldsUpdate} from '../utils/modelUpdate.js'; export class TemplateService { /** * Get all templates for a project with pagination */ public static async list( projectId: string, page = 1, pageSize = 20, search?: string, type?: Template['type'], ): Promise> { const skip = (page - 1) * pageSize; const where: Prisma.TemplateWhereInput = { projectId, ...(type ? {type} : {}), ...(search ? { OR: [ {name: {contains: search, mode: 'insensitive' as const}}, {description: {contains: search, mode: 'insensitive' as const}}, {subject: {contains: search, mode: 'insensitive' as const}}, ], } : {}), }; const [templates, total] = await Promise.all([ prisma.template.findMany({ where, skip, take: pageSize, orderBy: {createdAt: 'desc'}, }), prisma.template.count({where}), ]); return { data: templates, total, page, pageSize, totalPages: Math.ceil(total / pageSize), }; } /** * Get a single template by ID */ public static async get(projectId: string, templateId: string): Promise