feat: Add instructor registration endpoint and rename student registration to learner registration.
This commit is contained in:
parent
e379ed83c8
commit
a38389cc9f
2 changed files with 122 additions and 1 deletions
|
|
@ -67,13 +67,61 @@ export class AuthController {
|
||||||
return await this.authService.login(body);
|
return await this.authService.login(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User registration
|
||||||
|
* @summary Register a new instructor account
|
||||||
|
* @param body Registration data
|
||||||
|
* @returns Created user information
|
||||||
|
*/
|
||||||
|
@Post('register-instructor')
|
||||||
|
@SuccessResponse('201', 'Registration successful')
|
||||||
|
@Response('400', 'Validation error')
|
||||||
|
@Response('409', 'Username or email already exists')
|
||||||
|
@Example<RegisterResponse>({
|
||||||
|
user: {
|
||||||
|
id: 4,
|
||||||
|
username: 'newinstructor',
|
||||||
|
email: 'instructor@example.com',
|
||||||
|
updated_at: new Date('2024-01-01T00:00:00Z'),
|
||||||
|
created_at: new Date('2024-01-01T00:00:00Z'),
|
||||||
|
role: {
|
||||||
|
code: 'INSTRUCTOR',
|
||||||
|
name: {
|
||||||
|
th: 'ผู้สอน',
|
||||||
|
en: 'Instructor'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
profile: {
|
||||||
|
prefix: {
|
||||||
|
th: 'นาย',
|
||||||
|
en: 'Mr.'
|
||||||
|
},
|
||||||
|
first_name: 'John',
|
||||||
|
last_name: 'Doe',
|
||||||
|
phone: null,
|
||||||
|
avatar_url: null,
|
||||||
|
birth_date: null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
message: 'Registration successful'
|
||||||
|
})
|
||||||
|
public async registerInstructor(@Body() body: RegisterRequest): Promise<RegisterResponse> {
|
||||||
|
// Validate input
|
||||||
|
const { error } = registerSchema.validate(body);
|
||||||
|
if (error) {
|
||||||
|
throw new ValidationError(error.details[0].message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.authService.registerInstructor(body);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User registration
|
* User registration
|
||||||
* @summary Register a new student account
|
* @summary Register a new student account
|
||||||
* @param body Registration data
|
* @param body Registration data
|
||||||
* @returns Created user information
|
* @returns Created user information
|
||||||
*/
|
*/
|
||||||
@Post('register')
|
@Post('register-learner')
|
||||||
@SuccessResponse('201', 'Registration successful')
|
@SuccessResponse('201', 'Registration successful')
|
||||||
@Response('400', 'Validation error')
|
@Response('400', 'Validation error')
|
||||||
@Response('409', 'Username or email already exists')
|
@Response('409', 'Username or email already exists')
|
||||||
|
|
|
||||||
|
|
@ -139,6 +139,79 @@ export class AuthService {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async registerInstructor(data: RegisterRequest): Promise<RegisterResponse> {
|
||||||
|
const { username, email, password, first_name, last_name, prefix, phone } = data;
|
||||||
|
|
||||||
|
// Check if username already exists
|
||||||
|
const existingUsername = await prisma.user.findUnique({
|
||||||
|
where: { username }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingUsername) {
|
||||||
|
throw new ValidationError('Username already exists');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if email already exists
|
||||||
|
const existingEmail = await prisma.user.findUnique({
|
||||||
|
where: { email }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingEmail) {
|
||||||
|
throw new ValidationError('Email already exists');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if phone number already exists in user profiles
|
||||||
|
const existingPhone = await prisma.userProfile.findFirst({
|
||||||
|
where: { phone }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingPhone) {
|
||||||
|
throw new ValidationError('Phone number already exists');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get INSTRUCTOR role
|
||||||
|
const instructorRole = await prisma.role.findUnique({
|
||||||
|
where: { code: 'INSTRUCTOR' }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!instructorRole) {
|
||||||
|
logger.error('INSTRUCTOR role not found in database');
|
||||||
|
throw new Error('System configuration error');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash password
|
||||||
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
|
|
||||||
|
// Create user with profile
|
||||||
|
const user = await prisma.user.create({
|
||||||
|
data: {
|
||||||
|
username,
|
||||||
|
email,
|
||||||
|
password: hashedPassword,
|
||||||
|
role_id: instructorRole.id,
|
||||||
|
profile: {
|
||||||
|
create: {
|
||||||
|
prefix: prefix ? prefix as Prisma.InputJsonValue : Prisma.JsonNull,
|
||||||
|
first_name,
|
||||||
|
last_name,
|
||||||
|
phone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
role: true,
|
||||||
|
profile: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.info('New user registered', { userId: user.id, username: user.username });
|
||||||
|
|
||||||
|
return {
|
||||||
|
user: this.formatUserResponse(user),
|
||||||
|
message: 'Registration successful'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refresh access token
|
* Refresh access token
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue