Skip to content

Commit 2dcac57

Browse files
committed
Fix tx template delete query.
1 parent f26f7c6 commit 2dcac57

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

i18n/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@
502502
"subscribers.status.unconfirmed": "Unconfirmed",
503503
"subscribers.status.unsubscribed": "Unsubscribed",
504504
"subscribers.subscribersDeleted": "{num} subscriber(s) deleted",
505-
"templates.cantDeleteDefault": "Cannot delete default template",
505+
"templates.cantDeleteDefault": "Cannot delete non-existent or default template",
506506
"templates.default": "Default",
507507
"templates.dummyName": "Dummy campaign",
508508
"templates.dummySubject": "Dummy campaign subject",

internal/core/templates.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"database/sql"
45
"net/http"
56

67
"github.com/knadh/listmonk/models"
@@ -74,8 +75,7 @@ func (c *Core) SetDefaultTemplate(id int) error {
7475
// DeleteTemplate deletes a given template.
7576
func (c *Core) DeleteTemplate(id int) error {
7677
var delID int
77-
if err := c.q.DeleteTemplate.Get(&delID, id); err != nil {
78-
// TODO: Fix this. Deletes but always throws a "no result set" error.
78+
if err := c.q.DeleteTemplate.Get(&delID, id); err != nil && err != sql.ErrNoRows {
7979
return echo.NewHTTPError(http.StatusInternalServerError,
8080
c.i18n.Ts("globals.messages.errorDeleting", "name", "{globals.terms.template}", "error", pqErrMsg(err)))
8181
}

queries.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,10 +776,12 @@ WITH tpl AS (
776776
DELETE FROM templates WHERE id = $1 AND (SELECT COUNT(id) FROM templates) > 1 AND is_default = false RETURNING id
777777
),
778778
def AS (
779-
SELECT id FROM templates WHERE is_default = true LIMIT 1
779+
SELECT id FROM templates WHERE is_default = true AND type='campaign' LIMIT 1
780+
),
781+
up AS (
782+
UPDATE campaigns SET template_id = (SELECT id FROM def) WHERE (SELECT id FROM tpl) > 0 AND template_id = $1
780783
)
781-
UPDATE campaigns SET template_id = (SELECT id FROM def) WHERE (SELECT id FROM tpl) > 0 AND template_id = $1
782-
RETURNING (SELECT id FROM tpl);
784+
SELECT id FROM tpl;
783785

784786

785787
-- media

0 commit comments

Comments
 (0)