feat: Implement instructor search and improve instructor management with email/username lookup and avatar presigned URLs.

This commit is contained in:
JakkrapartXD 2026-01-29 15:52:10 +07:00
parent 38e7f1bf06
commit f4a12c686b
4 changed files with 228 additions and 25 deletions

View file

@ -16,6 +16,7 @@ import {
import { UserResponse } from '../types/user.types';
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
import nodemailer from 'nodemailer';
import { getPresignedUrl } from '../config/minio';
export class AuthService {
/**
@ -59,7 +60,7 @@ export class AuthService {
return {
token,
refreshToken,
user: this.formatUserResponse(user)
user: await this.formatUserResponse(user)
};
}
@ -134,7 +135,7 @@ export class AuthService {
logger.info('New user registered', { userId: user.id, username: user.username });
return {
user: this.formatUserResponse(user),
user: this.formatUserResponseSync(user),
message: 'Registration successful'
};
}
@ -207,10 +208,10 @@ export class AuthService {
logger.info('New user registered', { userId: user.id, username: user.username });
return {
user: this.formatUserResponse(user),
user: this.formatUserResponseSync(user),
message: 'Registration successful'
};
}
}
/**
* Refresh access token
@ -370,9 +371,43 @@ export class AuthService {
}
/**
* Format user response
* Format user response with presigned URL for avatar (for login)
*/
private formatUserResponse(user: any): UserResponse {
private async formatUserResponse(user: any): Promise<UserResponse> {
let avatar_url: string | null = null;
if (user.profile?.avatar_url) {
try {
avatar_url = await getPresignedUrl(user.profile.avatar_url, 3600);
} catch (err) {
logger.warn(`Failed to generate presigned URL for avatar: ${err}`);
}
}
return {
id: user.id,
username: user.username,
email: user.email,
updated_at: user.updated_at,
created_at: user.created_at,
role: {
code: user.role.code,
name: user.role.name
},
profile: user.profile ? {
prefix: user.profile.prefix,
first_name: user.profile.first_name,
last_name: user.profile.last_name,
phone: user.profile.phone,
avatar_url: avatar_url,
birth_date: user.profile.birth_date
} : undefined
};
}
/**
* Format user response without presigned URL (for register)
*/
private formatUserResponseSync(user: any): UserResponse {
return {
id: user.id,
username: user.username,
@ -393,6 +428,4 @@ export class AuthService {
} : undefined
};
}
}