121 lines
4.3 KiB
Go
121 lines
4.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// InboxMemberRepo implements GORM repository for InboxMember.
|
|
// Reference: Chatwoot app/models/inbox_member.rb
|
|
type InboxMemberRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewInboxMemberRepo creates a new InboxMember repository.
|
|
func NewInboxMemberRepo(db *gorm.DB) *InboxMemberRepo {
|
|
return &InboxMemberRepo{db: db}
|
|
}
|
|
|
|
// FindByID retrieves an inbox_member by primary key.
|
|
func (r *InboxMemberRepo) FindByID(ctx context.Context, id uint) (*model.InboxMember, error) {
|
|
var im model.InboxMember
|
|
err := r.withAgentPreloads(r.db.WithContext(ctx)).First(&im, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &im, nil
|
|
}
|
|
|
|
// FindByInboxAndUser retrieves an inbox_member by inbox and user.
|
|
func (r *InboxMemberRepo) FindByInboxAndUser(ctx context.Context, inboxID, userID uint) (*model.InboxMember, error) {
|
|
var im model.InboxMember
|
|
err := r.withAgentPreloads(r.db.WithContext(ctx)).Where("inbox_id = ? AND user_id = ?", inboxID, userID).First(&im).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &im, nil
|
|
}
|
|
|
|
// FindByInbox retrieves all members (agents) assigned to an inbox.
|
|
func (r *InboxMemberRepo) FindByInbox(ctx context.Context, inboxID uint) ([]model.InboxMember, error) {
|
|
var ims []model.InboxMember
|
|
err := r.withAgentPreloads(r.db.WithContext(ctx)).Where("inbox_id = ?", inboxID).
|
|
Order("id ASC").Find(&ims).Error
|
|
return ims, err
|
|
}
|
|
|
|
// FindByUser retrieves all inboxes a user is assigned to.
|
|
func (r *InboxMemberRepo) FindByUser(ctx context.Context, userID uint) ([]model.InboxMember, error) {
|
|
var ims []model.InboxMember
|
|
err := r.db.WithContext(ctx).Preload("Inbox").Where("user_id = ?", userID).
|
|
Order("id ASC").Find(&ims).Error
|
|
return ims, err
|
|
}
|
|
|
|
// FindByAccountID retrieves all inbox members for inboxes belonging to a given account.
|
|
func (r *InboxMemberRepo) FindByAccountID(ctx context.Context, accountID uint) ([]model.InboxMember, error) {
|
|
var ims []model.InboxMember
|
|
err := r.withAgentPreloads(r.db.WithContext(ctx)).
|
|
Joins("JOIN inboxes ON inboxes.id = inbox_members.inbox_id").
|
|
Where("inboxes.account_id = ?", accountID).
|
|
Order("inbox_members.id ASC").Find(&ims).Error
|
|
return ims, err
|
|
}
|
|
|
|
func (r *InboxMemberRepo) withAgentPreloads(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("User").Preload("Inbox")
|
|
}
|
|
|
|
// Create inserts a new inbox_member.
|
|
func (r *InboxMemberRepo) Create(ctx context.Context, im *model.InboxMember) error {
|
|
return r.db.WithContext(ctx).Create(im).Error
|
|
}
|
|
|
|
// Delete removes an inbox_member by ID.
|
|
func (r *InboxMemberRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.InboxMember{}, id).Error
|
|
}
|
|
|
|
// DeleteByInboxAndUser removes the membership by inbox and user.
|
|
func (r *InboxMemberRepo) DeleteByInboxAndUser(ctx context.Context, inboxID, userID uint) error {
|
|
return r.db.WithContext(ctx).Where("inbox_id = ? AND user_id = ?", inboxID, userID).Delete(&model.InboxMember{}).Error
|
|
}
|
|
|
|
// DeleteByInbox removes all members from an inbox.
|
|
func (r *InboxMemberRepo) DeleteByInbox(ctx context.Context, inboxID uint) error {
|
|
return r.db.WithContext(ctx).Where("inbox_id = ?", inboxID).Delete(&model.InboxMember{}).Error
|
|
}
|
|
|
|
// UpdateByInboxAndUser updates role/availability for a specific membership.
|
|
func (r *InboxMemberRepo) UpdateByInboxAndUser(ctx context.Context, inboxID, userID uint, updates map[string]interface{}) error {
|
|
result := r.db.WithContext(ctx).Model(&model.InboxMember{}).
|
|
Where("inbox_id = ? AND user_id = ?", inboxID, userID).
|
|
Updates(updates)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdateAvailabilityByUser updates the availability_status field for all inbox_member
|
|
// records of a given user within a specific account's inboxes.
|
|
func (r *InboxMemberRepo) UpdateAvailabilityByUser(ctx context.Context, accountID uint, userID uint, availability string) error {
|
|
result := r.db.WithContext(ctx).Model(&model.InboxMember{}).
|
|
Joins("JOIN inboxes ON inboxes.id = inbox_members.inbox_id").
|
|
Where("inboxes.account_id = ? AND inbox_members.user_id = ?", accountID, userID).
|
|
Update("availability_status", availability)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
return nil
|
|
}
|