-
Notifications
You must be signed in to change notification settings - Fork 0
/
sisu.go
847 lines (691 loc) · 22.8 KB
/
sisu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
// nolint: godox,nolintlint
// TODO: make some helper functions to avoid the repetition.
package main
import (
"bytes"
"database/sql"
"encoding/csv"
"errors"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
"changeme/repository"
"changeme/types"
"github.com/dimchansky/utfbom"
"github.com/gocarina/gocsv"
"go.uber.org/zap"
)
// FIXME: import is no longer called import.
// TODO: check if all of them are used.
var (
ErrAlreadyImported = errors.New("Arquivo deste tipo já foi importado")
ErrApplicationNotFoundOrNotAvail = errors.New("Inscrição não encontrada ou não disponível para esta ação")
ErrCloseFail = errors.New("Ainda existem alunos pendentes nesta chamada")
ErrCloseRollCall = errors.New("Erro desconhecido ao atualizar chamada, mais detalhes nos logs")
ErrCreateRollCall = errors.New("Existe uma chamada mais antiga que está aberta ou seleção de interessados não foi importada")
ErrDoneRollCallExists = errors.New("Existem chamadas finalizada")
ErrEmptyCSV = errors.New("Arquivo csv selecionado está vazio")
ErrGenericDB = errors.New("Erro desconhecido ao consultar o banco de dados, mais detalhes nos logs")
ErrInterestedSelectionExists = errors.New("Seleção de interessados existe")
ErrMalformedCSV = errors.New("Arquivo csv mal formatado ou inválido")
ErrMissingFirstImport = errors.New("Arquivo de aprovados deve ser importado antes do arquivo de alunos que manifestaram interesse")
ErrMissingRollCall = errors.New("Chamada não encontrada")
ErrNoApplications = errors.New("Não há inscrições deste tipo")
ErrNoFile = errors.New("Arquivo selecionado não existe")
ErrNoRollCalls = errors.New("Não existem chamadas criadas")
ErrNoSelection = errors.New("Não há seleção deste tipo importada")
ErrOpenFile = errors.New("Erro desconhecido ao abrir arquivo, mais detalhes nos logs")
ErrOpenRollCall = errors.New("Existe uma chamada mais recente que está aberta")
ErrParseFile = errors.New("Erro desconhecido ao processar arquivo CSV, mais detalhes nos logs")
ErrPermFile = errors.New("Sem permissão para abrir o arquivo selecionado")
ErrRollCallNotFoundOrNotAvail = errors.New("Chamada não encontrada ou não disponível para esta ação")
ErrSaveFile = errors.New("Erro desconhecido ao salvar inscrições, mais detalhes nos logs")
ErrUpdatedApplicationExists = errors.New("Existem inscrições atualizadas")
ErrNoApplicationsToFill = errors.New("Todas as vagas já foram preenchidas")
)
type SISU struct {
applicantRepo repository.ApplicantRepository
applicationRepo repository.ApplicationRepository
classRepo repository.ClassRepository
rollcallRepo repository.RollcallRepository
selectionRepo repository.SelectionRepository
service *RepositoryService
l *zap.SugaredLogger
}
// TODO: write test for this func
// TODO: validate file name and if its the same semester
// FIXME: why is it so big and complex?
func (s *SISU) LoadSelection(path string, kind types.SelectionKind) error { //nolint: funlen, cyclop
file, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return ErrNoFile
}
if os.IsPermission(err) {
return ErrPermFile
}
s.l.Errorw("os open",
"error", err,
)
return ErrOpenFile
}
status := "WAITING"
if kind == types.ApprovedSelection {
status = "APPROVED"
}
rawApplications, err := parseCSVApplicants(file, status)
if err != nil {
if errors.Is(err, gocsv.ErrEmptyCSVFile) {
return ErrEmptyCSV
}
if errors.Is(err, gocsv.ErrUnmatchedStructTags) ||
errors.Is(err, gocsv.ErrDoubleHeaderNames) ||
errors.Is(err, gocsv.ErrNoStructTags) {
return ErrMalformedCSV
}
s.l.Errorw("parse csv applications",
"file", file,
"status", status,
"error", err,
)
return ErrParseFile
}
applications := make([]*types.Application, len(rawApplications))
for i, csvApplicant := range rawApplications { //nolint: varnamelen
application, err := csvApplicant.ToApplication(status)
if err != nil {
return fmt.Errorf("csv applicant to application: %w", err)
}
applications[i] = application
}
var selection types.Selection
selection.Name = file.Name()
selection.Kind = kind
if len(rawApplications) > 0 {
selection.Date = rawApplications[0].Date
selection.Institution = rawApplications[0].Institution
selection.Course = rawApplications[0].Course
}
if err := s.selectionRepo.Begin(); err != nil {
return fmt.Errorf("begin: %w", err)
}
defer s.selectionRepo.Rollback()
if err := s.selectionRepo.CreateSelection(&selection); err != nil {
return fmt.Errorf("create selection: %w", err)
}
var rollcall types.Rollcall
if selection.Kind == types.ApprovedSelection {
r, err := s.rollcallRepo.CreateRollcall()
if err != nil {
return fmt.Errorf("create rollcall: %w", err)
}
rollcall = *r
}
for _, application := range applications {
application.Rollcall = rollcall
if err := s.classRepo.CreateClass(&application.Class); err != nil {
return fmt.Errorf("create class: %w", err)
}
if err := s.applicantRepo.CreateApplicant(&application.Applicant); err != nil {
return fmt.Errorf("create applicant: %w", err)
}
if err := s.applicationRepo.CreateApplication(application, selection.ID); err != nil {
return fmt.Errorf("create application: %w", err)
}
}
if err := s.selectionRepo.Commit(); err != nil {
return fmt.Errorf("commit: %w", err)
}
return nil
}
// FIX: this with andre.
func (s *SISU) CreateRollCall() error {
if err := s.rollcallRepo.Begin(); err != nil {
return err
}
defer s.rollcallRepo.Rollback()
rollcall, err := s.rollcallRepo.CreateRollcall()
if err != nil {
return err
}
if err := s.allocApplications(rollcall); err != nil {
return err
}
if err := s.rollcallRepo.Commit(); err != nil {
return err
}
return nil
}
func (s *SISU) allocApplications(rollcall *types.Rollcall) error {
var updated bool
classes, err := s.classRepo.FindClasses()
if err != nil {
return fmt.Errorf("find classes :%w", err)
}
for _, class := range classes {
status := "ENROLLED"
filter := types.ApplicationsFilter{
ClassID: &class.ID,
Status: &status,
}
applications, err := s.applicationRepo.FindApplications(filter)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("enrolled applications: %w", err)
}
seats := class.Seats - len(applications)
ok, err := s.fillClass(seats, class, rollcall)
if err != nil {
return fmt.Errorf("fill class: %w", err)
}
if ok {
updated = true
}
}
if !updated {
return ErrNoApplicationsToFill
}
return nil
}
type applicationsRanked []*types.Application
func (v applicationsRanked) Len() int { return len(v) }
func (v applicationsRanked) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (v applicationsRanked) Less(i, j int) bool { return v[i].Ranking < v[j].Ranking }
func (s *SISU) fillClass(seats int, class *types.Class, rollcall *types.Rollcall) (bool, error) {
status := "WAITING"
filter := types.ApplicationsFilter{
ClassID: &class.ID,
Status: &status,
}
applications, err := s.applicationRepo.FindApplications(filter)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return false, fmt.Errorf("find applications: %w", err)
}
sort.Sort(applicationsRanked(applications))
if len(applications) < seats {
seats = len(applications)
}
applications = applications[:seats]
if len(applications) == 0 {
return false, nil
}
for _, application := range applications {
status := "APPROVED"
update := types.ApplicationUpdate{
Status: &status,
RollcallID: rollcall.ID,
}
if _, err := s.applicationRepo.UpdateApplication(application.ID, update); err != nil {
return false, fmt.Errorf("update application: %w", err)
}
}
return true, nil
}
func (s *SISU) CloseRollCall(id int64) error {
status := "DONE"
update := types.RollcallUpdate{Status: &status}
if _, err := s.rollcallRepo.UpdateRollcall(id, update); err != nil {
return err
}
return nil
}
func (s *SISU) OpenRollCall(id int64) error {
status := "CALLING"
update := types.RollcallUpdate{Status: &status}
if _, err := s.rollcallRepo.UpdateRollcall(id, update); err != nil {
return err
}
return nil
}
func (s *SISU) EnrollApplication(id int64) error {
status := "ENROLLED"
update := types.ApplicationUpdate{Status: &status}
if _, err := s.applicationRepo.UpdateApplication(id, update); err != nil {
return err
}
return nil
}
func (s *SISU) ClearApplicationStatus(id int64) error {
status := "APPROVED"
update := types.ApplicationUpdate{Status: &status}
if _, err := s.applicationRepo.UpdateApplication(id, update); err != nil {
return err
}
return nil
}
func (s *SISU) DeleteRollcall(id int64) error {
if err := s.rollcallRepo.Begin(); err != nil {
return fmt.Errorf("rollcall repo begin: %w", err)
}
defer s.rollcallRepo.Rollback()
applicationFilter := types.ApplicationsFilter{
RollcallID: &id,
}
applications, err := s.applicationRepo.FindApplications(applicationFilter)
if err != nil {
return fmt.Errorf("find applications: %w", err)
}
status := "WAITING"
var rollcallID int64 = 0
applicationUpdate := types.ApplicationUpdate{
Status: &status,
RollcallID: &rollcallID,
}
for _, application := range applications {
if _, err := s.applicationRepo.UpdateApplication(application.ID, applicationUpdate); err != nil {
return fmt.Errorf("update application: %w", err)
}
}
if err := s.rollcallRepo.DeleteRollcall(id); err != nil {
return fmt.Errorf("delete rollcall: %w", err)
}
if err := s.rollcallRepo.Commit(); err != nil {
return fmt.Errorf("rollcall repo commit: %w", err)
}
return nil
}
func (s *SISU) AbsentApplication(id int64) error {
status := "ABSENT"
update := types.ApplicationUpdate{Status: &status}
if _, err := s.applicationRepo.UpdateApplication(id, update); err != nil {
return err
}
return nil
}
func (s *SISU) FetchApprovedSelection() (*types.Selection, error) {
selection, err := s.selectionRepo.FindSelectionByKind(types.ApprovedSelection)
if err != nil {
return nil, err
}
filter := types.ApplicationsFilter{
SelectionID: &selection.ID,
}
applications, err := s.applicationRepo.FindApplications(filter)
if err != nil {
return nil, err
}
selection.Applications = applications
return selection, nil
}
func (s *SISU) FetchInterestedSelection() (*types.Selection, error) {
selection, err := s.selectionRepo.FindSelectionByKind(types.InterestedSelection)
if err != nil {
return nil, fmt.Errorf("find selection by kind: %w", err)
}
filter := types.ApplicationsFilter{
SelectionID: &selection.ID,
}
applications, err := s.applicationRepo.FindApplications(filter)
if err != nil {
return nil, fmt.Errorf("find application by selection: %w", err)
}
selection.Applications = applications
return selection, nil
}
func (s *SISU) FetchRollcalls() ([]*types.Rollcall, error) {
rollcalls, err := s.rollcallRepo.FindRollcalls(types.RollcallsFilter{})
if err != nil {
return nil, err
}
return rollcalls, nil
}
func (s *SISU) FetchPeriods() ([]*types.Period, error) {
periods, err := s.classRepo.FindPeriods()
if err != nil {
return nil, err
}
return periods, nil
}
func (s *SISU) FetchRollcallNumber(id int64) (int64, error) {
rollcall, err := s.rollcallRepo.FindRollcallByID(&id)
if err != nil {
return 0, err
}
return rollcall.Number, nil
}
func (s *SISU) FetchApplicationsByRollCall(id int64) ([]*types.Application, error) {
filter := types.ApplicationsFilter{
RollcallID: &id,
}
applications, err := s.applicationRepo.FindApplications(filter)
if err != nil {
return nil, err
}
return applications, nil
}
func (s *SISU) FetchSelection(selection types.SelectionKind) (*types.Selection, error) {
switch selection {
case types.ApprovedSelection:
return s.FetchApprovedSelection()
case types.InterestedSelection:
return s.FetchInterestedSelection()
}
// TODO
return nil, errors.New("TODO")
}
func (s *SISU) FetchPeriodName(periodID int64) (string, error) {
period, err := s.classRepo.FindPeriodByID(periodID)
if err != nil {
return "", fmt.Errorf("find period by id: %w", err)
}
return period.Name, nil
}
func (s *SISU) WebsitePDF(rollcallID int64, periodID int64, file string) error {
selection, err := s.selectionRepo.FindSelectionByKind(types.ApprovedSelection)
if err != nil {
return fmt.Errorf("find selection by kind: %w", err)
}
classes, err := s.classRepo.FindClassesByPeriodID(periodID)
if err != nil {
return fmt.Errorf("find class by period id: %w", err)
}
rollcall, err := s.rollcallRepo.FindRollcallByID(&rollcallID)
if err != nil {
return fmt.Errorf("find rollcall by id: %w", err)
}
pdf := PDF{
selection: selection,
classes: classes,
rollcall: rollcall,
applicationRepo: &s.applicationRepo,
}
if err := pdf.ToWebsite(file); err != nil {
return fmt.Errorf("pdf to website: %w", err)
}
return nil
}
func (s *SISU) EnrollmentPDF(rollcallID int64, periodID int64, file string) error {
selection, err := s.selectionRepo.FindSelectionByKind(types.ApprovedSelection)
if err != nil {
return fmt.Errorf("find selection by kind: %w", err)
}
classes, err := s.classRepo.FindClassesByPeriodID(periodID)
if err != nil {
return fmt.Errorf("find class by period id: %w", err)
}
rollcall, err := s.rollcallRepo.FindRollcallByID(&rollcallID)
if err != nil {
return fmt.Errorf("find rollcall by id: %w", err)
}
pdf := PDF{
selection: selection,
classes: classes,
rollcall: rollcall,
applicationRepo: &s.applicationRepo,
}
if err := pdf.ToEnrollment(file); err != nil {
return fmt.Errorf("pdf to website: %w", err)
}
return nil
}
func (s *SISU) EmailPDF(rollcallID int64, periodID int64, file string) error {
selection, err := s.selectionRepo.FindSelectionByKind(types.ApprovedSelection)
if err != nil {
return fmt.Errorf("find selection by kind: %w", err)
}
classes, err := s.classRepo.FindClassesByPeriodID(periodID)
if err != nil {
return fmt.Errorf("find class by period id: %w", err)
}
rollcall, err := s.rollcallRepo.FindRollcallByID(&rollcallID)
if err != nil {
return fmt.Errorf("find rollcall by id: %w", err)
}
pdf := PDF{
selection: selection,
classes: classes,
rollcall: rollcall,
applicationRepo: &s.applicationRepo,
}
if err := pdf.ToEmail(file); err != nil {
return fmt.Errorf("pdf to website: %w", err)
}
return nil
}
func (s *SISU) TeacherPDF(periodID int64, file string) error {
selection, err := s.selectionRepo.FindSelectionByKind(types.ApprovedSelection)
if err != nil {
return fmt.Errorf("find selection by kind: %w", err)
}
classes, err := s.classRepo.FindClassesByPeriodID(periodID)
if err != nil {
return fmt.Errorf("find class by period id: %w", err)
}
pdf := PDF{
selection: selection,
classes: classes,
rollcall: nil,
applicationRepo: &s.applicationRepo,
}
if err := pdf.ToTeacher(file); err != nil {
return fmt.Errorf("pdf to website: %w", err)
}
return nil
}
func (s *SISU) DeleteSelection(kind types.SelectionKind) error {
if err := s.selectionRepo.Begin(); err != nil {
return fmt.Errorf("begin: %w", err)
}
defer s.selectionRepo.Rollback()
selection, err := s.FetchSelection(kind)
if err != nil {
return fmt.Errorf("fetch selection: %w", err)
}
for _, a := range selection.Applications {
if err := s.applicantRepo.DeleteApplicant(a.Applicant.ID); err != nil {
return fmt.Errorf("delete applicant: %w", err)
}
if err := s.applicationRepo.DeleteApplication(a.ID); err != nil {
return fmt.Errorf("delete application: %w", err)
}
}
if kind == types.ApprovedSelection {
if err := s.rollcallRepo.DeleteRollcall(1); err != nil {
return fmt.Errorf("delete rollcall: %w", err)
}
}
if err := s.selectionRepo.DeleteSelection(selection.ID); err != nil {
return fmt.Errorf("delete selection: %w", err)
}
if err := s.selectionRepo.Commit(); err != nil {
return fmt.Errorf("commit: %w", err)
}
return nil
}
func (s *SISU) ExportCSV(file string) error {
status := "ENROLLED"
filter := types.ApplicationsFilter{
Status: &status,
}
applications, err := s.applicationRepo.FindApplications(filter)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("find applications: %w", err)
}
return ApplicationsToCSV(applications, file)
}
func (s *SISU) Backup(file string) error {
return s.service.Backup(file)
}
func (s *SISU) Restore(file string) error {
return s.service.Restore(file)
}
func (s *SISU) Destroy() error {
err := s.rollcallRepo.Close()
if err != nil {
return fmt.Errorf("close: %w", err)
}
s.service.Destroy()
return nil
}
type csvApplication struct {
Stage string `csv:"NU_ETAPA"`
SchedulePeriod string `csv:"DS_TURNO"`
Seats string `csv:"QT_VAGAS_CONCORRENCIA"`
EnrollmentID string `csv:"CO_INSCRICAO_ENEM"`
Name string `csv:"NO_INSCRITO"`
CPF string `csv:"NU_CPF_INSCRITO"`
Date string `csv:"DT_OPERACAO"`
SocialName string `csv:"NO_SOCIAL"`
BirthDate string `csv:"DT_NASCIMENTO"`
Sex string `csv:"TP_SEXO"`
MotherName string `csv:"NO_MAE"`
AddressLine string `csv:"DS_LOGRADOURO"`
HouseNumber string `csv:"NU_ENDERECO"`
AddressLine2 string `csv:"DS_COMPLEMENTO"`
State string `csv:"SG_UF_INSCRITO"`
Municipality string `csv:"NO_MUNICIPIO"`
Neighborhood string `csv:"NO_BAIRRO"`
CEP string `csv:"NU_CEP"`
Phone1 string `csv:"NU_FONE1"`
Phone2 string `csv:"NU_FONE2"`
Email string `csv:"DS_EMAIL"`
LanguagesScore string `csv:"NU_NOTA_L"`
HumanitiesScore string `csv:"NU_NOTA_CH"`
NaturalSciencesScore string `csv:"NU_NOTA_CN"`
MathematicsScore string `csv:"NU_NOTA_M"`
EssayScore string `csv:"NU_NOTA_R"`
Option string `csv:"ST_OPCAO"`
Quota string `csv:"NO_MODALIDADE_CONCORRENCIA"`
CompositeScore string `csv:"NU_NOTA_CANDIDATO"`
MinimumScore string `csv:"NU_NOTACORTE_CONCORRIDA"`
Ranking string `csv:"NU_CLASSIFICACAO"`
Institution string `csv:"SG_IES"`
Course string `csv:"NO_CURSO"`
}
// TODO: write test for this func.
func parseCSVApplicants(
csvApplicationReader io.Reader,
status string,
) ([]*csvApplication, error) {
preparedCSVReader, err := prepareCSV(csvApplicationReader)
if err != nil {
return nil, fmt.Errorf("prepare CSV: %w", err)
}
// We use LazyQuotes because some fields are surrounded by quotes while
// others don't. Same comment from prepareCSV() applies here, we have no clue
// if this is like this at source.
gocsv.SetCSVReader(func(in io.Reader) gocsv.CSVReader {
c := csv.NewReader(in)
c.Comma = ';'
c.LazyQuotes = true
return c
})
csvApplicants := []*csvApplication{}
if err := gocsv.Unmarshal(preparedCSVReader, &csvApplicants); err != nil {
return nil, fmt.Errorf("CSV unmarshal: %w", err)
}
return csvApplicants, nil
}
func (a *csvApplication) ToApplication(status string) (*types.Application, error) {
minimumScore, err := scoreStringToFloat(a.MinimumScore)
if err != nil {
return nil, fmt.Errorf("minimum score string to float: %w", err)
}
seats, err := strconv.Atoi(a.Seats)
if err != nil {
return nil, fmt.Errorf("seats string to int: %w", err)
}
option, err := strconv.Atoi(a.Option)
if err != nil {
return nil, fmt.Errorf("option string to int: %w", err)
}
languagesScore, err := scoreStringToFloat(a.LanguagesScore)
if err != nil {
return nil, fmt.Errorf("languages score string to float: %w", err)
}
humanitiesScore, err := scoreStringToFloat(a.HumanitiesScore)
if err != nil {
return nil, fmt.Errorf("humanities score string to float: %w", err)
}
naturalSciencesScore, err := scoreStringToFloat(a.NaturalSciencesScore)
if err != nil {
return nil, fmt.Errorf("natural sciences score string to float: %w", err)
}
mathematicsScore, err := scoreStringToFloat(a.MathematicsScore)
if err != nil {
return nil, fmt.Errorf("mathematics score string to float: %w", err)
}
essayScore, err := scoreStringToFloat(a.EssayScore)
if err != nil {
return nil, fmt.Errorf("essay score string to float: %w", err)
}
compositeScore, err := scoreStringToFloat(a.CompositeScore)
if err != nil {
return nil, fmt.Errorf("composite score string to float: %w", err)
}
ranking, err := strconv.Atoi(a.Ranking)
if err != nil {
return nil, fmt.Errorf("ranking string to int: %w", err)
}
return &types.Application{
ID: 0,
Status: status,
EnrollmentID: a.EnrollmentID,
Option: option,
LanguagesScore: languagesScore,
HumanitiesScore: humanitiesScore,
NaturalSciencesScore: naturalSciencesScore,
MathematicsScore: mathematicsScore,
EssayScore: essayScore,
CompositeScore: compositeScore,
Ranking: ranking,
Applicant: types.Applicant{
CPF: a.CPF,
Name: a.Name,
SocialName: a.SocialName,
BirthDate: a.BirthDate,
Sex: a.Sex,
MotherName: a.MotherName,
AddressLine: a.AddressLine,
AddressLine2: a.AddressLine2,
HouseNumber: a.HouseNumber,
Neighborhood: a.Neighborhood,
Municipality: a.Municipality,
State: a.State,
CEP: a.CEP,
Email: a.Email,
Phone1: a.Phone1,
Phone2: a.Phone2,
},
Class: types.Class{
ID: 0,
Period: types.Period{
Name: a.SchedulePeriod,
},
Quota: types.Quota{
Name: a.Quota,
},
Seats: seats,
MinimumScore: minimumScore,
},
}, nil
}
func scoreStringToFloat(s string) (float64, error) {
score, err := strconv.ParseFloat(strings.ReplaceAll(s, ",", "."), 64)
if err != nil {
return 0, fmt.Errorf("parse float: %w", err)
}
return score, nil
}
/*
SISU's CSV file has peculiarities that require handling before parsing:
- First character in some cases is a UTF-8 BOM, hence the SkipOnly call;
- Line breaks are encoded using CR instead of LF (go expects LF);
- The first line is separated by commas instead of semicolons (as the rest
of the file is).
Note that we're not sure if the file comes like this or if all of this is made
by someones excel before the CSV gets here, but we have no direct access to
the source file, so we have to deal with it.
*/
func prepareCSV(reader io.Reader) (io.Reader, error) {
rawCSV, err := io.ReadAll(utfbom.SkipOnly(reader))
if err != nil {
return nil, fmt.Errorf("read all: %w", err)
}
lines := strings.Split(string(rawCSV), "\r")
lines[0] = strings.ReplaceAll(lines[0], ",", ";")
preparedReader := bytes.NewReader([]byte(strings.Join(lines, "\n")))
return preparedReader, nil
}