2014-03-25 03:31:33 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2015-02-21 06:11:22 +00:00
|
|
|
"errors"
|
2014-03-25 03:31:33 +00:00
|
|
|
"net/mail"
|
|
|
|
"time"
|
2014-03-26 19:32:48 +00:00
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2014-03-25 03:31:33 +00:00
|
|
|
)
|
|
|
|
|
2015-02-21 06:11:22 +00:00
|
|
|
// Group contains the fields needed for a user -> group mapping
|
|
|
|
// Groups contain 1..* Targets
|
2014-03-25 03:31:33 +00:00
|
|
|
type Group struct {
|
|
|
|
Id int64 `json:"id"`
|
2014-03-27 18:19:57 +00:00
|
|
|
UserId int64 `json:"-"`
|
2014-03-25 03:31:33 +00:00
|
|
|
Name string `json:"name"`
|
2014-03-26 04:53:51 +00:00
|
|
|
ModifiedDate time.Time `json:"modified_date"`
|
|
|
|
Targets []Target `json:"targets" sql:"-"`
|
|
|
|
}
|
|
|
|
|
2017-01-14 23:26:04 +00:00
|
|
|
// GroupSummaries is a struct representing the overview of Groups.
|
|
|
|
type GroupSummaries struct {
|
|
|
|
Total int64 `json:"total"`
|
|
|
|
Groups []GroupSummary `json:"groups"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupSummary represents a summary of the Group model. The only
|
|
|
|
// difference is that, instead of listing the Targets (which could be expensive
|
|
|
|
// for large groups), it lists the target count.
|
|
|
|
type GroupSummary struct {
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ModifiedDate time.Time `json:"modified_date"`
|
|
|
|
NumTargets int64 `json:"num_targets"`
|
|
|
|
}
|
|
|
|
|
2015-02-21 06:11:22 +00:00
|
|
|
// GroupTarget is used for a many-to-many relationship between 1..* Groups and 1..* Targets
|
2014-03-26 04:53:51 +00:00
|
|
|
type GroupTarget struct {
|
|
|
|
GroupId int64 `json:"-"`
|
|
|
|
TargetId int64 `json:"-"`
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 06:11:22 +00:00
|
|
|
// Target contains the fields needed for individual targets specified by the user
|
|
|
|
// Groups contain 1..* Targets, but 1 Target may belong to 1..* Groups
|
2014-03-25 03:31:33 +00:00
|
|
|
type Target struct {
|
2014-06-22 02:06:16 +00:00
|
|
|
Id int64 `json:"-"`
|
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
|
|
|
Email string `json:"email"`
|
2015-02-21 17:36:08 +00:00
|
|
|
Position string `json:"position"`
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 02:03:53 +00:00
|
|
|
// ErrNoEmailSpecified is thrown when no email is specified for the Target
|
|
|
|
var ErrEmailNotSpecified = errors.New("No email address specified")
|
|
|
|
|
2015-02-21 06:11:22 +00:00
|
|
|
// ErrGroupNameNotSpecified is thrown when a group name is not specified
|
|
|
|
var ErrGroupNameNotSpecified = errors.New("Group name not specified")
|
|
|
|
|
|
|
|
// ErrNoTargetsSpecified is thrown when no targets are specified by the user
|
|
|
|
var ErrNoTargetsSpecified = errors.New("No targets specified")
|
|
|
|
|
|
|
|
// Validate performs validation on a group given by the user
|
|
|
|
func (g *Group) Validate() error {
|
|
|
|
switch {
|
|
|
|
case g.Name == "":
|
|
|
|
return ErrGroupNameNotSpecified
|
|
|
|
case len(g.Targets) == 0:
|
|
|
|
return ErrNoTargetsSpecified
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// GetGroups returns the groups owned by the given user.
|
|
|
|
func GetGroups(uid int64) ([]Group, error) {
|
|
|
|
gs := []Group{}
|
2014-03-27 18:19:57 +00:00
|
|
|
err := db.Where("user_id=?", uid).Find(&gs).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return gs, err
|
|
|
|
}
|
|
|
|
for i, _ := range gs {
|
2014-03-26 04:53:51 +00:00
|
|
|
gs[i].Targets, err = GetTargets(gs[i].Id)
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gs, nil
|
|
|
|
}
|
|
|
|
|
2017-01-14 23:26:04 +00:00
|
|
|
// GetGroupSummaries returns the summaries for the groups
|
|
|
|
// created by the given uid.
|
|
|
|
func GetGroupSummaries(uid int64) (GroupSummaries, error) {
|
|
|
|
gs := GroupSummaries{}
|
|
|
|
query := db.Table("groups").Where("user_id=?", uid)
|
|
|
|
err := query.Select("id, name, modified_date").Scan(&gs.Groups).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return gs, err
|
|
|
|
}
|
|
|
|
for i := range gs.Groups {
|
|
|
|
query = db.Table("group_targets").Where("group_id=?", gs.Groups[i].Id)
|
|
|
|
err = query.Count(&gs.Groups[i].NumTargets).Error
|
|
|
|
if err != nil {
|
|
|
|
return gs, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gs.Total = int64(len(gs.Groups))
|
|
|
|
return gs, nil
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// GetGroup returns the group, if it exists, specified by the given id and user_id.
|
|
|
|
func GetGroup(id int64, uid int64) (Group, error) {
|
|
|
|
g := Group{}
|
2014-03-27 18:19:57 +00:00
|
|
|
err := db.Where("user_id=? and id=?", uid, id).Find(&g).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return g, err
|
|
|
|
}
|
2014-03-26 04:53:51 +00:00
|
|
|
g.Targets, err = GetTargets(g.Id)
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
2017-01-14 23:26:04 +00:00
|
|
|
// GetGroupSummary returns the summary for the requested group
|
|
|
|
func GetGroupSummary(id int64, uid int64) (GroupSummary, error) {
|
|
|
|
g := GroupSummary{}
|
|
|
|
query := db.Table("groups").Where("user_id=? and id=?", uid, id)
|
|
|
|
err := query.Select("id, name, modified_date").Scan(&g).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return g, err
|
|
|
|
}
|
|
|
|
query = db.Table("group_targets").Where("group_id=?", id)
|
|
|
|
err = query.Count(&g.NumTargets).Error
|
|
|
|
if err != nil {
|
|
|
|
return g, err
|
|
|
|
}
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// GetGroupByName returns the group, if it exists, specified by the given name and user_id.
|
|
|
|
func GetGroupByName(n string, uid int64) (Group, error) {
|
|
|
|
g := Group{}
|
2014-03-27 18:19:57 +00:00
|
|
|
err := db.Where("user_id=? and name=?", uid, n).Find(&g).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return g, err
|
|
|
|
}
|
2014-03-26 04:53:51 +00:00
|
|
|
g.Targets, err = GetTargets(g.Id)
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-02-07 20:31:41 +00:00
|
|
|
return g, err
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostGroup creates a new group in the database.
|
2014-03-27 18:19:57 +00:00
|
|
|
func PostGroup(g *Group) error {
|
2015-02-21 06:11:22 +00:00
|
|
|
if err := g.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Insert the group into the DB
|
2014-03-26 19:32:48 +00:00
|
|
|
err = db.Save(g).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, t := range g.Targets {
|
|
|
|
insertTargetIntoGroup(t, g.Id)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutGroup updates the given group if found in the database.
|
2014-03-27 18:19:57 +00:00
|
|
|
func PutGroup(g *Group) error {
|
2015-08-05 05:23:05 +00:00
|
|
|
if err := g.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-26 13:31:03 +00:00
|
|
|
// Fetch group's existing targets from database.
|
2014-03-25 03:31:33 +00:00
|
|
|
ts := []Target{}
|
2014-03-26 04:53:51 +00:00
|
|
|
ts, err = GetTargets(g.Id)
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Printf("Error getting targets from group ID: %d", g.Id)
|
|
|
|
return err
|
|
|
|
}
|
2016-06-26 13:31:03 +00:00
|
|
|
// Check existing targets, removing any that are no longer in the group.
|
2014-03-25 03:31:33 +00:00
|
|
|
tExists := false
|
|
|
|
for _, t := range ts {
|
|
|
|
tExists = false
|
|
|
|
// Is the target still in the group?
|
|
|
|
for _, nt := range g.Targets {
|
|
|
|
if t.Email == nt.Email {
|
|
|
|
tExists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the target does not exist in the group any longer, we delete it
|
|
|
|
if !tExists {
|
2014-03-26 20:01:49 +00:00
|
|
|
err = db.Where("group_id=? and target_id=?", g.Id, t.Id).Delete(&GroupTarget{}).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Printf("Error deleting email %s\n", t.Email)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-26 13:31:03 +00:00
|
|
|
// Add any targets that are not in the database yet.
|
2014-03-25 03:31:33 +00:00
|
|
|
for _, nt := range g.Targets {
|
|
|
|
// Check and see if the target already exists in the db
|
|
|
|
tExists = false
|
|
|
|
for _, t := range ts {
|
|
|
|
if t.Email == nt.Email {
|
|
|
|
tExists = true
|
2016-07-02 12:01:24 +00:00
|
|
|
nt.Id = t.Id
|
2014-03-25 03:31:33 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-07-02 12:01:24 +00:00
|
|
|
// Add target if not in database, otherwise update target information.
|
2014-03-25 03:31:33 +00:00
|
|
|
if !tExists {
|
|
|
|
insertTargetIntoGroup(nt, g.Id)
|
2016-07-02 12:01:24 +00:00
|
|
|
} else {
|
|
|
|
UpdateTarget(nt)
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-26 20:01:49 +00:00
|
|
|
err = db.Save(g).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-27 18:19:57 +00:00
|
|
|
// DeleteGroup deletes a given group by group ID and user ID
|
|
|
|
func DeleteGroup(g *Group) error {
|
|
|
|
// Delete all the group_targets entries for this group
|
|
|
|
err := db.Where("group_id=?", g.Id).Delete(&GroupTarget{}).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Delete the group itself
|
|
|
|
err = db.Delete(g).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
func insertTargetIntoGroup(t Target, gid int64) error {
|
|
|
|
if _, err = mail.ParseAddress(t.Email); err != nil {
|
|
|
|
Logger.Printf("Invalid email %s\n", t.Email)
|
|
|
|
return err
|
|
|
|
}
|
2014-03-26 19:32:48 +00:00
|
|
|
trans := db.Begin()
|
2014-03-26 20:01:49 +00:00
|
|
|
trans.Where(t).FirstOrCreate(&t)
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
2014-03-26 19:32:48 +00:00
|
|
|
Logger.Printf("Error adding target: %s\n", t.Email)
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-03-26 20:01:49 +00:00
|
|
|
err = trans.Where("group_id=? and target_id=?", gid, t.Id).Find(&GroupTarget{}).Error
|
2016-03-09 04:37:55 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
2014-03-26 20:01:49 +00:00
|
|
|
err = trans.Save(&GroupTarget{GroupId: gid, TargetId: t.Id}).Error
|
2014-03-26 19:32:48 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
Logger.Printf("Error adding many-many mapping for %s\n", t.Email)
|
|
|
|
return err
|
|
|
|
}
|
2014-03-26 19:32:48 +00:00
|
|
|
err = trans.Commit().Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Printf("Error committing db changes\n")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-02 12:01:24 +00:00
|
|
|
// UpdateTarget updates the given target information in the database.
|
|
|
|
func UpdateTarget(target Target) error {
|
|
|
|
targetInfo := map[string]interface{}{
|
|
|
|
"first_name": target.FirstName,
|
|
|
|
"last_name": target.LastName,
|
|
|
|
"position": target.Position,
|
|
|
|
}
|
|
|
|
err := db.Model(&target).Where("id = ?", target.Id).Updates(targetInfo).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Printf("Error updating target information for %s\n", target.Email)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-21 06:11:22 +00:00
|
|
|
// GetTargets performs a many-to-many select to get all the Targets for a Group
|
2014-03-26 04:53:51 +00:00
|
|
|
func GetTargets(gid int64) ([]Target, error) {
|
|
|
|
ts := []Target{}
|
2015-02-21 17:36:08 +00:00
|
|
|
err := db.Table("targets").Select("targets.id, targets.email, targets.first_name, targets.last_name, targets.position").Joins("left join group_targets gt ON targets.id = gt.target_id").Where("gt.group_id=?", gid).Scan(&ts).Error
|
2014-03-26 19:32:48 +00:00
|
|
|
return ts, err
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|