Initial push of Plunk Next
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
import {Controller, Delete, Get, Middleware, Patch, Post} from '@overnightjs/core';
|
||||
import {TemplateType} from '@plunk/db';
|
||||
import type {Request, Response} from 'express';
|
||||
|
||||
import type {AuthResponse} from '../middleware/auth.js';
|
||||
import {requireAuth} from '../middleware/auth.js';
|
||||
import {DomainService} from '../services/DomainService.js';
|
||||
import {TemplateService} from '../services/TemplateService.js';
|
||||
|
||||
@Controller('templates')
|
||||
export class Templates {
|
||||
/**
|
||||
* GET /templates
|
||||
* List all templates for the authenticated project
|
||||
*/
|
||||
@Get('')
|
||||
@Middleware([requireAuth])
|
||||
public async list(req: Request, res: Response) {
|
||||
const auth = res.locals.auth as AuthResponse;
|
||||
const page = parseInt(req.query.page as string) || 1;
|
||||
const pageSize = Math.min(parseInt(req.query.pageSize as string) || 20, 100);
|
||||
const search = req.query.search as string | undefined;
|
||||
const type = req.query.type as TemplateType | undefined;
|
||||
|
||||
const result = await TemplateService.list(auth.projectId!, page, pageSize, search, type);
|
||||
|
||||
return res.status(200).json(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /templates/:id
|
||||
* Get a specific template by ID
|
||||
*/
|
||||
@Get(':id')
|
||||
@Middleware([requireAuth])
|
||||
public async get(req: Request, res: Response) {
|
||||
const auth = res.locals.auth as AuthResponse;
|
||||
const templateId = req.params.id;
|
||||
|
||||
if (!templateId) {
|
||||
return res.status(400).json({error: 'Template ID is required'});
|
||||
}
|
||||
|
||||
const template = await TemplateService.get(auth.projectId!, templateId);
|
||||
|
||||
return res.status(200).json(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /templates
|
||||
* Create a new template
|
||||
*/
|
||||
@Post('')
|
||||
@Middleware([requireAuth])
|
||||
public async create(req: Request, res: Response) {
|
||||
const auth = res.locals.auth as AuthResponse;
|
||||
const {name, description, subject, body, from, fromName, replyTo, type} = req.body;
|
||||
|
||||
if (!name) {
|
||||
return res.status(400).json({error: 'Name is required'});
|
||||
}
|
||||
|
||||
if (!subject) {
|
||||
return res.status(400).json({error: 'Subject is required'});
|
||||
}
|
||||
|
||||
if (!body) {
|
||||
return res.status(400).json({error: 'Body is required'});
|
||||
}
|
||||
|
||||
if (!from) {
|
||||
return res.status(400).json({error: 'From address is required'});
|
||||
}
|
||||
|
||||
// Verify domain ownership and verification
|
||||
await DomainService.verifyEmailDomain(from, auth.projectId!);
|
||||
|
||||
const template = await TemplateService.create(auth.projectId!, {
|
||||
name,
|
||||
description,
|
||||
subject,
|
||||
body,
|
||||
from,
|
||||
fromName,
|
||||
replyTo,
|
||||
type,
|
||||
});
|
||||
|
||||
return res.status(201).json(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH /templates/:id
|
||||
* Update a template
|
||||
*/
|
||||
@Patch(':id')
|
||||
@Middleware([requireAuth])
|
||||
public async update(req: Request, res: Response) {
|
||||
const auth = res.locals.auth as AuthResponse;
|
||||
const templateId = req.params.id;
|
||||
const {name, description, subject, body, from, fromName, replyTo, type} = req.body;
|
||||
|
||||
if (!templateId) {
|
||||
return res.status(400).json({error: 'Template ID is required'});
|
||||
}
|
||||
|
||||
// Verify domain ownership and verification if 'from' is being updated
|
||||
if (from) {
|
||||
await DomainService.verifyEmailDomain(from, auth.projectId!);
|
||||
}
|
||||
|
||||
const template = await TemplateService.update(auth.projectId!, templateId, {
|
||||
name,
|
||||
description,
|
||||
subject,
|
||||
body,
|
||||
from,
|
||||
fromName,
|
||||
replyTo,
|
||||
type,
|
||||
});
|
||||
|
||||
return res.status(200).json(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /templates/:id
|
||||
* Delete a template
|
||||
*/
|
||||
@Delete(':id')
|
||||
@Middleware([requireAuth])
|
||||
public async delete(req: Request, res: Response) {
|
||||
const auth = res.locals.auth as AuthResponse;
|
||||
const templateId = req.params.id;
|
||||
|
||||
if (!templateId) {
|
||||
return res.status(400).json({error: 'Template ID is required'});
|
||||
}
|
||||
|
||||
await TemplateService.delete(auth.projectId!, templateId);
|
||||
|
||||
return res.status(204).send();
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /templates/:id/duplicate
|
||||
* Duplicate a template
|
||||
*/
|
||||
@Post(':id/duplicate')
|
||||
@Middleware([requireAuth])
|
||||
public async duplicate(req: Request, res: Response) {
|
||||
const auth = res.locals.auth as AuthResponse;
|
||||
const templateId = req.params.id;
|
||||
|
||||
if (!templateId) {
|
||||
return res.status(400).json({error: 'Template ID is required'});
|
||||
}
|
||||
|
||||
const template = await TemplateService.duplicate(auth.projectId!, templateId);
|
||||
|
||||
return res.status(201).json(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /templates/:id/usage
|
||||
* Get template usage statistics
|
||||
*/
|
||||
@Get(':id/usage')
|
||||
@Middleware([requireAuth])
|
||||
public async getUsage(req: Request, res: Response) {
|
||||
const auth = res.locals.auth as AuthResponse;
|
||||
const templateId = req.params.id;
|
||||
|
||||
if (!templateId) {
|
||||
return res.status(400).json({error: 'Template ID is required'});
|
||||
}
|
||||
|
||||
const usage = await TemplateService.getUsage(auth.projectId!, templateId);
|
||||
|
||||
return res.status(200).json(usage);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user