fix: resolve user invitation unique constraint error and add integration tests

- Fix platform user invitation to handle global email uniqueness properly
- Replace findFirst with findUnique for email validation
- Add clear error messages for email conflicts across companies
- Create comprehensive CSV import workflow integration tests
- Create comprehensive session processing pipeline integration tests
- Cover end-to-end flows from import to AI analysis completion
This commit is contained in:
2025-07-11 16:30:43 +02:00
committed by Kaj Kowalski
parent fa7e815a3b
commit e7818f5e4f
3 changed files with 956 additions and 7 deletions

View File

@ -51,19 +51,36 @@ export async function POST(
);
}
// Check if user already exists in this company
const existingUser = await prisma.user.findFirst({
// Check if user already exists (emails must be globally unique)
const existingUser = await prisma.user.findUnique({
where: {
email,
companyId,
},
select: {
id: true,
companyId: true,
company: {
select: {
name: true,
},
},
},
});
if (existingUser) {
return NextResponse.json(
{ error: "User already exists in this company" },
{ status: 400 }
);
if (existingUser.companyId === companyId) {
return NextResponse.json(
{ error: "User already exists in this company" },
{ status: 400 }
);
} else {
return NextResponse.json(
{
error: `Email already in use by a user in company: ${existingUser.company.name}. Each email address can only be used once across all companies.`
},
{ status: 400 }
);
}
}
// Generate a temporary password (in a real app, you'd send an invitation email)