UNPKG

@grouparoo/core

Version:
164 lines (163 loc) 6.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppRefreshQueryDestroy = exports.AppRefreshQueryView = exports.AppRefreshQueryTest = exports.AppRefreshQueryEdit = exports.AppRefreshQueryCreate = exports.AppRefreshQueryRun = void 0; const authenticatedAction_1 = require("../classes/actions/authenticatedAction"); const AppRefreshQuery_1 = require("../models/AppRefreshQuery"); const apiData_1 = require("../modules/apiData"); const configWriter_1 = require("../modules/configWriter"); class AppRefreshQueryRun extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "appRefreshQuery:run"; this.description = "run an appRefreshQuery to check for new data and enqueue schedules if needed"; this.outputExample = {}; this.permission = { topic: "app", mode: "write" }; this.inputs = { id: { required: true } }; } async runWithinTransaction({ params, }) { let valueUpdated = false; const appRefreshQuery = await AppRefreshQuery_1.AppRefreshQuery.findById(params.id); if (!appRefreshQuery) throw new Error(`No app refresh query ${params.id} found`); const sampleValue = await appRefreshQuery.query(); await appRefreshQuery.update({ lastConfirmedAt: new Date() }); let runs = []; if (sampleValue !== appRefreshQuery.value) { await appRefreshQuery.update({ value: sampleValue, lastChangedAt: new Date(), }); runs = await appRefreshQuery.triggerSchedules(true); valueUpdated = true; } return { valueUpdated, appRefreshQuery: await appRefreshQuery.apiData(), runs, }; } } exports.AppRefreshQueryRun = AppRefreshQueryRun; class AppRefreshQueryCreate extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "appRefreshQuery:create"; this.description = "create an app refresh query"; this.outputExample = {}; this.permission = { topic: "app", mode: "write" }; this.inputs = { appId: { required: true }, refreshQuery: { required: false }, recurringFrequency: { required: false, formatter: apiData_1.APIData.ensureNumber }, state: { required: false }, }; } async runWithinTransaction({ params, }) { const appRefreshQuery = await AppRefreshQuery_1.AppRefreshQuery.create({ appId: params.appId, }); if (params.refreshQuery) await appRefreshQuery.update({ refreshQuery: params.refreshQuery }); if (params.recurringFrequency) await appRefreshQuery.update({ recurringFrequency: params.recurringFrequency, }); if (params.state) await appRefreshQuery.update({ state: params.state }); await configWriter_1.ConfigWriter.run(); return { appRefreshQuery: await appRefreshQuery.apiData() }; } } exports.AppRefreshQueryCreate = AppRefreshQueryCreate; class AppRefreshQueryEdit extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "appRefreshQuery:edit"; this.description = "edit an app query refresh"; this.outputExample = {}; this.permission = { topic: "app", mode: "write" }; this.inputs = { id: { required: true }, appId: { required: false }, refreshQuery: { required: false }, recurringFrequency: { required: false }, state: { required: false }, }; } async runWithinTransaction({ params, }) { const appRefreshQuery = await AppRefreshQuery_1.AppRefreshQuery.findById(params.id); await appRefreshQuery.update(params); const sampleValue = await appRefreshQuery.query(); await appRefreshQuery.update({ lastConfirmedAt: new Date() }); if (sampleValue !== appRefreshQuery.value) { await appRefreshQuery.update({ value: sampleValue, lastChangedAt: new Date(), }); await appRefreshQuery.triggerSchedules(true); } await configWriter_1.ConfigWriter.run(); return { appRefreshQuery: await appRefreshQuery.apiData() }; } } exports.AppRefreshQueryEdit = AppRefreshQueryEdit; class AppRefreshQueryTest extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "appRefreshQuery:test"; this.description = "test the query for a given appRefreshQuery"; this.permission = { topic: "app", mode: "write" }; this.outputExample = {}; this.inputs = { id: { required: true }, refreshQuery: { required: false }, }; } async runWithinTransaction({ params, }) { const appRefreshQuery = await AppRefreshQuery_1.AppRefreshQuery.findById(params.id); const test = await appRefreshQuery.test(params.refreshQuery); if (test.error) test.error = String(test.error); return { test, appRefreshQuery: await appRefreshQuery.apiData(), }; } } exports.AppRefreshQueryTest = AppRefreshQueryTest; class AppRefreshQueryView extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "appRefreshQuery:view"; this.description = "view an app refresh query"; this.outputExample = {}; this.permission = { topic: "app", mode: "read" }; this.inputs = { id: { required: true }, }; } async runWithinTransaction({ params, }) { const appRefreshQuery = await AppRefreshQuery_1.AppRefreshQuery.findById(params.id); return { appRefreshQuery: await appRefreshQuery.apiData() }; } } exports.AppRefreshQueryView = AppRefreshQueryView; class AppRefreshQueryDestroy extends authenticatedAction_1.AuthenticatedAction { constructor() { super(...arguments); this.name = "appRefreshQuery:destroy"; this.description = "destroy an appRefreshQuery"; this.outputExample = {}; this.permission = { topic: "app", mode: "write" }; this.inputs = { id: { required: true }, }; } async runWithinTransaction({ params, }) { const appRefreshQuery = await AppRefreshQuery_1.AppRefreshQuery.findById(params.id); await appRefreshQuery.destroy(); await configWriter_1.ConfigWriter.run(); return { success: true }; } } exports.AppRefreshQueryDestroy = AppRefreshQueryDestroy;