| | 1 | | using Syki.Back.Features.Academic.StartClasses; |
| | 2 | | using Syki.Back.Features.Student.CreateStudentEnrollment; |
| | 3 | | using Syki.Back.Features.Academic.UpdateEnrollmentPeriod; |
| | 4 | | using Syki.Back.Features.Academic.CreateEnrollmentPeriod; |
| | 5 | | using Syki.Back.Features.Academic.ReleaseClassesForEnrollment; |
| | 6 | |
|
| | 7 | | namespace Syki.Back.Features.Cross.SeedInstitutionData; |
| | 8 | |
|
| | 9 | | [CommandDescription("Realizar seed de matrículas da instituição")] |
| | 10 | | public record SeedInstitutionEnrollmentsCommand(Guid InstitutionId, Guid AdsCourseCurriculumId) : ICommand; |
| | 11 | |
|
| 0 | 12 | | public class SeedInstitutionEnrollmentsCommandHandler( |
| 0 | 13 | | SykiDbContext ctx, |
| 0 | 14 | | StartClassesService startClassesService, |
| 0 | 15 | | CreateEnrollmentPeriodService createEnrollmentPeriodService, |
| 0 | 16 | | UpdateEnrollmentPeriodService updateEnrollmentPeriodService, |
| 0 | 17 | | CreateStudentEnrollmentService createStudentEnrollmentService, |
| 0 | 18 | | ReleaseClassesForEnrollmentService releaseClassesForEnrollmentService) : ICommandHandler<SeedInstitutionEnrollmentsC |
| | 19 | | { |
| | 20 | | public async Task Handle(CommandId commandId, SeedInstitutionEnrollmentsCommand command) |
| | 21 | | { |
| 0 | 22 | | var id = command.InstitutionId; |
| 0 | 23 | | var today = DateTime.UtcNow.ToDateOnly(); |
| 0 | 24 | | var academicPeriod = $"{today.Year}.1"; |
| 0 | 25 | | var firstDay = new DateTime(today.Year, 1, 1).ToDateOnly(); |
| 0 | 26 | | var lastDay = new DateTime(today.Year, 12, 31).ToDateOnly(); |
| | 27 | |
|
| 0 | 28 | | await createEnrollmentPeriodService.CreateWithThrowOnError(id, new() |
| 0 | 29 | | { |
| 0 | 30 | | Id = academicPeriod, |
| 0 | 31 | | StartAt = firstDay, |
| 0 | 32 | | EndAt = lastDay, |
| 0 | 33 | | }); |
| | 34 | |
|
| 0 | 35 | | var classes = await ctx.Classes.Where(x => x.InstitutionId == id).Select(x => x.Id).ToListAsync(); |
| 0 | 36 | | await releaseClassesForEnrollmentService.ReleaseWithThrowOnError(id, new() { Classes = classes }); |
| | 37 | |
|
| 0 | 38 | | var adsCourseOfferingId = await ctx.CourseOfferings |
| 0 | 39 | | .Where(x => x.InstitutionId == id && x.CourseCurriculumId == command.AdsCourseCurriculumId) |
| 0 | 40 | | .Select(x => x.Id).FirstAsync(); |
| | 41 | |
|
| 0 | 42 | | var students = await ctx.Students.Where(x => x.CourseOfferingId == adsCourseOfferingId).Select(x => new { x.Id } |
| 0 | 43 | | foreach (var student in students) |
| | 44 | | { |
| 0 | 45 | | await createStudentEnrollmentService.CreateWithThrowOnError(id, student.Id, command.AdsCourseCurriculumId, n |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | await updateEnrollmentPeriodService.UpdateWithThrowOnError(id, academicPeriod, new() { StartAt = new DateTime(to |
| | 49 | |
|
| 0 | 50 | | await startClassesService.StartWithThrowOnError(id, new() { Classes = classes }); |
| | 51 | |
|
| 0 | 52 | | ctx.AddCommand(id, new SeedInstitutionLessonAttendancesCommand(id), parentId: commandId); |
| 0 | 53 | | } |
| | 54 | | } |