-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathemail.controller.ts
More file actions
75 lines (68 loc) · 2.39 KB
/
Copy pathemail.controller.ts
File metadata and controls
75 lines (68 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Body, Controller, HttpCode, Post, UseGuards } from '@nestjs/common';
import {
ApiExcludeController,
ApiOperation,
ApiResponse,
ApiSecurity,
ApiTags,
} from '@nestjs/swagger';
import { tasks } from '@trigger.dev/sdk';
import { HybridAuthGuard } from '../auth/hybrid-auth.guard';
import { PermissionGuard } from '../auth/permission.guard';
import { RequirePermission } from '../auth/require-permission.decorator';
import { SendEmailDto } from './dto/send-email.dto';
import { SendBatchEmailDto } from './dto/send-batch-email.dto';
import type { sendEmailTask } from '../trigger/email/send-email';
import type { sendBatchEmailTask } from '../trigger/email/send-batch-email';
@ApiExcludeController()
@ApiTags('Internal - Email')
@Controller({ path: 'internal/email', version: '1' })
@UseGuards(HybridAuthGuard, PermissionGuard)
@ApiSecurity('apikey')
export class EmailController {
@Post('send')
@HttpCode(200)
@RequirePermission('email', 'send')
@ApiOperation({
summary: 'Send an email via the centralized Trigger task (internal)',
})
@ApiResponse({ status: 200, description: 'Email task triggered' })
async sendEmail(@Body() dto: SendEmailDto) {
const fromAddress = dto.system
? (process.env.RESEND_FROM_SYSTEM ?? process.env.RESEND_FROM_DEFAULT)
: (dto.from ?? process.env.RESEND_FROM_DEFAULT);
const handle = await tasks.trigger<typeof sendEmailTask>('send-email', {
to: dto.to,
subject: dto.subject,
html: dto.html,
from: fromAddress,
cc: dto.cc,
scheduledAt: dto.scheduledAt,
attachments: dto.attachments,
});
return { success: true, taskId: handle.id };
}
@Post('send-batch')
@HttpCode(200)
@RequirePermission('email', 'send')
@ApiOperation({
summary: 'Send a batch of emails via the centralized Trigger task (internal)',
})
@ApiResponse({ status: 200, description: 'Batch email task triggered' })
async sendBatchEmail(@Body() dto: SendBatchEmailDto) {
const fromAddress =
process.env.RESEND_FROM_SYSTEM ?? process.env.RESEND_FROM_DEFAULT;
const emails = dto.emails.map((email) => ({
to: email.to,
subject: email.subject,
html: email.html,
from: email.from ?? fromAddress,
cc: email.cc,
}));
const handle = await tasks.trigger<typeof sendBatchEmailTask>(
'send-batch-email',
{ emails },
);
return { success: true, taskId: handle.id };
}
}