feat: Implement admin create template API endpoint
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import Base from './base.js';
|
||||
import Flow from './flow.js';
|
||||
|
||||
class Template extends Base {
|
||||
static tableName = 'templates';
|
||||
@@ -15,6 +16,13 @@ class Template extends Base {
|
||||
updatedAt: { type: 'string' },
|
||||
},
|
||||
};
|
||||
|
||||
static async create(name, flowId) {
|
||||
const flow = await Flow.query().findById(flowId).throwIfNotFound();
|
||||
const flowData = await flow.export();
|
||||
|
||||
return this.query().insertAndFetch({ name, flowData });
|
||||
}
|
||||
}
|
||||
|
||||
export default Template;
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import Crypto from 'crypto';
|
||||
import Template from './template.ee.js';
|
||||
import { createFlow } from '../../test/factories/flow';
|
||||
import { createStep } from '../../test/factories/step';
|
||||
|
||||
describe('Template model', () => {
|
||||
it('tableName should return correct name', () => {
|
||||
@@ -9,4 +12,62 @@ describe('Template model', () => {
|
||||
it('jsonSchema should have correct validations', () => {
|
||||
expect(Template.jsonSchema).toMatchSnapshot();
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('should throw an error if the flow does not exist', async () => {
|
||||
const nonExistentFlowId = Crypto.randomUUID();
|
||||
const templateName = 'Test Template';
|
||||
|
||||
await expect(
|
||||
Template.create(templateName, nonExistentFlowId)
|
||||
).rejects.toThrowError('NotFoundError');
|
||||
});
|
||||
|
||||
it('should create template with the name', async () => {
|
||||
const flow = await createFlow();
|
||||
const templateName = 'Test Template';
|
||||
|
||||
const template = await Template.create(templateName, flow.id);
|
||||
|
||||
expect(template.name).toStrictEqual(templateName);
|
||||
});
|
||||
|
||||
it('should create template with the flow data', async () => {
|
||||
const flow = await createFlow();
|
||||
|
||||
const triggerStep = await createStep({
|
||||
flowId: flow.id,
|
||||
type: 'trigger',
|
||||
appKey: 'webhook',
|
||||
key: 'catchRawWebhook',
|
||||
});
|
||||
|
||||
await createStep({
|
||||
flowId: flow.id,
|
||||
type: 'action',
|
||||
appKey: 'ntfy',
|
||||
key: 'sendMessage',
|
||||
parameters: {
|
||||
topic: 'Test notification',
|
||||
message: `Message: {{step.${triggerStep.id}.body.message}} by {{step.${triggerStep.id}.body.sender}}`,
|
||||
},
|
||||
});
|
||||
|
||||
const templateName = 'Test Template';
|
||||
const template = await Template.create(templateName, flow.id);
|
||||
|
||||
const exportedFlowData = await flow.export();
|
||||
|
||||
expect(template.flowData).toMatchObject({
|
||||
name: exportedFlowData.name,
|
||||
steps: template.flowData.steps.map((step) => ({
|
||||
appKey: step.appKey,
|
||||
key: step.key,
|
||||
name: step.name,
|
||||
position: step.position,
|
||||
type: step.type,
|
||||
})),
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user