refactor: simplify instructor search to query all instructors without course-specific filtering
This commit is contained in:
parent
f4a12c686b
commit
0641b2547a
3 changed files with 12 additions and 29 deletions
|
|
@ -198,19 +198,18 @@ export class CoursesInstructorController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ค้นหาผู้สอนสำหรับเพิ่มเข้าคอร์ส
|
* ค้นหาผู้สอนทั้งหมดในระบบ
|
||||||
* Search instructors to add to course
|
* Search all instructors in database
|
||||||
* @param courseId - รหัสคอร์ส / Course ID
|
|
||||||
* @param query - คำค้นหา (email หรือ username) / Search query (email or username)
|
* @param query - คำค้นหา (email หรือ username) / Search query (email or username)
|
||||||
*/
|
*/
|
||||||
@Get('{courseId}/search-instructors')
|
@Get('search-instructors')
|
||||||
@Security('jwt', ['instructor'])
|
@Security('jwt', ['instructor'])
|
||||||
@SuccessResponse('200', 'Instructors found')
|
@SuccessResponse('200', 'Instructors found')
|
||||||
@Response('401', 'Invalid or expired token')
|
@Response('401', 'Invalid or expired token')
|
||||||
public async searchInstructors(@Request() request: any, @Path() courseId: number, @Query() query: string): Promise<SearchInstructorResponse> {
|
public async searchInstructors(@Request() request: any, @Query() query: string): Promise<SearchInstructorResponse> {
|
||||||
const token = request.headers.authorization?.replace('Bearer ', '');
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
||||||
if (!token) throw new ValidationError('No token provided');
|
if (!token) throw new ValidationError('No token provided');
|
||||||
return await CoursesInstructorService.searchInstructors({ token, course_id: courseId, query });
|
return await CoursesInstructorService.searchInstructors({ token, query });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -348,31 +348,16 @@ export class CoursesInstructorService {
|
||||||
|
|
||||||
static async searchInstructors(input: SearchInstructorInput): Promise<SearchInstructorResponse> {
|
static async searchInstructors(input: SearchInstructorInput): Promise<SearchInstructorResponse> {
|
||||||
try {
|
try {
|
||||||
const decoded = jwt.verify(input.token, config.jwt.secret) as { id: number };
|
jwt.verify(input.token, config.jwt.secret) as { id: number };
|
||||||
|
|
||||||
// Validate user is instructor of this course
|
// Search all instructors by email or username
|
||||||
await this.validateCourseInstructor(input.token, input.course_id);
|
|
||||||
|
|
||||||
// Get existing instructors of this course
|
|
||||||
const existingInstructors = await prisma.courseInstructor.findMany({
|
|
||||||
where: { course_id: input.course_id },
|
|
||||||
select: { user_id: true }
|
|
||||||
});
|
|
||||||
const existingUserIds = existingInstructors.map(i => i.user_id);
|
|
||||||
|
|
||||||
// Search users by email or username (only instructors role)
|
|
||||||
const users = await prisma.user.findMany({
|
const users = await prisma.user.findMany({
|
||||||
where: {
|
where: {
|
||||||
AND: [
|
OR: [
|
||||||
{
|
{ email: { contains: input.query, mode: 'insensitive' } },
|
||||||
OR: [
|
{ username: { contains: input.query, mode: 'insensitive' } },
|
||||||
{ email: { contains: input.query, mode: 'insensitive' } },
|
],
|
||||||
{ username: { contains: input.query, mode: 'insensitive' } },
|
role: { code: 'instructor' }
|
||||||
]
|
|
||||||
},
|
|
||||||
{ role: { code: 'instructor' } },
|
|
||||||
{ id: { notIn: existingUserIds } } // Exclude already added instructors
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
profile: true
|
profile: true
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,6 @@ export interface addinstructorCourse {
|
||||||
export interface SearchInstructorInput {
|
export interface SearchInstructorInput {
|
||||||
token: string;
|
token: string;
|
||||||
query: string;
|
query: string;
|
||||||
course_id: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SearchInstructorResult {
|
export interface SearchInstructorResult {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue