Skip to content

Commit ea41969

Browse files
committed
Improve distinct values retrieval
1 parent 322888b commit ea41969

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

apps/meteor/ee/server/apps/communication/endpoints/appLogsDistinctInstanceHandler.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv';
1+
import { ajv } from '@rocket.chat/rest-typings';
22

33
import type { AppsRestApi } from '../rest';
44

55
// This might be a good candidate for a default validator function exported by @rocket.chat/rest-typings
6-
const errorResponse = ajv.compile({
6+
const errorResponse = ajv.compile<unknown>({
77
additionalProperties: false,
88
type: 'object',
99
properties: {
@@ -28,7 +28,7 @@ const errorResponse = ajv.compile({
2828

2929
export const registerAppLogsDistinctInstanceHandler = ({ api, _orch }: AppsRestApi) =>
3030
void api.get(
31-
':id/logs/instanceIds',
31+
':id/logs/distinctValues',
3232
{
3333
authRequired: true,
3434
permissionsRequired: ['manage-apps'],
@@ -45,9 +45,16 @@ export const registerAppLogsDistinctInstanceHandler = ({ api, _orch }: AppsRestA
4545
}),
4646
401: errorResponse,
4747
403: errorResponse,
48+
404: errorResponse,
4849
},
4950
},
5051
async function action() {
52+
const app = await _orch.getManager()?.getOneById(this.urlParams.id);
53+
54+
if (!app) {
55+
return api.notFound(`No app found with id: ${this.urlParams.id}`);
56+
}
57+
5158
const result = await _orch.getLogStorage().distinctValues(this.urlParams.id);
5259

5360
return api.success(result);

apps/meteor/tests/end-to-end/apps/app-logs-distinct-instances.ts renamed to apps/meteor/tests/end-to-end/apps/app-logs-distinct-values.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,46 @@ import { installTestApp, cleanupApps } from '../../data/apps/helper';
77
import { updatePermission } from '../../data/permissions.helper';
88
import { IS_EE } from '../../e2e/config/constants';
99

10-
(IS_EE ? describe : describe.skip)('Apps - Logs Distinct Instances', () => {
10+
(IS_EE ? describe : describe.skip)('Apps - Logs Distinct Values', () => {
11+
let appId: string;
12+
1113
before((done) => getCredentials(done));
1214

1315
before(async () => {
1416
await cleanupApps();
15-
await installTestApp();
17+
const app = await installTestApp();
18+
appId = app.id;
1619
});
1720

1821
after(() => Promise.all([cleanupApps(), updatePermission('manage-apps', ['admin'])]));
1922

20-
it('should return distinct instance IDs successfully', (done) => {
23+
it('should return distinct values successfully', (done) => {
2124
void request
22-
.get(apps('/logs/instanceIds'))
25+
.get(apps(`/${appId}/logs/distinctValues`))
2326
.set(credentials)
2427
.expect(200)
2528
.expect('Content-Type', 'application/json')
2629
.expect((res) => {
2730
expect(res.body).to.have.a.property('success', true);
2831
expect(res.body).to.have.a.property('instanceIds').that.is.an('array');
32+
expect(res.body).to.have.a.property('methods').that.is.an('array');
2933

30-
const { instanceIds } = res.body;
31-
const uniqueInstanceIds = [...new Set(instanceIds)];
34+
const { instanceIds, methods } = res.body;
35+
const uniqueInstanceIds = new Set(instanceIds);
36+
const uniqueMethods = new Set(methods);
3237

3338
// All instance IDs should be unique
34-
expect(instanceIds).to.have.lengthOf(uniqueInstanceIds.length);
39+
expect(instanceIds).to.have.lengthOf(uniqueInstanceIds.size);
40+
41+
// All methods should be unique
42+
expect(methods).to.have.lengthOf(uniqueMethods.size);
3543
})
3644
.end(done);
3745
});
3846

3947
it('should require authentication', (done) => {
4048
void request
41-
.get(apps('/logs/instanceIds'))
49+
.get(apps(`/${appId}/logs/distinctValues`))
4250
.expect(401)
4351
.expect('Content-Type', 'application/json')
4452
.expect((res) => {
@@ -52,7 +60,7 @@ import { IS_EE } from '../../e2e/config/constants';
5260
void updatePermission('manage-apps', []).then(
5361
() =>
5462
void request
55-
.get(apps('/logs/instanceIds'))
63+
.get(apps(`/${appId}/logs/distinctValues`))
5664
.set(credentials)
5765
.expect(403)
5866
.expect('Content-Type', 'application/json')
@@ -64,19 +72,36 @@ import { IS_EE } from '../../e2e/config/constants';
6472
);
6573
});
6674

67-
it('should return empty array when no instances exist', (done) => {
68-
// Clean up all apps first to ensure no instances
69-
void cleanupApps().then(() => {
75+
it('should return 404 for non-existent app', (done) => {
76+
void request
77+
.get(apps('/non-existent-app-id/logs/distinctValues'))
78+
.set(credentials)
79+
.expect(404)
80+
.expect('Content-Type', 'application/json')
81+
.expect((res) => {
82+
expect(res.body).to.have.a.property('success', false);
83+
expect(res.body).to.have.a.property('error');
84+
})
85+
.end(done);
86+
});
87+
88+
it('should return empty arrays when no logs exist', (done) => {
89+
// Clean up all apps first to ensure no logs
90+
void cleanupApps().then(async () => {
91+
// Install a fresh app
92+
const app = await installTestApp();
7093
void request
71-
.get(apps('/logs/instanceIds'))
94+
.get(apps(`/${app.id}/logs/distinctValues`))
7295
.set(credentials)
7396
.expect(200)
7497
.expect('Content-Type', 'application/json')
7598
.expect((res) => {
7699
expect(res.body).to.have.a.property('success', true);
77100
expect(res.body).to.have.a.property('instanceIds').that.is.an('array');
78-
// Could be empty or have instances from other tests
101+
expect(res.body).to.have.a.property('methods').that.is.an('array');
102+
// Could be empty or have values from other tests
79103
expect(res.body.instanceIds).to.satisfy((arr: any[]) => Array.isArray(arr));
104+
expect(res.body.methods).to.satisfy((arr: any[]) => Array.isArray(arr));
80105
})
81106
.end(done);
82107
});

0 commit comments

Comments
 (0)