public async Task <JsonResult> UpdateLead([Required] UpdateLeadViewModel model)
 {
     if (!ModelState.IsValid)
     {
         return(JsonModelStateErrors());
     }
     return(await JsonAsync(_leadService.UpdateLeadAsync(model)));
 }
示例#2
0
        /// <summary>
        /// Update lead
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual async Task <ResultModel> UpdateLeadAsync([Required] UpdateLeadViewModel model)
        {
            if (model == null)
            {
                return(new InvalidParametersResultModel());
            }

            var lead = await _context.Leads
                       .Include(i => i.Team)
                       .ThenInclude(i => i.TeamMembers)
                       .FirstOrDefaultAsync(x => x.Id.Equals(model.Id));

            if (lead == null)
            {
                return(new NotFoundResultModel());
            }

            var lastStageId = lead.StageId;
            var lastStateId = lead.LeadStateId;

            lead.Name         = model.Name;
            lead.CurrencyCode = model.CurrencyCode;
            lead.Value        = model.Value;
            if (model.OrganizationId != null)
            {
                lead.OrganizationId = model.OrganizationId.Value;
            }
            lead.DeadLine = model.DeadLine;
            if (model.LeadStateId != null)
            {
                lead.LeadStateId = model.LeadStateId.Value;
            }
            lead.StageId          = model.StageId;
            lead.ContactId        = model.ContactId;
            lead.SolutionTypeId   = model.SolutionTypeId;
            lead.SourceId         = model.SourceId;
            lead.TechnologyTypeId = model.TechnologyTypeId;
            lead.ProductTypeId    = model.ProductTypeId;
            lead.ServiceTypeId    = model.ServiceTypeId;
            lead.Description      = model.Description;
            _context.Leads.Update(lead);


            var result = await _context.PushAsync();

            if (result.IsSuccess)
            {
                var listNotificationUserId = lead.Team.TeamMembers.Select(s => s.UserId);

                if (lastStageId != lead.StageId)
                {
                    var newStage = await _context.Stages.FindByIdAsync(lead.StageId);

                    var lastStage = await _context.Stages.FindByIdAsync(lastStageId);

                    var notification = new Notification
                    {
                        Content            = $"Lead '{lead?.Name}' change stage '{lastStage?.Name}' to '{newStage?.Name}'",
                        Subject            = "Info",
                        NotificationTypeId = NotificationType.Info
                    };
                    await _notify.SendNotificationAsync(listNotificationUserId, notification);

                    await _notify.SendNotificationToSystemAdminsAsync(notification);
                }

                if (lastStateId != lead.LeadStateId)
                {
                    var newState = await _context.States.FindByIdAsync(lead.LeadStateId);

                    var lastState = await _context.States.FindByIdAsync(lastStateId);


                    var notification = new Notification
                    {
                        Content            = $"Lead '{lead?.Name}' change state '{lastState?.Name}' to '{newState?.Name}'",
                        Subject            = "Info",
                        NotificationTypeId = NotificationType.Info
                    };
                    await _notify.SendNotificationAsync(listNotificationUserId, notification);

                    await _notify.SendNotificationToSystemAdminsAsync(notification);
                }
            }

            return(result);
        }