@@ -11,6 +11,7 @@ import { CoursesService } from '../courses/courses.service';
1111import { SectionsService } from '../sections/sections.service' ;
1212import { ProfessorsService } from '../professors/professors.service' ;
1313import { Professor } from '../professors/entities/professor.entity' ;
14+ import { Period } from '../periods/entities/period.entity' ;
1415
1516@Injectable ( )
1617export 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 ( / \( 0 1 \) / . 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 ( / \( 0 2 \) / . 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 ( / \( 0 1 \) / . 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 ( / \( 0 2 \) / . 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