UNPKG

@grouparoo/core

Version:
104 lines (103 loc) 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RunView = exports.RunEdit = exports.RunsList = void 0; const authenticatedAction_1 = require("../classes/actions/authenticatedAction"); const Run_1 = require("../models/Run"); const sequelize_1 = require("sequelize"); const Schedule_1 = require("../models/Schedule"); const apiData_1 = require("../modules/apiData"); class RunsList extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "runs:list"; this.description = "list the runs"; this.outputExample = {}; this.permission = { topic: "system", mode: "read" }; this.inputs = { creatorId: { required: false }, topic: { required: false }, state: { required: false }, hasError: { required: false }, limit: { required: true, default: 100, formatter: apiData_1.APIData.ensureNumber }, offset: { required: true, default: 0, formatter: apiData_1.APIData.ensureNumber }, order: { required: false, formatter: apiData_1.APIData.ensureArray, default: [["updatedAt", "desc"]], }, }; } async runWithinTransaction({ params }) { let creatorId = params.creatorId; if (params.topic === "source") { const schedule = await Schedule_1.Schedule.scope(null).findOne({ where: { sourceId: params.creatorId }, }); if (!schedule) { throw new Error("no schedule for this source"); } else { creatorId = schedule.id; } } const where = {}; if (creatorId) where["creatorId"] = creatorId; if (params.state) where["state"] = params.state; if (params.hasError === "true") where["error"] = { [sequelize_1.Op.ne]: null }; if (params.hasError === "false") where["error"] = { [sequelize_1.Op.eq]: null }; const runs = await Run_1.Run.scope(null).findAll({ where, limit: params.limit, offset: params.offset, order: params.order, }); return { runs: await Promise.all(runs.map((run) => run.apiData())), total: await Run_1.Run.scope(null).count({ where }), }; } } exports.RunsList = RunsList; class RunEdit extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "run:edit"; this.description = "edit a run"; this.outputExample = {}; this.permission = { topic: "system", mode: "write" }; this.inputs = { id: { required: true }, state: { required: true }, }; } async runWithinTransaction({ params }) { const run = await Run_1.Run.findById(params.id); await run.update({ state: params.state }); return { run: await run.apiData() }; } } exports.RunEdit = RunEdit; class RunView extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "run:view"; this.description = "view a run"; this.outputExample = {}; this.permission = { topic: "system", mode: "read" }; this.inputs = { id: { required: true }, }; } async runWithinTransaction({ params }) { const run = await Run_1.Run.findById(params.id); return { run: await run.apiData(), quantizedTimeline: await run.quantizedTimeline(), }; } } exports.RunView = RunView;