Skip to content

Commit

Permalink
Fix tx template delete query.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Jul 9, 2022
1 parent f26f7c6 commit 2dcac57
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@
"subscribers.status.unconfirmed": "Unconfirmed",
"subscribers.status.unsubscribed": "Unsubscribed",
"subscribers.subscribersDeleted": "{num} subscriber(s) deleted",
"templates.cantDeleteDefault": "Cannot delete default template",
"templates.cantDeleteDefault": "Cannot delete non-existent or default template",
"templates.default": "Default",
"templates.dummyName": "Dummy campaign",
"templates.dummySubject": "Dummy campaign subject",
Expand Down
4 changes: 2 additions & 2 deletions internal/core/templates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"database/sql"
"net/http"

"github.com/knadh/listmonk/models"
Expand Down Expand Up @@ -74,8 +75,7 @@ func (c *Core) SetDefaultTemplate(id int) error {
// DeleteTemplate deletes a given template.
func (c *Core) DeleteTemplate(id int) error {
var delID int
if err := c.q.DeleteTemplate.Get(&delID, id); err != nil {
// TODO: Fix this. Deletes but always throws a "no result set" error.
if err := c.q.DeleteTemplate.Get(&delID, id); err != nil && err != sql.ErrNoRows {
return echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorDeleting", "name", "{globals.terms.template}", "error", pqErrMsg(err)))
}
Expand Down
8 changes: 5 additions & 3 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,12 @@ WITH tpl AS (
DELETE FROM templates WHERE id = $1 AND (SELECT COUNT(id) FROM templates) > 1 AND is_default = false RETURNING id
),
def AS (
SELECT id FROM templates WHERE is_default = true LIMIT 1
SELECT id FROM templates WHERE is_default = true AND type='campaign' LIMIT 1
),
up AS (
UPDATE campaigns SET template_id = (SELECT id FROM def) WHERE (SELECT id FROM tpl) > 0 AND template_id = $1
)
UPDATE campaigns SET template_id = (SELECT id FROM def) WHERE (SELECT id FROM tpl) > 0 AND template_id = $1
RETURNING (SELECT id FROM tpl);
SELECT id FROM tpl;


-- media
Expand Down

0 comments on commit 2dcac57

Please sign in to comment.