Fix tenant creation page error handling and logging
Some checks failed
CI - Production Readiness / Verify (push) Has been cancelled

This commit is contained in:
2026-04-21 20:02:38 +07:00
parent 311551c31a
commit f48c87e36d
2 changed files with 73 additions and 26 deletions

View File

@ -1153,13 +1153,13 @@ export async function createTenant(formData: FormData) {
const session = await requireSuperAdminSession();
const auditContext = await getAuditContext(session);
const name = formValue(formData, "name");
const companyName = formValue(formData, "companyName") || name;
const slug = formValue(formData, "slug").toLowerCase();
const timezone = formValue(formData, "timezone");
const name = formValue(formData, "name").trim();
const companyName = (formValue(formData, "companyName") || name).trim();
const slug = formValue(formData, "slug").trim().toLowerCase();
const timezone = formValue(formData, "timezone").trim();
const planId = formValue(formData, "planId");
const adminFullName = formValue(formData, "adminFullName") || `Admin ${name}`;
const adminEmail = formValue(formData, "adminEmail");
const adminFullName = (formValue(formData, "adminFullName") || `Admin ${name}`).trim();
const adminEmail = formValue(formData, "adminEmail").trim() || undefined;
const adminPassword = formValue(formData, "adminPassword");
if (!name || !slug || !timezone || !planId) {
@ -1191,17 +1191,33 @@ export async function createTenant(formData: FormData) {
adminEmail: string;
};
const tenantCreation = await prisma.$transaction(async (tx) => {
const createdTenant = await tx.tenant.create({
data: {
name,
companyName: companyName || name,
slug,
timezone,
status: TenantStatus.ACTIVE,
planId
}
});
let tenantCreation: {
tenant: {
id: string;
name: string;
slug: string;
planId: string;
timezone: string;
status: TenantStatus;
createdAt: Date;
updatedAt: Date;
companyName: string;
};
adminInvite: AdminInvitePayload | null;
};
try {
tenantCreation = await prisma.$transaction(async (tx) => {
const createdTenant = await tx.tenant.create({
data: {
name,
companyName: companyName || name,
slug,
timezone,
status: TenantStatus.ACTIVE,
planId
}
});
if (adminEmail) {
const shouldInviteAdmin = !adminPassword;
@ -1243,11 +1259,21 @@ export async function createTenant(formData: FormData) {
}
}
return {
tenant: createdTenant,
adminInvite: null
};
});
return {
tenant: createdTenant,
adminInvite: null
};
});
} catch (error) {
console.error("[createTenant] transaction failed", {
actorUserId: session.userId,
tenantName: name,
tenantSlug: slug,
planId,
error: error instanceof Error ? error.message : String(error)
});
redirect("/super-admin/tenants/new?error=tenant_creation_failed");
}
const tenant = tenantCreation.tenant;
const adminInvite = tenantCreation.adminInvite;