Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

fix(templates): preserve stylesheets in template export/import

Jonas Kaninda9abfb17a9cbc
diff --git a/internal/handlers/data_export.go b/internal/handlers/data_export.go
index e4463fe..03c4f2e 100644
--- a/internal/handlers/data_export.go
+++ b/internal/handlers/data_export.go
@@ -28,7 +28,7 @@ const exportLimit = 10000
type ExportStyleSheet struct {
Name string `json:"name"`
- CSS string `json:"css"`
+ CSS string `json:"css,omitempty"`
}
type ExportLanguage struct {
@@ -147,6 +147,7 @@ func exportTemplates(
Version: v.Version,
SampleData: v.SampleData,
IsActive: isActive,
+ StyleSheet: exportVersionStyleSheet(v.StyleSheet, false),
Localizations: exportLocs,
})
}
@@ -162,6 +163,39 @@ func exportTemplates(
return result, nil
}
+func exportVersionStyleSheet(ss *models.StyleSheet, includeCSS bool) *ExportStyleSheet {
+ if ss == nil {
+ return nil
+ }
+ es := &ExportStyleSheet{Name: ss.Name}
+ if includeCSS {
+ es.CSS = ss.CSS
+ }
+ return es
+}
+
+func findOrCreateStyleSheet(es *ExportStyleSheet, scope repositories.ResourceScope, repo *repositories.StyleSheetRepository) *uint {
+ if es == nil || es.Name == "" {
+ return nil
+ }
+ if existing, err := repo.FindByNameInScope(scope, es.Name); err == nil && existing != nil {
+ return &existing.ID
+ }
+ if es.CSS == "" {
+ return nil
+ }
+ ss := &models.StyleSheet{
+ UserID: scope.UserID,
+ WorkspaceID: scope.WorkspaceID,
+ Name: es.Name,
+ CSS: es.CSS,
+ }
+ if err := repo.Create(ss); err != nil {
+ return nil
+ }
+ return &ss.ID
+}
+
func exportStylesheets(stylesheets []models.StyleSheet) []ExportStyleSheet {
result := make([]ExportStyleSheet, 0, len(stylesheets))
for _, ss := range stylesheets {
@@ -275,7 +309,9 @@ func importTemplates(
templateRepo *repositories.TemplateRepository,
versionRepo *repositories.TemplateVersionRepository,
localizationRepo *repositories.TemplateLocalizationRepository,
+ stylesheetRepo *repositories.StyleSheetRepository,
) int {
+ scope := repositories.ResourceScope{UserID: userID, WorkspaceID: workspaceID}
var count int
for _, tmplData := range data {
if tmplData.Name == "" {
@@ -306,9 +342,10 @@ func importTemplates(
continue
}
v := &models.TemplateVersion{
- TemplateID: tmpl.ID,
- Version: nextVersion,
- SampleData: ev.SampleData,
+ TemplateID: tmpl.ID,
+ Version: nextVersion,
+ SampleData: ev.SampleData,
+ StyleSheetID: findOrCreateStyleSheet(ev.StyleSheet, scope, stylesheetRepo),
}
if err := versionRepo.Create(v); err != nil {
continue
diff --git a/internal/handlers/template_handler.go b/internal/handlers/template_handler.go
index 3dacd29..326c000 100644
--- a/internal/handlers/template_handler.go
+++ b/internal/handlers/template_handler.go
@@ -266,12 +266,13 @@ type ExportVersion struct {
Version int `json:"version"`
SampleData string `json:"sample_data"`
IsActive bool `json:"is_active"`
+ StyleSheet *ExportStyleSheet `json:"stylesheet,omitempty"`
Localizations []ExportLocalization `json:"localizations"`
}
type TemplateExport struct {
- PostaVersion string `json:"posta_version"`
- ExportedAt string `json:"exported_at"`
+ PostaVersion string `json:"posta_version,omitempty"`
+ ExportedAt string `json:"exported_at,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
DefaultLanguage string `json:"default_language"`
@@ -316,6 +317,7 @@ func (h *TemplateHandler) Export(c *okapi.Context, req *ExportTemplateRequest) e
Version: v.Version,
SampleData: v.SampleData,
IsActive: isActive,
+ StyleSheet: exportVersionStyleSheet(v.StyleSheet, true),
Localizations: exportLocs,
})
}
@@ -375,9 +377,10 @@ func (h *TemplateHandler) Import(c *okapi.Context, req *ImportTemplateRequest) e
}
v := &models.TemplateVersion{
- TemplateID: tmpl.ID,
- Version: nextVersion,
- SampleData: ev.SampleData,
+ TemplateID: tmpl.ID,
+ Version: nextVersion,
+ SampleData: ev.SampleData,
+ StyleSheetID: findOrCreateStyleSheet(ev.StyleSheet, scope, h.stylesheetRepo),
}
if err := h.versionRepo.Create(v); err != nil {
return c.AbortInternalServerError(fmt.Sprintf("failed to create version %d", ev.Version))
diff --git a/internal/handlers/workspace_data_handler.go b/internal/handlers/workspace_data_handler.go
index 9c5ce51..857cfbd 100644
--- a/internal/handlers/workspace_data_handler.go
+++ b/internal/handlers/workspace_data_handler.go
@@ -222,7 +222,7 @@ func (h *WorkspaceDataHandler) Import(c *okapi.Context, req *ImportWorkspaceData
var total int
total += importLanguages(data.Languages, userID, &wsID, h.languageRepo)
total += importStylesheets(data.Stylesheets, userID, &wsID, h.stylesheetRepo)
- total += importTemplates(data.Templates, userID, &wsID, h.templateRepo, h.versionRepo, h.localizationRepo)
+ total += importTemplates(data.Templates, userID, &wsID, h.templateRepo, h.versionRepo, h.localizationRepo, h.stylesheetRepo)
total += importContacts(data.Contacts, userID, &wsID, h.db)
total += importContactLists(data.ContactLists, userID, &wsID, h.db)
total += importSuppressions(data.Suppressions, userID, &wsID, h.suppressionRepo)
diff --git a/internal/storage/repositories/stylesheet_repo.go b/internal/storage/repositories/stylesheet_repo.go
index 41c18bd..97a287f 100644
--- a/internal/storage/repositories/stylesheet_repo.go
+++ b/internal/storage/repositories/stylesheet_repo.go
@@ -80,6 +80,14 @@ func (r *StyleSheetRepository) FindByUserID(userID uint, limit, offset int) ([]m
return sheets, total, nil
}
+func (r *StyleSheetRepository) FindByNameInScope(scope ResourceScope, name string) (*models.StyleSheet, error) {
+ var ss models.StyleSheet
+ if err := ApplyScope(r.db, scope).Where("name = ?", name).First(&ss).Error; err != nil {
+ return nil, err
+ }
+ return &ss, nil
+}
+
func (r *StyleSheetRepository) FindByScope(scope ResourceScope, limit, offset int) ([]models.StyleSheet, int64, error) {
var items []models.StyleSheet
var total int64
diff --git a/web/src/api/types.ts b/web/src/api/types.ts
index d1a65c6..5b81ba0 100644
--- a/web/src/api/types.ts
+++ b/web/src/api/types.ts
@@ -172,6 +172,7 @@ export interface TemplateExportVersion {
version: number
sample_data: string
is_active: boolean
+ stylesheet?: ExportStyleSheet | null
localizations: TemplateExportLocalization[]
}