feat: Complete export flow rest API endpoint

This commit is contained in:
Faruk AYDIN
2025-01-10 17:21:43 +03:00
parent 169c86a748
commit c180b98460
10 changed files with 362 additions and 19 deletions

View File

@@ -1,4 +1,5 @@
import { ValidationError } from 'objection';
import slugify from 'slugify';
import Base from './base.js';
import Step from './step.js';
import User from './user.js';
@@ -7,6 +8,7 @@ import ExecutionStep from './execution-step.js';
import globalVariable from '../helpers/global-variable.js';
import logger from '../helpers/logger.js';
import Telemetry from '../helpers/telemetry/index.js';
import exportFlow from '../helpers/export-flow.js';
import flowQueue from '../queues/flow.js';
import {
REMOVE_AFTER_30_DAYS_OR_150_JOBS,
@@ -426,6 +428,24 @@ class Flow extends Base {
}
}
slugifyNameAsFilename() {
const slug = slugify(this.name, {
lower: true,
strict: true,
replacement: '-',
});
return `${slug}.json`;
}
async export() {
const exportedFlow = await exportFlow(this);
const exportedFlowAsString = JSON.stringify(exportedFlow, null, 2);
const slug = this.slugifyNameAsFilename();
return { exportedFlowAsString, slug };
}
async $beforeUpdate(opt, queryContext) {
await super.$beforeUpdate(opt, queryContext);

View File

@@ -10,6 +10,7 @@ import { createFlow } from '../../test/factories/flow.js';
import { createStep } from '../../test/factories/step.js';
import { createExecution } from '../../test/factories/execution.js';
import { createExecutionStep } from '../../test/factories/execution-step.js';
import * as exportFlow from '../helpers/export-flow.js';
describe('Flow model', () => {
it('tableName should return correct name', () => {
@@ -506,6 +507,69 @@ describe('Flow model', () => {
});
});
describe('slugifyNameAsFilename', () => {
it('should generate a slug file name from flow name', async () => {
const flow = await createFlow({
name: 'My Flow Name',
});
const slug = flow.slugifyNameAsFilename();
expect(slug).toBe('my-flow-name.json');
});
});
describe('export', () => {
it('should call slugifyNameAsFilename method', async () => {
const flow = await createFlow({
name: 'My Flow Name',
});
const slugifyNameAsFilenameSpy = vi
.spyOn(flow, 'slugifyNameAsFilename')
.mockImplementation(() => 'my-flow-name.json');
await flow.export();
expect(slugifyNameAsFilenameSpy).toHaveBeenCalledOnce();
});
it('should call exportFlow method', async () => {
const flow = await createFlow();
const exportFlowSpy = vi
.spyOn(exportFlow, 'default')
.mockImplementation(() => {});
await flow.export();
expect(exportFlowSpy).toHaveBeenCalledOnce();
});
it('should return exportedFlowAsString and slug', async () => {
const flow = await createFlow();
const exportedFlowAsString = {
name: 'My Flow Name',
};
const slug = 'slug';
vi.spyOn(exportFlow, 'default').mockReturnValue(exportedFlowAsString);
vi.spyOn(flow, 'slugifyNameAsFilename').mockReturnValue(slug);
const expectedExportedFlowAsString = JSON.stringify(
exportedFlowAsString,
null,
2
);
expect(await flow.export()).toStrictEqual({
exportedFlowAsString: expectedExportedFlowAsString,
slug: 'slug',
});
});
});
describe('throwIfHavingLessThanTwoSteps', () => {
it('should throw validation error with less than two steps', async () => {
const flow = await createFlow();