|
| 1 | +import PermissionChecker from '../../services/user/permissionChecker' |
| 2 | +import Permissions from '../../security/permissions' |
| 3 | +import AutomationService from '../../services/automationService' |
| 4 | +import ApiResponseHandler from '../apiResponseHandler' |
| 5 | +import { |
| 6 | + AutomationCriteria, |
| 7 | + AutomationState, |
| 8 | + AutomationTrigger, |
| 9 | + AutomationType, |
| 10 | +} from '../../types/automationTypes' |
| 11 | + |
| 12 | +/** |
| 13 | + * GET /tenant/{tenantId}/automation |
| 14 | + * @summary Get all automation data for tenant |
| 15 | + * @tag Automations |
| 16 | + * @security Bearer |
| 17 | + * @description Get all existing automation data for tenant. |
| 18 | + * @pathParam {string} tenantId - Your workspace/tenant ID |
| 19 | + * @queryParam {string} [filter[type]] - Filter by type of automation |
| 20 | + * @queryParam {string} [filter[trigger]] - Filter by trigger type of automation |
| 21 | + * @queryParam {string} [filter[state]] - Filter by state of automation |
| 22 | + * @queryParam {number} [offset] - Skip the first n results. Default 0. |
| 23 | + * @queryParam {number} [limit] - Limit the number of results. Default 50. |
| 24 | + * @response 200 - Ok |
| 25 | + * @responseContent {AutomationPage} 200.application/json |
| 26 | + * @responseExample {AutomationPage} 200.application/json.AutomationPage |
| 27 | + * @response 401 - Unauthorized |
| 28 | + * @response 429 - Too many requests |
| 29 | + */ |
| 30 | +export default async (req, res) => { |
| 31 | + try { |
| 32 | + new PermissionChecker(req).validateHas(Permissions.values.automationRead) |
| 33 | + |
| 34 | + let offset = 0 |
| 35 | + if (req.query.offset) { |
| 36 | + offset = parseInt(req.query.offset, 10) |
| 37 | + } |
| 38 | + let limit = 50 |
| 39 | + if (req.query.limit) { |
| 40 | + limit = parseInt(req.query.limit, 10) |
| 41 | + } |
| 42 | + |
| 43 | + const criteria: AutomationCriteria = { |
| 44 | + type: req.query.filter?.type ? (req.query.filter.type as AutomationType) : undefined, |
| 45 | + trigger: req.query.filter?.trigger |
| 46 | + ? (req.query.filter?.trigger as AutomationTrigger) |
| 47 | + : undefined, |
| 48 | + state: req.query.filter?.state ? (req.query.filter.state as AutomationState) : undefined, |
| 49 | + limit, |
| 50 | + offset, |
| 51 | + } |
| 52 | + |
| 53 | + const payload = await new AutomationService(req).findAndCountAll(criteria) |
| 54 | + |
| 55 | + await ApiResponseHandler.success(req, res, payload) |
| 56 | + } catch (error) { |
| 57 | + await ApiResponseHandler.error(req, res, error) |
| 58 | + } |
| 59 | +} |
0 commit comments