Beispiel #1
0
        public async Task <object> Build(DbSms sms, IDictionary <string, string> templateObject, CancellationToken cancellationToken = default)
        {
            // 1. Create empty body
            var body = new JObject();

            foreach (var(key, template) in templateObject)
            {
                // 2. Iterate via each body property
                var value = template;

                do
                {
                    // 3. Replace placeholders with real values
                    foreach (var(templateKey, templateBuilder) in _templateBuilders.Where(tb => value.Contains(tb.Key)))
                    {
                        value = value.Replace(templateKey, await templateBuilder.Build(sms, cancellationToken));
                    }
                } while (_templateBuilders.Any(tb => value.Contains(tb.Key)));

                // 4. Set real value to message
                body[key] = value;
            }

            _logger.Information("Build message {Message}", body.ToString(Formatting.None));

            return(body);
        }
Beispiel #2
0
 public object InsertDbSms(string url, DbSms data)
 {
     using (var client = new JsonServiceClient(url))
     {
         return(client.Send <object>(new InsertDbSmsRequest(data)));
     }
 }
Beispiel #3
0
        public async Task HandleAsync(DbSms sms, CancellationToken token = default)
        {
            _logger.Debug("Try to build message. Sms {@Sms}", sms);
            var message = await _messageBuilder.Build(sms, _routeConfiguration.Body, token);

            _logger.Debug("Try to send message. Message {@Message}", message);
            await _messageHttpService.SendSms(_routeConfiguration, message, token);
        }
Beispiel #4
0
        public Task <string> Build(DbSms sms, CancellationToken cancellationToken = default)
        {
            var messageType = sms.ResourceId == -1
                ? Constants.Resources.Types.Registration
                : Constants.Resources.Types.Invitation;

            if (_resourcesConfiguration.Messages.TryGetValue(messageType, out var resource) &&
                resource.TryGetValue(sms.LanguageId.ToString("D"), out var messageContent))
            {
                return(Task.FromResult(messageContent));
            }

            throw new ArgumentException($"Message not found. Type: {messageType}. Language: {sms.LanguageId}");
        }
Beispiel #5
0
        public Task <string> Build(DbSms sms, CancellationToken cancellationToken = default)
        {
            var phoneNumber = sms.ClientPhone;

            // 1. Remove from number first '+' or '0'
            // +380501234567 -> 380501234567
            // 0501234567 -> 501234567
            var result = phoneNumber.TrimStart('+').TrimStart('0');

            // 2. Prepend '380' at start if number start with another symbols
            // 501234567 -> 380501234567
            if (!result.StartsWith("380"))
            {
                result = $"380{result}";
            }

            return(Task.FromResult(result));
        }
Beispiel #6
0
        public async Task <string> Build(DbSms sms, CancellationToken cancellationToken = default)
        {
            var job = await _jobsRepository.GetJobById(sms.JobId, sms.TerminalId, cancellationToken);

            switch (sms.LanguageId)
            {
            case Language.Default:
                return(job.DescriptionUa);

            case Language.Russian:
                return(job.DescriptionRu);

            case Language.Ukrainian:
                return(job.DescriptionUa);

            case Language.English:
                return(job.DescriptionEn);

            default:
                throw new ArgumentOutOfRangeException($"Unknown language {sms.LanguageId}", nameof(sms.LanguageId));
            }
        }
Beispiel #7
0
        public Task <DbSms[]> TakeAndPromote(DbSms dbSms, string newState, int batchSize = int.MaxValue,
                                             CancellationToken cancellationToken         = default)
        {
            var query = BuildQuery(
                () => new
            {
                dbSms.OrderId,
                dbSms.TerminalId,
                State        = newState,
                CurrentState = dbSms.State,
                dbSms.LastUpdateTime,
                dbSms.SetTime,
                BatchSize = batchSize,
            },
                async(connection, command) =>
            {
                var sms = await connection.QueryAsync <DbSms>(command);
                return(sms.ToArray());
            },
                UpdateQueryBySms);

            return(ExecuteAsync(query, cancellationToken));
        }
Beispiel #8
0
        public async Task HandleAsync(DbSms sms, CancellationToken token = default)
        {
            try
            {
                await _current.HandleAsync(sms, token);

                if (_successor != null)
                {
                    await _successor.HandleAsync(sms, token);
                }
            }
            catch (Exception e)
            {
                if (_fallback == null)
                {
                    _logger.Error(e, "Handler failed with exception. No fallback scenario.");
                    throw;
                }

                _logger.Warning(e, "Handler failed with exception. Fallback scenario start.");
                await _fallback.HandleAsync(sms, token);
            }
        }
 public object InsertDbSms(DbSms data)
 {
     return(ExecuteInsert("InsertDbSms", data));
 }
Beispiel #10
0
 public Task <string> Build(DbSms sms, CancellationToken cancellationToken = default)
 {
     return(Task.FromResult(sms.OrderId.ToString()));
 }
Beispiel #11
0
 public object InsertDbSms(DbSms data)
 {
     return(Dao.InsertDbSms(data));
 }
Beispiel #12
0
 // operation should be done even if cancellation requested
 public Task HandleAsync(DbSms sms, CancellationToken token = default) => _repository.TakeAndPromote(sms, _promoteState, cancellationToken: CancellationToken.None);
Beispiel #13
0
 public InsertDbSmsRequest(DbSms dbSms)
 {
     Time         = dbSms.Time;
     IsAlarm      = dbSms.IsAlarm;
     AlarmContent = dbSms.AlarmContent;
 }
Beispiel #14
0
 public object InsertDbSms(DbSms data)
 {
     return(_daoService.InsertDbSms(data));
 }
Beispiel #15
0
        public async Task <string> Build(DbSms sms, CancellationToken cancellationToken = default)
        {
            var resource = await _resourceRepository.GetResource(sms.ResourceId, sms.TerminalId, cancellationToken);

            return(resource.PlaceId.ToString());
        }