Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using FluentValidation;
- using Halza.MobApi.Core;
- using Halza.MobApi.Services.Features.Allergies;
- using Halza.MobApi.Services.TranslationResources;
- using Halza.SharedDataAccess.Entities.Accounts;
- using Halza.SharedDataAccess.Entities.Accounts.Enums;
- using Halza.SharedDataAccess.Entities.Allergies.Enums;
- using Halza.SharedDataAccess.Entities.MedicalData;
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using SaveAllergiesModel = Halza.MobApi.Services.Features.Allergies.SaveAllergies.Request;
- namespace Halza.MobApi.Services.Features.Accounts
- {
- public class UpdateProfileDetails
- {
- public class Request : BaseRequest<Response>
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public DateTime? Birthday { get; set; }
- public GenderEnum? Gender { get; set; }
- public string EmergencyContactNumber { get; set; }
- public string ContactNumber { get; set; }
- public string Address { get; set; }
- public string PostCode { get; set; }
- public string City { get; set; }
- public decimal Height { get; set; }
- public decimal Weight { get; set; }
- public BloodGroupEnum? BloodGroup { get; set; }
- public bool Smoking { get; set; }
- public string PastSugeries { get; set; }
- public Guid? NationalityId { get; set; }
- public Guid? CountryOfResidenceId { get; set; }
- public RaceEnum? Ethnicity { get; set; }
- public string PassportNumber { get; set; }
- public string InsuriencePolicyNumber { get; set; }
- public DateTime? InsuriencePolicyExpiryDate { get; set; }
- public string InsurienceCompany { get; set; }
- public string Allergies { get; set; }
- }
- public class Response : BaseResponse
- {
- }
- public class Validator : BaseValidator<Request>
- {
- public override IValidator Setup()
- {
- RuleFor(x => x.FirstName).NotEmpty().WithMessage(Translate(nameof(Translations.FirstNameRequired)));
- RuleFor(x => x.LastName).NotEmpty().WithMessage(Translate(nameof(Translations.LastNameRequired)));
- RuleFor(x => x.Birthday).NotEmpty().WithMessage(Translate(nameof(Translations.BirthdayRequired)))
- .LessThan(DateTime.Today).WithMessage(Translate(nameof(Translations.BirthdayRequired)));
- return this;
- }
- }
- public class Handler : BaseHandler<Request, Response>
- {
- public override async Task<Response> Handle(Request request, System.Threading.CancellationToken cancellationToken)
- {
- await UpdateAccount(request);
- await UpdateHealthParameterRecordReadingByType(request.Weight, "Weight");
- await UpdateHealthParameterRecordReadingByType(request.Height, "Height");
- return new Response();
- }
- async Task UpdateHealthParameterRecordReadingByType(decimal healthParameterValue, string healthParameterName)
- {
- if (healthParameterValue == 0)
- return;
- var healthParameterReadings = await _ef.HealthParameterReadings.FirstOrDefaultAsync(x => x.Name.Equals(healthParameterName));
- if (!await _ef.NN_Account_HealthParameters.AnyAsync(x => x.HealthParameter.Equals(healthParameterReadings.HealthParameterId)
- && x.AccountId.Equals(_currentUser.Id)))
- {
- Add(new NN_Account_HealthParameter
- {
- AccountId = _currentUser.Id,
- HealthParameterId = healthParameterReadings.HealthParameterId,
- IsVisible = true
- });
- }
- if (await _ef.HealthParameterReadingRecords
- .Include(x => x.HealthParameter)
- .Include(x => x.Account)
- .Where(x => x.HealthParameter.Name.Equals(healthParameterName)
- && x.AccountId.Equals(_currentUser.Id))
- .OrderByDescending(t => t.ModifiedOn)
- .Select(x => x.Value)
- .FirstOrDefaultAsync() != healthParameterValue && healthParameterValue != 0)
- Add(new HealthParameterReadingRecord
- {
- AccountId = _currentUser.Id,
- HealthParameterId = healthParameterReadings.HealthParameterId,
- HealthParameterReadingId = healthParameterReadings.Id,
- Value = healthParameterValue,
- DateOfRecord = DateTime.UtcNow,
- RecordNumber = Guid.NewGuid()
- });
- }
- async Task UpdateAccount(Request request)
- {
- var account = await _ef.Accounts.OfType<Patient>().FirstOrDefaultAsync(x => x.Id.Equals(_currentUser.Id) && !x.IsDeleted);
- if (account == null)
- throw new CoreException(Translate(nameof(Translations.AccountNotExists)));
- if (request.Birthday > DateTime.UtcNow.AddYears(-21))
- {
- if (!string.IsNullOrEmpty(account.EmailAddress) && !await _ef.SubscriberDetails.Where(x => x.Email.Equals(account.EmailAddress.Trim())).AnyAsync())
- throw new CoreException(Translate(nameof(Translations.YouShouldBeOlderThan21)));
- else if (!await _ef.SubscriberDetails.Where(x => x.UserId.Equals(account.Id)).AnyAsync())
- throw new CoreException(Translate(nameof(Translations.YouShouldBeOlderThan21)));
- }
- account.FirstName = request.FirstName.Trim();
- account.LastName = request.LastName.Trim();
- account.DateOfBirth = request.Birthday;
- account.Gender = request.Gender;
- account.EmergencyContactNumber = request.EmergencyContactNumber;
- account.ContactNumber = request.ContactNumber;
- account.StreetAddress = request.Address;
- account.ZipCode = request.PostCode;
- account.City = request.City;
- account.BloodGroup = request.BloodGroup;
- account.IsSmoker = request.Smoking;
- account.PastSurgeries = request.PastSugeries;
- account.NationalityId = request.NationalityId;
- account.CountryId = request.CountryOfResidenceId;
- account.Race = request.Ethnicity;
- account.Passport = request.PassportNumber;
- account.InsurancePolicyNumber = request.InsuriencePolicyNumber;
- account.InsuranceExpiryDate = request.InsuriencePolicyExpiryDate;
- account.InsuranceCompany = request.InsurienceCompany;
- Update(account);
- if(!String.IsNullOrEmpty(request.Allergies))
- {
- var allergiesFood = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId.Equals(_currentUser.Id) && x.AllergiesType.Equals(AllergiesTypeEnum.Food));
- var allergiesDrug = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId.Equals(_currentUser.Id) && x.AllergiesType.Equals(AllergiesTypeEnum.Drug));
- SaveAllergies.Request model = new SaveAllergies.Request();
- if (allergiesFood == null && allergiesDrug == null)
- {
- model.FoodAllergies = "";
- model.DrugAllergies = "";
- model.OtherAllergies = request.Allergies;
- }
- else
- {
- string[] allergiesAll = request.Allergies.Split(',');
- var allergiesOther = new List<string>();
- for (int i = 0; i < allergiesAll.Length; i++)
- if (!allergiesFood.AllergiesNames.Contains(allergiesAll[i]) && !allergiesDrug.AllergiesNames.Contains(allergiesAll[i]))
- allergiesOther.Add(allergiesAll[i]);
- model.FoodAllergies = allergiesFood.AllergiesNames;
- model.DrugAllergies = allergiesDrug.AllergiesNames;
- model.OtherAllergies = string.Join(",", allergiesOther);
- }
- await UpdateAllergies(model);
- }
- }
- async Task UpdateAllergies(SaveAllergiesModel request)
- {
- var accountAllergies = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId == _currentUser.Id && x.AllergiesType == AllergiesTypeEnum.Drug);
- accountAllergies = accountAllergies ?? new SharedDataAccess.Entities.Allergies.Allergies();
- accountAllergies.AccountId = _currentUser.Id;
- accountAllergies.AllergiesNames = request.DrugAllergies;
- accountAllergies.AllergiesType = AllergiesTypeEnum.Drug;
- Update(accountAllergies);
- accountAllergies = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId == _currentUser.Id && x.AllergiesType == AllergiesTypeEnum.Food);
- accountAllergies = accountAllergies ?? new SharedDataAccess.Entities.Allergies.Allergies();
- accountAllergies.AccountId = _currentUser.Id;
- accountAllergies.AllergiesNames = request.FoodAllergies;
- accountAllergies.AllergiesType = AllergiesTypeEnum.Food;
- Update(accountAllergies);
- accountAllergies = await _ef.Allergies.FirstOrDefaultAsync(x => x.AccountId == _currentUser.Id && x.AllergiesType == AllergiesTypeEnum.Others);
- accountAllergies = accountAllergies ?? new SharedDataAccess.Entities.Allergies.Allergies();
- accountAllergies.AccountId = _currentUser.Id;
- accountAllergies.AllergiesNames = request.OtherAllergies;
- accountAllergies.AllergiesType = AllergiesTypeEnum.Others;
- Update(accountAllergies);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement