Example #1
0
        public async Task OnDispatchCompleteAsync(OutputResult outputResult)
        {
            // If dispatch failed, upate the telemetry
            if (!outputResult.Delivered && outputResult.Targets != null && outputResult.Targets.Count > 0)
            {
                // Update record detail
                var details = outputResult.Targets.Select(t => new ReportDetail
                {
                    PhoneNumber = t,
                    MessageId   = outputResult.MessageId.ToString(),
                    State       = ErrorCodeHelper.ConvertMessageStateFromRequestOutcome(outputResult.DeliveryResponse.DeliveryOutcome),
                    StateDetail = outputResult.DeliveryResponse.DeliveryDetail
                }).ToList();

                var updated = await this.UpdateReportAndDetails(details, outputResult.ConnectorIdentifier);

                // Update agent meta if agent exist
                var key = this.GetAgentKey(outputResult.ConnectorIdentifier);
                if (updated > 0 && !string.IsNullOrEmpty(key) && this.agents.TryGetValue(key, out ReportAgent agent))
                {
                    var meta = await this.store.GetAgentMetadataAsync(outputResult.ConnectorIdentifier);

                    if (meta != null)
                    {
                        meta.PendingReceive -= Math.Min(outputResult.Targets.Count, meta.PendingReceive);
                        await this.store.CreateOrUpdateAgentMetadataAsync(meta);

                        if (meta.PendingReceive <= 0)
                        {
                            SmsProviderEventSource.Current.Info(SmsProviderEventSource.EmptyTrackingId, this, nameof(this.OnDispatchCompleteAsync), OperationStates.Succeeded, $"Report agent stopping (complete). connectorName={agent.Credential.ConnectorName} connectorKey={agent.Credential.ConnectorId}");
                            agent.UnSubscribe();
                        }
                    }
                }
            }

            // If succeed with custom message Id, update the summary
            else if (outputResult.Delivered && !string.IsNullOrEmpty(outputResult.DeliveryResponse.CustomMessageId))
            {
                await this.telemetryManager.OnMessageDispatchedAsync(
                    outputResult.EngagementAccount,
                    outputResult.MessageId.ToString(),
                    outputResult.DeliveryResponse.CustomMessageId,
                    outputResult.Targets.ToList(),
                    outputResult.ConnectorIdentifier);
            }

            await this.telemetryManager.InsertMessageBatchRecordAsync(outputResult);
        }