diff --git a/controllers/api.go b/controllers/api.go index 57c06b38..6adb8c17 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -220,6 +220,7 @@ func API_Templates_Id(w http.ResponseWriter, r *http.Request) { id, _ := strconv.ParseInt(vars["id"], 0, 64) t, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64)) if checkError(err, w, "Template not found", http.StatusNotFound) { + Logger.Println(err) return } switch { diff --git a/models/campaign.go b/models/campaign.go index 1720e529..2bb3f8e1 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -141,7 +141,7 @@ func PostCampaign(c *Campaign, uid int64) error { for _, g := range c.Groups { // Insert a result for each target in the group for _, t := range g.Targets { - r := Result{Email: t.Email, Status: STATUS_UNKNOWN, CampaignId: c.Id, UserId: c.UserId} + r := Result{Email: t.Email, Status: STATUS_UNKNOWN, CampaignId: c.Id, UserId: c.UserId, FirstName: t.FirstName, LastName: t.LastName} r.GenerateId() err = db.Save(&r).Error if err != nil { diff --git a/models/result.go b/models/result.go index 5c05e38b..456fbc18 100644 --- a/models/result.go +++ b/models/result.go @@ -14,6 +14,8 @@ type Result struct { UserId int64 `json:"-"` RId string `json:"id"` Email string `json:"email"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` Status string `json:"status" sql:"not null"` } diff --git a/models/template.go b/models/template.go index d4e78dac..d406fa39 100644 --- a/models/template.go +++ b/models/template.go @@ -3,6 +3,8 @@ package models import ( "errors" "time" + + "github.com/jinzhu/gorm" ) type Template struct { @@ -39,10 +41,13 @@ func GetTemplates(uid int64) ([]Template, error) { } for i, _ := range ts { err = db.Where("template_id=?", ts[i].Id).Find(&ts[i].Attachments).Error - if err != nil { + if err != nil && err != gorm.RecordNotFound { Logger.Println(err) return ts, err } + if err == gorm.RecordNotFound { + err = nil + } } return ts, err } @@ -56,10 +61,13 @@ func GetTemplate(id int64, uid int64) (Template, error) { return t, err } err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error - if err != nil { + if err != nil && err != gorm.RecordNotFound { Logger.Println(err) return t, err } + if err == gorm.RecordNotFound { + err = nil + } return t, err } @@ -98,11 +106,14 @@ func PostTemplate(t *Template) error { // Per the PUT Method RFC, it presumes all data for a template is provided. func PutTemplate(t *Template) error { // Delete all attachments, and replace with new ones - err := db.Where("template_id=?", t.Id).Delete(&Attachment{}).Error - if err != nil { + err := db.Debug().Where("template_id=?", t.Id).Delete(&Attachment{}).Error + if err != nil && err != gorm.RecordNotFound { Logger.Println(err) return err } + if err == gorm.RecordNotFound { + err = nil + } for i, _ := range t.Attachments { t.Attachments[i].TemplateId = t.Id err := db.Save(&t.Attachments[i]).Error @@ -111,7 +122,7 @@ func PutTemplate(t *Template) error { return err } } - err = db.Where("id=?", t.Id).Save(t).Error + err = db.Debug().Where("id=?", t.Id).Save(t).Error if err != nil { Logger.Println(err) return err diff --git a/worker/worker.go b/worker/worker.go index 9f2856ca..169c3f44 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -49,27 +49,28 @@ func processCampaign(c *models.Campaign) { } for _, t := range c.Results { // Parse the templates - var buff bytes.Buffer + var html_buff bytes.Buffer + var text_buff bytes.Buffer tmpl, err := template.New("html_template").Parse(c.Template.HTML) if err != nil { Logger.Println(err) } - err = tmpl.Execute(&buff, t) + err = tmpl.Execute(&html_buff, t) if err != nil { Logger.Println(err) } - e.HTML = buff.Bytes() - buff.Reset() + e.HTML = html_buff.Bytes() + //buff.Reset() tmpl, err = template.New("text_template").Parse(c.Template.Text) if err != nil { Logger.Println(err) } - err = tmpl.Execute(&buff, t) + err = tmpl.Execute(&text_buff, t) if err != nil { Logger.Println(err) } - e.Text = buff.Bytes() - buff.Reset() + e.Text = text_buff.Bytes() + //buff.Reset() Logger.Println("Creating email using template") e.To = []string{t.Email} err = e.Send(c.SMTP.Host, auth)