| | 1 | | using Syki.Back.Features.Cross.CreateUser; |
| | 2 | | using Syki.Back.Features.Cross.SendResetPasswordToken; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Features.Academic.CreateStudent; |
| | 5 | |
|
| 362 | 6 | | public class CreateStudentService(SykiDbContext ctx, CreateUserService createService, SendResetPasswordTokenService send |
| | 7 | | { |
| | 8 | | public async Task<OneOf<StudentOut, SykiError>> Create(Guid institutionId, CreateStudentIn data) |
| | 9 | | { |
| 96 | 10 | | await using var transaction = await ctx.Database.BeginTransactionAsync(); |
| | 11 | |
|
| 96 | 12 | | var courseOfferingExists = await ctx.CourseOfferings |
| 96 | 13 | | .AnyAsync(o => o.InstitutionId == institutionId && o.Id == data.CourseOfferingId); |
| 97 | 14 | | if (!courseOfferingExists) return new CourseOfferingNotFound(); |
| | 15 | |
|
| 95 | 16 | | var userIn = CreateUserIn.NewStudent(institutionId, data.Name, data.Email, data.PhoneNumber); |
| 95 | 17 | | var result = await createService.Create(userIn); |
| | 18 | |
|
| 113 | 19 | | if (result.IsError()) return result.GetError(); |
| | 20 | |
|
| 77 | 21 | | var user = result.GetSuccess(); |
| 77 | 22 | | var student = new SykiStudent(user.Id, institutionId, data.Name, data.CourseOfferingId); |
| | 23 | |
|
| 77 | 24 | | ctx.Add(student); |
| 77 | 25 | | ctx.Add(SykiTask.LinkOldNotifications(user.Id, institutionId)); |
| | 26 | |
|
| 77 | 27 | | await sendService.Send(new(user.Email)); |
| | 28 | |
|
| 77 | 29 | | await transaction.CommitAsync(); |
| | 30 | |
|
| 77 | 31 | | return student.ToOut(); |
| 96 | 32 | | } |
| | 33 | | } |