private async Task <PlanningCase> SetFieldValue(PlanningCase planningCase, int caseId, Language language)
        {
            var planning = await _dbContext.Plannings.FirstOrDefaultAsync(x => x.Id == planningCase.PlanningId).ConfigureAwait(false);

            var caseIds = new List <int> {
                planningCase.MicrotingSdkCaseId
            };
            var fieldValues = await _sdkCore.Advanced_FieldValueReadList(caseIds, language).ConfigureAwait(false);

            if (planning == null)
            {
                return(planningCase);
            }
            if (planning.NumberOfImagesEnabled)
            {
                planningCase.NumberOfImages = 0;
                foreach (var fieldValue in fieldValues)
                {
                    if (fieldValue.FieldType == Constants.FieldTypes.Picture)
                    {
                        if (fieldValue.UploadedData != null)
                        {
                            planningCase.NumberOfImages += 1;
                        }
                    }
                }
            }

            return(planningCase);
        }
        private async Task <PlanningCase> SetFieldValue(PlanningCase itemCase, int caseId)
        {
            var item     = _dbContext.Items.SingleOrDefault(x => x.Id == itemCase.ItemId);
            var planning = _dbContext.Plannings.SingleOrDefault(x => x.Id == item.PlanningId);
            var caseIds  = new List <int>();

            caseIds.Add(itemCase.MicrotingSdkCaseId);
            var fieldValues = await _sdkCore.Advanced_FieldValueReadList(caseIds);

            if (planning == null)
            {
                return(itemCase);
            }
            if (planning.NumberOfImagesEnabled)
            {
                itemCase.NumberOfImages = 0;
                foreach (var fieldValue in fieldValues)
                {
                    if (fieldValue.FieldType == Constants.FieldTypes.Picture)
                    {
                        if (fieldValue.UploadedData != null)
                        {
                            itemCase.NumberOfImages += 1;
                        }
                    }
                }
            }

            return(itemCase);
        }
        public async Task Handle(PlanningCaseCreate message)
        {
            var planning = await _dbContext.Plannings
                           .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                           .FirstOrDefaultAsync(x => x.Id == message.PlanningId);

            await using MicrotingDbContext microtingDbContext = _sdkCore.DbContextHelper.GetDbContext();
            if (planning != null)
            {
                var siteIds = _dbContext.PlanningSites
                              .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed &&
                                     x.PlanningId == planning.Id)
                              .Select(x => x.SiteId)
                              .ToList();

                var planningCases = await _dbContext.PlanningCases
                                    .Where(x => x.PlanningId == planning.Id &&
                                           x.WorkflowState != Constants.WorkflowStates.Retracted &&
                                           x.WorkflowState != Constants.WorkflowStates.Removed)
                                    .ToListAsync();

                foreach (PlanningCase cPlanningCase in planningCases)
                {
                    cPlanningCase.WorkflowState = Constants.WorkflowStates.Retracted;
                    await cPlanningCase.Update(_dbContext);
                }

                PlanningCase planningCase = new PlanningCase()
                {
                    PlanningId          = planning.Id,
                    Status              = 66,
                    MicrotingSdkeFormId = message.RelatedEFormId
                };
                await planningCase.Create(_dbContext);

                foreach (var siteId in siteIds)
                {
                    var casesToDelete = await _dbContext.PlanningCaseSites.
                                        Where(x => x.PlanningId == planning.Id &&
                                              x.MicrotingSdkSiteId == siteId &&
                                              x.WorkflowState != Constants.WorkflowStates.Retracted).ToListAsync();

                    Log.LogEvent($"ItemCaseCreateHandler.Task: Found {casesToDelete.Count} PlanningCaseSites, which has not yet been retracted, so retracting now.");

                    foreach (var caseToDelete in casesToDelete)
                    {
                        Log.LogEvent($"ItemCaseCreateHandler.Task: Trying to retract the case with Id: {caseToDelete.Id}");
                        var sdkCase = await microtingDbContext.Cases.FirstOrDefaultAsync(x => x.Id == caseToDelete.MicrotingSdkCaseId);

                        if (sdkCase is { MicrotingUid: { } })
Esempio n. 4
0
        public async Task PlanningCase_Save_DoesSave()
        {
            // Arrange
            var planning = new Planning();

            await planning.Create(DbContext);

            var commonTranslationModels = new List <PlanningNameTranslation>()
            {
                new PlanningNameTranslation()
                {
                    Name     = Guid.NewGuid().ToString(),
                    Language = new Language()
                    {
                        LanguageCode = "da", Name = "Danish"
                    },
                    Planning = planning
                }
            };

            foreach (var translationModel in commonTranslationModels)
            {
                await translationModel.Create(DbContext);
            }


            var planningCase = new PlanningCase
            {
                MicrotingSdkSiteId  = 24,
                MicrotingSdkCaseId  = 34,
                MicrotingSdkeFormId = 234,
                Status     = 66,
                PlanningId = planning.Id,
            };

            // Act
            await planningCase.Create(DbContext);

            var planningCases        = DbContext.PlanningCases.AsNoTracking().ToList();
            var planningCaseVersions = DbContext.PlanningCaseVersions.AsNoTracking().ToList();

            // Assert
            Assert.AreEqual(1, planningCases.Count);
            Assert.AreEqual(1, planningCaseVersions.Count);
            Assert.AreEqual(planningCase.MicrotingSdkSiteId, planningCases[0].MicrotingSdkSiteId);
            Assert.AreEqual(planningCase.MicrotingSdkCaseId, planningCases[0].MicrotingSdkCaseId);
            Assert.AreEqual(planningCase.MicrotingSdkeFormId, planningCases[0].MicrotingSdkeFormId);
            Assert.AreEqual(planningCase.Status, planningCases[0].Status);
            Assert.AreEqual(Constants.WorkflowStates.Created, planningCases[0].WorkflowState);
            Assert.AreEqual(planningCase.Id, planningCases[0].Id);
            Assert.AreEqual(planningCase.PlanningId, planningCases[0].PlanningId);
            Assert.AreEqual(1, planningCases[0].Version);

            Assert.AreEqual(planningCase.MicrotingSdkSiteId, planningCaseVersions[0].MicrotingSdkSiteId);
            Assert.AreEqual(planningCase.MicrotingSdkCaseId, planningCaseVersions[0].MicrotingSdkCaseId);
            Assert.AreEqual(planningCase.MicrotingSdkeFormId, planningCaseVersions[0].MicrotingSdkeFormId);
            Assert.AreEqual(planningCase.Status, planningCaseVersions[0].Status);
            Assert.AreEqual(Constants.WorkflowStates.Created, planningCaseVersions[0].WorkflowState);
            Assert.AreEqual(planningCase.Id, planningCaseVersions[0].PlanningCaseId);
            Assert.AreEqual(planningCase.PlanningId, planningCaseVersions[0].PlanningId);
            Assert.AreEqual(1, planningCaseVersions[0].Version);
        }
Esempio n. 5
0
        public async Task Pair(PlanningPnModel planningPnModel, int assignmentSiteId, int relatedEFormId, int planningId)
        {
            var sdkCore =
                await _coreService.GetCore();

            await using var sdkDbContext = sdkCore.DbContextHelper.GetDbContext();
            var sdkSite = await sdkDbContext.Sites.SingleAsync(x => x.Id == assignmentSiteId);

            var language = await sdkDbContext.Languages.SingleAsync(x => x.Id == sdkSite.LanguageId);

            var mainElement = await sdkCore.ReadeForm(relatedEFormId, language);

            var folder = await sdkDbContext.Folders.SingleAsync(x => x.Id == planningPnModel.Folder.EFormSdkFolderId);

            var folderId = folder.MicrotingUid.ToString();

            var planning = await _dbContext.Plannings.SingleAsync(x => x.Id == planningId);

            // get planning cases
            var planningCase = await _dbContext.PlanningCases
                               .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                               .Where(x => x.WorkflowState != Constants.WorkflowStates.Retracted)
                               .Where(x => x.WorkflowState != Constants.WorkflowStates.Processed)
                               .Where(x => x.PlanningId == planningPnModel.Id)
                               .FirstOrDefaultAsync(x => x.MicrotingSdkeFormId == relatedEFormId);

            if (planning.RepeatEvery == 0 && planning.RepeatType == RepeatType.Day)
            {
                planningCase = new PlanningCase()
                {
                    PlanningId          = planningPnModel.Id,
                    Status              = 66,
                    MicrotingSdkeFormId = relatedEFormId
                };
                await planningCase.Create(_dbContext);
            }

            if (planningCase == null)
            {
                planningCase = new PlanningCase()
                {
                    PlanningId          = planningPnModel.Id,
                    Status              = 66,
                    MicrotingSdkeFormId = relatedEFormId
                };
                await planningCase.Create(_dbContext);
            }

            var casesToDelete = await _dbContext.PlanningCaseSites
                                .Where(x => x.PlanningId == planningPnModel.Id &&
                                       x.MicrotingSdkSiteId == assignmentSiteId &&
                                       x.WorkflowState !=
                                       Constants.WorkflowStates.Retracted)
                                .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                                .ToListAsync();

            foreach (var caseToDelete in casesToDelete)
            {
                var theCase =
                    await sdkDbContext.Cases.SingleOrDefaultAsync(x => x.Id == caseToDelete.MicrotingSdkCaseId);

                if (theCase != null)
                {
                    if (theCase.MicrotingUid != null)
                    {
                        await sdkCore.CaseDelete((int)theCase.MicrotingUid);
                    }
                }
                else
                {
                    var checkListSite =
                        await sdkDbContext.CheckListSites.SingleOrDefaultAsync(x =>
                                                                               x.Id == caseToDelete.MicrotingCheckListSitId);

                    if (checkListSite != null)
                    {
                        await sdkCore.CaseDelete(checkListSite.MicrotingUid);
                    }
                }

                caseToDelete.WorkflowState = Constants.WorkflowStates.Retracted;
                await caseToDelete.Update(_dbContext);
            }

            if (planningCase.Status == 100)
            {
                var planningCaseSite =
                    await _dbContext.PlanningCaseSites.SingleOrDefaultAsync(x =>
                                                                            x.PlanningCaseId == planningCase.Id &&
                                                                            x.MicrotingSdkSiteId == assignmentSiteId &&
                                                                            x.WorkflowState != Constants.WorkflowStates.Removed);

                if (planningCaseSite == null)
                {
                    planningCaseSite = new PlanningCaseSite()
                    {
                        MicrotingSdkSiteId  = assignmentSiteId,
                        MicrotingSdkeFormId = relatedEFormId,
                        Status         = 2,
                        PlanningId     = planningPnModel.Id,
                        PlanningCaseId = planningCase.Id
                    };

                    await planningCaseSite.Create(_dbContext);
                }

                planningCaseSite.Status        = planningCaseSite.Status == 100 ? planningCaseSite.Status : 2;
                planningCaseSite.WorkflowState = Constants.WorkflowStates.Retracted;
                await planningCaseSite.Update(_dbContext);
            }

            if (planningCase.Status != 100)
            {
                var translation = _dbContext.PlanningNameTranslation
                                  .Single(x => x.LanguageId == language.Id && x.PlanningId == planningPnModel.Id).Name;

                mainElement.Label = string.IsNullOrEmpty(planningPnModel.PlanningNumber)
                    ? ""
                    : planningPnModel.PlanningNumber;
                if (!string.IsNullOrEmpty(translation))
                {
                    mainElement.Label +=
                        string.IsNullOrEmpty(mainElement.Label) ? $"{translation}" : $" - {translation}";
                }

                if (!string.IsNullOrEmpty(planningPnModel.BuildYear))
                {
                    mainElement.Label += string.IsNullOrEmpty(mainElement.Label)
                        ? $"{planningPnModel.BuildYear}"
                        : $" - {planningPnModel.BuildYear}";
                }

                if (!string.IsNullOrEmpty(planningPnModel.Type))
                {
                    mainElement.Label += string.IsNullOrEmpty(mainElement.Label)
                        ? $"{planningPnModel.Type}"
                        : $" - {planningPnModel.Type}";
                }

                if (mainElement.ElementList.Count == 1)
                {
                    mainElement.ElementList[0].Label = mainElement.Label;
                }
                mainElement.CheckListFolderName = folderId;
                mainElement.StartDate           = DateTime.Now.ToUniversalTime();
                mainElement.EndDate             = DateTime.Now.AddYears(10).ToUniversalTime();
                if (planning.RepeatType == RepeatType.Day && planning.RepeatEvery == 0)
                {
                    mainElement.Repeated = 0;
                }
                // mainElement.PushMessageBody = mainElement.Label;
                // mainElement.PushMessageTitle = folder.Name;
                // if (folder.ParentId != null)
                // {
                //     var parentFolder = await sdkDbContext.Folders.SingleAsync(x => x.Id == folder.ParentId);
                //     mainElement.PushMessageTitle = parentFolder.Name;
                //     mainElement.PushMessageBody = $"{folder.Name}\n{mainElement.Label}";
                // }

                var planningCaseSite =
                    await _dbContext.PlanningCaseSites.SingleOrDefaultAsync(x =>
                                                                            x.PlanningCaseId == planningCase.Id &&
                                                                            x.MicrotingSdkSiteId == assignmentSiteId &&
                                                                            x.WorkflowState != Constants.WorkflowStates.Retracted &&
                                                                            x.WorkflowState != Constants.WorkflowStates.Removed);

                if (planningCaseSite == null)
                {
                    planningCaseSite = new PlanningCaseSite()
                    {
                        MicrotingSdkSiteId  = assignmentSiteId,
                        MicrotingSdkeFormId = relatedEFormId,
                        Status         = 66,
                        PlanningId     = planningPnModel.Id,
                        PlanningCaseId = planningCase.Id
                    };

                    await planningCaseSite.Create(_dbContext);
                }

                if (planningCaseSite.MicrotingSdkCaseDoneAt.HasValue)
                {
                    var unixTimestamp = (long)(planningCaseSite.MicrotingSdkCaseDoneAt.Value
                                               .Subtract(new DateTime(1970, 1, 1)))
                                        .TotalSeconds;

                    mainElement.ElementList[0].Description.InderValue = unixTimestamp.ToString();
                }

                if (planningCaseSite.MicrotingSdkCaseId < 1 && planning.StartDate <= DateTime.Now)
                {
                    // ReSharper disable once PossibleInvalidOperationException
                    if (planning.PushMessageOnDeployment)
                    {
                        string body = "";
                        folder = await getTopFolderName((int)planning.SdkFolderId, sdkDbContext);

                        if (folder != null)
                        {
                            planning.SdkFolderId = sdkDbContext.Folders
                                                   .FirstOrDefault(y => y.Id == planning.SdkFolderId)
                                                   ?.Id;
                            FolderTranslation folderTranslation =
                                await sdkDbContext.FolderTranslations.SingleOrDefaultAsync(x =>
                                                                                           x.FolderId == folder.Id && x.LanguageId == sdkSite.LanguageId);

                            body = $"{folderTranslation.Name} ({sdkSite.Name};{DateTime.Now:dd.MM.yyyy})";
                        }

                        PlanningNameTranslation planningNameTranslation =
                            await _dbContext.PlanningNameTranslation.SingleOrDefaultAsync(x =>
                                                                                          x.PlanningId == planning.Id &&
                                                                                          x.LanguageId == sdkSite.LanguageId);

                        mainElement.PushMessageBody  = body;
                        mainElement.PushMessageTitle = planningNameTranslation.Name;
                    }
                    var caseId = await sdkCore.CaseCreate(mainElement, "", (int)sdkSite.MicrotingUid, null);

                    if (caseId != null)
                    {
                        if (sdkDbContext.Cases.Any(x => x.MicrotingUid == caseId))
                        {
                            planningCaseSite.MicrotingSdkCaseId =
                                sdkDbContext.Cases.Single(x => x.MicrotingUid == caseId).Id;
                        }
                        else
                        {
                            planningCaseSite.MicrotingCheckListSitId =
                                sdkDbContext.CheckListSites.Single(x => x.MicrotingUid == caseId).Id;
                        }
                        await planningCaseSite.Update(_dbContext);
                    }
                }

                var now = DateTime.UtcNow;
                switch (planning.RepeatType)
                {
                case RepeatType.Day:
                    planning.NextExecutionTime = now.AddDays(planning.RepeatEvery);
                    break;

                case RepeatType.Week:
                    var startOfWeek =
                        new DateTime(now.Year, now.Month, now.Day, 0, 0, 0).StartOfWeek(
                            (DayOfWeek)planning.DayOfWeek);
                    planning.NextExecutionTime = startOfWeek.AddDays(planning.RepeatEvery * 7);
                    break;

                case RepeatType.Month:
                    planning.DayOfMonth ??= 1;
                    if (planning.DayOfMonth == 0)
                    {
                        planning.DayOfMonth = 1;
                    }
                    var startOfMonth = new DateTime(now.Year, now.Month, (int)planning.DayOfMonth, 0, 0, 0);
                    planning.NextExecutionTime = startOfMonth.AddMonths(planning.RepeatEvery);
                    break;
                }
                planning.LastExecutedTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
                await planning.Update(_dbContext);
            }
        }
Esempio n. 6
0
        public async Task Handle(ItemCaseCreate message)
        {
            var item = await _dbContext.Items.SingleOrDefaultAsync(x => x.Id == message.ItemId);

            await using MicrotingDbContext dbContext = _sdkCore.dbContextHelper.GetDbContext();
            if (item != null)
            {
                var planning = await _dbContext.Plannings
                               .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                               .SingleOrDefaultAsync(x => x.Id == message.PlanningId);

                var siteIds = planning.PlanningSites
                              .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                              .Select(x => x.SiteId)
                              .ToList();

                var mainElement = await _sdkCore.TemplateRead(message.RelatedEFormId);

                var folderId = dbContext.folders.Single(x => x.Id == item.eFormSdkFolderId).MicrotingUid.ToString();

                var planningCase = await _dbContext.PlanningCases.SingleOrDefaultAsync(x => x.ItemId == item.Id && x.WorkflowState != Constants.WorkflowStates.Retracted);

                if (planningCase != null)
                {
                    planningCase.WorkflowState = Constants.WorkflowStates.Retracted;
                    await planningCase.Update(_dbContext);
                }

                planningCase = new PlanningCase()
                {
                    ItemId = item.Id,
                    Status = 66,
                    MicrotingSdkeFormId = message.RelatedEFormId
                };
                await planningCase.Create(_dbContext);

                foreach (var siteId in siteIds)
                {
                    var casesToDelete = await _dbContext.PlanningCaseSites.
                                        Where(x => x.ItemId == item.Id && x.MicrotingSdkSiteId == siteId && x.WorkflowState != Constants.WorkflowStates.Retracted).ToListAsync();

                    foreach (var caseToDelete in casesToDelete)
                    {
                        var caseDto = await _sdkCore.CaseLookupCaseId(caseToDelete.MicrotingSdkCaseId);

                        if (caseDto.MicrotingUId != null)
                        {
                            await _sdkCore.CaseDelete((int)caseDto.MicrotingUId);
                        }
                        caseToDelete.WorkflowState = Constants.WorkflowStates.Retracted;
                        await caseToDelete.Update(_dbContext);
                    }

                    mainElement.Label = string.IsNullOrEmpty(item.ItemNumber) ? "" : item.ItemNumber;
                    if (!string.IsNullOrEmpty(item.Name))
                    {
                        mainElement.Label += string.IsNullOrEmpty(mainElement.Label) ? $"{item.Name}" : $" - {item.Name}";
                    }

                    if (!string.IsNullOrEmpty(item.BuildYear))
                    {
                        mainElement.Label += string.IsNullOrEmpty(mainElement.Label) ? $"{item.BuildYear}" : $" - {item.BuildYear}";
                    }

                    if (!string.IsNullOrEmpty(item.Type))
                    {
                        mainElement.Label += string.IsNullOrEmpty(mainElement.Label) ? $"{item.Type}" : $" - {item.Type}";
                    }
                    mainElement.ElementList[0].Label = mainElement.Label;
                    mainElement.CheckListFolderName  = folderId;
                    mainElement.StartDate            = DateTime.Now.ToUniversalTime();
                    mainElement.EndDate = DateTime.Now.AddYears(10).ToUniversalTime();

                    var planningCaseSite =
                        await _dbContext.PlanningCaseSites.SingleOrDefaultAsync(x => x.PlanningCaseId == planningCase.Id && x.MicrotingSdkSiteId == siteId);

                    if (planningCaseSite == null)
                    {
                        planningCaseSite = new PlanningCaseSite()
                        {
                            MicrotingSdkSiteId  = siteId,
                            MicrotingSdkeFormId = message.RelatedEFormId,
                            Status         = 66,
                            ItemId         = item.Id,
                            PlanningCaseId = planningCase.Id
                        };

                        await planningCaseSite.Create(_dbContext);
                    }

                    if (planningCaseSite.MicrotingSdkCaseId >= 1)
                    {
                        continue;
                    }
                    await using var sdkDbContext = _sdkCore.dbContextHelper.GetDbContext();
                    var sdkSite = await sdkDbContext.sites.SingleAsync(x => x.Id == siteId);

                    var caseId = await _sdkCore.CaseCreate(mainElement, "", (int)sdkSite.MicrotingUid, null);

                    if (caseId != null)
                    {
                        var caseDto = await _sdkCore.CaseLookupMUId((int)caseId);

                        if (caseDto?.CaseId != null)
                        {
                            planningCaseSite.MicrotingSdkCaseId = (int)caseDto.CaseId;
                        }
                        await planningCaseSite.Update(_dbContext);
                    }
                }
            }
        }
        public async Task Handle(eFormCompleted message)
        {
            await using MicrotingDbContext sdkDbContext = _sdkCore.DbContextHelper.GetDbContext();
            Console.WriteLine($"Checking PlanningCaseSites with MicrotingSdkCaseId == {message.caseId}");
            Console.WriteLine($"Checking Cases with Id == {message.caseId}");
            Console.WriteLine($"Checking Cases with MicrotingCheckUid == {message.CheckId}");
            var planningCaseSite =
                await _dbContext.PlanningCaseSites.FirstOrDefaultAsync(x => x.MicrotingSdkCaseId == message.caseId);

            var dbCase = await sdkDbContext.Cases.FirstOrDefaultAsync(x => x.Id == message.caseId) ?? await sdkDbContext.Cases.FirstOrDefaultAsync(x => x.MicrotingCheckUid == message.CheckId);

            if (planningCaseSite == null)
            {
                Console.WriteLine($"Checking CheckListSites with MicrotingUid == {message.MicrotingUId}");
                var checkListSite = await sdkDbContext.CheckListSites.FirstOrDefaultAsync(x =>
                                                                                          x.MicrotingUid == message.MicrotingUId);

                if (checkListSite != null)
                {
                    Console.WriteLine($"Checking PlanningCaseSites with MicrotingCheckListSitId == {checkListSite.Id}");
                    planningCaseSite =
                        await _dbContext.PlanningCaseSites.FirstOrDefaultAsync(x =>
                                                                               x.MicrotingCheckListSitId == checkListSite.Id);
                }
            }
            if (planningCaseSite != null)
            {
                Planning planning =
                    await _dbContext.Plannings.FirstAsync(x => x.Id == planningCaseSite.PlanningId);

                Site site = await sdkDbContext.Sites.FirstAsync(x => x.Id == dbCase.SiteId);

                Language language = await sdkDbContext.Languages.FirstAsync(x => x.Id == site.LanguageId);

                if (dbCase.MicrotingUid != null && dbCase.MicrotingCheckUid != null)
                {
                    ReplyElement theCase =
                        await _sdkCore.CaseRead((int)dbCase.MicrotingUid, (int)dbCase.MicrotingCheckUid, language);

                    if (planning.RepeatType == RepeatType.Day && planning.RepeatEvery == 0)
                    {
                        var planningCase =
                            await _dbContext.PlanningCases.FirstOrDefaultAsync(x =>
                                                                               x.Id == planningCaseSite.PlanningCaseId);

                        if (planningCase != null && planningCase.Status != 100)
                        {
                            planningCase.Status = 100;
                            planningCase.MicrotingSdkCaseDoneAt = theCase.DoneAt;
                            planningCase.MicrotingSdkCaseId     = dbCase.Id;
                            planningCase.DoneByUserId           = theCase.DoneById;
                            planningCase.DoneByUserName         = site.Name;
                            planningCase.MicrotingSdkeFormId    = (int)dbCase.CheckListId;
                            planningCase.WorkflowState          = Constants.WorkflowStates.Processed;

                            planningCase = await SetFieldValue(planningCase, theCase.Id, language);

                            await planningCase.Update(_dbContext);
                        }
                        else
                        {
                            if (planningCase == null || planningCase.MicrotingSdkCaseId != dbCase.Id)
                            {
                                planningCase = new PlanningCase
                                {
                                    Status = 100,
                                    MicrotingSdkCaseDoneAt = theCase.DoneAt,
                                    MicrotingSdkCaseId     = dbCase.Id,
                                    DoneByUserId           = theCase.DoneById,
                                    DoneByUserName         = site.Name,
                                    WorkflowState          = Constants.WorkflowStates.Processed,
                                    MicrotingSdkeFormId    = (int)dbCase.CheckListId,
                                    PlanningId             = planning.Id
                                };
                                await planningCase.Create(_dbContext);

                                planningCase = await SetFieldValue(planningCase, theCase.Id, language);

                                await planningCase.Update(_dbContext);
                            }
                        }
                    }
                    else
                    {
                        planningCaseSite.Status = 100;
                        planningCaseSite        = await SetFieldValue(planningCaseSite, theCase.Id, language);

                        planningCaseSite.MicrotingSdkCaseDoneAt = theCase.DoneAt;
                        planningCaseSite.DoneByUserId           = theCase.DoneById;
                        var worker = await sdkDbContext.Workers.SingleAsync(x => x.Id == planningCaseSite.DoneByUserId);

                        planningCaseSite.DoneByUserName = $"{worker.FirstName} {worker.LastName}";
                        await planningCaseSite.Update(_dbContext);

                        var planningCase =
                            await _dbContext.PlanningCases.SingleOrDefaultAsync(x =>
                                                                                x.Id == planningCaseSite.PlanningCaseId);

                        if (planningCase.Status != 100)
                        {
                            planningCase.Status = 100;
                            planningCase.MicrotingSdkCaseDoneAt = theCase.DoneAt;
                            planningCase.MicrotingSdkCaseId     = dbCase.Id;
                            planningCase.DoneByUserId           = theCase.DoneById;
                            planningCase.DoneByUserName         = site.Name;
                            planningCase.WorkflowState          = Constants.WorkflowStates.Processed;
                            // planningCase.DoneByUserName = $"{site.Result.FirstName} {site.Result.LastName}";

                            planningCase = await SetFieldValue(planningCase, theCase.Id, language);

                            await planningCase.Update(_dbContext);
                        }

                        planning.DoneInPeriod = true;
                        await planning.Update(_dbContext);

                        await RetractFromMicroting(planningCase.Id);
                    }
                }
            }
        }
        public async Task Handle(PlanningCaseSingleCreate message)
        {
            var planning = await _dbContext.Plannings.FirstAsync(x => x.Id == message.PlanningId);

            var siteId = message.PlanningSiteId;

            await using MicrotingDbContext sdkDbContext = _sdkCore.DbContextHelper.GetDbContext();
            var sdkSite = await sdkDbContext.Sites.FirstAsync(x => x.Id == siteId);

            Language language = await sdkDbContext.Languages.FirstAsync(x => x.Id == sdkSite.LanguageId);

            var mainElement = await _sdkCore.ReadeForm(message.RelatedEFormId, language);

            var folder = await sdkDbContext.Folders.FirstAsync(x => x.Id == planning.SdkFolderId);

            var folderId = folder.MicrotingUid.ToString();

            var planningCase = await _dbContext.PlanningCases
                               .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                               .Where(x => x.PlanningId == planning.Id)
                               .Where(x => x.Status == 66)
                               .Where(x => x.MicrotingSdkeFormId == message.RelatedEFormId)
                               .FirstOrDefaultAsync();

            if (planningCase == null)
            {
                planningCase = new PlanningCase()
                {
                    PlanningId          = planning.Id,
                    Status              = 66,
                    MicrotingSdkeFormId = message.RelatedEFormId
                };
                await planningCase.Create(_dbContext);
            }

            var casesToDelete = await _dbContext.PlanningCaseSites
                                .Where(x => x.PlanningId == planning.Id &&
                                       x.MicrotingSdkSiteId == siteId &&
                                       x.WorkflowState !=
                                       Constants.WorkflowStates.Retracted)
                                .ToListAsync();

            Log.LogEvent(
                $"ItemCaseCreateHandler.Task: Found {casesToDelete.Count} PlanningCaseSites, which has not yet been retracted, so retracting now.");

            foreach (var caseToDelete in casesToDelete)
            {
                Log.LogEvent($"ItemCaseCreateHandler.Task: Trying to retract the case with Id: {caseToDelete.Id}");
                var caseDto = await _sdkCore.CaseLookupCaseId(caseToDelete.MicrotingSdkCaseId);

                if (caseDto.MicrotingUId != null)
                {
                    await _sdkCore.CaseDelete((int)caseDto.MicrotingUId);
                }
                caseToDelete.WorkflowState = Constants.WorkflowStates.Retracted;
                await caseToDelete.Update(_dbContext);
            }

            var translation = _dbContext.PlanningNameTranslation
                              .Single(x => x.LanguageId == language.Id && x.PlanningId == planning.Id).Name;

            mainElement.Label = string.IsNullOrEmpty(planning.PlanningNumber) ? "" : planning.PlanningNumber;
            if (!string.IsNullOrEmpty(translation))
            {
                mainElement.Label += string.IsNullOrEmpty(mainElement.Label) ? $"{translation}" : $" - {translation}";
            }

            if (!string.IsNullOrEmpty(planning.BuildYear))
            {
                mainElement.Label += string.IsNullOrEmpty(mainElement.Label)
                    ? $"{planning.BuildYear}"
                    : $" - {planning.BuildYear}";
            }

            if (!string.IsNullOrEmpty(planning.Type))
            {
                mainElement.Label += string.IsNullOrEmpty(mainElement.Label) ? $"{planning.Type}" : $" - {planning.Type}";
            }

            mainElement.ElementList[0].Label = mainElement.Label;
            mainElement.CheckListFolderName  = folderId;
            mainElement.StartDate            = DateTime.Now.ToUniversalTime();
            mainElement.EndDate = (DateTime)planning.NextExecutionTime !;

            var planningCaseSite =
                await _dbContext.PlanningCaseSites.FirstOrDefaultAsync(x =>
                                                                       x.PlanningCaseId == planningCase.Id && x.MicrotingSdkSiteId == siteId);

            if (planningCaseSite == null)
            {
                planningCaseSite = new PlanningCaseSite()
                {
                    MicrotingSdkSiteId  = siteId,
                    MicrotingSdkeFormId = message.RelatedEFormId,
                    Status         = 66,
                    PlanningId     = planning.Id,
                    PlanningCaseId = planningCase.Id
                };

                await planningCaseSite.Create(_dbContext);
            }

            if (planningCaseSite.MicrotingSdkCaseDoneAt.HasValue)
            {
                long unixTimestamp = (long)(planningCaseSite.MicrotingSdkCaseDoneAt.Value
                                            .Subtract(new DateTime(1970, 1, 1)))
                                     .TotalSeconds;

                mainElement.ElementList[0].Description.InderValue = unixTimestamp.ToString();
            }

            if (planningCaseSite.MicrotingSdkCaseId < 1)
            {
                var caseId = await _sdkCore.CaseCreate(mainElement, "", (int)sdkSite.MicrotingUid !, null);

                if (caseId != null)
                {
                    var caseDto = await _sdkCore.CaseLookupMUId((int)caseId);

                    if (caseDto?.CaseId != null)
                    {
                        planningCaseSite.MicrotingSdkCaseId = (int)caseDto.CaseId;
                    }
                    await planningCaseSite.Update(_dbContext);
                }
            }
        }
Esempio n. 9
0
        public async Task UploadedData_Update_DoesUpdate()
        {
            // Arrange
            var planning = new Planning();

            await planning.Create(DbContext);

            var commonTranslationModels = new List <PlanningNameTranslation>()
            {
                new PlanningNameTranslation()
                {
                    Name     = Guid.NewGuid().ToString(),
                    Language = new Language()
                    {
                        LanguageCode = "da", Name = "Danish"
                    },
                    Planning = planning
                }
            };

            foreach (var translationModel in commonTranslationModels)
            {
                await translationModel.Create(DbContext);
            }

            var planningCase = new PlanningCase
            {
                MicrotingSdkSiteId  = 24,
                MicrotingSdkCaseId  = 34,
                MicrotingSdkeFormId = 234,
                Status     = 66,
                PlanningId = planning.Id,
            };

            await planningCase.Create(DbContext);

            var uploadedData = new UploadedData
            {
                PlanningCaseId = planningCase.Id,
                Checksum       = Guid.NewGuid().ToString(),
                Extension      = Guid.NewGuid().ToString(),
                CurrentFile    = Guid.NewGuid().ToString(),
                UploaderType   = Guid.NewGuid().ToString(),
                FileLocation   = Guid.NewGuid().ToString(),
                FileName       = Guid.NewGuid().ToString()
            };
            await uploadedData.Create(DbContext);

            var newCheckSum     = Guid.NewGuid().ToString();
            var newExtension    = Guid.NewGuid().ToString();
            var newCurrentFile  = Guid.NewGuid().ToString();
            var newUploaderType = Guid.NewGuid().ToString();
            var newFileLocation = Guid.NewGuid().ToString();
            var newFileName     = Guid.NewGuid().ToString();

            uploadedData.Checksum     = newCheckSum;
            uploadedData.Extension    = newExtension;
            uploadedData.CurrentFile  = newCurrentFile;
            uploadedData.UploaderType = newUploaderType;
            uploadedData.FileLocation = newFileLocation;
            uploadedData.FileName     = newFileName;
            //Act
            await uploadedData.Update(DbContext);

            var dbUploadedData          = DbContext.UploadedDatas.AsNoTracking().First();
            var uploadedDataList        = DbContext.UploadedDatas.AsNoTracking().ToList();
            var uploadedDataVersionList = DbContext.UploadedDataVersions.AsNoTracking().ToList();

            //Assert
            Assert.NotNull(dbUploadedData);

            Assert.AreEqual(1, uploadedDataList.Count);
            Assert.AreEqual(2, uploadedDataVersionList.Count);

            Assert.AreEqual(newCheckSum, dbUploadedData.Checksum);
            Assert.AreEqual(newExtension, dbUploadedData.Extension);
            Assert.AreEqual(newCurrentFile, dbUploadedData.CurrentFile);
            Assert.AreEqual(newUploaderType, dbUploadedData.UploaderType);
            Assert.AreEqual(newFileLocation, dbUploadedData.FileLocation);
            Assert.AreEqual(newFileName, dbUploadedData.FileName);
            Assert.AreEqual(uploadedData.PlanningCaseId, dbUploadedData.PlanningCaseId);
        }