33 * @remarks Encapsulates the business logic for managing professor appeals.
44 */
55
6- import { EntityManager } from '@mikro-orm/core'
6+ import { EntityManager , FilterQuery } from '@mikro-orm/core'
77import { Appeal } from './appeal.entity.js'
8- import { CreateAppealType , UpdateAppealSchema , UpdateAppealType } from './appeal.schemas.js'
8+ import { CreateAppealType , SearchAppealsQuery , UpdateAppealSchema , UpdateAppealType } from './appeal.schemas.js'
99import { User } from '../user/user.entity.js'
1010import { ProfessorService } from '../professor/professor.services.js'
1111import { ObjectId } from '@mikro-orm/mongodb'
@@ -58,13 +58,37 @@ export class AppealService {
5858 }
5959
6060 /**
61- * Retrieves all appeals, populating the associated user information.
62- * @returns {Promise<Appeal[]> } A promise that resolves to an array of all appeals.
61+ * Retrieves all appeals based on filter, sort, and pagination parameters.
62+ * @param {SearchAppealsQuery } query - The validated query parameters.
63+ * @returns {Promise<{appeals: Appeal[], total: number}> } An object with the list of appeals and the total count.
6364 */
64- public async findAll ( ) : Promise < Appeal [ ] > {
65- this . logger . info ( 'Fetching all appeals.' )
65+ public async findAll ( query : SearchAppealsQuery ) : Promise < { appeals : Appeal [ ] , total : number } > {
66+ this . logger . info ( { query } , 'Fetching all appeals with filters .' )
6667
67- return this . em . find ( Appeal , { } , { populate : [ 'user' ] } )
68+ const where : FilterQuery < Appeal > = { } ;
69+
70+ if ( query . status ) {
71+ where . state = query . status ;
72+ }
73+
74+ if ( query . q ) {
75+ const searchQuery = { $ilike : `%${ query . q } %` } ;
76+ where . user = {
77+ $or : [
78+ { name : searchQuery } ,
79+ { surname : searchQuery }
80+ ]
81+ } ;
82+ }
83+
84+ const [ appeals , total ] = await this . em . findAndCount ( Appeal , where , {
85+ populate : [ 'user' ] ,
86+ orderBy : { [ query . sortBy ] : query . sortOrder } ,
87+ limit : query . limit ,
88+ offset : query . offset ,
89+ } ) ;
90+
91+ return { appeals, total } ;
6892 }
6993
7094 /**
@@ -74,7 +98,6 @@ export class AppealService {
7498 */
7599 public async findOne ( id : string ) : Promise < Appeal | null > {
76100 this . logger . info ( { appealId : id } , 'Fetching appeal.' )
77-
78101 const objectId = new ObjectId ( id ) ;
79102 return this . em . findOne ( Appeal , { _id : objectId } , { populate : [ 'user' ] } )
80103 }
0 commit comments