-
-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathdomain.ts
More file actions
113 lines (100 loc) · 2.92 KB
/
domain.ts
File metadata and controls
113 lines (100 loc) · 2.92 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { z } from "zod";
import {
createTRPCRouter,
teamProcedure,
protectedProcedure,
domainProcedure,
} from "~/server/api/trpc";
import { db } from "~/server/db";
import {
createDomain,
deleteDomain,
getDomain,
getDomains,
updateDomain,
setMailFromLabel,
} from "~/server/service/domain-service";
import { sendEmail } from "~/server/service/email-service";
import { SesSettingsService } from "~/server/service/ses-settings-service";
export const domainRouter = createTRPCRouter({
getAvailableRegions: protectedProcedure.query(async () => {
const settings = await SesSettingsService.getAllSettings();
return settings.map((setting) => setting.region);
}),
createDomain: teamProcedure
.input(z.object({ name: z.string(), region: z.string() }))
.mutation(async ({ ctx, input }) => {
return createDomain(
ctx.team.id,
input.name,
input.region,
ctx.team.sesTenantId ?? undefined
);
}),
startVerification: domainProcedure.mutation(async ({ ctx, input }) => {
await ctx.db.domain.update({
where: { id: input.id },
data: { isVerifying: true },
});
}),
domains: teamProcedure.query(async ({ ctx }) => {
return getDomains(ctx.team.id);
}),
getDomain: domainProcedure.query(async ({ input, ctx }) => {
return getDomain(input.id, ctx.team.id);
}),
updateDomain: domainProcedure
.input(
z.object({
clickTracking: z.boolean().optional(),
openTracking: z.boolean().optional(),
})
)
.mutation(async ({ input }) => {
return updateDomain(input.id, {
clickTracking: input.clickTracking,
openTracking: input.openTracking,
});
}),
setMailFromLabel: domainProcedure
.input(
z.object({
id: z.number(),
mailFromLabel: z.string().max(63).nullable(),
}),
)
.mutation(async ({ ctx, input }) => {
return setMailFromLabel(input.id, ctx.team.id, input.mailFromLabel);
}),
deleteDomain: domainProcedure.mutation(async ({ input }) => {
await deleteDomain(input.id);
return { success: true };
}),
sendTestEmailFromDomain: domainProcedure.mutation(
async ({
ctx: {
session: { user },
team,
},
input,
}) => {
const domain = await db.domain.findFirst({
where: { id: input.id, teamId: team.id },
});
if (!domain) {
throw new Error("Domain not found");
}
if (!user.email) {
throw new Error("User email not found");
}
return sendEmail({
teamId: team.id,
to: user.email,
from: `hello@${domain.name}`,
subject: "useSend test email",
text: "hello,\n\nuseSend is the best open source sending platform\n\ncheck out https://usesend.com",
html: "<p>hello,</p><p>useSend is the best open source sending platform<p><p>check out <a href='https://usesend.com'>usesend.com</a>",
});
}
),
});