Align GoChat with Chatwoot frontend contracts
This commit is contained in:
@@ -548,6 +548,67 @@ func TestSlaPolicyHandler_ListInboxes_Success(t *testing.T) {
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func TestSlaPolicyHandler_InboxAssociationChatwootPayloadAndSideEffects(t *testing.T) {
|
||||
handler, db := setupSlaPolicyHandlerTest(t)
|
||||
router := setupSlaPolicyTestRouter(handler)
|
||||
aid := slaHandlerAccountID(db)
|
||||
accountUID := slaHandlerAccountIDUint(db)
|
||||
|
||||
svc := service.NewSlaPolicyService(
|
||||
repository.NewSlaPolicyRepo(db),
|
||||
repository.NewAppliedSlaRepo(db),
|
||||
repository.NewSlaEventRepo(db),
|
||||
repository.NewSlaPolicyInboxRepo(db),
|
||||
)
|
||||
policy, err := svc.Create(nil, accountUID, &service.CreateSlaPolicyRequest{
|
||||
Name: "Inbox Linked SLA", FirstResponseTimeThreshold: 10, NextResponseTimeThreshold: 20, ResolutionTimeThreshold: 100,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
inbox := &model.Inbox{Name: "Priority Inbox", AccountID: accountUID, ChannelType: "web_widget"}
|
||||
require.NoError(t, db.Create(inbox).Error)
|
||||
|
||||
addBody, _ := json.Marshal(map[string]any{"inbox_id": inbox.ID})
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodPost, "/api/v1/accounts/"+aid+"/sla_policies/"+strconv.FormatUint(uint64(policy.ID), 10)+"/inboxes", bytes.NewBuffer(addBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusCreated, w.Code, w.Body.String())
|
||||
|
||||
var addResp map[string]any
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &addResp))
|
||||
payload := addResp["data"].(map[string]any)
|
||||
assert.Equal(t, float64(policy.ID), payload["sla_policy_id"])
|
||||
assert.Equal(t, float64(inbox.ID), payload["inbox_id"])
|
||||
assert.Equal(t, float64(accountUID), payload["account_id"])
|
||||
assert.Equal(t, true, addResp["success"])
|
||||
|
||||
var associationCount int64
|
||||
require.NoError(t, db.Model(&model.SlaPolicyInbox{}).Where("sla_policy_id = ? AND inbox_id = ? AND account_id = ?", policy.ID, inbox.ID, accountUID).Count(&associationCount).Error)
|
||||
assert.Equal(t, int64(1), associationCount)
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest(http.MethodGet, "/api/v1/accounts/"+aid+"/sla_policies/"+strconv.FormatUint(uint64(policy.ID), 10)+"/inboxes", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
|
||||
var listResp map[string]any
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &listResp))
|
||||
listPayload := listResp["data"].([]any)
|
||||
require.Len(t, listPayload, 1)
|
||||
listed := listPayload[0].(map[string]any)
|
||||
assert.Equal(t, float64(policy.ID), listed["sla_policy_id"])
|
||||
assert.Equal(t, float64(inbox.ID), listed["inbox_id"])
|
||||
assert.Equal(t, float64(accountUID), listed["account_id"])
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest(http.MethodDelete, "/api/v1/accounts/"+aid+"/sla_policies/"+strconv.FormatUint(uint64(policy.ID), 10)+"/inboxes/"+strconv.FormatUint(uint64(inbox.ID), 10), nil)
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusNoContent, w.Code, w.Body.String())
|
||||
|
||||
require.NoError(t, db.Model(&model.SlaPolicyInbox{}).Where("sla_policy_id = ? AND inbox_id = ? AND account_id = ?", policy.ID, inbox.ID, accountUID).Count(&associationCount).Error)
|
||||
assert.Equal(t, int64(0), associationCount)
|
||||
}
|
||||
|
||||
// ========== Applied SLA reports ==========
|
||||
|
||||
func TestSlaPolicyHandler_ListAppliedSlas_ChatwootPayloadAndFilters(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user