|
1 | | -import { isAppLogsProps } from '@rocket.chat/rest-typings'; |
| 1 | +import { isAppLogsProps, ajv } from '@rocket.chat/rest-typings'; |
2 | 2 |
|
3 | 3 | import { getPaginationItems } from '../../../../../app/api/server/helpers/getPaginationItems'; |
4 | 4 | import type { AppsRestApi } from '../rest'; |
5 | 5 | import { makeAppLogsQuery } from './lib/makeAppLogsQuery'; |
6 | 6 |
|
| 7 | +const errorResponse = ajv.compile<{ |
| 8 | + success: false; |
| 9 | + error: string; |
| 10 | +}>({ |
| 11 | + additionalProperties: false, |
| 12 | + type: 'object', |
| 13 | + properties: { |
| 14 | + error: { type: 'string' }, |
| 15 | + status: { type: 'string', nullable: true }, |
| 16 | + message: { type: 'string', nullable: true }, |
| 17 | + success: { type: 'boolean', description: 'Indicates if the request was successful.' }, |
| 18 | + }, |
| 19 | + required: ['success', 'error'], |
| 20 | +}); |
| 21 | + |
7 | 22 | export const registerAppLogsHandler = ({ api, _manager, _orch }: AppsRestApi) => |
8 | | - void api.addRoute( |
| 23 | + void api.get( |
9 | 24 | ':id/logs', |
10 | | - { authRequired: true, permissionsRequired: ['manage-apps'], validateParams: isAppLogsProps }, |
11 | 25 | { |
12 | | - async get() { |
13 | | - const proxiedApp = _manager.getOneById(this.urlParams.id); |
| 26 | + authRequired: true, |
| 27 | + permissionsRequired: ['manage-apps'], |
| 28 | + query: isAppLogsProps, |
| 29 | + response: { |
| 30 | + 200: ajv.compile({ |
| 31 | + type: 'object', |
| 32 | + properties: { |
| 33 | + offset: { type: 'number' }, |
| 34 | + logs: { type: 'array' }, |
| 35 | + count: { type: 'number' }, |
| 36 | + total: { type: 'number' }, |
| 37 | + success: { type: 'boolean' }, |
| 38 | + }, |
| 39 | + required: ['offset', 'logs', 'count', 'total', 'success'], |
| 40 | + additionalProperties: false, |
| 41 | + }), |
| 42 | + 400: errorResponse, |
| 43 | + 401: errorResponse, |
| 44 | + 403: errorResponse, |
| 45 | + 404: errorResponse, |
| 46 | + }, |
| 47 | + }, |
| 48 | + async function action() { |
| 49 | + const proxiedApp = _manager.getOneById(this.urlParams.id); |
14 | 50 |
|
15 | | - if (!proxiedApp) { |
16 | | - return api.notFound(`No App found by the id of: ${this.urlParams.id}`); |
17 | | - } |
| 51 | + if (!proxiedApp) { |
| 52 | + return api.notFound(`No App found by the id of: ${this.urlParams.id}`); |
| 53 | + } |
18 | 54 |
|
19 | | - if (this.queryParams.appId && this.queryParams.appId !== this.urlParams.id) { |
20 | | - return api.notFound(`Invalid query parameter "appId": ${this.queryParams.appId}`); |
21 | | - } |
| 55 | + if (this.queryParams.appId && this.queryParams.appId !== this.urlParams.id) { |
| 56 | + return api.notFound(`Invalid query parameter "appId": ${this.queryParams.appId}`); |
| 57 | + } |
22 | 58 |
|
23 | | - const { offset, count } = await getPaginationItems(this.queryParams); |
24 | | - const { sort } = await this.parseJsonQuery(); |
| 59 | + const { offset, count } = await getPaginationItems(this.queryParams); |
| 60 | + const { sort } = await this.parseJsonQuery(); |
25 | 61 |
|
26 | | - const options = { |
27 | | - sort: sort || { _updatedAt: -1 }, |
28 | | - skip: offset, |
29 | | - limit: count, |
30 | | - }; |
| 62 | + const options = { |
| 63 | + sort: sort || { _updatedAt: -1 }, |
| 64 | + skip: offset, |
| 65 | + limit: count, |
| 66 | + }; |
31 | 67 |
|
32 | | - let query: Record<string, any>; |
| 68 | + let query: Record<string, any>; |
33 | 69 |
|
34 | | - try { |
35 | | - query = makeAppLogsQuery({ appId: this.urlParams.id, ...this.queryParams }); |
36 | | - } catch (error) { |
37 | | - return api.failure({ error: error instanceof Error ? error.message : 'Unknown error' }); |
38 | | - } |
| 70 | + try { |
| 71 | + query = makeAppLogsQuery({ appId: this.urlParams.id, ...this.queryParams }); |
| 72 | + } catch (error) { |
| 73 | + return api.failure({ error: error instanceof Error ? error.message : 'Unknown error' }); |
| 74 | + } |
39 | 75 |
|
40 | | - const result = await _orch.getLogStorage().findPaginated(query, options); |
| 76 | + const result = await _orch.getLogStorage().findPaginated(query, options); |
41 | 77 |
|
42 | | - return api.success({ offset, logs: result.logs, count: result.logs.length, total: result.total }); |
43 | | - }, |
| 78 | + return api.success({ offset, logs: result.logs, count: result.logs.length, total: result.total }); |
44 | 79 | }, |
45 | 80 | ); |
0 commit comments