Fix tx template delete query.

This commit is contained in:
Kailash Nadh 2022-07-02 16:55:42 +05:30
parent f26f7c60af
commit 2dcac57cba
3 changed files with 8 additions and 6 deletions

View file

@ -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",

View file

@ -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)))
}

View file

@ -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
RETURNING (SELECT id FROM tpl);
)
SELECT id FROM tpl;
-- media