Team/user CRMFS export + scoped chat
This commit is contained in:
57
Frontend/server/utils/auth.ts
Normal file
57
Frontend/server/utils/auth.ts
Normal 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 };
|
||||
}
|
||||
Reference in New Issue
Block a user