1- /** @type {import('./$types').PageServerLoad } */
2- export async function load ( ) {
3- return { } ;
4- } ;
1+ import { prisma } from '$lib/prisma' ;
2+ import { fail , redirect } from '@sveltejs/kit' ;
3+
4+ export const actions = {
5+ addUser : async ( { request, locals } ) => {
6+ if ( ! locals . org || ! locals . org . id ) {
7+ return fail ( 500 , { error : 'Organization not found' } ) ;
8+ }
9+ const organizationId = locals . org . id ;
10+
11+ const formData = await request . formData ( ) ;
12+ const email = formData . get ( 'email' ) ?. toString ( ) ;
13+ const name = formData . get ( 'name' ) ?. toString ( ) ;
14+ const role = formData . get ( 'role' ) ?. toString ( ) ;
15+
16+ // Basic validation
17+ if ( ! email || ! name || ! role ) {
18+ return fail ( 400 , { error : 'Missing required fields' , data : { email, name, role } } ) ;
19+ }
20+
21+ if ( role !== 'ADMIN' && role !== 'USER' ) {
22+ return fail ( 400 , { error : 'Invalid role specified' , data : { email, name, role } } ) ;
23+ }
24+
25+ let userIdToLink ;
26+
27+ try {
28+ // Check if user exists globally
29+ let existingUser = await prisma . user . findUnique ( {
30+ where : { email } ,
31+ } ) ;
32+
33+ if ( existingUser ) {
34+ userIdToLink = existingUser . id ;
35+ // Check if user is already in this organization
36+ const existingUserOrganization = await prisma . userOrganization . findFirst ( {
37+ where : {
38+ userId : existingUser . id ,
39+ organizationId : organizationId ,
40+ } ,
41+ } ) ;
42+
43+ if ( existingUserOrganization ) {
44+ return fail ( 400 , { error : 'User already exists in this organization' , data : { email, name, role } } ) ;
45+ }
46+ // If user exists globally but not in this org, we'll link them later
47+ } else {
48+ // Create new user if they don't exist globally
49+ // Assuming user_id should be unique, often email is used or a generated cuid/uuid
50+ // For now, let's assume user_id can be the email if it's meant to be a unique string identifier
51+ // and the actual primary key is 'id' (auto-increment or CUID).
52+ // If 'user_id' is meant to be the Clerk/Auth0 ID, this might need adjustment
53+ // based on how that ID is obtained or if it's set post-creation.
54+ // The schema provided has `user_id String @unique`
55+ const newUser = await prisma . user . create ( {
56+ data : {
57+ email,
58+ name,
59+ user_id : email , // Assuming email can serve as the initial unique user_id
60+ } ,
61+ } ) ;
62+ userIdToLink = newUser . id ;
63+ }
64+ } catch ( error ) {
65+ console . error ( 'Error finding or creating user:' , error ) ;
66+ return fail ( 500 , { error : 'Could not process user information' , data : { email, name, role } } ) ;
67+ }
68+
69+ try {
70+ // Link user to the organization
71+ await prisma . userOrganization . create ( {
72+ data : {
73+ userId : userIdToLink ,
74+ organizationId : organizationId ,
75+ role : role , // 'ADMIN' or 'USER'
76+ } ,
77+ } ) ;
78+
79+ // On success, it's often good to redirect to avoid form resubmission issues,
80+ // or return a success object that the page can use to update its state.
81+ // For this task, returning a success object is specified.
82+ return { success : true , message : 'User added successfully!' } ;
83+
84+ } catch ( error ) {
85+ console . error ( 'Error linking user to organization:' , error ) ;
86+ // This could happen if, for example, a race condition occurred or a DB constraint was violated.
87+ // The earlier check for existingUserOrganization should prevent most common cases.
88+ return fail ( 500 , { error : 'Could not add user to organization' , data : { email, name, role } } ) ;
89+ }
90+ } ,
91+ } ;
0 commit comments