public MergeOrganisationStandardDeliveryArea(OrganisationStandardDeliveryArea sourceOrganisationStandardDeliveryArea, string replicates)
 {
     Replicates             = replicates;
     DeliveryAreaId         = sourceOrganisationStandardDeliveryArea.DeliveryAreaId;
     OrganisationStandardId = sourceOrganisationStandardDeliveryArea.OrganisationStandardId;
     Status   = sourceOrganisationStandardDeliveryArea.Status;
     Comments = sourceOrganisationStandardDeliveryArea.Comments;
     OrganisationStandardDeliveryAreaId = sourceOrganisationStandardDeliveryArea.Id;
 }
        private void MergeOrganisationStandardsAndVersions(Organisation primaryOrganisation, Organisation secondaryOrganisation, string createdByUser, DateTime secondaryStandardsEffectiveTo)
        {
            // Grab the primary contact for the primary organisation because we're going to need it.
            var primaryContact = _dbContext.Contacts.FirstOrDefault(c => c.EndPointAssessorOrganisationId == primaryOrganisation.EndPointAssessorOrganisationId && c.Email == primaryOrganisation.PrimaryContact);

            // Read all the standards from the secondary organisation.

            foreach (var secondaryOrganisationStandard in secondaryOrganisation.OrganisationStandards)
            {
                // Does the primary organisation have this standard already?
                var primaryOrganisationStandard = primaryOrganisation.OrganisationStandards.FirstOrDefault(pos => pos.StandardCode == secondaryOrganisationStandard.StandardCode);

                if (null == primaryOrganisationStandard)
                {
                    // No - so add the standard to the primary organisation.

                    primaryOrganisationStandard = new OrganisationStandard()
                    {
                        DateStandardApprovedOnRegister = secondaryOrganisationStandard.DateStandardApprovedOnRegister,
                        OrganisationStandardData       = secondaryOrganisationStandard.OrganisationStandardData,
                        Comments      = $"This standard/version has been acquired from a merge with {secondaryOrganisation.EndPointAssessorOrganisationId} on {DateTime.UtcNow.ToString("dd MMM yyyy")}. {secondaryOrganisationStandard.Comments}",
                        ContactId     = primaryContact?.Id,
                        EffectiveFrom = secondaryOrganisationStandard.EffectiveFrom,
                        EffectiveTo   = secondaryOrganisationStandard.EffectiveTo,
                        EndPointAssessorOrganisationId = primaryOrganisation.EndPointAssessorOrganisationId,
                        StandardCode      = secondaryOrganisationStandard.StandardCode,
                        StandardReference = secondaryOrganisationStandard.StandardReference,
                        Status            = secondaryOrganisationStandard.Status,

                        OrganisationStandardVersions      = new List <OrganisationStandardVersion>(),
                        OrganisationStandardDeliveryAreas = new List <OrganisationStandardDeliveryArea>(),
                    };
                    primaryOrganisationStandard.Comments = primaryOrganisationStandard.Comments.Substring(0, Math.Min(primaryOrganisationStandard.Comments.Length, 500));
                    primaryOrganisation.OrganisationStandards.Add(primaryOrganisationStandard);
                }

                // Now read all the versions for this standard for the secondary organisation.

                foreach (var secondaryOrganisationStandardVersion in secondaryOrganisationStandard.OrganisationStandardVersions)
                {
                    // Does the standard version exist for this standard in the primary organisation?

                    var primaryOrganisationStandardVersion = primaryOrganisationStandard.OrganisationStandardVersions.FirstOrDefault(posv => posv.StandardUId == secondaryOrganisationStandardVersion.StandardUId && posv.Version == secondaryOrganisationStandardVersion.Version);
                    if (null == primaryOrganisationStandardVersion)
                    {
                        // No - so add the standard version to the standard for the primary organisation.

                        primaryOrganisationStandardVersion = new OrganisationStandardVersion()
                        {
                            DateVersionApproved = secondaryOrganisationStandardVersion.DateVersionApproved,
                            Comments            = $"This standard/version has been acquired from a merge with {secondaryOrganisation.EndPointAssessorOrganisationId} on {DateTime.UtcNow.ToString("dd MMM yyyy")}. {secondaryOrganisationStandardVersion.Comments}",
                            EffectiveFrom       = secondaryOrganisationStandardVersion.EffectiveFrom,
                            EffectiveTo         = secondaryOrganisationStandardVersion.EffectiveTo,
                            StandardUId         = secondaryOrganisationStandardVersion.StandardUId,
                            Status  = secondaryOrganisationStandardVersion.Status,
                            Version = secondaryOrganisationStandardVersion.Version,
                        };
                        primaryOrganisationStandardVersion.Comments = primaryOrganisationStandardVersion.Comments.Substring(0, Math.Min(primaryOrganisationStandardVersion.Comments.Length, 500));
                        primaryOrganisationStandard.OrganisationStandardVersions.Add(primaryOrganisationStandardVersion);
                    }

                    // Mark the secondary standard version as ending
                    secondaryOrganisationStandardVersion.EffectiveTo = secondaryStandardsEffectiveTo;
                    secondaryOrganisationStandardVersion.Comments    = $"This standard version has been merged in to {primaryOrganisation.EndPointAssessorOrganisationId} on {DateTime.UtcNow.ToString("dd MMM yyyy")}. ${secondaryOrganisationStandardVersion.Comments}";
                    secondaryOrganisationStandardVersion.Comments    = secondaryOrganisationStandardVersion.Comments.Substring(0, Math.Min(secondaryOrganisationStandardVersion.Comments.Length, 500));
                }

                foreach (var secondaryOrganisationStandardDeliveryArea in secondaryOrganisationStandard.OrganisationStandardDeliveryAreas)
                {
                    // Does the primary organisation standard have this delivery area?
                    var primaryOrganisationStandardDeliveryArea = primaryOrganisationStandard.OrganisationStandardDeliveryAreas.FirstOrDefault(posda => posda.DeliveryAreaId == secondaryOrganisationStandardDeliveryArea.DeliveryAreaId);

                    if (null == primaryOrganisationStandardDeliveryArea)
                    {
                        // No - so add the area

                        primaryOrganisationStandardDeliveryArea = new OrganisationStandardDeliveryArea()
                        {
                            DeliveryAreaId       = secondaryOrganisationStandardDeliveryArea.DeliveryAreaId,
                            Comments             = $"This delivery area has been acquired from a merge with {secondaryOrganisation.EndPointAssessorOrganisationId} on {DateTime.UtcNow.ToString("dd MMM yyyy")}. {secondaryOrganisationStandardDeliveryArea.Comments}",
                            Status               = secondaryOrganisationStandardDeliveryArea.Status,
                            OrganisationStandard = primaryOrganisationStandard,
                            DeliveryArea         = secondaryOrganisationStandardDeliveryArea.DeliveryArea,
                        };
                        primaryOrganisationStandardDeliveryArea.Comments = primaryOrganisationStandardDeliveryArea.Comments.Substring(0, Math.Min(primaryOrganisationStandardDeliveryArea.Comments.Length, 500));
                        primaryOrganisationStandard.OrganisationStandardDeliveryAreas.Add(primaryOrganisationStandardDeliveryArea);
                    }

                    secondaryOrganisationStandardDeliveryArea.Comments = $"This delivery area has been merged in to {primaryOrganisation.EndPointAssessorOrganisationId} on {DateTime.UtcNow.ToString("dd MMM yyyy")}. {secondaryOrganisationStandardDeliveryArea.Comments}";
                    secondaryOrganisationStandardDeliveryArea.Comments = secondaryOrganisationStandardDeliveryArea.Comments.Substring(0, Math.Min(secondaryOrganisationStandardDeliveryArea.Comments.Length, 500));
                }

                // Now set the effectiveTo and comments @ToDo: need content

                secondaryOrganisationStandard.EffectiveTo = secondaryStandardsEffectiveTo;
                secondaryOrganisationStandard.Comments    = $"This standard has been merged in to Organisation {primaryOrganisation.EndPointAssessorOrganisationId} on {DateTime.UtcNow.ToString("dd MMM yyyy")}. {secondaryOrganisationStandard.Comments}";
                secondaryOrganisationStandard.Comments    = secondaryOrganisationStandard.Comments.Substring(0, Math.Min(secondaryOrganisationStandard.Comments.Length, 500));
            }
        }
Ejemplo n.º 3
0
        public void When_Constructed_From_OrganisationStandardDeliveryArea_Then_Values_Should_Be_Copied(OrganisationStandardDeliveryArea sourceObject)
        {
            var objectUnderTest = new MergeOrganisationStandardDeliveryArea(sourceObject, "Before");

            objectUnderTest.Replicates.Should().Be("Before");
            objectUnderTest.DeliveryAreaId.Should().Be(sourceObject.DeliveryAreaId);
            objectUnderTest.OrganisationStandardId.Should().Be(sourceObject.OrganisationStandardId);
            objectUnderTest.Comments.Should().Be(sourceObject.Comments);
            objectUnderTest.Status.Should().Be(sourceObject.Status);
            objectUnderTest.OrganisationStandardDeliveryAreaId.Should().Be(sourceObject.Id);
        }