|
| 1 | +import { ajv, validateUnauthorizedErrorResponse } from '@rocket.chat/rest-typings'; |
| 2 | + |
1 | 3 | import { getStatistics, getLastStatistics } from '../../../statistics/server'; |
2 | 4 | import telemetryEvent from '../../../statistics/server/lib/telemetryEvents'; |
3 | 5 | import { API } from '../api'; |
4 | 6 | import { getPaginationItems } from '../helpers/getPaginationItems'; |
5 | 7 |
|
6 | | -API.v1.addRoute( |
| 8 | +API.v1.get( |
7 | 9 | 'statistics', |
8 | | - { authRequired: true }, |
9 | 10 | { |
10 | | - async get() { |
11 | | - const { refresh = 'false' } = this.queryParams; |
12 | | - |
13 | | - return API.v1.success( |
14 | | - await getLastStatistics({ |
15 | | - userId: this.userId, |
16 | | - refresh: refresh === 'true', |
17 | | - }), |
18 | | - ); |
| 11 | + authRequired: true, |
| 12 | + response: { |
| 13 | + 200: ajv.compile<Record<string, unknown>>({ |
| 14 | + type: 'object', |
| 15 | + properties: { |
| 16 | + success: { |
| 17 | + type: 'boolean', |
| 18 | + enum: [true], |
| 19 | + }, |
| 20 | + }, |
| 21 | + required: ['success'], |
| 22 | + }), |
| 23 | + 401: validateUnauthorizedErrorResponse, |
19 | 24 | }, |
20 | 25 | }, |
| 26 | + async function action() { |
| 27 | + const { refresh = 'false' } = this.queryParams; |
| 28 | + |
| 29 | + return API.v1.success( |
| 30 | + await getLastStatistics({ |
| 31 | + userId: this.userId, |
| 32 | + refresh: refresh === 'true', |
| 33 | + }), |
| 34 | + ); |
| 35 | + }, |
21 | 36 | ); |
22 | 37 |
|
23 | | -API.v1.addRoute( |
| 38 | +API.v1.get( |
24 | 39 | 'statistics.list', |
25 | | - { authRequired: true }, |
26 | 40 | { |
27 | | - async get() { |
28 | | - const { offset, count } = await getPaginationItems(this.queryParams); |
29 | | - const { sort, fields, query } = await this.parseJsonQuery(); |
30 | | - |
31 | | - return API.v1.success( |
32 | | - await getStatistics({ |
33 | | - userId: this.userId, |
34 | | - query, |
35 | | - pagination: { |
36 | | - offset, |
37 | | - count, |
38 | | - sort, |
39 | | - fields, |
| 41 | + authRequired: true, |
| 42 | + response: { |
| 43 | + 200: ajv.compile<{ |
| 44 | + statistics: unknown[]; |
| 45 | + count: number; |
| 46 | + offset: number; |
| 47 | + total: number; |
| 48 | + }>({ |
| 49 | + type: 'object', |
| 50 | + properties: { |
| 51 | + statistics: { type: 'array' }, |
| 52 | + count: { type: 'number' }, |
| 53 | + offset: { type: 'number' }, |
| 54 | + total: { type: 'number' }, |
| 55 | + success: { |
| 56 | + type: 'boolean', |
| 57 | + enum: [true], |
40 | 58 | }, |
41 | | - }), |
42 | | - ); |
| 59 | + }, |
| 60 | + required: ['statistics', 'count', 'offset', 'total', 'success'], |
| 61 | + }), |
| 62 | + 401: validateUnauthorizedErrorResponse, |
43 | 63 | }, |
44 | 64 | }, |
| 65 | + async function action() { |
| 66 | + const { offset, count } = await getPaginationItems(this.queryParams); |
| 67 | + const { sort, fields, query } = await this.parseJsonQuery(); |
| 68 | + |
| 69 | + return API.v1.success( |
| 70 | + await getStatistics({ |
| 71 | + userId: this.userId, |
| 72 | + query, |
| 73 | + pagination: { |
| 74 | + offset, |
| 75 | + count, |
| 76 | + sort, |
| 77 | + fields, |
| 78 | + }, |
| 79 | + }), |
| 80 | + ); |
| 81 | + }, |
45 | 82 | ); |
46 | 83 |
|
47 | | -API.v1.addRoute( |
| 84 | +API.v1.post( |
48 | 85 | 'statistics.telemetry', |
49 | | - { authRequired: true }, |
50 | 86 | { |
51 | | - post() { |
52 | | - const events = this.bodyParams; |
| 87 | + authRequired: true, |
| 88 | + response: { |
| 89 | + 200: ajv.compile<void>({ |
| 90 | + type: 'object', |
| 91 | + properties: { |
| 92 | + success: { |
| 93 | + type: 'boolean', |
| 94 | + enum: [true], |
| 95 | + }, |
| 96 | + }, |
| 97 | + required: ['success'], |
| 98 | + additionalProperties: false, |
| 99 | + }), |
| 100 | + 401: validateUnauthorizedErrorResponse, |
| 101 | + }, |
| 102 | + }, |
| 103 | + function action() { |
| 104 | + const events = this.bodyParams; |
53 | 105 |
|
54 | | - events?.params?.forEach((event) => { |
55 | | - const { eventName, ...params } = event; |
56 | | - void telemetryEvent.call(eventName, params); |
57 | | - }); |
| 106 | + events?.params?.forEach((event: { eventName: string; [key: string]: unknown }) => { |
| 107 | + const { eventName, ...params } = event; |
| 108 | + void telemetryEvent.call(eventName, params); |
| 109 | + }); |
58 | 110 |
|
59 | | - return API.v1.success(); |
60 | | - }, |
| 111 | + return API.v1.success(); |
61 | 112 | }, |
62 | 113 | ); |
0 commit comments