Beispiel #1
0
        public static Campaign UpdateCampaignContact(this Campaign campaign, IPrimaryContactModel contactModel, AllReadyContext _context)
        {
            if (campaign.CampaignContacts == null)
            {
                campaign.CampaignContacts = new List<CampaignContact>();
            }

            var primaryCampaignContact = campaign.CampaignContacts.SingleOrDefault(tc => tc.ContactType == (int)ContactTypes.Primary);
            var contactId = primaryCampaignContact?.ContactId;
            Contact primaryContact;

            var contactInfo = string.Concat(contactModel.PrimaryContactEmail?.Trim() + contactModel.PrimaryContactFirstName?.Trim(), contactModel.PrimaryContactLastName?.Trim(), contactModel.PrimaryContactPhoneNumber?.Trim());

            if (contactId == null)
            {
                primaryContact = new Contact();
            }
            else
            {
                primaryContact = _context.Contacts.Single(c => c.Id == contactId);
            }

            if (string.IsNullOrWhiteSpace(contactInfo) && primaryCampaignContact != null)
            {
                //Delete
                _context.CampaignContacts.Remove(primaryCampaignContact);
                _context.Remove(primaryCampaignContact);//Not Needed?
                _context.Remove(primaryCampaignContact.Contact);
            }
            else
            {
                primaryContact.Email = contactModel.PrimaryContactEmail;
                primaryContact.FirstName = contactModel.PrimaryContactFirstName;
                primaryContact.LastName = contactModel.PrimaryContactLastName;
                primaryContact.PhoneNumber = contactModel.PrimaryContactPhoneNumber;
                _context.Update(primaryContact);

                if (primaryCampaignContact == null)
                {
                    primaryCampaignContact = new CampaignContact
                    {
                        Contact = primaryContact,
                        Campaign = campaign,
                        ContactType = (int)ContactTypes.Primary
                    };
                    campaign.CampaignContacts.Add(primaryCampaignContact);
                    _context.Update(primaryCampaignContact);
                }
            }
            return campaign;
        }
        public async Task SetPrimaryContactInCampaignSummaryViewModel()
        {
            var campaignContact = new CampaignContact
            {
                ContactType = (int) ContactTypes.Primary,
                Contact = new Contact { FirstName = "FirstName", LastName = "LastName", Email = "Email", PhoneNumber = "111-1111-1111" }
            };

            Context.Campaigns.First().CampaignContacts = new List<CampaignContact> { campaignContact };
            Context.SaveChanges();

            var handler = new CampaignSummaryQueryHandler(Context);

            var result = await handler.Handle(new CampaignSummaryQuery { CampaignId = 111 });

            result.PrimaryContactFirstName.ShouldBe(campaignContact.Contact.FirstName);
            result.PrimaryContactLastName.ShouldBe(campaignContact.Contact.LastName);
            result.PrimaryContactEmail.ShouldBe(campaignContact.Contact.Email);
            result.PrimaryContactPhoneNumber.ShouldBe(campaignContact.Contact.PhoneNumber);
        }
        protected override void LoadTestData()
        {
            var testOrg = new Organization
            {
                Name = "My Test Campaign",
                LogoUrl = "http://www.htbox.org/testCampaign",
                WebUrl = "http://www.htbox.org",
                Campaigns = new List<Campaign>()
            };

            var testCampaign1 = new Campaign
            {
                Name = "Test Campaign 1",
                ManagingOrganization = testOrg,
                CampaignContacts = new List<CampaignContact>()
            };

            var testCampaign2 = new Campaign
            {
                Name = "Test Campaign 2",
                ManagingOrganization = testOrg,
                CampaignContacts = new List<CampaignContact>()
            };

            var primaryCampaignContact1 = new CampaignContact
            {
                ContactType = (int)ContactTypes.Primary,
                Contact = new Contact
                {
                    Email = "*****@*****.**"
                }
            };

            var primaryCampaignContact2 = new CampaignContact
            {
                ContactType = (int)ContactTypes.Primary,
                Contact = new Contact()
            };

            var testEvent1 = new Event
            {
                Id = 5,
                Name = "Test Event Name",
                Campaign = testCampaign1,
                CampaignId = testCampaign1.Id,
                StartDateTime = DateTime.UtcNow.AddDays(10),
                EndDateTime = DateTime.UtcNow.AddDays(10).AddHours(8),
                Location = new Location { Id = 2 },
                RequiredSkills = new List<EventSkill>()
            };

            var testEvent2 = new Event
            {
                Id = 7,
                Name = "Test Event 2",
                Campaign = testCampaign2,
                CampaignId = testCampaign2.Id,
                StartDateTime = DateTime.UtcNow.AddDays(3),
                EndDateTime = DateTime.UtcNow.AddDays(3).AddHours(4),
                Location = new Location { Id = 5 },
                RequiredSkills = new List<EventSkill>()
            };

            var username1 = $"*****@*****.**";
            var user1 = new ApplicationUser { UserName = username1, Email = username1, EmailConfirmed = true };
            Context.Users.Add(user1);

            var username2 = $"*****@*****.**";
            var user2 = new ApplicationUser { UserName = username2, Email = username2, EmailConfirmed = true };
            Context.Users.Add(user2);

            testCampaign1.CampaignContacts.Add(primaryCampaignContact1);
            testCampaign2.CampaignContacts.Add(primaryCampaignContact2);

            testOrg.Campaigns.Add(testCampaign1);
            testOrg.Campaigns.Add(testCampaign2);

            Context.Organizations.Add(testOrg);
            Context.Events.Add(testEvent1);
            Context.Events.Add(testEvent2);

            var testTask1 = new AllReadyTask
            {
                Id = 7,
                Event = testEvent1,
                EventId = testEvent1.Id
                // Required Skills?
            };

            var testTask2 = new AllReadyTask
            {
                Id = 9,
                Event = testEvent2,
                EventId = testEvent2.Id
                // Required Skills?
            };

            Context.Tasks.Add(testTask1);
            Context.Tasks.Add(testTask2);

            Context.SaveChanges();
        }