Team/user CRMFS export + scoped chat

This commit is contained in:
Ruslan Bakiev
2026-02-18 09:37:48 +07:00
parent 513a394b93
commit a8db021597
17 changed files with 1872 additions and 23 deletions

View File

@@ -0,0 +1,57 @@
import { prisma } from "./prisma";
import type { H3Event } from "h3";
export type AuthContext = {
teamId: string;
userId: string;
conversationId: string;
};
// Minimal temporary auth: pick from headers or auto-provision a default team/user.
export async function getAuthContext(event: H3Event): Promise<AuthContext> {
const hdrTeam = getHeader(event, "x-team-id")?.trim();
const hdrUser = getHeader(event, "x-user-id")?.trim();
const hdrConv = getHeader(event, "x-conversation-id")?.trim();
// Ensure default team/user exist.
const user =
(hdrUser ? await prisma.user.findUnique({ where: { id: hdrUser } }) : null) ??
(await prisma.user.upsert({
where: { id: "demo-user" },
update: { email: "demo@clientsflow.local", name: "Demo User" },
create: { id: "demo-user", email: "demo@clientsflow.local", name: "Demo User" },
}));
const team =
(hdrTeam
? await prisma.team.findUnique({ where: { id: hdrTeam } })
: null) ??
(await prisma.team.upsert({
where: { id: "demo-team" },
update: { name: "Demo Team" },
create: { id: "demo-team", name: "Demo Team" },
}));
await prisma.teamMember.upsert({
where: { teamId_userId: { teamId: team.id, userId: user.id } },
update: {},
create: { teamId: team.id, userId: user.id, role: "OWNER" },
});
const conversation =
(hdrConv
? await prisma.chatConversation.findUnique({ where: { id: hdrConv } })
: null) ??
(await prisma.chatConversation.upsert({
where: { id: `pilot-${team.id}` },
update: {},
create: {
id: `pilot-${team.id}`,
teamId: team.id,
createdByUserId: user.id,
title: "Pilot",
},
}));
return { teamId: team.id, userId: user.id, conversationId: conversation.id };
}