Update domain page UI

This commit is contained in:
KMKoushik
2024-04-13 07:34:13 +10:00
parent 3217387373
commit 293277ed32
9 changed files with 643 additions and 17 deletions

View File

@@ -13,6 +13,7 @@ import {
getDomain,
updateDomain,
} from "~/server/service/domain-service";
import { sendEmail } from "~/server/service/email-service";
export const domainRouter = createTRPCRouter({
createDomain: teamProcedure
@@ -61,4 +62,37 @@ export const domainRouter = createTRPCRouter({
await deleteDomain(input.id);
return { success: true };
}),
sendTestEmailFromDomain: teamProcedure
.input(z.object({ id: z.number() }))
.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: "Test mail",
text: "Hello this is a test mail",
html: "<p>Hello this is a test mail</p>",
});
}
),
});

View File

@@ -8,16 +8,16 @@ export async function sendEmail(
) {
const { to, from, subject, text, html, teamId } = emailContent;
const domains = await db.domain.findMany({ where: { teamId } });
const fromDomain = from.split("@")[1];
if (!fromDomain) {
throw new Error("From email is not valid");
}
const domain = domains.find((domain) => domain.name === fromDomain);
const domain = await db.domain.findFirst({
where: { teamId, name: fromDomain },
});
if (!domain) {
throw new Error("Domain not found. Add domain to unsend first");
throw new Error(
"Domain of from email is wrong. Use the email verified by unsend"
);
}
if (domain.status !== "SUCCESS") {