2014-03-25 03:31:33 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2014-06-03 18:27:20 +00:00
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2014-03-25 03:31:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//Campaign is a struct representing a created campaign
|
|
|
|
type Campaign struct {
|
|
|
|
Id int64 `json:"id"`
|
2014-03-26 04:53:51 +00:00
|
|
|
UserId int64 `json:"-"`
|
|
|
|
Name string `json:"name" sql:"not null"`
|
|
|
|
CreatedDate time.Time `json:"created_date"`
|
|
|
|
CompletedDate time.Time `json:"completed_date"`
|
2014-06-03 18:27:20 +00:00
|
|
|
TemplateId int64 `json:"-"`
|
2014-03-28 04:31:51 +00:00
|
|
|
Template Template `json:"template"` //This may change
|
2014-03-25 03:31:33 +00:00
|
|
|
Status string `json:"status"`
|
2014-06-05 04:54:46 +00:00
|
|
|
EmailsSent string `json:"emails_sent"`
|
2014-03-26 04:53:51 +00:00
|
|
|
Results []Result `json:"results,omitempty"`
|
|
|
|
Groups []Group `json:"groups,omitempty"`
|
2014-06-21 17:19:49 +00:00
|
|
|
Events []Event `json:"timeline,omitemtpy"`
|
2014-06-05 04:54:46 +00:00
|
|
|
SMTP SMTP `json:"smtp"`
|
2014-06-03 18:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Campaign) Validate() (string, bool) {
|
|
|
|
switch {
|
|
|
|
case c.Name == "":
|
|
|
|
return "Must specify campaign name", false
|
|
|
|
case len(c.Groups) == 0:
|
|
|
|
return "No groups specified", false
|
|
|
|
case c.Template.Name == "":
|
|
|
|
return "No template specified", false
|
|
|
|
}
|
|
|
|
return "", true
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 02:01:01 +00:00
|
|
|
func (c *Campaign) UpdateStatus(s string) error {
|
|
|
|
// This could be made simpler, but I think there's a bug in gorm
|
|
|
|
return db.Table("campaigns").Where("id=?", c.Id).Update("status", s).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Campaign) AddEvent (e Event) error {
|
|
|
|
e.CampaignId = c.Id
|
|
|
|
e.Time = time.Now()
|
|
|
|
return db.Debug().Save(&e).Error
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
type Result struct {
|
2014-03-26 04:53:51 +00:00
|
|
|
Id int64 `json:"-"`
|
|
|
|
CampaignId int64 `json:"-"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Status string `json:"status" sql:"not null"`
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 02:01:01 +00:00
|
|
|
func (r *Result) UpdateStatus(s string) error {
|
|
|
|
return db.Debug().Table("results").Where("id=?", r.Id).Update("status", s).Error
|
|
|
|
}
|
|
|
|
|
2014-06-21 17:19:49 +00:00
|
|
|
type Event struct {
|
|
|
|
Id int64 `json:"-"`
|
|
|
|
CampaignId int64 `json:"-"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Time time.Time `json:"time"`
|
2014-06-26 02:01:01 +00:00
|
|
|
Message string `json:"message"`
|
2014-06-21 17:19:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// GetCampaigns returns the campaigns owned by the given user.
|
|
|
|
func GetCampaigns(uid int64) ([]Campaign, error) {
|
|
|
|
cs := []Campaign{}
|
2014-03-26 20:01:49 +00:00
|
|
|
err := db.Model(&User{Id: uid}).Related(&cs).Error
|
2014-03-26 04:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2014-03-27 02:42:07 +00:00
|
|
|
for i, _ := range cs {
|
2014-03-26 20:01:49 +00:00
|
|
|
err := db.Model(&cs[i]).Related(&cs[i].Results).Error
|
2014-03-26 04:53:51 +00:00
|
|
|
if err != nil {
|
2014-06-27 00:55:56 +00:00
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = db.Model(&cs[i]).Related(&cs[i].Events).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = db.Table("templates").Where("id=?", cs[i].TemplateId).Find(&cs[i].Template).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
return cs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCampaign returns the campaign, if it exists, specified by the given id and user_id.
|
|
|
|
func GetCampaign(id int64, uid int64) (Campaign, error) {
|
|
|
|
c := Campaign{}
|
2014-03-26 20:01:49 +00:00
|
|
|
err := db.Where("id = ?", id).Where("user_id = ?", uid).Find(&c).Error
|
2014-03-27 02:42:07 +00:00
|
|
|
if err != nil {
|
2014-03-25 03:31:33 +00:00
|
|
|
return c, err
|
|
|
|
}
|
2014-03-26 20:01:49 +00:00
|
|
|
err = db.Model(&c).Related(&c.Results).Error
|
2014-06-27 00:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
err = db.Model(&c).Related(&c.Events).Error
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
err = db.Table("templates").Where("id=?", c.TemplateId).Find(&c.Template).Error
|
2014-03-27 02:42:07 +00:00
|
|
|
return c, err
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostCampaign inserts a campaign and all associated records into the database.
|
|
|
|
func PostCampaign(c *Campaign, uid int64) error {
|
2014-06-21 17:19:49 +00:00
|
|
|
// Fill in the details
|
2014-06-26 02:01:01 +00:00
|
|
|
c.UserId = uid
|
2014-06-21 17:19:49 +00:00
|
|
|
c.CreatedDate = time.Now()
|
|
|
|
c.CompletedDate = time.Time{}
|
|
|
|
c.Status = QUEUED
|
2014-03-25 03:31:33 +00:00
|
|
|
// Check to make sure all the groups already exist
|
|
|
|
for i, g := range c.Groups {
|
|
|
|
c.Groups[i], err = GetGroupByName(g.Name, uid)
|
2014-06-03 18:27:20 +00:00
|
|
|
if err == gorm.RecordNotFound {
|
2014-03-25 03:31:33 +00:00
|
|
|
Logger.Printf("Error - Group %s does not exist", g.Name)
|
|
|
|
return err
|
|
|
|
} else if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-06-03 18:27:20 +00:00
|
|
|
// Check to make sure the template exists
|
|
|
|
t, err := GetTemplateByName(c.Template.Name, uid)
|
|
|
|
if err == gorm.RecordNotFound {
|
|
|
|
Logger.Printf("Error - Template %s does not exist", t.Name)
|
|
|
|
return err
|
|
|
|
} else if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2014-06-05 04:54:46 +00:00
|
|
|
c.Template = t
|
2014-06-03 18:27:20 +00:00
|
|
|
c.TemplateId = t.Id
|
2014-03-25 03:31:33 +00:00
|
|
|
// Insert into the DB
|
2014-03-26 20:01:49 +00:00
|
|
|
err = db.Save(c).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2014-06-26 02:01:01 +00:00
|
|
|
err = c.AddEvent(Event{Message:"Campaign Created"})
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
// Insert all the results
|
|
|
|
for _, g := range c.Groups {
|
|
|
|
// Insert a result for each target in the group
|
|
|
|
for _, t := range g.Targets {
|
2014-03-26 04:53:51 +00:00
|
|
|
r := Result{Email: t.Email, Status: "Unknown", CampaignId: c.Id}
|
2014-03-26 20:01:49 +00:00
|
|
|
err := db.Save(&r).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Printf("Error adding result record for target %s\n", t.Email)
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2014-06-26 02:01:01 +00:00
|
|
|
c.Results = append(c.Results, r)
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-26 20:01:49 +00:00
|
|
|
//DeleteCampaign deletes the specified campaign
|
2014-03-25 03:31:33 +00:00
|
|
|
func DeleteCampaign(id int64) error {
|
2014-03-26 20:01:49 +00:00
|
|
|
// Delete all the campaign results
|
2014-05-27 21:13:30 +00:00
|
|
|
err := db.Where("campaign_id=?", id).Delete(&Result{}).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
2014-03-26 04:53:51 +00:00
|
|
|
Logger.Println(err)
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-03-26 20:01:49 +00:00
|
|
|
// Delete the campaign
|
|
|
|
err = db.Delete(&Campaign{Id: id}).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
2014-03-26 04:53:51 +00:00
|
|
|
Logger.Panicln(err)
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|