fix: catch and log failed email attempts

This commit is contained in:
Henry
2026-02-20 15:42:07 +00:00
parent 8a9cb8fabf
commit 71ecf2e6d3

View File

@@ -7,8 +7,8 @@ import * as memberRepo from "@kan/db/repository/member.repo";
import * as subscriptionRepo from "@kan/db/repository/subscription.repo"; import * as subscriptionRepo from "@kan/db/repository/subscription.repo";
import * as userRepo from "@kan/db/repository/user.repo"; import * as userRepo from "@kan/db/repository/user.repo";
import * as workspaceRepo from "@kan/db/repository/workspace.repo"; import * as workspaceRepo from "@kan/db/repository/workspace.repo";
import { generateUID } from "@kan/shared/utils";
import { sendEmail } from "@kan/email"; import { sendEmail } from "@kan/email";
import { generateUID } from "@kan/shared/utils";
import { createStripeClient } from "@kan/stripe"; import { createStripeClient } from "@kan/stripe";
import { socialProvidersPlugin } from "./providers"; import { socialProvidersPlugin } from "./providers";
@@ -139,10 +139,11 @@ export function createPlugins(db: dbClient) {
let newSlug = workspace.publicId; let newSlug = workspace.publicId;
if (workspace.slug !== workspace.publicId) { if (workspace.slug !== workspace.publicId) {
const isPublicIdAvailable = await workspaceRepo.isWorkspaceSlugAvailable( const isPublicIdAvailable =
db, await workspaceRepo.isWorkspaceSlugAvailable(
workspace.publicId, db,
); workspace.publicId,
);
if (!isPublicIdAvailable) { if (!isPublicIdAvailable) {
newSlug = generateUID(); newSlug = generateUID();
} }
@@ -172,69 +173,77 @@ export function createPlugins(db: dbClient) {
magicLink({ magicLink({
expiresIn: 60 * 60 * 24 * 7, // 7 days expiresIn: 60 * 60 * 24 * 7, // 7 days
sendMagicLink: async ({ email, url }) => { sendMagicLink: async ({ email, url }) => {
const decodedUrl = decodeURIComponent(url); try {
console.log("Sending magic link to:", email, "URL:", url); const decodedUrl = decodeURIComponent(url);
console.log( console.log("Sending magic link to:", email, "URL:", url);
"Magic link contains invite:", console.log(
decodedUrl.includes("type=invite"), "Magic link contains invite:",
); decodedUrl.includes("type=invite"),
if (decodedUrl.includes("type=invite")) { );
let inviterName = ""; if (decodedUrl.includes("type=invite")) {
let workspaceName = ""; let inviterName = "";
let workspaceName = "";
try { try {
const urlObj = new URL(url); const urlObj = new URL(url);
const callbackUrl = urlObj.searchParams.get("callbackURL"); const callbackUrl = urlObj.searchParams.get("callbackURL");
if (callbackUrl) { if (callbackUrl) {
const callbackParams = new URL( const callbackParams = new URL(
callbackUrl, callbackUrl,
process.env.NEXT_PUBLIC_BASE_URL, process.env.NEXT_PUBLIC_BASE_URL,
).searchParams; ).searchParams;
const memberPublicId = callbackParams.get("memberPublicId"); const memberPublicId = callbackParams.get("memberPublicId");
if (memberPublicId) { if (memberPublicId) {
const member = await memberRepo.getByPublicId( const member = await memberRepo.getByPublicId(
db, db,
memberPublicId, memberPublicId,
); );
if (member) { if (member) {
const [workspace, inviter] = await Promise.all([ const [workspace, inviter] = await Promise.all([
workspaceRepo.getById(db, member.workspaceId), workspaceRepo.getById(db, member.workspaceId),
userRepo.getById(db, member.createdBy), userRepo.getById(db, member.createdBy),
]); ]);
if (workspace) workspaceName = workspace.name; if (workspace) workspaceName = workspace.name;
if (inviter) inviterName = inviter.name ?? ""; if (inviter) inviterName = inviter.name ?? "";
}
} }
} }
} catch (error) {
console.error("Failed to fetch invite details:", error);
} }
} catch (error) {
console.error("Failed to fetch invite details:", error);
}
await sendEmail( await sendEmail(
email,
workspaceName
? `Invitation to join the workspace ${workspaceName}`
: "Invitation to join workspace",
"JOIN_WORKSPACE",
{
magicLoginUrl: url,
inviterName,
workspaceName,
},
);
} else {
await sendEmail(
email,
process.env.NEXT_PUBLIC_WHITE_LABEL_HIDE_POWERED_BY === "true"
? "Sign in to your account"
: "Sign in to Kan",
"MAGIC_LINK",
{
magicLoginUrl: url,
},
);
}
} catch (error) {
console.error("Error sending magic link:", {
email, email,
workspaceName url,
? `Invitation to join the workspace ${workspaceName}` error,
: "Invitation to join workspace", });
"JOIN_WORKSPACE",
{
magicLoginUrl: url,
inviterName,
workspaceName,
},
);
} else {
await sendEmail(
email,
process.env.NEXT_PUBLIC_WHITE_LABEL_HIDE_POWERED_BY === "true"
? "Sign in to your account"
: "Sign in to Kan",
"MAGIC_LINK",
{
magicLoginUrl: url,
},
);
} }
}, },
}), }),