using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Globalization; using System.Linq; using taloyhtio.idp.parser.common.domain; using taloyhtio.idp.parser.common.events; //using webapi.Domain.Events; //using webapi.Domain.SeedWork; namespace taloyhtio.idp.parser.common.model { [Table("mdbFlats")] public class Flat : Entity, IAggregateRoot { public Flat() { } public Flat(Guid taloyhtioPMCId, string pmsCondoName, string flatTitle, Guid id) { PMCTaloyhtioId = taloyhtioPMCId; CondoPMS = pmsCondoName; FlatTitle = flatTitle; Id = id; PartitionKey = PMCTaloyhtioId.ToString(); //RowKey = Id.ToString(); } /// /// Guid for identifying the flat /// //public Guid Id { get; set; } /// /// i.e. /// Church street 4 A 1 (usually high-rise has this form) /// Church street 4 A(usually terraced house has this form, letter actually points to a flat) /// NOTE: flat titles which are loaded to AzureDB table should be the same as used in PMS /// public string FlatTitle { get; set; } /// /// Parent Condo of the flat in the same format as returned from PMS /// public string CondoPMS { get; set; } /// /// Taloyhtio PMC Id of condo to which current flat belongs to /// public Guid PMCTaloyhtioId { get; set; } List _users = new List(); public List Users => _users; /// ///Depending on actual method which will be used for loading flats to AzureDB (see below) there also may be information about users which live in each flat /// public string FlatUsers { get { return _users == null || !_users.Any() ? null : JsonConvert.SerializeObject(_users); } set { try { _users = JsonConvert.DeserializeObject>(value); } catch (Exception) { _users = new List(); } } } public void SendApproval(Guid pmcId, Guid userId) { AddDomainEvent(new UserMappingApproveDomainEvent(pmcId, this.Id, userId)); } public void AssignUser(Guid userId, string pmsUserName = null) { AddDomainEvent(new AddUserToFlatMappingDomainEvent(this, userId, pmsUserName)); } /// /// Revoke user to flat mapping /// public void RevokeUserAssignment(string userName, Guid condoId) { AddDomainEvent(new RevokeUserAssignmentDomainEvent(userName, condoId, this)); } public bool CompareUsersByPMSUserName(string pmsUserName) { if (!Users?.Any() ?? false) return false; foreach (var user in Users) { //Check by full matched name if (!string.IsNullOrEmpty(user.DisplayName) && user.DisplayName.Equals(pmsUserName, StringComparison.InvariantCultureIgnoreCase)) { return true; } } return false; } public bool CompareUsersByPIN(string pin, string userFirstName, string userLastName, DateTime? userBirthday, out string matchedPMSUserName) { matchedPMSUserName = null; if (!Users?.Any() ?? true) return false; foreach (var user in Users) { //Check by full matched PIN if (!string.IsNullOrEmpty(user.PIN) && user.PIN.Equals(pin, StringComparison.InvariantCultureIgnoreCase)) { matchedPMSUserName = user.DisplayName; return true; } //Check by partly matched PIN //var pmsUserName = user.DisplayName; var pinParts = user.PIN.Split('-')?[0]; if (DateTime.TryParseExact(pinParts, "ddMMyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var userPINBirthDate)) { var result = (userBirthday.HasValue && userPINBirthDate.Date == userBirthday.Value.Date); if (result) { var userNameParts = userFirstName.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var pmsLastName = userNameParts.Last(); for (var i = 0; i < userNameParts.Count() - 1; i++) { var pmsFirstName = userNameParts[i]; var userDispalyName = $"{userFirstName} {userLastName}"; if (userDispalyName.Equals($"{pmsFirstName} {pmsLastName}", StringComparison.InvariantCultureIgnoreCase) || (userDispalyName.Equals($"{pmsLastName} {pmsFirstName}", StringComparison.InvariantCultureIgnoreCase))) { matchedPMSUserName = user.DisplayName; return true; } } } } } return false; } } }