Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 28 additions & 46 deletions src/modules/curriculum/services/curriculum.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,7 @@ export class CurriculumService {
Object.assign(filter, { id: { $in: departmentIds } });
}

if (query.search) {
const escaped = this.EscapeLikeWildcards(query.search);
Object.assign(filter, {
$and: [
{
$or: [
{ code: { $ilike: `%${escaped}%` } },
{ name: { $ilike: `%${escaped}%` } },
],
},
],
});
}
this.ApplySearchFilter(filter, query.search, ['code', 'name']);

const departments = await this.em.find(Department, filter, {
orderBy: { name: QueryOrder.ASC_NULLS_LAST },
Expand All @@ -83,7 +71,7 @@ export class CurriculumService {
}
}

const departmentFilter: Record<string, unknown> = {
const departmentFilter: FilterQuery<Department> = {
semester: query.semesterId,
};

Expand All @@ -98,21 +86,9 @@ export class CurriculumService {

const filter: FilterQuery<Program> = {
department: departmentFilter,
} as FilterQuery<Program>;

if (query.search) {
const escaped = this.EscapeLikeWildcards(query.search);
Object.assign(filter, {
$and: [
{
$or: [
{ code: { $ilike: `%${escaped}%` } },
{ name: { $ilike: `%${escaped}%` } },
],
},
],
});
}
};

this.ApplySearchFilter(filter, query.search, ['code', 'name']);

const programs = await this.em.find(Program, filter, {
populate: ['department'],
Expand Down Expand Up @@ -177,7 +153,7 @@ export class CurriculumService {
}

// Build filter
const departmentFilter: Record<string, unknown> = {
const departmentFilter: FilterQuery<Department> = {
semester: query.semesterId,
};

Expand All @@ -190,7 +166,7 @@ export class CurriculumService {
departmentFilter.id = { $in: departmentIds };
}

const programFilter: Record<string, unknown> = {
const programFilter: FilterQuery<Program> = {
department: departmentFilter,
};

Expand All @@ -200,21 +176,9 @@ export class CurriculumService {

const filter: FilterQuery<Course> = {
program: programFilter,
} as FilterQuery<Course>;

if (query.search) {
const escaped = this.EscapeLikeWildcards(query.search);
Object.assign(filter, {
$and: [
{
$or: [
{ shortname: { $ilike: `%${escaped}%` } },
{ fullname: { $ilike: `%${escaped}%` } },
],
},
],
});
}
};

this.ApplySearchFilter(filter, query.search, ['shortname', 'fullname']);

const courses = await this.em.find(Course, filter, {
populate: ['program'],
Expand All @@ -233,6 +197,24 @@ export class CurriculumService {
}
}

private ApplySearchFilter(
filter: Record<string, unknown>,
search: string | undefined,
fields: [string, string],
): void {
if (!search) return;
const escaped = this.EscapeLikeWildcards(search);
Object.assign(filter, {
$and: [
{
$or: fields.map((field) => ({
[field]: { $ilike: `%${escaped}%` },
})),
},
],
});
}

private EscapeLikeWildcards(input: string): string {
return input
.replace(/\\/g, '\\\\')
Expand Down
Loading