oneuptime/Workflow/API/Workflow.ts

92 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-02-28 14:09:44 +00:00
import Express, {
ExpressRequest,
ExpressResponse,
ExpressRouter,
} from 'CommonServer/Utils/Express';
import Response from 'CommonServer/Utils/Response';
import ObjectID from 'Common/Types/ObjectID';
import BadDataException from 'Common/Types/Exception/BadDataException';
import WorkflowService from 'CommonServer/Services/WorkflowService';
import Workflow from 'Model/Models/Workflow';
import ClusterKeyAuthorization from 'CommonServer/Middleware/ClusterKeyAuthorization';
import ComponentCode from 'CommonServer/Types/Workflow/ComponentCode';
import Components from 'CommonServer/Types/Workflow/Components/Index';
2023-02-28 22:21:33 +00:00
import TriggerCode from 'CommonServer/Types/Workflow/TriggerCode';
2023-02-28 14:09:44 +00:00
export default class WorkflowAPI {
public router!: ExpressRouter;
public constructor() {
this.router = Express.getRouter();
2023-03-01 09:27:18 +00:00
this.router.get(
`/update/:workflowId`,
ClusterKeyAuthorization.isAuthorizedServiceMiddleware,
this.updateWorkflow
);
2023-02-28 14:09:44 +00:00
2023-03-01 09:27:18 +00:00
this.router.post(
`/update/:workflowId`,
ClusterKeyAuthorization.isAuthorizedServiceMiddleware,
this.updateWorkflow
);
2023-02-28 14:09:44 +00:00
}
public async updateWorkflow(
req: ExpressRequest,
res: ExpressResponse
): Promise<void> {
// add this workflow to the run queue and return the 200 response.
if (!req.params['workflowId']) {
return Response.sendErrorResponse(
req,
res,
new BadDataException('workflowId not found in URL')
);
}
const workflow: Workflow | null = await WorkflowService.findOneById({
id: new ObjectID(req.params['workflowId']),
select: {
_id: true,
triggerId: true,
},
props: {
isRoot: true,
2023-03-01 09:27:18 +00:00
},
2023-02-28 14:09:44 +00:00
});
2023-02-28 22:21:33 +00:00
if (!workflow) {
2023-02-28 14:09:44 +00:00
return Response.sendJsonObjectResponse(req, res, {
status: 'Workflow not found',
});
}
2023-03-01 09:27:18 +00:00
if (!workflow.triggerId) {
2023-02-28 23:18:01 +00:00
return Response.sendJsonObjectResponse(req, res, {
status: 'Trigger not found in workflow',
});
}
2023-02-28 14:09:44 +00:00
2023-03-01 09:27:18 +00:00
const componentCode: ComponentCode | undefined =
Components[workflow.triggerId];
2023-02-28 14:09:44 +00:00
2023-02-28 22:21:33 +00:00
if (!componentCode) {
2023-02-28 14:09:44 +00:00
return Response.sendJsonObjectResponse(req, res, {
status: 'Component not found',
});
}
2023-02-28 22:21:33 +00:00
if (componentCode instanceof TriggerCode) {
await componentCode.update({
2023-03-01 09:27:18 +00:00
workflowId: workflow.id!,
2023-02-28 22:21:33 +00:00
});
}
2023-02-28 14:09:44 +00:00
return Response.sendJsonObjectResponse(req, res, {
status: 'Updated',
});
}
}