Browse Source

Bug fixes on PUT /api/template

Hotfix for buffer issue in worker.go.. going to find out the root cause and fix it properly
pull/24/head
Jordan 7 years ago
parent
commit
33b9ec2196
5 changed files with 28 additions and 13 deletions
  1. +1
    -0
      controllers/api.go
  2. +1
    -1
      models/campaign.go
  3. +2
    -0
      models/result.go
  4. +16
    -5
      models/template.go
  5. +8
    -7
      worker/worker.go

+ 1
- 0
controllers/api.go View File

@ -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 {

+ 1
- 1
models/campaign.go View File

@ -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 {

+ 2
- 0
models/result.go View File

@ -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"`
}

+ 16
- 5
models/template.go View File

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

+ 8
- 7
worker/worker.go View File

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

Loading…
Cancel
Save