From 2dcac57cbaa604ac092d5088d8f9d4cf469a8cb1 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sat, 2 Jul 2022 16:55:42 +0530 Subject: [PATCH] Fix tx template delete query. --- i18n/en.json | 2 +- internal/core/templates.go | 4 ++-- queries.sql | 8 +++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/i18n/en.json b/i18n/en.json index 863825b..fe4b261 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -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", diff --git a/internal/core/templates.go b/internal/core/templates.go index 42379ca..307f8d7 100644 --- a/internal/core/templates.go +++ b/internal/core/templates.go @@ -1,6 +1,7 @@ package core import ( + "database/sql" "net/http" "github.com/knadh/listmonk/models" @@ -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))) } diff --git a/queries.sql b/queries.sql index 8fec76a..0015c53 100644 --- a/queries.sql +++ b/queries.sql @@ -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