using Shared.DTOs; using Back.Data.Contracts; using Back.Data.Models; using Microsoft.EntityFrameworkCore; namespace Back.Services { public class ServValidatinMsg { private readonly IAsyncRepository _verificationCodeRepo; private readonly IAsyncRepository _ticket; private readonly IAsyncRepository _UserRepo; private readonly IAsyncRepository _CompanyRepo; private readonly servSendMsg _servSendMsg; public ServValidatinMsg(IAsyncRepository verificationCodeRepo , IAsyncRepository ticket, IAsyncRepository UserRepo , IAsyncRepository CompanyRepo, servSendMsg servSendMsg) { _verificationCodeRepo = verificationCodeRepo; _ticket = ticket; _UserRepo = UserRepo; _CompanyRepo = CompanyRepo; _servSendMsg = servSendMsg; } public async Task GetCodeByPrm(string Prm) { return await _verificationCodeRepo.Get(w => w.prm == Prm).FirstOrDefaultAsync(); } public async Task GetVerificationCode(int ID) { return await _verificationCodeRepo.Get(w => w.Code == ID).FirstOrDefaultAsync(); } public async Task GetVerificationCodeByID(int ID) { return await _verificationCodeRepo.Get(w => w.ID == ID).FirstOrDefaultAsync(); } public async Task Delete(VerificationCode model) { return await _verificationCodeRepo.DeleteAsync(model); } public async Task SubmittedTicket(VerificationCode code) { var ticket = await _ticket.Get(w => w.ID == Convert.ToInt32(code.prm)).FirstOrDefaultAsync(); if (ticket != null && ticket.Status== StatusTicket.unknownPerson) { ticket.Status = StatusTicket.Awaitingreview; return await _ticket.UpdateAsync(ticket); } return false; } public async Task SubmittedCompanyRegistration(VerificationCode code) { var user = await _UserRepo.Get(w => w.ID == Convert.ToInt32(code.val) && !w.IsActive).FirstOrDefaultAsync(); var company = await _CompanyRepo.Get(w => w.ID == Convert.ToInt32(code.prm) && !w.IsActive).FirstOrDefaultAsync(); if (user != null && company != null) { user.IsActive = true; if (await _UserRepo.UpdateAsync(user) != null) { company.IsActive = true; if (await _CompanyRepo.UpdateAsync(company)) { _servSendMsg.SuccessfulRegistration(user.Mobile, user.Mobile,user.Mobile); return true; } } } return false; } public async Task GenerateCode(VerificationCode code) { code.Code = Random.Shared.Next(1000, 9000); while (await GetVerificationCode(code.Code) != null) code.Code = Random.Shared.Next(1000, 9000); var indb= await _verificationCodeRepo.AddAsync(code); code.ID = indb.ID; return code; } } }