Persist Campaign Group assignments on campaign creation

pull/3047/head
Andrew Walter 2023-12-10 10:21:15 +11:00
parent 63f0549efb
commit a28dcc5df8
3 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,10 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS `campaign_groups` (campaign_id bigint,group_id bigint );
CREATE UNIQUE INDEX IF NOT EXISTS uniqueCampaignIdGroupId ON campaign_groups (campaign_id, group_id);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE `campaign_groups`;
-- +goose StatementEnd

View File

@ -0,0 +1,10 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS `campaign_groups` (campaign_id bigint,group_id bigint );
CREATE UNIQUE INDEX IF NOT EXISTS uniqueCampaignIdGroupId ON campaign_groups (campaign_id, group_id);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE `campaign_groups`;
-- +goose StatementEnd

View File

@ -71,6 +71,12 @@ type CampaignStats struct {
Error int64 `json:"error"` Error int64 `json:"error"`
} }
// CampaignGroup is used for a many-to-many relationship between 1..* Campaigns and 1..* Groups
type CampaignGroup struct {
CampaignId int64 `json:"-"`
GroupId int64 `json:"-"`
}
// Event contains the fields for an event // Event contains the fields for an event
// that occurs during the campaign // that occurs during the campaign
type Event struct { type Event struct {
@ -527,15 +533,34 @@ func PostCampaign(c *Campaign, uid int64) error {
c.SMTP = s c.SMTP = s
c.SMTPId = s.Id c.SMTPId = s.Id
// Insert into the DB // Insert into the DB
err = db.Save(c).Error tx := db.Begin()
err = tx.Save(c).Error
if err != nil {
log.Error(err)
tx.Rollback()
return err
}
// Add the associated groups to the Campaign's group assignments
for _, g := range c.Groups {
err = InsertGroupIntoCampaign(tx, g.Id, c)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return err return err
} }
}
err = tx.Commit().Error
if err != nil {
log.Error(err)
return err
}
err = AddEvent(&Event{Message: "Campaign Created"}, c.Id) err = AddEvent(&Event{Message: "Campaign Created"}, c.Id)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }
// Import the users // Import the users
return UpdateUsers(c, totalRecipients) return UpdateUsers(c, totalRecipients)
} }
@ -674,3 +699,18 @@ func UpdateUsers(c *Campaign, totalRecipients int) error {
return tx.Commit().Error return tx.Commit().Error
} }
// InsertGroupIntoCampaign inserts the specified group into the Campaign's group assignments.
func InsertGroupIntoCampaign(tx *gorm.DB, gid int64, c *Campaign) error {
// ToDo: Handle duplicate entries
err := tx.Save(&CampaignGroup{CampaignId: c.Id, GroupId: gid}).Error
if err != nil {
log.Error(err)
tx.Rollback()
return err
}
return nil
}