Esempio n. 1
0
        public async Task <LeadResult> UpdateLead(Guid leadId, LeadInput model)
        {
            _logger.LogInformation(GetLogMessage("Updating Lead: {0}; Input: {@input}"), leadId, model);
            var retVal = new LeadResult()
            {
                LeadId = leadId
            };

            var entity = await Repository.FirstOrDefaultAsync(n => n.Id == leadId);

            entity.InjectFrom <NullableInjection>(model);
            entity.UpdatedById = _userInfo.Value.UserId;
            entity.Updated     = DateTimeOffset.UtcNow;
            entity.ObjectState = ObjectState.Modified;

            var leadResult = Repository.InsertOrUpdateGraph(entity, true);

            _logger.LogDebug(GetLogMessage("{0} records updated in database"), leadResult);

            if (leadResult > 0)
            {
                retVal.Succeeded = true;
                await Task.Run(() =>
                {
                    RaiseEvent(new LeadUpdatedEvent
                    {
                        LeadId = retVal.LeadId.Value
                    });
                });
            }


            return(retVal);
        }
Esempio n. 2
0
        private async Task <LeadResult> DeleteLead(Guid leadId)
        {
            _logger.LogInformation(GetLogMessage("Lead:{0}"), leadId);
            var retVal = new LeadResult();

            var lead = await Repository.FirstOrDefaultAsync(x => x.Id == leadId);

            lead.UpdatedById = _userInfo.Value.UserId;
            lead.Updated     = DateTimeOffset.UtcNow;
            lead.IsDeleted   = true;

            var records = await Repository.UpdateAsync(lead, true);

            _logger.LogDebug(GetLogMessage("{0} records updated"));
            if (records > 0)
            {
                retVal.Succeeded = true;
                await Task.Run(() =>
                {
                    RaiseEvent(new LeadDeletedEvent
                    {
                        LeadId = leadId
                    });
                });
            }


            return(retVal);
        }
        private async Task <LeadResult> RejectLead(Guid leadId, LeadRejectInput input)
        {
            _logger.LogInformation(GetLogMessage("Rejecting lead: {0}; for Reason: {1}"), leadId, input.RejectionReason);
            var retVal = new LeadResult()
            {
                LeadId = leadId
            };

            var entity = await Repository
                         .FirstOrDefaultAsync(n => n.Id == leadId);

            if (entity.Status == LeadStatus.Promoted)
            {
                retVal.ErrorMessage = "Lead has already been promoted";
                return(retVal);
            }

            if (entity.Status != LeadStatus.Rejected)
            {
                _logger.LogDebug(GetLogMessage("Lead is able to be rejected"));

                entity.Status          = LeadStatus.Rejected;
                entity.RejectionReason = input.RejectionReason;
                entity.UpdatedById     = _userInfo.Value.UserId;
                entity.Updated         = DateTimeOffset.UtcNow;
                entity.ObjectState     = ObjectState.Modified;

                entity.StatusTransitions.Add(new LeadStatusTransition()
                {
                    Status      = LeadStatus.Rejected,
                    ObjectState = ObjectState.Added
                });

                var leadResult = Repository.InsertOrUpdateGraph(entity, true);

                _logger.LogDebug(GetLogMessage("{0} records updated"), leadResult);

                if (leadResult > 0)
                {
                    retVal.Succeeded = true;
                    await Task.Run(() =>
                    {
                        RaiseEvent(new LeadRejectedEvent
                        {
                            LeadId = leadId
                        });
                    });
                }
            }
            else
            {
                _logger.LogDebug(GetLogMessage("Lead is already rejected"));
            }

            return(retVal);
        }
        public async Task <LeadResult> QualifyLead(IProviderAgencyOwner ao, Guid leadId, LeadQualifyInput input)
        {
            _logger.LogInformation(GetLogMessage($@"LeadId: {leadId}"));

            var retVal = new LeadResult()
            {
                LeadId = leadId
            };

            var entity = await Repository
                         .Queryable()
                         .ForAgencyOwner(ao)
                         .Where(x => x.Id == leadId)
                         .FirstAsync();

            if (entity.Status != LeadStatus.Qualified)
            {
                entity.InjectFrom(input);
                entity.AccountManagerId             = input.AccountManagerId;
                entity.AccountManagerOrganizationId = ao.OrganizationId;
                entity.Status      = LeadStatus.Qualified;
                entity.UpdatedById = _userInfo.Value.UserId;
                entity.Updated     = DateTimeOffset.UtcNow;
                entity.ObjectState = ObjectState.Modified;

                entity.StatusTransitions.Add(new LeadStatusTransition()
                {
                    Status      = LeadStatus.Qualified,
                    ObjectState = ObjectState.Added
                });

                var leadResult = Repository.Update(entity, true);

                _logger.LogDebug(GetLogMessage("{0} records added"), leadResult);

                if (leadResult > 0)
                {
                    var individualBonusResult = await _individualBonusIntents.Create(new CreateBonusIntentOptions()
                    {
                        LeadId = leadId
                    });

                    var organizationBonusResult = await _organizationBonusIntents.Create(new CreateBonusIntentOptions()
                    {
                        LeadId = leadId
                    });

                    if (individualBonusResult.Succeeded && organizationBonusResult.Succeeded)
                    {
                        retVal.Succeeded = true;
                        await Task.Run(() =>
                        {
                            RaiseEvent(new LeadQualifiedEvent
                            {
                                LeadId = leadId
                            });
                        });
                    }
                }
            }
            else
            {
                _logger.LogDebug(GetLogMessage("Lead is already qualified"));
            }

            return(retVal);
        }
Esempio n. 5
0
        private async Task <LeadResult> CreateLead(IOrganizationMarketer ma, Guid providerOrganizationId, LeadInput input)
        {
            _logger.LogInformation(GetLogMessage("MA: {0}"), ma.OrganizationId);

            var retVal = new LeadResult();

            if (await CountLeadsPerProviderByEmail(providerOrganizationId, input.EmailAddress) > 0)
            {
                retVal.ErrorMessage = "Email has already been used as a lead for this organization";
                retVal.Succeeded    = false;

                return(retVal);
            }

            _logger.LogDebug(GetLogMessage("Email hasn't been used by this organization"));

            var isExternal = providerOrganizationId != ma.OrganizationId;

            _logger.LogDebug(GetLogMessage("External Lead: {0}"), isExternal);

            var marketer = await _marketerService.Repository.Queryable()
                           .Include(x => x.Marketer)
                           .ThenInclude(x => x.Person)
                           .Where(x => x.MarketerId == ma.MarketerId && x.OrganizationId == ma.OrganizationId)
                           .FirstAsync();

            var marketerBonus        = marketer.MarketerBonus;
            var marketerAgencyBonus  = 0m;
            var marketerAgencyStream = 0m;
            var marketerStream       = marketer.MarketerStream;


            _logger.LogTrace(
                GetLogMessage(
                    $@"Marketer Found: {marketer.MarketerId}"));

            if (isExternal)
            {
                var marketingAgreement = await _marketingAgreements.Queryable()
                                         .Where(x => x.ProviderOrganizationId == providerOrganizationId &&
                                                x.MarketingOrganizationId == ma.OrganizationId)
                                         .FirstOrDefaultAsync();


                if (marketingAgreement == null)
                {
                    retVal.ErrorMessage =
                        "Marketing agreement doesn't exist between marketing and provider organization";
                    return(retVal);
                }


                if (marketingAgreement.Status != AgreementStatus.Approved)
                {
                    retVal.ErrorMessage = "Marketing agreement is not approved";
                    return(retVal);
                }

                _logger.LogDebug(
                    GetLogMessage(
                        $@"Marketing Agreement found to be valid"));

                marketerBonus        = marketingAgreement.MarketerBonus;
                marketerAgencyBonus  = marketingAgreement.MarketingAgencyBonus;
                marketerAgencyStream = marketingAgreement.MarketingAgencyStream;
                marketerStream       = marketingAgreement.MarketerStream;
            }


            var lead = new Lead
            {
                Iso2                   = input.Iso2,
                ProvinceState          = input.ProvinceState,
                MarketerId             = marketer.MarketerId,
                MarketerOrganizationId = marketer.OrganizationId,
                MarketerStream         = marketerStream,
                MarketerBonus          = marketerBonus,
                MarketingAgencyBonus   = marketerAgencyBonus,
                MarketingAgencyStream  = marketerAgencyStream,
                ProviderOrganizationId = providerOrganizationId,
                UpdatedById            = marketer.MarketerId,
                CreatedById            = marketer.MarketerId, // cant be _userInfo.UserId
                ObjectState            = ObjectState.Added,
                Status                 = LeadStatus.New
            };

            lead.StatusTransitions.Add(new LeadStatusTransition()
            {
                Status      = LeadStatus.New,
                ObjectState = ObjectState.Added
            });

            lead.InjectFrom(input);

            var records = Repository.Insert(lead, true);

            _logger.LogDebug(GetLogMessage("{0} records updated in db"), records);

            if (records > 0)
            {
                retVal.LeadId    = lead.Id;
                retVal.Succeeded = true;

                await Task.Run(() => RaiseEvent(new LeadCreatedEvent
                {
                    LeadId = lead.Id
                }));
            }

            return(retVal);
        }