public async Task <IActionResult> UpdateBranchStatus([FromBody] OrgUserModel model) { var msg = new JMessage() { Error = false }; try { var org = await _context.AdOrganizations.FirstOrDefaultAsync(x => x.OrgAddonCode == model.OrgAddonCode); if (org == null) { msg.Error = true; msg.Title = model.OrgAddonCode.StartsWith("d_") ? CommonUtil.ResourceValue("ADM_BRANCH_MSG_CHOOSE_BRANCH") : CommonUtil.ResourceValue("ADM_BRANCH_MSG_BRANCH_EXITS"); } else { var oldValue = org.IsEnabled; // Update new status org.IsEnabled = model.IsEnabled; _context.Update(org); //_actionLog.InsertActionLog("VIB_ORGANIZATION", $"Update branch '{org.OrgCode} - {org.OrgName}' to {(org.IsEnabled ? "enable" : "disable")}", oldValue, org.IsEnabled, "Update", false, null); await _context.SaveChangesAsync(); msg.Title = string.Format($"{CommonUtil.ResourceValue("ADM_BRANCH_MSG_BRANCH_UPDATE")} '{org.OrgCode} - {org.OrgName}' {CommonUtil.ResourceValue("ADM_BRANCH_MSG_UTIL")} {(org.IsEnabled ? CommonUtil.ResourceValue("ADM_BRANCH_MSG_OPEN") : CommonUtil.ResourceValue("ADM_BRANCH_MSG_CLOSE"))}"); } } catch (Exception e) { msg.Error = true; msg.Title = CommonUtil.ResourceValue("ADM_BRANCH_MSG_ERR_BRANCH_UPDATE"); _actionLog.InsertActionLog("VIB_ORGANIZATION", "Update branch status failed: " + e.Message, null, null, "Error"); } return(Json(msg)); }
public async Task <IActionResult> UpdateUserInOrg([FromBody] OrgUserModel model) { var msg = new JMessage() { Error = false }; try { if (string.IsNullOrEmpty(model.OrgAddonCode)) { msg.Error = true; msg.Title = string.Format(CommonUtil.ResourceValue("ERR_REQUIRED"), CommonUtil.ResourceValue("BRANCH").ToLower()); } else { var org = await _context.AdOrganizations.FirstOrDefaultAsync(x => x.OrgAddonCode == model.OrgAddonCode); if (org == null) { msg.Error = true; msg.Title = model.OrgAddonCode.StartsWith("d_") ? "This is Division, please select branch level!" : "Branch is not exists!"; } else { if (model.ListUser.Count > 0) { // Add user to branch var listAdd = _context.Users.Where(x => model.ListUser.Any(y => y == x.Id) && x.BranchId != model.OrgAddonCode); if (listAdd.Any()) { foreach (var user in listAdd) { if (user.BranchId == model.OrgAddonCode) { continue; } var oldBranch = user.BranchId; user.BranchId = model.OrgAddonCode; user.UpdatedDate = DateTime.Now; user.UpdatedBy = ESEIM.AppContext.UserName; _context.Update(user); //_actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user: "******"Update", false, user.UserName); } } // Remove user out branch var listDel = _context.Users.Where(x => model.ListUser.All(y => y != x.Id) && x.BranchId == model.OrgAddonCode); if (listDel.Any()) { foreach (var user in listDel) { var oldBranch = user.BranchId; user.BranchId = null; user.UpdatedDate = DateTime.Now; user.UpdatedBy = ESEIM.AppContext.UserName; _context.Update(user); //_actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user: "******"Update", false, user.UserName); } } } else { var listUser = _context.Users.Where(x => x.BranchId == model.OrgAddonCode); if (listUser.Any()) { foreach (var user in listUser) { var oldBranch = user.BranchId; user.BranchId = null; user.UpdatedDate = DateTime.Now; user.UpdatedBy = ESEIM.AppContext.UserName; _context.Update(user); //_actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user: "******"Update", false, user.UserName); } } } await _context.SaveChangesAsync(); msg.Title = string.Format(CommonUtil.ResourceValue("MSG_UPDATE_SUCCESS"), "tài khoản"); } } } catch (Exception e) { msg.Error = true; msg.Title = "Lỗi khi cập nhập"; _actionLog.InsertActionLog("ASP_NET_USERS", "Update branch of user failed: " + e.Message, null, null, "Error"); } return(Json(msg)); }