public async Task <CrudResult <List <UserCommunicationData> > > GetUserCommunicationsForDisplay(Guid identUserId) { try { var result = CrudResult <List <UserCommunicationData> > .Create(); var user = await Context.Users .Include(x => x.SecurityUserAccessRoles) .Include(x => x.UserCarrierScacs) .Include(x => x.UserShippers) .SingleOrDefaultAsync(x => x.IdentUserId == identUserId); var userCarrierIds = user.UserCarrierScacs.Select(ucs => ucs.CarrierId).Distinct().ToArray(); var userShipperIds = user.UserShippers.Select(us => us.CustomerId).ToArray(); var userSecurityAccessRoleIds = user.SecurityUserAccessRoles.Select(suar => suar.AccessRoleId).ToArray(); var communications = await Context .UserCommunications .Where(uc => !uc.UserCommunicationAcknowledgements.Any(uca => uca.Acknowledged && uca.UserId == user.UserId) && uc.EffectiveDate <= DateTime.Now && (uc.ExpirationDate == null || uc.ExpirationDate > DateTime.Now) && (uc.AllUsers || ((uc.UserCommunicationCarriers .Any(ucc => userCarrierIds.Contains(ucc.CarrierId)) || uc.UserCommunicationShippers .Any(ucs => userShipperIds.Contains(ucs.CustomerId)) || uc.UserCommunicationUsers .Select(ucu => ucu.UserId) .Contains(user.UserId)) && (uc.UserCommunicationSecurityAccessRoles.Count() == 0 || uc.UserCommunicationSecurityAccessRoles .Any(ucsar => userSecurityAccessRoleIds.Contains(ucsar.AccessRoleId))) || (!uc.UserCommunicationCarriers.Any() && !uc.UserCommunicationShippers.Any() && !uc.UserCommunicationUsers.Any() && uc.UserCommunicationSecurityAccessRoles .Any(ucsar => userSecurityAccessRoleIds.Contains(ucsar.AccessRoleId)))))) .OrderBy(uc => uc.EffectiveDate) .ToListAsync(); var communicationData = Mapper.Map <List <UserCommunicationData> >(communications); result.Data = communicationData; return(result); } catch (Exception ex) { return(CrudResult <List <UserCommunicationData> > .Create(ex)); } }
public async Task <CrudResult <List <UserCommunicationData> > > Acknowledge(Guid identUserId, AcknowledgeUserCommunication acknowledgeUserCommunication) { try { var user = await Context.Users.SingleOrDefaultAsync(u => u.IdentUserId == UserContext.UserId); var result = CrudResult <List <UserCommunicationData> > .Create(); var newAcknowledgement = new UserCommunicationAcknowledgementEntity() { Acknowledged = true, AcknowledgedDate = DateTime.Now, UserId = user.UserId, UserCommunicationId = acknowledgeUserCommunication.UserCommunicationId }; Context.UserCommunicationAcknowledgements.Add(newAcknowledgement); await Context.SaveChangesAsync(user.Username); var getUserCommunicationsResult = await GetUserCommunicationsForDisplay(identUserId); if (result.Successful) { result.Data = getUserCommunicationsResult.Data; } else { result.Data = new List <UserCommunicationData>(); result.AddWarning("Unable to retive updated User Communications"); result.AddExceptions(getUserCommunicationsResult.Exceptions.ToArray()); } return(result); } catch (Exception ex) { return(CrudResult <List <UserCommunicationData> > .Create(ex)); } }