public async Task <OperationResult> UpdatePairings(List <PairingUpdateModel> updateModels) { var sdkCore = await _coreService.GetCore(); await using var sdkDbContext = sdkCore.DbContextHelper.GetDbContext(); try { var plannings = await _dbContext.Plannings .Where(y => y.WorkflowState != Constants.WorkflowStates.Removed) .Select(x => new { PlanningId = x.Id, Entities = x.PlanningSites .Where(y => y.WorkflowState != Constants.WorkflowStates.Removed) .ToList(), x.RelatedEFormId, }) .ToListAsync(); var pairingModel = updateModels .GroupBy(x => x.PlanningId) .Select(x => new { PlanningId = x.Key, Models = x.ToList(), }).ToList(); foreach (var pairing in pairingModel) { var planning = plannings.FirstOrDefault(x => x.PlanningId == pairing.PlanningId); if (planning != null) { // for remove var deviceUserIdsForRemove = pairing.Models .Where(x => !x.Paired) .Select(x => x.DeviceUserId) .ToList(); var forRemove = planning.Entities .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => deviceUserIdsForRemove.Contains(x.SiteId)) .ToList(); var planningCaseSites = await _dbContext.PlanningCaseSites .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed && x.WorkflowState != Constants.WorkflowStates.Retracted) .Where(x => deviceUserIdsForRemove.Contains(x.MicrotingSdkSiteId)) .Where(x => x.PlanningId == planning.PlanningId) .ToListAsync(); foreach (var siteplanningSite in forRemove) { await siteplanningSite.Delete(_dbContext); } foreach (var planningCaseSite in planningCaseSites) { // if (planningCaseSite.MicrotingSdkCaseId != 0) // { // var result = await sdkDbContext.Cases.SingleAsync(x => x.Id == planningCaseSite.MicrotingSdkCaseId); // if (result.MicrotingUid != null) // { // await sdkCore.CaseDelete((int)result.MicrotingUid); // } // // await planningCaseSite.Delete(_dbContext); // } var theCase = await sdkDbContext.Cases.SingleOrDefaultAsync(x => x.Id == planningCaseSite.MicrotingSdkCaseId); if (theCase != null) { if (theCase.MicrotingUid != null) { await sdkCore.CaseDelete((int)theCase.MicrotingUid); } } else { var checkListSite = await sdkDbContext.CheckListSites.SingleOrDefaultAsync(x => x.Id == planningCaseSite.MicrotingCheckListSitId); if (checkListSite != null) { await sdkCore.CaseDelete(checkListSite.MicrotingUid); } } await planningCaseSite.Delete(_dbContext); } // var planningCases = await _dbContext.PlanningCases // .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) // .Where(x => deviceUserIdsForRemove.Contains(x.DoneByUserId)) // .ToListAsync(); // // foreach (var planningCase in planningCases) // { // await planningCase.Delete(_dbContext); // } // for create var sitesForCreateIds = pairing.Models .Where(x => x.Paired) .Select(x => x.DeviceUserId) .ToList(); var planningSitesIds = planning.Entities .Select(x => x.SiteId) .ToList(); var sitesForCreate = sitesForCreateIds .Where(x => !planningSitesIds.Contains(x)) .ToList(); foreach (var assignmentSiteId in sitesForCreate) { var newPlanningSite = new PlanningSite { CreatedByUserId = _userService.UserId, UpdatedByUserId = _userService.UserId, PlanningId = pairing.PlanningId, SiteId = assignmentSiteId, }; await newPlanningSite.Create(_dbContext); var dataResult = await _planningService.Read(planning.PlanningId); if (dataResult.Success) { await _pairItemWichSiteHelper.Pair(dataResult.Model, assignmentSiteId, planning.RelatedEFormId, planning.PlanningId); } } } } return(new OperationResult( true, _itemsPlanningLocalizationService.GetString("PairingsUpdatedSuccessfully"))); } catch (Exception e) { Trace.TraceError(e.Message); return(new OperationResult(false, _itemsPlanningLocalizationService.GetString("ErrorWhileUpdatingItemsPlanning") + $" {e.Message}")); } }
public async Task <OperationResult> PairSingle(PlanningAssignSitesModel requestModel) { var sdkCore = await _coreService.GetCore(); await using var sdkDbContext = sdkCore.DbContextHelper.GetDbContext(); try { var planning = await _dbContext.Plannings .Include(x => x.PlanningSites) .Where(x => x.Id == requestModel.PlanningId) .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .FirstOrDefaultAsync(); if (planning == null) { return(new OperationResult(false, _itemsPlanningLocalizationService.GetString("PlanningNotFound"))); } // for remove var assignmentsRequestIds = requestModel.Assignments .Select(x => x.SiteId) .ToList(); var forRemove = planning.PlanningSites .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => !assignmentsRequestIds.Contains(x.SiteId)) .ToList(); foreach (var planningSite in forRemove) { await planningSite.Delete(_dbContext); } var planningCases = await _dbContext.PlanningCases .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => assignmentsRequestIds.Contains(x.DoneByUserId)) .ToListAsync(); foreach (var planningCase in planningCases) { var planningCaseSites = await _dbContext.PlanningCaseSites .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => x.PlanningCaseId == planningCase.Id) .ToListAsync(); foreach (var planningCaseSite in planningCaseSites.Where(planningCaseSite => planningCaseSite.MicrotingSdkCaseId != 0)) { var theCase = await sdkDbContext.Cases.SingleOrDefaultAsync(x => x.Id == planningCaseSite.MicrotingSdkCaseId); if (theCase != null) { if (theCase.MicrotingUid != null) { await sdkCore.CaseDelete((int)theCase.MicrotingUid); } } else { var checkListSite = await sdkDbContext.CheckListSites.SingleOrDefaultAsync(x => x.Id == planningCaseSite.MicrotingCheckListSitId); if (checkListSite != null) { await sdkCore.CaseDelete(checkListSite.MicrotingUid); } } } // Delete planning case await planningCase.Delete(_dbContext); } // for create var assignmentsIds = planning.PlanningSites .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Select(x => x.SiteId) .ToList(); var assignmentsForCreate = requestModel.Assignments .Where(x => !assignmentsIds.Contains(x.SiteId)) .Select(x => x.SiteId) .ToList(); foreach (var assignmentSiteId in assignmentsForCreate) { var planningSite = new PlanningSite { CreatedByUserId = _userService.UserId, UpdatedByUserId = _userService.UserId, PlanningId = planning.Id, SiteId = assignmentSiteId, }; await planningSite.Create(_dbContext); var dataResult = await _planningService.Read(planning.Id); if (dataResult.Success) { await _pairItemWichSiteHelper.Pair(dataResult.Model, assignmentSiteId, planning.RelatedEFormId, planning.Id); } } return(new OperationResult(true, _itemsPlanningLocalizationService.GetString("PairingUpdatedSuccessfully"))); } catch (Exception e) { Trace.TraceError(e.Message); return(new OperationResult(false, _itemsPlanningLocalizationService.GetString("ErrorWhileUpdatingItemsPlanning") + $" {e.Message}")); } }