feat(crm): align shared attachment payloads
This commit is contained in:
@@ -1230,3 +1230,35 @@ func (s *ContactService) GetContactableInboxes(ctx context.Context, accountID, c
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ListAttachments returns a contact's message attachments across account-scoped conversations.
|
||||
// Reference: Chatwoot Api::V1::Accounts::Contacts::AttachmentsController#index.
|
||||
func (s *ContactService) ListAttachments(ctx context.Context, accountID, contactID uint, offset, limit int) ([]model.Attachment, int64, error) {
|
||||
if _, err := s.repo.FindByAccountAndID(ctx, accountID, contactID); err != nil {
|
||||
return nil, 0, errors.New("contact not found")
|
||||
}
|
||||
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
db := s.repo.DB()
|
||||
scope := db.WithContext(ctx).
|
||||
Where("account_id = ? AND message_id IN (SELECT messages.id FROM messages JOIN conversations ON conversations.id = messages.conversation_id WHERE conversations.account_id = ? AND conversations.contact_id = ?)", accountID, accountID, contactID)
|
||||
|
||||
var total int64
|
||||
if err := scope.Model(&model.Attachment{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var attachments []model.Attachment
|
||||
if err := scope.
|
||||
Preload("Message").
|
||||
Order("created_at DESC, id DESC").
|
||||
Offset(offset).
|
||||
Limit(limit).
|
||||
Find(&attachments).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return attachments, total, nil
|
||||
}
|
||||
|
||||
@@ -529,23 +529,22 @@ func (s *MessageService) ListAttachments(ctx context.Context, accountID, convers
|
||||
var total int64
|
||||
|
||||
db := s.repo.DB()
|
||||
err := db.WithContext(ctx).
|
||||
Where("account_id = ? AND message_id IN (SELECT id FROM messages WHERE conversation_id = ?)", accountID, conversationID).
|
||||
Order("id ASC").
|
||||
Find(&attachments).Error
|
||||
if err != nil {
|
||||
scope := db.WithContext(ctx).
|
||||
Where("account_id = ? AND message_id IN (SELECT id FROM messages WHERE conversation_id = ?)", accountID, conversationID)
|
||||
if err := scope.Model(&model.Attachment{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
if err := scope.
|
||||
Preload("Message").
|
||||
Order("created_at DESC, id DESC").
|
||||
Offset(offset).
|
||||
Limit(limit).
|
||||
Find(&attachments).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
total = int64(len(attachments))
|
||||
start := offset
|
||||
if start > len(attachments) {
|
||||
start = len(attachments)
|
||||
}
|
||||
end := start + limit
|
||||
if end > len(attachments) {
|
||||
end = len(attachments)
|
||||
}
|
||||
|
||||
return attachments[start:end], total, nil
|
||||
return attachments, total, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user