Skip to content

Commit e16f88c

Browse files
committed
enhance/solve-reliability-and-security-codesmells
1 parent 7e29cef commit e16f88c

File tree

35 files changed

+253
-247
lines changed

35 files changed

+253
-247
lines changed

.github/workflows/run_test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ on:
44
push:
55
branches:
66
- main
7+
- develop
8+
- staging
79
pull_request:
810
branches:
911
- main
12+
- develop
13+
- staging
1014

1115
jobs:
1216
test:

apps/api/src/assistance-applications/assistance-applications.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class AssistanceApplicationsController {
7171

7272
@Get(':id/document')
7373
findOneDocument(@Param('id') id: string) {
74-
return this.assistanceApplicationsService.findOneDocument(id);
74+
return this.assistanceApplicationsService.findOne(id, true);
7575
}
7676

7777
@Patch(':id')

apps/api/src/assistance-applications/assistance-applications.service.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,15 @@ export class AssistanceApplicationsService {
6969
return results;
7070
}
7171

72-
async findOne(id: string) {
72+
async findOne(id: string, withDocument: boolean = false) {
7373
const application = await this.assistanceApplicationRepository.findOne({
7474
where: { id },
7575
relations: {
7676
graduatedAssistance: {
7777
professor: true,
7878
},
7979
student: true,
80-
document: true,
81-
},
82-
});
83-
if (!application) {
84-
throw new NotFoundException(
85-
`Assistance Application with ID ${id} not found`,
86-
);
87-
}
88-
return application;
89-
}
90-
91-
async findOneDocument(id: string) {
92-
const application = await this.assistanceApplicationRepository.findOne({
93-
where: { id },
94-
relations: {
95-
graduatedAssistance: {
96-
professor: true,
97-
},
98-
student: true,
99-
document: true,
80+
document: withDocument,
10081
},
10182
});
10283
if (!application) {

apps/api/src/billboards/billboards.service.ts

Lines changed: 161 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { CoursesService } from '../courses/courses.service';
1111
import { SectionsService } from '../sections/sections.service';
1212
import { ProfessorsService } from '../professors/professors.service';
1313
import { Professor } from '../professors/entities/professor.entity';
14+
import { Period } from '../periods/entities/period.entity';
1415

1516
@Injectable()
1617
export class BillboardsService {
@@ -24,166 +25,190 @@ export class BillboardsService {
2425
private readonly professorService: ProfessorsService,
2526
) {}
2627

27-
async create(sectionsDto: CreateSectionDto[]) {
28-
const billboardCoursesMap = new Map<string, Course>();
28+
async getOrCreatePeriodBySectionDto(sectionDto: CreateSectionDto) {
29+
const mapPeriod = (raw: string) => (raw.startsWith('1') ? '10' : '20');
30+
const year = Number(sectionDto.period.slice(0, 4));
31+
const semester = sectionDto.period.slice(4, 5);
32+
const rawPeriod = sectionDto.period.slice(4, 6);
33+
const periodStr = mapPeriod(rawPeriod);
2934

30-
const normalizeName = (name: string) =>
31-
name.toLowerCase().trim().replace(/\s+/g, ' ');
35+
let foundPeriod = await this.periodsService.findOneByPeriodAndYear(
36+
periodStr,
37+
year,
38+
);
3239

33-
const mapPeriod = (raw: string) => (raw.startsWith('1') ? '10' : '20');
40+
foundPeriod ??= await this.periodsService.create({
41+
period: periodStr,
42+
year: year,
43+
semester: semester === '1' ? 1 : 2,
44+
});
3445

35-
for (const section of sectionsDto) {
36-
const year = Number(section.period.slice(0, 4));
37-
const semester = section.period.slice(4, 5);
38-
const rawPeriod = section.period.slice(4, 6);
39-
const periodStr = mapPeriod(rawPeriod);
40-
41-
let periodFound = await this.periodsService.findOneByPeriodAndYear(
42-
periodStr,
43-
year,
44-
);
45-
if (!periodFound) {
46-
periodFound = await this.periodsService.create({
47-
period: periodStr,
48-
year: year,
49-
semester: semester === '1' ? 1 : 2,
50-
});
51-
}
52-
if (!periodFound) {
53-
throw new Error('Period not found');
54-
}
46+
return foundPeriod;
47+
}
5548

56-
let existingCourse = await this.courseService.findByCodeAndPeriod(
57-
section.code,
58-
periodFound,
49+
async getOrCreateSectionBySectionDto(
50+
sectionDto: CreateSectionDto,
51+
period: Period,
52+
foundProfessors: Professor[],
53+
supportProfessors: Professor[],
54+
) {
55+
const existingSection = await this.sectionService.findByNRCAndPeriod(
56+
sectionDto.NRC,
57+
period,
58+
);
59+
60+
let sectionToUse: Section;
61+
if (existingSection != null) {
62+
sectionToUse = await this.sectionService.updateProfessorsSection(
63+
existingSection,
64+
foundProfessors,
65+
supportProfessors,
5966
);
60-
const existingSection = await this.sectionService.findByNRCAndPeriod(
61-
section.NRC,
62-
periodFound,
67+
} else {
68+
const newSectionDto = new CreateSectionDto();
69+
newSectionDto.NRC = sectionDto.NRC;
70+
newSectionDto.section = sectionDto.section;
71+
if (foundProfessors.length === 0 && supportProfessors.length === 0) {
72+
throw new Error('No professors found for the section');
73+
}
74+
sectionToUse = await this.sectionService.create(
75+
newSectionDto,
76+
supportProfessors,
77+
foundProfessors,
78+
period,
6379
);
80+
}
81+
return sectionToUse;
82+
}
6483

65-
if (section.professors != null && section.professors !== '') {
66-
const professorsArr = section.professors.split('|');
67-
const supportProfessors: Professor[] = [];
68-
const findedProfessors: Professor[] = [];
69-
70-
await Promise.all(
71-
professorsArr.map(async (prof) => {
72-
const cleanedName = cleanName(prof);
73-
const searchProfessor = await this.professorService.findByName(
74-
normalizeName(cleanedName),
75-
);
76-
if (searchProfessor) {
77-
if (/\(01\)/.test(prof)) {
78-
if (
79-
!findedProfessors.find(
80-
(p) => p.user.id === searchProfessor.user.id,
81-
)
82-
) {
83-
findedProfessors.push(searchProfessor);
84-
}
85-
} else if (/\(02\)/.test(prof)) {
86-
if (
87-
!supportProfessors.find(
88-
(p) => p.user.id === searchProfessor.user.id,
89-
)
90-
) {
91-
supportProfessors.push(searchProfessor);
92-
}
93-
}
94-
}
95-
}),
84+
async getOrCreateCourseBySectionDto(
85+
sectionDto: CreateSectionDto,
86+
period: Period,
87+
sectionToUse: Section,
88+
) {
89+
let existingCourse = await this.courseService.findByCodeAndPeriod(
90+
sectionDto.code,
91+
period,
92+
);
93+
if (existingCourse) {
94+
const sectionAlreadyIncluded = existingCourse.sections?.some(
95+
(sec) => sec.NRC === sectionToUse.NRC,
96+
);
97+
if (!sectionAlreadyIncluded) {
98+
existingCourse = await this.courseService.updateSections(
99+
sectionToUse,
100+
existingCourse,
96101
);
102+
}
103+
} else {
104+
const courseDto = new CreateCourseDto();
105+
courseDto.code = sectionDto.code;
106+
courseDto.credits = +sectionDto.credits;
107+
courseDto.departament = sectionDto.departament;
108+
courseDto.name = sectionDto.name;
109+
existingCourse = await this.courseService.create(courseDto, sectionToUse);
110+
}
111+
return existingCourse;
112+
}
97113

98-
let sectionToUse: Section;
99-
if (existingSection != null) {
100-
sectionToUse = await this.sectionService.updateProfessorsSection(
101-
existingSection,
102-
findedProfessors,
103-
supportProfessors,
104-
);
105-
} else {
106-
const newSectionDto = new CreateSectionDto();
107-
newSectionDto.NRC = section.NRC;
108-
newSectionDto.section = section.section;
109-
if (findedProfessors.length === 0 && supportProfessors.length === 0) {
110-
throw new Error('No professors found for the section');
111-
}
112-
sectionToUse = await this.sectionService.create(
113-
newSectionDto,
114-
supportProfessors,
115-
findedProfessors,
116-
periodFound,
117-
);
118-
}
114+
async getProfessors(section: CreateSectionDto) {
115+
const normalizeName = (name: string) =>
116+
name.toLowerCase().trim().replace(/\s+/g, ' ');
117+
const professorsArr = section.professors.split('|');
118+
const supportProfessors: Professor[] = [];
119+
const foundProfessors: Professor[] = [];
119120

120-
if (existingCourse) {
121-
const sectionAlreadyIncluded = existingCourse.sections?.some(
122-
(sec) => sec.NRC === sectionToUse.NRC,
123-
);
124-
if (!sectionAlreadyIncluded) {
125-
existingCourse = await this.courseService.updateSections(
126-
sectionToUse,
127-
existingCourse,
128-
);
121+
await Promise.all(
122+
professorsArr.map(async (prof) => {
123+
const cleanedName = cleanName(prof);
124+
const searchProfessor = await this.professorService.findByName(
125+
normalizeName(cleanedName),
126+
);
127+
if (searchProfessor) {
128+
if (/\(01\)/.test(prof)) {
129+
if (
130+
!foundProfessors.find(
131+
(p) => p.user.id === searchProfessor.user.id,
132+
)
133+
) {
134+
foundProfessors.push(searchProfessor);
135+
}
136+
} else if (/\(02\)/.test(prof)) {
137+
if (
138+
!supportProfessors.find(
139+
(p) => p.user.id === searchProfessor.user.id,
140+
)
141+
) {
142+
supportProfessors.push(searchProfessor);
143+
}
129144
}
130-
billboardCoursesMap.set(existingCourse.code, existingCourse);
131-
} else {
132-
const courseDto = new CreateCourseDto();
133-
courseDto.code = section.code;
134-
courseDto.credits = +section.credits;
135-
courseDto.departament = section.departament;
136-
courseDto.name = section.name;
137-
existingCourse = await this.courseService.create(
138-
courseDto,
139-
sectionToUse,
140-
);
141-
billboardCoursesMap.set(section.code, existingCourse);
142145
}
146+
}),
147+
);
148+
return [supportProfessors, foundProfessors];
149+
}
150+
151+
async mergeCourses(billboardExisting: Billboard, billboard: Billboard) {
152+
const combinedCourses = [
153+
...billboardExisting.courses,
154+
...billboard.courses,
155+
];
156+
const uniqueCoursesMap = new Map(
157+
combinedCourses.map((course) => [course.code, course]),
158+
);
159+
billboardExisting.courses = Array.from(uniqueCoursesMap.values());
160+
161+
billboardExisting.period = billboard.period;
162+
billboardExisting.publicated = true;
163+
await this.billboardRepository.save(billboardExisting);
164+
return billboardExisting;
165+
}
166+
167+
async getBillboardFromSectionsDto(sectionsDto: CreateSectionDto[]) {
168+
const billboard: Billboard = new Billboard();
169+
const billboardCoursesMap = new Map<string, Course>();
170+
171+
for (const section of sectionsDto) {
172+
const foundPeriod = await this.getOrCreatePeriodBySectionDto(section);
173+
if (section.professors != null && section.professors !== '') {
174+
const [supportProfessors, foundProfessors] =
175+
await this.getProfessors(section);
176+
177+
const sectionToUse = await this.getOrCreateSectionBySectionDto(
178+
section,
179+
foundPeriod,
180+
foundProfessors,
181+
supportProfessors,
182+
);
183+
184+
const existingCourse = await this.getOrCreateCourseBySectionDto(
185+
section,
186+
foundPeriod,
187+
sectionToUse,
188+
);
189+
190+
billboardCoursesMap.set(existingCourse.code, existingCourse);
143191
}
144192
}
145193

146-
const firstsection = sectionsDto[0];
147-
const billboardYear = Number(firstsection.period.slice(0, 4));
148-
const firstRawPeriod = firstsection.period.slice(4, 6);
149-
const firstPeriodStr = mapPeriod(firstRawPeriod);
150-
151-
let periodForBillboard = await this.periodsService.findOneByPeriodAndYear(
152-
firstPeriodStr,
153-
billboardYear,
194+
const periodForBillboard = await this.getOrCreatePeriodBySectionDto(
195+
sectionsDto[0],
154196
);
155-
if (!periodForBillboard) {
156-
periodForBillboard = await this.periodsService.create({
157-
period: firstPeriodStr,
158-
year: billboardYear,
159-
semester: firstsection.period.slice(4, 5) === '1' ? 1 : 2,
160-
});
161-
}
162197

163-
const billboard: Billboard = new Billboard();
164198
billboard.publicated = true;
165199
billboard.period = periodForBillboard;
166200
billboard.courses = Array.from(billboardCoursesMap.values());
201+
return billboard;
202+
}
167203

168-
const billboardExisting = await this.findOne(
204+
async create(sectionsDto: CreateSectionDto[]) {
205+
const billboard = await this.getBillboardFromSectionsDto(sectionsDto);
206+
const existingBillboard = await this.findOne(
169207
billboard.period.year + billboard.period.period,
170208
);
171-
if (billboardExisting) {
172-
const combinedCourses = [
173-
...billboardExisting.courses,
174-
...billboard.courses,
175-
];
176-
const uniqueCoursesMap = new Map(
177-
combinedCourses.map((course) => [course.code, course]),
178-
);
179-
billboardExisting.courses = Array.from(uniqueCoursesMap.values());
180-
181-
billboardExisting.period = billboard.period;
182-
billboardExisting.publicated = true;
183-
await this.billboardRepository.save(billboardExisting);
184-
return billboardExisting;
209+
if (existingBillboard) {
210+
return await this.mergeCourses(existingBillboard, billboard);
185211
}
186-
187212
return await this.billboardRepository.save(billboard);
188213
}
189214

0 commit comments

Comments
 (0)