Initial commit
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { supabase } from '../../supabaseClient';
|
||||
import { CCUser, convertToCCUser } from '../../services/auth/authService';
|
||||
import { EmailCredentials } from '../../services/auth/authService';
|
||||
import { formatEmailForDatabase } from '../graph/neoDBService';
|
||||
import { RegistrationResponse } from '../../services/auth/authService';
|
||||
import { neoRegistrationService } from '../graph/neoRegistrationService';
|
||||
import { storageService, StorageKeys } from './localStorageService';
|
||||
import { logger } from '../../debugConfig';
|
||||
|
||||
const REGISTRATION_SERVICE = 'registration-service';
|
||||
|
||||
export class RegistrationService {
|
||||
private static instance: RegistrationService;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
static getInstance(): RegistrationService {
|
||||
if (!RegistrationService.instance) {
|
||||
RegistrationService.instance = new RegistrationService();
|
||||
}
|
||||
return RegistrationService.instance;
|
||||
}
|
||||
|
||||
async register(credentials: EmailCredentials, displayName: string): Promise<RegistrationResponse> {
|
||||
try {
|
||||
logger.debug(REGISTRATION_SERVICE, '🔄 Starting registration', {
|
||||
email: credentials.email,
|
||||
role: credentials.role,
|
||||
hasDisplayName: !!displayName
|
||||
});
|
||||
|
||||
// Generate username from email (or use another method)
|
||||
const username = formatEmailForDatabase(credentials.email);
|
||||
|
||||
// 1. First sign up the user in auth
|
||||
const { data: authData, error: signUpError } = await supabase.auth.signUp({
|
||||
email: credentials.email,
|
||||
password: credentials.password,
|
||||
options: {
|
||||
data: {
|
||||
user_type: credentials.role,
|
||||
username: username,
|
||||
display_name: displayName
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (signUpError) {
|
||||
logger.error(REGISTRATION_SERVICE, '❌ Supabase signup error', { error: signUpError });
|
||||
throw signUpError;
|
||||
}
|
||||
|
||||
if (!authData.user) {
|
||||
logger.error(REGISTRATION_SERVICE, '❌ No user data after registration');
|
||||
throw new Error('No user data after registration');
|
||||
}
|
||||
|
||||
const ccUser: CCUser = convertToCCUser(authData.user, authData.user.user_metadata);
|
||||
|
||||
// 2. Update the profile with the correct user type
|
||||
const { error: updateError } = await supabase
|
||||
.from('profiles')
|
||||
.update({
|
||||
user_type: credentials.role,
|
||||
username: username,
|
||||
display_name: displayName
|
||||
})
|
||||
.eq('id', authData.user.id)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (updateError) {
|
||||
logger.error(REGISTRATION_SERVICE, '❌ Failed to update profile', updateError);
|
||||
throw updateError;
|
||||
}
|
||||
|
||||
storageService.set(StorageKeys.IS_NEW_REGISTRATION, true);
|
||||
|
||||
// 3. Create Neo4j nodes
|
||||
try {
|
||||
const userNode = await neoRegistrationService.registerNeo4JUser(
|
||||
ccUser,
|
||||
username, // Pass username for database operations
|
||||
credentials.role
|
||||
);
|
||||
|
||||
logger.info(REGISTRATION_SERVICE, '✅ Registration successful with Neo4j setup', {
|
||||
userId: ccUser.id,
|
||||
hasUserNode: !!userNode
|
||||
});
|
||||
|
||||
return {
|
||||
user: ccUser,
|
||||
accessToken: authData.session?.access_token || null,
|
||||
userRole: credentials.role,
|
||||
message: 'Registration successful'
|
||||
};
|
||||
} catch (neo4jError) {
|
||||
logger.warn(REGISTRATION_SERVICE, '⚠️ Neo4j setup problem', {
|
||||
userId: ccUser.id,
|
||||
error: neo4jError
|
||||
});
|
||||
// Return success even if Neo4j setup is pending
|
||||
return {
|
||||
user: ccUser,
|
||||
accessToken: authData.session?.access_token || null,
|
||||
userRole: credentials.role,
|
||||
message: 'Registration successful - Neo4j setup pending'
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(REGISTRATION_SERVICE, '❌ Registration failed:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const registrationService = RegistrationService.getInstance();
|
||||
Reference in New Issue
Block a user