Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.27 KB | None | 0 0
  1. using FluentValidation;
  2. using Halza.MobApi.Core;
  3. using Halza.MobApi.Services.Features.Allergies;
  4. using Halza.MobApi.Services.TranslationResources;
  5. using Halza.SharedDataAccess.Entities.Accounts;
  6. using Halza.SharedDataAccess.Entities.Accounts.Enums;
  7. using Halza.SharedDataAccess.Entities.Allergies.Enums;
  8. using Halza.SharedDataAccess.Entities.MedicalData;
  9. using Microsoft.EntityFrameworkCore;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using SaveAllergiesModel = Halza.MobApi.Services.Features.Allergies.SaveAllergies.Request;
  17.  
  18. namespace Halza.MobApi.Services.Features.Accounts
  19. {
  20.     public class UpdateProfileDetails
  21.     {
  22.         public class Request : BaseRequest<Response>
  23.         {
  24.             public string FirstName { get; set; }
  25.             public string LastName { get; set; }
  26.             public DateTime? Birthday { get; set; }
  27.             public GenderEnum? Gender { get; set; }
  28.             public string EmergencyContactNumber { get; set; }
  29.             public string ContactNumber { get; set; }
  30.             public string Address { get; set; }
  31.             public string PostCode { get; set; }
  32.             public string City { get; set; }
  33.             public decimal Height { get; set; }
  34.             public decimal Weight { get; set; }
  35.             public BloodGroupEnum? BloodGroup { get; set; }
  36.             public bool Smoking { get; set; }
  37.             public string PastSugeries { get; set; }
  38.             public Guid? NationalityId { get; set; }
  39.             public Guid? CountryOfResidenceId { get; set; }
  40.             public RaceEnum? Ethnicity { get; set; }
  41.             public string PassportNumber { get; set; }
  42.             public string InsuriencePolicyNumber { get; set; }
  43.             public DateTime? InsuriencePolicyExpiryDate { get; set; }
  44.             public string InsurienceCompany { get; set; }
  45.             public string Allergies { get; set; }
  46.         }
  47.  
  48.         public class Response : BaseResponse
  49.         {
  50.  
  51.         }
  52.  
  53.         public class Validator : BaseValidator<Request>
  54.         {
  55.             public override IValidator Setup()
  56.             {
  57.                 RuleFor(x => x.FirstName).NotEmpty().WithMessage(Translate(nameof(Translations.FirstNameRequired)));
  58.                 RuleFor(x => x.LastName).NotEmpty().WithMessage(Translate(nameof(Translations.LastNameRequired)));
  59.                 RuleFor(x => x.Birthday).NotEmpty().WithMessage(Translate(nameof(Translations.BirthdayRequired)))
  60.                                         .LessThan(DateTime.Today).WithMessage(Translate(nameof(Translations.BirthdayRequired)));
  61.  
  62.                 return this;
  63.             }
  64.         }
  65.  
  66.         public class Handler : BaseHandler<Request, Response>
  67.         {
  68.             public override async Task<Response> Handle(Request request, System.Threading.CancellationToken cancellationToken)
  69.             {
  70.                 await UpdateAccount(request);
  71.  
  72.                 await UpdateHealthParameterRecordReadingByType(request.Weight, "Weight");
  73.  
  74.                 await UpdateHealthParameterRecordReadingByType(request.Height, "Height");
  75.  
  76.                 return new Response();
  77.             }
  78.  
  79.             async Task UpdateHealthParameterRecordReadingByType(decimal healthParameterValue, string healthParameterName)
  80.             {
  81.                 if (healthParameterValue == 0)
  82.                     return;
  83.  
  84.                 var healthParameterReadings = await _ef.HealthParameterReadings.FirstOrDefaultAsync(x => x.Name.Equals(healthParameterName));
  85.  
  86.                 if (!await _ef.NN_Account_HealthParameters.AnyAsync(x => x.HealthParameter.Equals(healthParameterReadings.HealthParameterId)
  87.                                                                       && x.AccountId.Equals(_currentUser.Id)))
  88.                 {
  89.                     Add(new NN_Account_HealthParameter
  90.                     {
  91.                         AccountId = _currentUser.Id,
  92.                         HealthParameterId = healthParameterReadings.HealthParameterId,
  93.                         IsVisible = true
  94.                     });
  95.                 }
  96.  
  97.                 if (await _ef.HealthParameterReadingRecords
  98.                                                          .Include(x => x.HealthParameter)
  99.                                                          .Include(x => x.Account)
  100.                                                          .Where(x => x.HealthParameter.Name.Equals(healthParameterName)
  101.                                                                   && x.AccountId.Equals(_currentUser.Id))
  102.                                                          .OrderByDescending(t => t.ModifiedOn)
  103.                                                          .Select(x => x.Value)
  104.                                                          .FirstOrDefaultAsync() != healthParameterValue && healthParameterValue != 0)
  105.  
  106.                     Add(new HealthParameterReadingRecord
  107.                     {
  108.                         AccountId = _currentUser.Id,
  109.                         HealthParameterId = healthParameterReadings.HealthParameterId,
  110.                         HealthParameterReadingId = healthParameterReadings.Id,
  111.                         Value = healthParameterValue,
  112.                         DateOfRecord = DateTime.UtcNow,
  113.                         RecordNumber = Guid.NewGuid()
  114.                     });
  115.             }
  116.  
  117.             async Task UpdateAccount(Request request)
  118.             {
  119.                 var account = await _ef.Accounts.OfType<Patient>().FirstOrDefaultAsync(x => x.Id.Equals(_currentUser.Id) && !x.IsDeleted);
  120.  
  121.                 if (account == null)
  122.                     throw new CoreException(Translate(nameof(Translations.AccountNotExists)));
  123.  
  124.                 if (request.Birthday > DateTime.UtcNow.AddYears(-21))
  125.                 {
  126.                     if (!string.IsNullOrEmpty(account.EmailAddress) && !await _ef.SubscriberDetails.Where(x => x.Email.Equals(account.EmailAddress.Trim())).AnyAsync())
  127.                         throw new CoreException(Translate(nameof(Translations.YouShouldBeOlderThan21)));
  128.                     else if (!await _ef.SubscriberDetails.Where(x => x.UserId.Equals(account.Id)).AnyAsync())
  129.                         throw new CoreException(Translate(nameof(Translations.YouShouldBeOlderThan21)));
  130.                 }
  131.  
  132.                 account.FirstName = request.FirstName.Trim();
  133.                 account.LastName = request.LastName.Trim();
  134.                 account.DateOfBirth = request.Birthday;
  135.                 account.Gender = request.Gender;
  136.                 account.EmergencyContactNumber = request.EmergencyContactNumber;
  137.                 account.ContactNumber = request.ContactNumber;
  138.                 account.StreetAddress = request.Address;
  139.                 account.ZipCode = request.PostCode;
  140.                 account.City = request.City;
  141.                 account.BloodGroup = request.BloodGroup;
  142.                 account.IsSmoker = request.Smoking;
  143.                 account.PastSurgeries = request.PastSugeries;
  144.                 account.NationalityId = request.NationalityId;
  145.                 account.CountryId = request.CountryOfResidenceId;
  146.                 account.Race = request.Ethnicity;
  147.                 account.Passport = request.PassportNumber;
  148.                 account.InsurancePolicyNumber = request.InsuriencePolicyNumber;
  149.                 account.InsuranceExpiryDate = request.InsuriencePolicyExpiryDate;
  150.                 account.InsuranceCompany = request.InsurienceCompany;
  151.                 Update(account);
  152.  
  153.                 if(!String.IsNullOrEmpty(request.Allergies))
  154.                 {
  155.                     var allergiesFood = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId.Equals(_currentUser.Id) && x.AllergiesType.Equals(AllergiesTypeEnum.Food));
  156.                     var allergiesDrug = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId.Equals(_currentUser.Id) && x.AllergiesType.Equals(AllergiesTypeEnum.Drug));
  157.  
  158.                     SaveAllergies.Request model = new SaveAllergies.Request();
  159.  
  160.                     if (allergiesFood == null && allergiesDrug == null)
  161.                     {
  162.                         model.FoodAllergies = "";
  163.                         model.DrugAllergies = "";
  164.                         model.OtherAllergies = request.Allergies;
  165.                     }
  166.                     else
  167.                     {
  168.                         string[] allergiesAll = request.Allergies.Split(',');
  169.                         var allergiesOther = new List<string>();
  170.                         for (int i = 0; i < allergiesAll.Length; i++)
  171.                             if (!allergiesFood.AllergiesNames.Contains(allergiesAll[i]) && !allergiesDrug.AllergiesNames.Contains(allergiesAll[i]))
  172.                                 allergiesOther.Add(allergiesAll[i]);
  173.  
  174.                         model.FoodAllergies = allergiesFood.AllergiesNames;
  175.                         model.DrugAllergies = allergiesDrug.AllergiesNames;
  176.                         model.OtherAllergies = string.Join(",", allergiesOther);
  177.                     }
  178.              
  179.                     await UpdateAllergies(model);
  180.                 }
  181.                    
  182.             }
  183.  
  184.             async Task  UpdateAllergies(SaveAllergiesModel request)
  185.             {
  186.                 var accountAllergies = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId == _currentUser.Id && x.AllergiesType == AllergiesTypeEnum.Drug);
  187.  
  188.                 accountAllergies = accountAllergies ?? new SharedDataAccess.Entities.Allergies.Allergies();
  189.                 accountAllergies.AccountId = _currentUser.Id;
  190.                 accountAllergies.AllergiesNames = request.DrugAllergies;
  191.                 accountAllergies.AllergiesType = AllergiesTypeEnum.Drug;
  192.                 Update(accountAllergies);
  193.  
  194.                 accountAllergies = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId == _currentUser.Id && x.AllergiesType == AllergiesTypeEnum.Food);
  195.  
  196.                 accountAllergies = accountAllergies ?? new SharedDataAccess.Entities.Allergies.Allergies();
  197.                 accountAllergies.AccountId = _currentUser.Id;
  198.                 accountAllergies.AllergiesNames = request.FoodAllergies;
  199.                 accountAllergies.AllergiesType = AllergiesTypeEnum.Food;
  200.                 Update(accountAllergies);
  201.  
  202.                 accountAllergies = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId == _currentUser.Id && x.AllergiesType == AllergiesTypeEnum.Others);
  203.  
  204.                 accountAllergies = accountAllergies ?? new SharedDataAccess.Entities.Allergies.Allergies();
  205.                 accountAllergies.AccountId = _currentUser.Id;
  206.                 accountAllergies.AllergiesNames = request.OtherAllergies;
  207.                 accountAllergies.AllergiesType = AllergiesTypeEnum.Others;
  208.                 Update(accountAllergies);
  209.             }
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement