public async Task CreateDefaultTemplatesAsync() { var templateEntities = (await _templateWriter.GetAsync())?.ToArray(); var templateIds = Enum.GetValues(typeof(TemplateEnum)).Cast <TemplateEnum>(); foreach (var templateId in templateIds) { var template = templateEntities?.FirstOrDefault(e => e.Template.Id == templateId)?.Template; if (template == null) { template = new SmsTemplate { Id = templateId, DefaultLang = _defaultLang, DefaultBrand = _defaultBrand, BrandLangBodies = GetTemplateLangBodies(templateId), Params = GetTemplateBodyParams(templateId) }; var newTemplateEntity = TemplateMyNoSqlEntity.Create(template); await _templateWriter.InsertAsync(newTemplateEntity); _logger.LogInformation("Template (ID: {templateId}) doesn't exist, creating the new one.", templateId); } } }
public async Task <SendResponse> DeleteBodyAsync(EditTemplateRequest request) { var partitionKey = TemplateMyNoSqlEntity.GeneratePartitionKey(); var rowKey = TemplateMyNoSqlEntity.GenerateRowKey(request.TemplateId.ToString()); var templateEntity = await _templateWriter.GetAsync(partitionKey, rowKey); if (templateEntity == null) { _logger.LogError("Template with part.key {partitionKey} & row key {rowKey} doesn't exist.", partitionKey, rowKey); return(new SendResponse { Result = SmsSendResult.TEMPLATE_NOT_FOUND, ErrorMessage = "Template doesn't exist." }); } templateEntity.Template.BrandLangBodies.First(b => b.Brand == request.Brand).LangBodies .Remove(request.Lang); if (!templateEntity.Template.BrandLangBodies.First(b => b.Brand == request.Brand).LangBodies.Any()) { templateEntity.Template.BrandLangBodies = templateEntity.Template.BrandLangBodies .Where(b => b.Brand != request.Brand).ToArray(); } await _templateWriter.InsertOrReplaceAsync(templateEntity); return(new SendResponse { Result = SmsSendResult.OK }); }
private async Task <string> SendMessageAsync <T>(T request) where T : ISendMessageRequest { var partitionKey = TemplateMyNoSqlEntity.GeneratePartitionKey(); var rowKey = TemplateMyNoSqlEntity.GenerateRowKey(request.Type.ToString()); var templateEntity = await _templateWriter.GetAsync(partitionKey, rowKey); if (templateEntity == null) { _logger.LogError("Template with part.key {partitionKey} & row key {rowKey} doesn't exist.", partitionKey, rowKey); return(null); } var brand = request.Brand; var brandLangBodies = templateEntity.Template.BrandLangBodies.FirstOrDefault(b => b.Brand == brand); if (brandLangBodies == null) { _logger.LogInformation("Template (ID: {templateId}) for brand {brand} doesn't exist, switch to the default brand ({defaultBrand}).", templateEntity.Template.Id, request.Brand, templateEntity.Template.DefaultBrand); brand = templateEntity.Template.DefaultBrand; brandLangBodies = templateEntity.Template.BrandLangBodies.FirstOrDefault(b => b.Brand == brand); if (brandLangBodies == null) { _logger.LogInformation("Template (ID: {templateId}) for default brand ({defaultBrand}) doesn't exist.", templateEntity.Template.Id, request.Lang, brand); return(null); } } if (!brandLangBodies.LangBodies.TryGetValue(request.Lang, out var templateBody)) //set brand, set lang { _logger.LogInformation("Template (ID: {templateId}) for brand {brand} with lang {lang} doesn't exist, switch to the default lang ({defaultLang}).", templateEntity.Template.Id, brand, request.Lang, templateEntity.Template.DefaultLang); if (!brandLangBodies.LangBodies.TryGetValue(templateEntity.Template.DefaultLang, out templateBody)) //set brand, default lang { _logger.LogInformation("Template (ID: {templateId}) for the default lang ({defaultLang}) doesn't exist. Switching to default brand ({defaultBrand}) and default lang ({defaultLang})", templateEntity.Template.Id, templateEntity.Template.DefaultBrand, templateEntity.Template.DefaultLang); var defaultBrandLangBodies = templateEntity.Template.BrandLangBodies.FirstOrDefault(b => b.Brand == templateEntity.Template.DefaultBrand); if (!defaultBrandLangBodies.LangBodies.TryGetValue(request.Lang, out templateBody)) //default brand, set lang { _logger.LogInformation("Template (ID: {templateId}) for defaultBrand {defaultBrand} with lang {lang} doesn't exist, switch to the default lang ({defaultLang}).", templateEntity.Template.Id, templateEntity.Template.DefaultBrand, request.Lang, templateEntity.Template.DefaultLang); if (!defaultBrandLangBodies.LangBodies.TryGetValue(templateEntity.Template.DefaultLang, out templateBody)) //default brand, default lang { _logger.LogError( "Template (ID: {templateId}) for the default brand ({defaultBrand}) and default lang ({defaultLang}) doesn't exist.", templateEntity.Template.Id, templateEntity.Template.DefaultBrand, templateEntity.Template.DefaultLang); return(null); } } } } return(templateBody); }
public async Task <SendResponse> EditTemplateAsync(EditTemplateRequest request) { var partitionKey = TemplateMyNoSqlEntity.GeneratePartitionKey(); var rowKey = TemplateMyNoSqlEntity.GenerateRowKey(request.TemplateId.ToString()); var templateEntity = await _templateWriter.GetAsync(partitionKey, rowKey); if (templateEntity == null) { _logger.LogError("Template with part.key {partitionKey} & row key {rowKey} doesn't exist.", partitionKey, rowKey); return(new SendResponse { Result = SmsSendResult.TEMPLATE_NOT_FOUND, ErrorMessage = "Template doesn't exist." }); } if (string.IsNullOrEmpty(request.TemplateBody)) { var blb = templateEntity.Template.BrandLangBodies.FirstOrDefault(b => b.Brand == request.Brand); if (blb != null) { var lng = request.Lang.ToString(); if (blb.LangBodies.ContainsKey(lng)) { blb.LangBodies.Remove(lng); _logger.LogInformation("Lang body with lang {lang} has been removed from brand ({brand}).", request.Lang, request.Brand); if (!blb.LangBodies.Any()) { templateEntity.Template.BrandLangBodies = templateEntity.Template.BrandLangBodies .Where(b => b.Brand != blb.Brand).ToArray(); _logger.LogInformation("Brand ({brand}) removed because it no longer has lang bodies.", request.Brand); } return(new SendResponse { Result = SmsSendResult.OK }); } } } var lang = request.Lang.ToString(); var brandLangBodies = templateEntity.Template.BrandLangBodies.FirstOrDefault(b => b.Brand == request.Brand); if (brandLangBodies == null) { _logger.LogInformation("Template (ID: {templateId}) for required brand {brand} doesn't exist, creating new one.", templateEntity.Template.Id, request.Brand); templateEntity.Template.BrandLangBodies = templateEntity.Template.BrandLangBodies.Append(new BrandLangBody { Brand = request.Brand, LangBodies = new Dictionary <string, string> { { lang, request.TemplateBody } } }).ToArray(); } else { if (!brandLangBodies.LangBodies.ContainsKey(lang)) { _logger.LogInformation("Template (ID: {templateId}) for brand {brand} with required lang {lang} doesn't exist, creating new one.", templateEntity.Template.Id, request.Brand, request.Lang); brandLangBodies.LangBodies.Add(lang, request.TemplateBody); } brandLangBodies.LangBodies[lang] = request.TemplateBody; } if (!string.IsNullOrEmpty(request.DefaultBrand)) { templateEntity.Template.DefaultBrand = request.DefaultBrand; } templateEntity.Template.DefaultLang = request.DefaultLang; await _templateWriter.InsertOrReplaceAsync(templateEntity); return(new SendResponse { Result = SmsSendResult.OK }); }