public void Test_Merge_With_Merged_Default_ContactPoint()
        {
            var p1  = TestHelper.CreatePractitioner("A", "1");
            var cp1 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(p1, "cp1", "cp1");
            var cp2 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(p1, "cp2", "cp2");

            // Merge into cp12, change name and description for easy identification.
            var cp12 = ExternalPractitionerContactPointTests.TestHelper.SimpleMerge(cp1, cp2);

            cp12.Name        = "cp12";
            cp12.Description = "cp12";

            // This test does the following:
            // cp1 - a merged cp that is chosen as new default
            // cp2 - merged, deactivated
            // cp12 - previous default
            var newDefault   = cp1;
            var deactivated  = new List <ExternalPractitionerContactPoint>();
            var replacements = new Dictionary <ExternalPractitionerContactPoint, ExternalPractitionerContactPoint>();

            var p2 = TestHelper.CreatePractitioner("B", "2");

            ExternalPractitioner.MergePractitioners(p1, p2,
                                                    p2.Name, p2.LicenseNumber, p2.BillingNumber, p2.ExtendedProperties,
                                                    newDefault, deactivated, replacements);
        }
        public void Test_Merged_Default_Contact_Point()
        {
            var pA    = TestHelper.CreatePractitioner("A", "1");
            var cpPA1 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(pA, "cpPA1", "cpPA1");
            var cpPA2 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(pA, "cpPA2", "cpPA2");

            Assert.IsTrue(cpPA1.IsDefaultContactPoint);             // oroginal default
            Assert.IsFalse(cpPA2.IsDefaultContactPoint);

            var pB    = TestHelper.CreatePractitioner("B", "2");
            var cpPB1 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(pB, "cpPB1", "cpPB1");
            var cpPB2 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(pB, "cpPB2", "cpPB2");

            Assert.IsTrue(cpPB1.IsDefaultContactPoint);
            Assert.IsFalse(cpPB2.IsDefaultContactPoint);

            var newDefault   = cpPB2;
            var deactivated  = new List <ExternalPractitionerContactPoint>();                                         // None
            var replacements = new Dictionary <ExternalPractitionerContactPoint, ExternalPractitionerContactPoint>(); // None
            var result       = ExternalPractitioner.MergePractitioners(pA, pB,
                                                                       pA.Name, pA.LicenseNumber, pA.BillingNumber, pA.ExtendedProperties,
                                                                       newDefault, deactivated, replacements);
            var result_cpPA1 = CollectionUtils.SelectFirst(result.ContactPoints, cp => cp.Name == newDefault.Name);

            Assert.AreEqual(result.ContactPoints.Count, 4);
            Assert.IsTrue(result_cpPA1.IsDefaultContactPoint);             // new default with the same content
        }
            /// <summary>
            /// Perform a simple merge of two practitioners.
            /// </summary>
            /// <remarks>
            /// Destination is the primary practitioner.  The result will have all info inherit from destination.
            /// No contact points are deactivated or replaced.
            /// </remarks>
            public static ExternalPractitioner SimpleMerge(ExternalPractitioner src, ExternalPractitioner dest)
            {
                var deactivatedContactPoints = new List <ExternalPractitionerContactPoint>();
                var contactPointReplacements = new Dictionary <ExternalPractitionerContactPoint, ExternalPractitionerContactPoint>();

                return(ExternalPractitioner.MergePractitioners(src, dest,
                                                               dest.Name, dest.LicenseNumber, dest.BillingNumber,
                                                               dest.ExtendedProperties, dest.DefaultContactPoint,
                                                               deactivatedContactPoints, contactPointReplacements));
            }
        public void Test_Deactivate_And_Replace_Contact_Point_2()
        {
            var pA    = TestHelper.CreatePractitioner("A", "1");
            var cpPA1 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(pA, "cpPA1", "cpPA1");
            var cpPA2 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(pA, "cpPA2", "cpPA2");

            var pB    = TestHelper.CreatePractitioner("B", "2");
            var cpPB1 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(pB, "cpPB1", "cpPB1");
            var cpPB2 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(pB, "cpPB2", "cpPB2");

            // This test does the following:
            // cpPA1 - no-op, simply migrated
            // cpPA2 - deactivated and replaced
            // cpPB1 - no-op, simply migrated
            // cpPB2 - new default and new replacement
            var newDefault  = cpPB2;
            var deactivated = new List <ExternalPractitionerContactPoint> {
                cpPA2
            };
            var replacements = new Dictionary <ExternalPractitionerContactPoint, ExternalPractitionerContactPoint>
            {
                { cpPA2, cpPB2 }
            };

            var result = ExternalPractitioner.MergePractitioners(pA, pB,
                                                                 pA.Name, pA.LicenseNumber, pA.BillingNumber, pA.ExtendedProperties,
                                                                 newDefault, deactivated, replacements);

            var result_cpPA1 = CollectionUtils.SelectFirst(result.ContactPoints, cp => cp.Name == cpPA1.Name);
            var result_cpPA2 = CollectionUtils.SelectFirst(result.ContactPoints, cp => cp.Name == cpPA2.Name);
            var result_cpPB1 = CollectionUtils.SelectFirst(result.ContactPoints, cp => cp.Name == cpPB1.Name);
            var result_cpPB2 = CollectionUtils.SelectFirst(result.ContactPoints, cp => cp.Name == cpPB2.Name);

            Assert.AreEqual(result.ContactPoints.Count, 3);
            Assert.IsNotNull(result_cpPA1);
            Assert.IsNull(result_cpPA2);             // replaced
            Assert.IsNotNull(result_cpPB1);
            Assert.IsNotNull(result_cpPB2);

            // Check default
            Assert.IsTrue(result_cpPB2.IsDefaultContactPoint);

            // Check deactivated status.  All are active, because the only deactivated cp is replaced.
            Assert.IsFalse(result_cpPA1.Deactivated);
            Assert.IsFalse(result_cpPB1.Deactivated);
            Assert.IsFalse(result_cpPB2.Deactivated);

            // Check MergedInto
            Assert.AreEqual(cpPA1.MergedInto, result_cpPA1);
            Assert.AreEqual(cpPA2.MergedInto, result_cpPB2);             // Replaced
            Assert.AreEqual(cpPB1.MergedInto, result_cpPB1);
            Assert.AreEqual(cpPB2.MergedInto, result_cpPB2);
        }
        public void Test_Merge_With_No_Default_Contact_Point()
        {
            var p1  = TestHelper.CreatePractitioner("A", "1");
            var cp1 = ExternalPractitionerContactPointTests.TestHelper.AddContactPoint(p1, "cp1", "cp1");

            Assert.AreEqual(p1.DefaultContactPoint, cp1);

            var p2 = TestHelper.CreatePractitioner("B", "2");

            Assert.IsNull(p2.DefaultContactPoint);

            var deactivatedContactPoints = new List <ExternalPractitionerContactPoint>();
            var contactPointReplacements = new Dictionary <ExternalPractitionerContactPoint, ExternalPractitionerContactPoint>();
            var result = ExternalPractitioner.MergePractitioners(p1, p2,
                                                                 p2.Name, p2.LicenseNumber, p2.BillingNumber,
                                                                 p2.ExtendedProperties, p2.DefaultContactPoint,
                                                                 deactivatedContactPoints, contactPointReplacements);

            Assert.AreEqual(result.ContactPoints.Count, 1);
            Assert.IsNull(result.DefaultContactPoint);
        }
Esempio n. 6
0
        public MergeExternalPractitionerResponse MergeExternalPractitioner(MergeExternalPractitionerRequest request)
        {
            Platform.CheckForNullReference(request, "request");
            Platform.CheckMemberIsSet(request.LeftPractitionerRef, "LeftPractitionerRef");
            Platform.CheckMemberIsSet(request.RightPractitionerRef, "RightPractitionerRef");

            var left  = PersistenceContext.Load <ExternalPractitioner>(request.LeftPractitionerRef, EntityLoadFlags.Proxy);
            var right = PersistenceContext.Load <ExternalPractitioner>(request.RightPractitionerRef, EntityLoadFlags.Proxy);

            // if we are only doing a cost estimate, exit here without modifying any data
            if (request.EstimateCostOnly)
            {
                var cost = EstimateAffectedRecords(right, left);
                return(new MergeExternalPractitionerResponse(cost));
            }

            // unpack the request, loading all required entities
            var nameAssembler = new PersonNameAssembler();
            var name          = new PersonName();

            nameAssembler.UpdatePersonName(request.Name, name);

            var defaultContactPoint = request.DefaultContactPointRef != null?
                                      PersistenceContext.Load <ExternalPractitionerContactPoint>(request.DefaultContactPointRef) : null;

            var deactivatedContactPoints = request.DeactivatedContactPointRefs == null ? new List <ExternalPractitionerContactPoint>() :
                                           CollectionUtils.Map(request.DeactivatedContactPointRefs,
                                                               (EntityRef cpRef) => PersistenceContext.Load <ExternalPractitionerContactPoint>(cpRef));

            var cpReplacements = CollectionUtils.Map(request.ContactPointReplacements ?? (new Dictionary <EntityRef, EntityRef>()),
                                                     kvp => new KeyValuePair <ExternalPractitionerContactPoint, ExternalPractitionerContactPoint>(
                                                         PersistenceContext.Load <ExternalPractitionerContactPoint>(kvp.Key, EntityLoadFlags.Proxy),
                                                         PersistenceContext.Load <ExternalPractitionerContactPoint>(kvp.Value, EntityLoadFlags.Proxy)));


            // merge the practitioners
            var result = ExternalPractitioner.MergePractitioners(left, right,
                                                                 name,
                                                                 request.LicenseNumber,
                                                                 request.BillingNumber,
                                                                 request.ExtendedProperties,
                                                                 defaultContactPoint,
                                                                 deactivatedContactPoints,
                                                                 cpReplacements
                                                                 );

            PersistenceContext.Lock(result, DirtyState.New);

            // if user has verify permission, verify the result
            if (Thread.CurrentPrincipal.IsInRole(AuthorityTokens.Admin.Data.ExternalPractitionerVerification))
            {
                result.MarkVerified();
            }

            // queue work items to migrate orders and visits
            foreach (var practitioner in new[] { right, left })
            {
                var queueItem = MergeWorkQueueItem.Create(practitioner.GetRef());
                PersistenceContext.Lock(queueItem, DirtyState.New);
            }

            PersistenceContext.SynchState();

            var assembler = new ExternalPractitionerAssembler();

            return(new MergeExternalPractitionerResponse(assembler.CreateExternalPractitionerSummary(result, this.PersistenceContext)));
        }