public async Task <ActionResult> Delete([FromBody] DeleteAccessRight model) { var companyId = _httpContextAccessor.HttpContext.Request?.Headers["CompanyId"].FirstOrDefault(); var createdBy = User.Claims.FirstOrDefault(s => s.Type == "userName").Value; model.CompanyId = companyId; model.CreatedBy = createdBy; var data = await _deleteAccessRightRequestClient.Request(model); return(Ok(data.UserRoles)); }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <UserPermissions> Delete(DeleteAccessRight model) { var data = new UserPermissions() { UserRoles = new List <UserRoles>(), AccessRights = new List <AccessRight>() }; foreach (var item in model.UserList) { var newData = await _context.AccessRight.Find(f => f.UserId == item && f.CompanyId == model.CompanyId).FirstOrDefaultAsync(); if (newData != null) { var roleList = newData.RoleList ?? new List <string>(); var newRoleList = roleList.Where(s => !model.RoleList.Contains(s)).ToList(); var filter = Builders <AccessRight> .Filter.Eq("Id", newData.Id); var update = Builders <AccessRight> .Update .Set(s => s.RoleList, newRoleList) .Set(s => s.UpdatedBy, model.CreatedBy) .CurrentDate(s => s.UpdatedAt); var options = new FindOneAndUpdateOptions <AccessRight> { ReturnDocument = ReturnDocument.After }; newData = await _context.AccessRight.FindOneAndUpdateAsync(filter, update, options); data.AccessRights.Add(newData); data.UserRoles.Add(new UserRoles() { UserId = item, Roles = newRoleList }); } } return(data); }