| | 1 | | using Syki.Back.Features.Cross.CreateUser; |
| | 2 | | using Syki.Back.Features.Cross.CreateInstitution; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Features.Cross.FinishUserRegister; |
| | 5 | |
|
| 291 | 6 | | public class FinishUserRegisterService(SykiDbContext ctx, CreateUserService service) : ICrossService |
| | 7 | | { |
| | 8 | | public async Task<OneOf<UserOut, SykiError>> Finish(FinishUserRegisterIn data) |
| | 9 | | { |
| 291 | 10 | | await using var transaction = await ctx.Database.BeginTransactionAsync(); |
| | 11 | |
|
| 291 | 12 | | _ = Guid.TryParse(data.Token, out var id); |
| 291 | 13 | | var register = await ctx.UserRegisters.FirstOrDefaultAsync(d => d.Id == id); |
| 302 | 14 | | if (register == null) return new InvalidRegistrationToken(); |
| | 15 | |
|
| 280 | 16 | | var finish = register.Finish(); |
| 281 | 17 | | if (finish.IsError()) return finish.GetError(); |
| | 18 | |
|
| 279 | 19 | | var institution = new Institution(register.Email.OnlyNumbers()); |
| 279 | 20 | | ctx.Add(institution); |
| | 21 | |
|
| 279 | 22 | | ctx.Add(SykiTask.SeedInstitutionData(institution.Id)); |
| | 23 | |
|
| 279 | 24 | | await ctx.SaveChangesAsync(); |
| | 25 | |
|
| 279 | 26 | | var userIn = CreateUserIn.NewAcademic(institution.Id, register.Email, data.Password); |
| 279 | 27 | | var result = await service.Create(userIn); |
| | 28 | |
|
| 287 | 29 | | if (result.IsError()) return result.GetError(); |
| | 30 | |
|
| 271 | 31 | | var user = result.GetSuccess(); |
| | 32 | |
|
| 271 | 33 | | await ctx.SaveChangesAsync(); |
| 271 | 34 | | await transaction.CommitAsync(); |
| | 35 | |
|
| 271 | 36 | | return user; |
| 291 | 37 | | } |
| | 38 | | } |