diff --git a/clients/postgres/clients.go b/clients/postgres/clients.go index 22dd39ac3..b36f3d11f 100644 --- a/clients/postgres/clients.go +++ b/clients/postgres/clients.go @@ -538,9 +538,10 @@ func (repo *clientRepo) retrieveClients(ctx context.Context, domainID, userID st final_clients c ` + connCountJoinQuery := connJoinQuery + if pm.Channel != "" { - connJoinQuery = ` - ,conn.connection_types + connCountJoinQuery = ` FROM final_clients c LEFT JOIN ( @@ -554,6 +555,8 @@ func (repo *clientRepo) retrieveClients(ctx context.Context, domainID, userID st conn.client_id, conn.channel_id ) conn ON c.id = conn.client_id ` + connJoinQuery = ` + ,conn.connection_types` + connCountJoinQuery } dbPage, err := ToDBClientsPage(pm) @@ -566,9 +569,9 @@ func (repo *clientRepo) retrieveClients(ctx context.Context, domainID, userID st if pm.OnlyTotal { cq := fmt.Sprintf(`%s SELECT COUNT(*) AS total_count - FROM final_clients c + %s %s; - `, bq, pageQuery) + `, bq, connCountJoinQuery, pageQuery) total, err := postgres.Total(ctx, repo.DB, cq, dbPage) if err != nil { @@ -644,9 +647,9 @@ func (repo *clientRepo) retrieveClients(ctx context.Context, domainID, userID st if len(items) == 0 { cq := fmt.Sprintf(`%s SELECT COUNT(*) AS total_count - FROM final_clients c + %s %s; - `, bq, pageQuery) + `, bq, connCountJoinQuery, pageQuery) total, err = postgres.Total(ctx, repo.DB, cq, dbPage) if err != nil { diff --git a/clients/postgres/clients_test.go b/clients/postgres/clients_test.go index a1ee1ad40..f941b9ff5 100644 --- a/clients/postgres/clients_test.go +++ b/clients/postgres/clients_test.go @@ -2783,6 +2783,47 @@ func TestRetrieveUserClients(t *testing.T) { }, }, }, + { + desc: "retrieve clients connected to a channel with only total", + domainID: domain.ID, + userID: userID, + pm: clients.Page{ + Offset: 0, + Limit: 10, + Channel: channelID, + Status: clients.AllStatus, + OnlyTotal: true, + }, + response: clients.ClientsPage{ + Page: clients.Page{ + Total: 1, + Offset: 0, + Limit: 10, + }, + Clients: []clients.Client(nil), + }, + }, + { + desc: "retrieve clients connected to a non-existent channel", + domainID: domain.ID, + userID: userID, + pm: clients.Page{ + Offset: 0, + Limit: 10, + Channel: testsutil.GenerateUUID(t), + Status: clients.AllStatus, + Order: defOrder, + Dir: ascDir, + }, + response: clients.ClientsPage{ + Page: clients.Page{ + Total: 0, + Offset: 0, + Limit: 10, + }, + Clients: []clients.Client(nil), + }, + }, } for _, tc := range cases {