| | 1 | | using Syki.Back.Features.Cross.GenerateJWT; |
| | 2 | | using Syki.Back.Features.Cross.FinishUserRegister; |
| | 3 | | using Syki.Back.Features.Cross.CreatePendingUserRegister; |
| | 4 | |
|
| | 5 | | namespace Syki.Back.Features.Cross.SkipUserRegister; |
| | 6 | |
|
| 0 | 7 | | public class SkipUserRegisterService( |
| 0 | 8 | | SykiDbContext ctx, |
| 0 | 9 | | GenerateJWTService generateJWTService, |
| 0 | 10 | | FinishUserRegisterService finishService, |
| 0 | 11 | | CreatePendingUserRegisterService createService |
| 0 | 12 | | ) : ICrossService |
| | 13 | | { |
| | 14 | | public async Task<OneOf<SkipUserRegisterLoginOut, SykiError>> Skip(SkipUserRegisterLoginIn data) |
| | 15 | | { |
| 0 | 16 | | var user = await ctx.Users.AsNoTracking().Where(x => x.Id == data.UserId).FirstOrDefaultAsync(); |
| | 17 | |
|
| 0 | 18 | | if (user == null) data.UserId = await Register(); |
| | 19 | |
|
| 0 | 20 | | return await Login(data.UserId); |
| 0 | 21 | | } |
| | 22 | |
|
| | 23 | | public async Task<Guid> Register() |
| | 24 | | { |
| 0 | 25 | | var email = $"academico.{Guid.NewGuid().ToString().OnlyNumbers()[..8]}@syki.skip.com"; |
| 0 | 26 | | var password = $"Academic@{Guid.NewGuid().ToString().OnlyNumbers()}"; |
| | 27 | |
|
| 0 | 28 | | await createService.Create(new(email)); |
| | 29 | |
|
| 0 | 30 | | var register = await ctx.UserRegisters.FirstAsync(d => d.Email == email); |
| | 31 | |
|
| 0 | 32 | | var user = await finishService.Finish(new(register.Id.ToString(), password)); |
| 0 | 33 | | return user.GetSuccess().Id; |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public async Task<SkipUserRegisterLoginOut> Login(Guid userId) |
| | 37 | | { |
| 0 | 38 | | var user = await ctx.Users.AsNoTracking().Where(x => x.Id == userId).FirstAsync(); |
| | 39 | |
|
| 0 | 40 | | var jwt = await generateJWTService.Generate(user.Email); |
| | 41 | |
|
| 0 | 42 | | return new() { UserId = userId, AccessToken = jwt }; |
| 0 | 43 | | } |
| | 44 | | } |