diff --git a/Surge365.MassEmailReact.Infrastructure/EntityMaps/EntityMapperConfiguration.cs b/Surge365.MassEmailReact.Infrastructure/EntityMaps/EntityMapperConfiguration.cs index b2c5724..8bdc784 100644 --- a/Surge365.MassEmailReact.Infrastructure/EntityMaps/EntityMapperConfiguration.cs +++ b/Surge365.MassEmailReact.Infrastructure/EntityMaps/EntityMapperConfiguration.cs @@ -16,8 +16,8 @@ namespace Surge365.MassEmailReact.Infrastructure.EntityMaps QueryMapper.AddMap(new TemplateMap()); QueryMapper.AddMap(new EmailDomainMap()); QueryMapper .AddMap(new MailingMap()); - QueryMapper.AddMap(new MailingEmailMap()); QueryMapper.AddMap(new MailingTemplateMap()); + QueryMapper.AddMap(new MailingEmailMap()); QueryMapper.AddMap(new MailingTargetMap()); QueryMapper.AddMap(new MailingStatisticMap()); } diff --git a/Surge365.MassEmailReact.Infrastructure/Repositories/MailingRepository.cs b/Surge365.MassEmailReact.Infrastructure/Repositories/MailingRepository.cs index f8e51dd..c423723 100644 --- a/Surge365.MassEmailReact.Infrastructure/Repositories/MailingRepository.cs +++ b/Surge365.MassEmailReact.Infrastructure/Repositories/MailingRepository.cs @@ -31,6 +31,10 @@ namespace Surge365.MassEmailReact.Infrastructure.Repositories { throw new InvalidOperationException("Mailing query mapping is missing. Make sure ConfigureCustomMaps() is called inside program.cs (program startup)."); } + if (!_queryMapper.EntityMaps.ContainsKey(typeof(MailingTemplate))) + { + throw new InvalidOperationException("MailingTemplate query mapping is missing. Make sure ConfigureCustomMaps() is called inside program.cs (program startup)."); + } #endif } @@ -38,7 +42,7 @@ namespace Surge365.MassEmailReact.Infrastructure.Repositories { ArgumentNullException.ThrowIfNull(_config); - var parameters = new List + var parameters = new List { new SqlParameter("@blast_key", id) }; @@ -46,15 +50,16 @@ namespace Surge365.MassEmailReact.Infrastructure.Repositories DataAccess dataAccess = GetDataAccess(); var dataSet = await dataAccess.CallRetrievalProcedureAsync(parameters, "mem_get_blast_by_id"); - // Handle multiple result sets var mailings = await _queryMapper.MapAsync(dataSet); var mailing = mailings.FirstOrDefault(); if (mailing == null) return null; - var templates = await _queryMapper.MapAsync(dataSet); - var template = templates.FirstOrDefault(); - if (mailing != null && template != null) - mailing.Template = template; + if (mailing.TemplateId > 0) + { + var template = await GetTemplateByIdAsync(id); + if (template != null) + mailing.Template = template; + } return mailing; } @@ -104,7 +109,7 @@ namespace Surge365.MassEmailReact.Infrastructure.Repositories var mailingList = mailings.ToList(); if (!mailingList.Any()) return mailingList; - var templates = await _queryMapper.MapAsync(dataSet); + var templates = await _queryMapper.MapAsync(dataSet, 1); var templateList = templates.ToList(); var mailingDictionary = mailingList.ToDictionary(t => t.Id!.Value); diff --git a/Surge365.MassEmailReact.Web/src/components/modals/MailingEdit.tsx b/Surge365.MassEmailReact.Web/src/components/modals/MailingEdit.tsx index 8b53f0e..bdf688c 100644 --- a/Surge365.MassEmailReact.Web/src/components/modals/MailingEdit.tsx +++ b/Surge365.MassEmailReact.Web/src/components/modals/MailingEdit.tsx @@ -321,6 +321,19 @@ const MailingEdit = ({ open, mailing, onClose, onSave }: MailingEditProps) => { if (mailing?.templateId) { const template = setupData.templates.find(t => t.id === mailing.templateId) || null; setCurrentTemplate(template); + + // For existing mailings, use the stored MailingTemplate data, not the base template + if (mailing && mailing.id > 0 && mailing.template) { + // Pre-fill with existing mailing template data + setValue("template.domainId", mailing.template.domainId || 0, { shouldValidate: true }); + setValue("template.subject", mailing.template.subject || "", { shouldValidate: true }); + setValue("template.fromName", mailing.template.fromName || "", { shouldValidate: true }); + } else if (template) { + // For new mailings, use the base template data + setValue("template.domainId", template.domainId, { shouldValidate: true }); + setValue("template.subject", template.subject ?? "", { shouldValidate: true }); + setValue("template.fromName", template.fromName ?? "", { shouldValidate: true }); + } } else { setCurrentTemplate(null); } @@ -514,7 +527,7 @@ const MailingEdit = ({ open, mailing, onClose, onSave }: MailingEditProps) => { field.onChange(newValue ? newValue.id : null); trigger("templateId"); setCurrentTemplate(newValue); - // Update the template object in the form data + // When user manually selects a template, update form with template data if (newValue) { setValue("template.domainId", newValue.domainId, { shouldValidate: true }); setValue("template.subject", newValue.subject ?? "", { shouldValidate: true });