feat(assignment-policies): align chatwoot payloads
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/gochat/gochat/internal/autoassignment"
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
)
|
||||
|
||||
// AssignmentPolicyRepo implements GORM repository for AssignmentPolicy.
|
||||
@@ -20,13 +20,13 @@ func NewAssignmentPolicyRepo(db *gorm.DB) *AssignmentPolicyRepo {
|
||||
}
|
||||
|
||||
// Create creates a new assignment policy.
|
||||
func (r *AssignmentPolicyRepo) Create(ctx context.Context, policy *autoassignment.AssignmentPolicy) error {
|
||||
func (r *AssignmentPolicyRepo) Create(ctx context.Context, policy *model.AssignmentPolicy) error {
|
||||
return r.db.WithContext(ctx).Create(policy).Error
|
||||
}
|
||||
|
||||
// FindByID retrieves an assignment policy by primary key.
|
||||
func (r *AssignmentPolicyRepo) FindByID(ctx context.Context, id uint) (*autoassignment.AssignmentPolicy, error) {
|
||||
var policy autoassignment.AssignmentPolicy
|
||||
func (r *AssignmentPolicyRepo) FindByID(ctx context.Context, id uint) (*model.AssignmentPolicy, error) {
|
||||
var policy model.AssignmentPolicy
|
||||
if err := r.db.WithContext(ctx).First(&policy, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -34,15 +34,28 @@ func (r *AssignmentPolicyRepo) FindByID(ctx context.Context, id uint) (*autoassi
|
||||
}
|
||||
|
||||
// GetByID retrieves an assignment policy by primary key (alias for FindByID matching service expectations).
|
||||
func (r *AssignmentPolicyRepo) GetByID(ctx context.Context, id uint) (*autoassignment.AssignmentPolicy, error) {
|
||||
func (r *AssignmentPolicyRepo) GetByID(ctx context.Context, id uint) (*model.AssignmentPolicy, error) {
|
||||
return r.FindByID(ctx, id)
|
||||
}
|
||||
|
||||
// FindByAccount retrieves the active assignment policy for an account.
|
||||
func (r *AssignmentPolicyRepo) FindByAccount(ctx context.Context, accountID uint) (*autoassignment.AssignmentPolicy, error) {
|
||||
var policy autoassignment.AssignmentPolicy
|
||||
// FindByAccount retrieves all assignment policies for an account.
|
||||
func (r *AssignmentPolicyRepo) FindByAccount(ctx context.Context, accountID uint) ([]model.AssignmentPolicy, error) {
|
||||
var policies []model.AssignmentPolicy
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("account_id = ? AND active = ?", accountID, true).
|
||||
Where("account_id = ?", accountID).
|
||||
Order("id ASC").
|
||||
Find(&policies).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return policies, nil
|
||||
|
||||
}
|
||||
|
||||
// FindByAccountAndID retrieves an account-scoped assignment policy.
|
||||
func (r *AssignmentPolicyRepo) FindByAccountAndID(ctx context.Context, accountID, id uint) (*model.AssignmentPolicy, error) {
|
||||
var policy model.AssignmentPolicy
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("account_id = ? AND id = ?", accountID, id).
|
||||
First(&policy).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -50,13 +63,24 @@ func (r *AssignmentPolicyRepo) FindByAccount(ctx context.Context, accountID uint
|
||||
}
|
||||
|
||||
// Update updates an assignment policy.
|
||||
func (r *AssignmentPolicyRepo) Update(ctx context.Context, policy *autoassignment.AssignmentPolicy) error {
|
||||
func (r *AssignmentPolicyRepo) Update(ctx context.Context, policy *model.AssignmentPolicy) error {
|
||||
return r.db.WithContext(ctx).Save(policy).Error
|
||||
}
|
||||
|
||||
// Delete soft-deletes an assignment policy.
|
||||
func (r *AssignmentPolicyRepo) Delete(ctx context.Context, id uint) error {
|
||||
return r.db.WithContext(ctx).Delete(&autoassignment.AssignmentPolicy{}, id).Error
|
||||
return r.db.WithContext(ctx).Delete(&model.AssignmentPolicy{}, id).Error
|
||||
|
||||
}
|
||||
|
||||
// CountAssignedInboxes returns how many inboxes use an assignment policy.
|
||||
func (r *AssignmentPolicyRepo) CountAssignedInboxes(ctx context.Context, policyID uint) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).
|
||||
Model(&model.InboxAssignmentPolicy{}).
|
||||
Where("assignment_policy_id = ?", policyID).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// InboxAssignmentPolicyRepo implements GORM repository for InboxAssignmentPolicy.
|
||||
@@ -71,13 +95,13 @@ func NewInboxAssignmentPolicyRepo(db *gorm.DB) *InboxAssignmentPolicyRepo {
|
||||
}
|
||||
|
||||
// Create creates a new inbox assignment policy.
|
||||
func (r *InboxAssignmentPolicyRepo) Create(ctx context.Context, policy *autoassignment.InboxAssignmentPolicy) error {
|
||||
func (r *InboxAssignmentPolicyRepo) Create(ctx context.Context, policy *model.InboxAssignmentPolicy) error {
|
||||
return r.db.WithContext(ctx).Create(policy).Error
|
||||
}
|
||||
|
||||
// FindByID retrieves an inbox assignment policy by primary key.
|
||||
func (r *InboxAssignmentPolicyRepo) FindByID(ctx context.Context, id uint) (*autoassignment.InboxAssignmentPolicy, error) {
|
||||
var policy autoassignment.InboxAssignmentPolicy
|
||||
func (r *InboxAssignmentPolicyRepo) FindByID(ctx context.Context, id uint) (*model.InboxAssignmentPolicy, error) {
|
||||
var policy model.InboxAssignmentPolicy
|
||||
if err := r.db.WithContext(ctx).First(&policy, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -85,22 +109,70 @@ func (r *InboxAssignmentPolicyRepo) FindByID(ctx context.Context, id uint) (*aut
|
||||
}
|
||||
|
||||
// FindByInbox retrieves the active assignment policy override for an inbox.
|
||||
func (r *InboxAssignmentPolicyRepo) FindByInbox(ctx context.Context, accountID, inboxID uint) (*autoassignment.InboxAssignmentPolicy, error) {
|
||||
var policy autoassignment.InboxAssignmentPolicy
|
||||
func (r *InboxAssignmentPolicyRepo) FindByInbox(ctx context.Context, accountID, inboxID uint) (*model.InboxAssignmentPolicy, error) {
|
||||
var policy model.InboxAssignmentPolicy
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("account_id = ? AND inbox_id = ? AND active = ?", accountID, inboxID, true).
|
||||
Joins("JOIN assignment_policies ON assignment_policies.id = inbox_assignment_policies.assignment_policy_id").
|
||||
Where("assignment_policies.account_id = ? AND inbox_assignment_policies.inbox_id = ?", accountID, inboxID).
|
||||
First(&policy).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &policy, nil
|
||||
}
|
||||
|
||||
// FindPolicyByInbox retrieves the assignment policy associated with an inbox.
|
||||
func (r *InboxAssignmentPolicyRepo) FindPolicyByInbox(ctx context.Context, accountID, inboxID uint) (*model.AssignmentPolicy, error) {
|
||||
var policy model.AssignmentPolicy
|
||||
if err := r.db.WithContext(ctx).
|
||||
Joins("JOIN inbox_assignment_policies ON inbox_assignment_policies.assignment_policy_id = assignment_policies.id").
|
||||
Where("assignment_policies.account_id = ? AND inbox_assignment_policies.inbox_id = ?", accountID, inboxID).
|
||||
First(&policy).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &policy, nil
|
||||
}
|
||||
|
||||
// FindInboxesByPolicy retrieves inboxes linked to an account-scoped policy.
|
||||
func (r *InboxAssignmentPolicyRepo) FindInboxesByPolicy(ctx context.Context, accountID, policyID uint) ([]model.Inbox, error) {
|
||||
var inboxes []model.Inbox
|
||||
err := r.db.WithContext(ctx).
|
||||
Joins("JOIN inbox_assignment_policies ON inbox_assignment_policies.inbox_id = inboxes.id").
|
||||
Joins("JOIN assignment_policies ON assignment_policies.id = inbox_assignment_policies.assignment_policy_id").
|
||||
Where("assignment_policies.account_id = ? AND assignment_policies.id = ?", accountID, policyID).
|
||||
Order("inboxes.id ASC").
|
||||
Find(&inboxes).Error
|
||||
return inboxes, err
|
||||
}
|
||||
|
||||
// ReplaceForInbox attaches one policy to an inbox, replacing an old association.
|
||||
func (r *InboxAssignmentPolicyRepo) ReplaceForInbox(ctx context.Context, inboxID, policyID uint) (*model.InboxAssignmentPolicy, error) {
|
||||
var policy model.InboxAssignmentPolicy
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("inbox_id = ?", inboxID).Delete(&model.InboxAssignmentPolicy{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
policy = model.InboxAssignmentPolicy{InboxID: inboxID, AssignmentPolicyID: policyID}
|
||||
return tx.Create(&policy).Error
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &policy, nil
|
||||
}
|
||||
|
||||
// DeleteByInbox removes an inbox assignment policy association.
|
||||
func (r *InboxAssignmentPolicyRepo) DeleteByInbox(ctx context.Context, inboxID uint) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Where("inbox_id = ?", inboxID).
|
||||
Delete(&model.InboxAssignmentPolicy{}).Error
|
||||
}
|
||||
|
||||
// Update updates an inbox assignment policy.
|
||||
func (r *InboxAssignmentPolicyRepo) Update(ctx context.Context, policy *autoassignment.InboxAssignmentPolicy) error {
|
||||
func (r *InboxAssignmentPolicyRepo) Update(ctx context.Context, policy *model.InboxAssignmentPolicy) error {
|
||||
return r.db.WithContext(ctx).Save(policy).Error
|
||||
}
|
||||
|
||||
// Delete soft-deletes an inbox assignment policy.
|
||||
func (r *InboxAssignmentPolicyRepo) Delete(ctx context.Context, id uint) error {
|
||||
return r.db.WithContext(ctx).Delete(&autoassignment.InboxAssignmentPolicy{}, id).Error
|
||||
return r.db.WithContext(ctx).Delete(&model.InboxAssignmentPolicy{}, id).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user