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); }
public object InsertDbSms(string url, DbSms data) { using (var client = new JsonServiceClient(url)) { return(client.Send <object>(new InsertDbSmsRequest(data))); } }
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); }
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}"); }
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)); }
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)); } }
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)); }
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)); }
public Task <string> Build(DbSms sms, CancellationToken cancellationToken = default) { return(Task.FromResult(sms.OrderId.ToString())); }
public object InsertDbSms(DbSms data) { return(Dao.InsertDbSms(data)); }
// operation should be done even if cancellation requested public Task HandleAsync(DbSms sms, CancellationToken token = default) => _repository.TakeAndPromote(sms, _promoteState, cancellationToken: CancellationToken.None);
public InsertDbSmsRequest(DbSms dbSms) { Time = dbSms.Time; IsAlarm = dbSms.IsAlarm; AlarmContent = dbSms.AlarmContent; }
public object InsertDbSms(DbSms data) { return(_daoService.InsertDbSms(data)); }
public async Task <string> Build(DbSms sms, CancellationToken cancellationToken = default) { var resource = await _resourceRepository.GetResource(sms.ResourceId, sms.TerminalId, cancellationToken); return(resource.PlaceId.ToString()); }