Reintroduce MailingEmailMap and enhance template handling
- Added `MailingEmailMap` back to `EntityMapperConfiguration.cs`. - Improved error handling in `MailingRepository.cs` by adding a check for `MailingTemplate` mapping. - Updated template retrieval logic to fetch by ID when `TemplateId` is greater than 0. - Modified `GetAllAsync` to include an additional parameter for mapping `MailingTemplate`. - Enhanced `MailingEdit.tsx` to pre-fill form fields with existing template data for existing mailings. - Updated template selection logic to ensure form data reflects the current state of the selected template.
This commit is contained in:
parent
5a6c57bade
commit
651b33171b
@ -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());
|
||||
}
|
||||
|
||||
@ -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<SqlParameter>
|
||||
var parameters = new List<SqlParameter>
|
||||
{
|
||||
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<Mailing>(dataSet);
|
||||
var mailing = mailings.FirstOrDefault();
|
||||
if (mailing == null) return null;
|
||||
|
||||
var templates = await _queryMapper.MapAsync<MailingTemplate>(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<MailingTemplate>(dataSet);
|
||||
var templates = await _queryMapper.MapAsync<MailingTemplate>(dataSet, 1);
|
||||
var templateList = templates.ToList();
|
||||
|
||||
var mailingDictionary = mailingList.ToDictionary(t => t.Id!.Value);
|
||||
|
||||
@ -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 });
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user