Example #1
0
        public async Task <TypedResult <ContactOutDto> > GetContact(Guid recordId)
        {
            try
            {
                var foundContactResult = (await ContactCollection.FindAsync(c => c.RecordId == recordId)).FirstOrDefault();
                if (foundContactResult == null)
                {
                    return(new NotFoundTypedResult <ContactOutDto>());
                }

                return(new SuccessfulTypedResult <ContactOutDto>(ContactOutDto.EntityToOutDto(foundContactResult)));
            }
            catch (Exception e)
            {
                return(new FailedTypedResult <ContactOutDto>(e));
            }
        }
Example #2
0
        public async Task <TypedResult <ContactOutDto> > UpdateContact(Guid recordId, UpdateContactDtoIn updateContactIn)
        {
            try
            {
                var foundContactResult = (await ContactCollection.FindAsync(c => c.RecordId == recordId)).FirstOrDefault();
                if (foundContactResult == null)
                {
                    return(new NotFoundTypedResult <ContactOutDto>());
                }

                foundContactResult.MailingAddress = new AddressEntity
                {
                    StreetLineOne = updateContactIn.MailingAddress.StreetLineOne,
                    StreetLineTwo = updateContactIn.MailingAddress.StreetLineTwo,
                    AptSuite      = updateContactIn.MailingAddress.AptSuite,
                    City          = updateContactIn.MailingAddress.City,
                    State         = updateContactIn.MailingAddress.State,
                    County        = updateContactIn.MailingAddress.County,
                    Zip           = updateContactIn.MailingAddress.Zip
                };
                foundContactResult.OtherAddress = new AddressEntity
                {
                    StreetLineOne = updateContactIn.OtherAddress.StreetLineOne,
                    StreetLineTwo = updateContactIn.OtherAddress.StreetLineTwo,
                    AptSuite      = updateContactIn.OtherAddress.AptSuite,
                    City          = updateContactIn.OtherAddress.City,
                    State         = updateContactIn.OtherAddress.State,
                    County        = updateContactIn.OtherAddress.County,
                    Zip           = updateContactIn.OtherAddress.Zip
                };

                foundContactResult.PrimaryName = new NameEntity
                {
                    FirstName          = updateContactIn.PrimaryName.FirstName,
                    LastName           = updateContactIn.PrimaryName.LastName,
                    MiddleName         = updateContactIn.PrimaryName.MiddleName,
                    Salutation         = updateContactIn.PrimaryName.Salutation,
                    PreferredFirstName = updateContactIn.PrimaryName.PreferredFirstName
                };
                foundContactResult.SecondaryName = new NameEntity
                {
                    FirstName          = updateContactIn.SecondaryName.FirstName,
                    LastName           = updateContactIn.SecondaryName.LastName,
                    MiddleName         = updateContactIn.SecondaryName.MiddleName,
                    Salutation         = updateContactIn.SecondaryName.Salutation,
                    PreferredFirstName = updateContactIn.SecondaryName.PreferredFirstName
                };
                foundContactResult.ModifiedBy         = updateContactIn.ModifiedBy;
                foundContactResult.LastModifiedTime   = DateTimeOffset.UtcNow;
                foundContactResult.AnnualSalary       = updateContactIn.AnnualSalary;
                foundContactResult.Tags               = updateContactIn.Tags.Select(x => x);
                foundContactResult.PrimaryContactInfo = new ContactInfoEntity
                {
                    Mobile = updateContactIn.PrimaryContactInfo.Mobile,
                    Fax    = updateContactIn.PrimaryContactInfo.Fax,
                    Email  = updateContactIn.PrimaryContactInfo.Email,
                    Home   = updateContactIn.PrimaryContactInfo.Home,
                    PreferredContactMethod = updateContactIn.PrimaryContactInfo.PreferredContactMethod
                };
                foundContactResult.SecondaryContactInfo = new ContactInfoEntity
                {
                    Mobile = updateContactIn.SecondaryContactInfo.Mobile,
                    Fax    = updateContactIn.SecondaryContactInfo.Fax,
                    Email  = updateContactIn.SecondaryContactInfo.Email,
                    Home   = updateContactIn.SecondaryContactInfo.Home,
                    PreferredContactMethod = updateContactIn.SecondaryContactInfo.PreferredContactMethod
                };

                var updatedContact = await ContactCollection.ReplaceOneAsync(c => c.RecordId == recordId, foundContactResult,
                                                                             new ReplaceOptions()
                {
                    IsUpsert = false
                });

                return(new SuccessfulTypedResult <ContactOutDto>(ContactOutDto.EntityToOutDto(foundContactResult)));
            }
            catch (Exception e)
            {
                return(new FailedTypedResult <ContactOutDto>(e));
            }
        }
        public async Task <TypedResult <ContactOutDto> > ConvertLead(Guid recordId, ConvertLeadDtoIn convertLeadDtoIn)
        {
            try
            {
                var foundLead = await LeadCollection.FindOneAndUpdateAsync <LeadEntity>(l => l.RecordId == recordId,
                                                                                        Builders <LeadEntity> .Update.Combine(
                                                                                            Builders <LeadEntity> .Update.Set(le => le.Converted, true),
                                                                                            Builders <LeadEntity> .Update.Set(le => le.ModifiedBy, convertLeadDtoIn.ModifiedBy),
                                                                                            Builders <LeadEntity> .Update.Set(le => le.LastModifiedTime, DateTimeOffset.UtcNow)
                                                                                            ), GetEntityAfterUpdateOption <LeadEntity>());

                if (foundLead == null)
                {
                    return(new NotFoundTypedResult <ContactOutDto>());
                }

                var dateTimeNow = DateTimeOffset.UtcNow;

                var newContact = new ContactEntity
                {
                    RecordId         = Guid.NewGuid(),
                    CreatedTime      = dateTimeNow,
                    CreatedByUserId  = convertLeadDtoIn.ModifiedBy,
                    ModifiedBy       = convertLeadDtoIn.ModifiedBy,
                    LastModifiedTime = dateTimeNow,
                    AnnualSalary     = foundLead.AnnualSalary,
                    LeadSource       = foundLead.LeadSource,
                    Tags             = foundLead.Tags.Select(x => x),
                    MailingAddress   = new AddressEntity
                    {
                        StreetLineOne = foundLead.Address.StreetLineOne,
                        StreetLineTwo = foundLead.Address.StreetLineTwo,
                        AptSuite      = foundLead.Address.AptSuite,
                        City          = foundLead.Address.City,
                        State         = foundLead.Address.State,
                        County        = foundLead.Address.County,
                        Zip           = foundLead.Address.Zip
                    },
                    OtherAddress = null,
                    PrimaryName  = new NameEntity
                    {
                        FirstName          = foundLead.Name.FirstName,
                        LastName           = foundLead.Name.LastName,
                        MiddleName         = foundLead.Name.MiddleName,
                        Salutation         = foundLead.Name.Salutation,
                        PreferredFirstName = foundLead.Name.PreferredFirstName
                    },
                    SecondaryName      = null,
                    PrimaryContactInfo = new ContactInfoEntity
                    {
                        Mobile = foundLead.ContactInfo.Mobile,
                        Fax    = foundLead.ContactInfo.Fax,
                        Email  = foundLead.ContactInfo.Email,
                        Home   = foundLead.ContactInfo.Home,
                        PreferredContactMethod = foundLead.ContactInfo.PreferredContactMethod
                    },
                    SecondaryContactInfo = null,
                    AssociatedLeadId     = foundLead.RecordId
                };

                await ContactCollection.InsertOneAsync(newContact);


                return(new SuccessfulTypedResult <ContactOutDto>(ContactOutDto.EntityToOutDto(newContact)));
            }
            catch (Exception e)
            {
                return(new FailedTypedResult <ContactOutDto>(e));
            }
        }