feat: Add create flow from template method to user model

This commit is contained in:
Faruk AYDIN
2025-03-10 16:26:10 +01:00
parent 8246adec03
commit cf80ba28eb
2 changed files with 46 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import Step from './step.js';
import Subscription from './subscription.ee.js';
import Folder from './folder.js';
import UsageData from './usage-data.ee.js';
import Template from './template.ee.js';
import Billing from '../helpers/billing/index.ee.js';
import NotAuthorizedError from '../errors/not-authorized.js';
@@ -685,6 +686,16 @@ class User extends Base {
return flow;
}
async createFlowFromTemplate(templateId) {
const template = await Template.query()
.findById(templateId)
.throwIfNotFound();
const flow = await Flow.import(this, template.flowData);
return flow;
}
async $beforeInsert(queryContext) {
await super.$beforeInsert(queryContext);

View File

@@ -34,7 +34,9 @@ import { createExecution } from '../../test/factories/execution.js';
import { createSubscription } from '../../test/factories/subscription.js';
import { createUsageData } from '../../test/factories/usage-data.js';
import { createFolder } from '../../test/factories/folder.js';
import { createTemplate } from '../../test/factories/template.js';
import Billing from '../helpers/billing/index.ee.js';
import Template from './template.ee.js';
describe('User model', () => {
it('tableName should return correct name', () => {
@@ -1529,6 +1531,39 @@ describe('User model', () => {
});
});
describe('createFlowFromTemplate', () => {
let user, template;
beforeEach(async () => {
user = await createUser();
template = await createTemplate();
});
it('should throw an error if template is not found', async () => {
const nonExistentTemplateId = Crypto.randomUUID();
await expect(
user.createFlowFromTemplate(nonExistentTemplateId)
).rejects.toThrow('NotFoundError');
});
it('should call Flow.import with the correct parameters', async () => {
vi.spyOn(Template.query(), 'findById').mockImplementation(() => ({
throwIfNotFound: () => template,
}));
const importSpy = vi.spyOn(Flow, 'import').mockResolvedValue({
id: Crypto.randomUUID(),
name: template.flowData.name,
steps: [],
});
await user.createFlowFromTemplate(template.id);
expect(importSpy).toHaveBeenCalledWith(user, template.flowData);
});
});
describe('$beforeInsert', () => {
it('should call super.$beforeInsert', async () => {
const superBeforeInsertSpy = vi