Skip to content

Commit

Permalink
Fix subscriber attribs update API.
Browse files Browse the repository at this point in the history
Change the behaviour where not passing attribs to the update API
overwrites the attribs with empty values. This commit changes the
behaviour so that in the absence of the attribs field in the
subscriber API, the existing value in the DB is retained.
  • Loading branch information
knadh committed Mar 10, 2021
1 parent f8e555d commit e8ad7a9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
21 changes: 19 additions & 2 deletions cmd/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ type subsWrap struct {
Page int `json:"page"`
}

type subUpdateReq struct {
models.Subscriber
RawAttribs json.RawMessage `json:"attribs"`
Lists pq.Int64Array `json:"lists"`
ListUUIDs pq.StringArray `json:"list_uuids"`
}

// subProfileData represents a subscriber's collated data in JSON
// for export.
type subProfileData struct {
Expand Down Expand Up @@ -293,7 +300,7 @@ func handleUpdateSubscriber(c echo.Context) error {
var (
app = c.Get("app").(*App)
id, _ = strconv.ParseInt(c.Param("id"), 10, 64)
req subimporter.SubReq
req subUpdateReq
)
// Get and validate fields.
if err := c.Bind(&req); err != nil {
Expand All @@ -310,11 +317,21 @@ func handleUpdateSubscriber(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("subscribers.invalidName"))
}

// If there's an attribs value, validate it.
if len(req.RawAttribs) > 0 {
var a models.SubscriberAttribs
if err := json.Unmarshal(req.RawAttribs, &a); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorUpdating",
"name", "{globals.terms.subscriber}", "error", err.Error()))
}
}

_, err := app.queries.UpdateSubscriber.Exec(id,
strings.ToLower(strings.TrimSpace(req.Email)),
strings.TrimSpace(req.Name),
req.Status,
req.Attribs,
req.RawAttribs,
req.Lists)
if err != nil {
app.log.Printf("error updating subscriber: %v", err)
Expand Down
20 changes: 13 additions & 7 deletions frontend/src/views/SubscriberForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ export default Vue.extend({
},
createSubscriber() {
const attribs = this.validateAttribs(this.form.strAttribs);
if (!attribs) {
return;
let attribs = {};
if (this.form.strAttribs) {
attribs = this.validateAttribs(this.form.strAttribs);
if (!attribs) {
return;
}
}
const data = {
Expand All @@ -124,9 +127,12 @@ export default Vue.extend({
},
updateSubscriber() {
const attribs = this.validateAttribs(this.form.strAttribs);
if (!attribs) {
return;
let attribs = {};
if (this.form.strAttribs) {
attribs = this.validateAttribs(this.form.strAttribs);
if (!attribs) {
return;
}
}
const data = {
Expand Down Expand Up @@ -158,7 +164,7 @@ export default Vue.extend({
attribs = JSON.parse(str);
} catch (e) {
this.$buefy.toast.open({
message: `${this.$t('subscribers.invalidJSON')}: e.toString()`,
message: `${this.$t('subscribers.invalidJSON')}: ${e.toString()}`,
type: 'is-danger',
duration: 3000,
queue: false,
Expand Down
2 changes: 1 addition & 1 deletion queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ WITH s AS (
email=(CASE WHEN $2 != '' THEN $2 ELSE email END),
name=(CASE WHEN $3 != '' THEN $3 ELSE name END),
status=(CASE WHEN $4 != '' THEN $4::subscriber_status ELSE status END),
attribs=(CASE WHEN $5::TEXT != '' THEN $5::JSONB ELSE attribs END),
attribs=(CASE WHEN $5 != '' THEN $5::JSONB ELSE attribs END),
updated_at=NOW()
WHERE id = $1 RETURNING id
),
Expand Down

0 comments on commit e8ad7a9

Please sign in to comment.