feat: Add create flow from template method to user model
This commit is contained in:
@@ -22,6 +22,7 @@ import Step from './step.js';
|
|||||||
import Subscription from './subscription.ee.js';
|
import Subscription from './subscription.ee.js';
|
||||||
import Folder from './folder.js';
|
import Folder from './folder.js';
|
||||||
import UsageData from './usage-data.ee.js';
|
import UsageData from './usage-data.ee.js';
|
||||||
|
import Template from './template.ee.js';
|
||||||
import Billing from '../helpers/billing/index.ee.js';
|
import Billing from '../helpers/billing/index.ee.js';
|
||||||
import NotAuthorizedError from '../errors/not-authorized.js';
|
import NotAuthorizedError from '../errors/not-authorized.js';
|
||||||
|
|
||||||
@@ -685,6 +686,16 @@ class User extends Base {
|
|||||||
return flow;
|
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) {
|
async $beforeInsert(queryContext) {
|
||||||
await super.$beforeInsert(queryContext);
|
await super.$beforeInsert(queryContext);
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ import { createExecution } from '../../test/factories/execution.js';
|
|||||||
import { createSubscription } from '../../test/factories/subscription.js';
|
import { createSubscription } from '../../test/factories/subscription.js';
|
||||||
import { createUsageData } from '../../test/factories/usage-data.js';
|
import { createUsageData } from '../../test/factories/usage-data.js';
|
||||||
import { createFolder } from '../../test/factories/folder.js';
|
import { createFolder } from '../../test/factories/folder.js';
|
||||||
|
import { createTemplate } from '../../test/factories/template.js';
|
||||||
import Billing from '../helpers/billing/index.ee.js';
|
import Billing from '../helpers/billing/index.ee.js';
|
||||||
|
import Template from './template.ee.js';
|
||||||
|
|
||||||
describe('User model', () => {
|
describe('User model', () => {
|
||||||
it('tableName should return correct name', () => {
|
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', () => {
|
describe('$beforeInsert', () => {
|
||||||
it('should call super.$beforeInsert', async () => {
|
it('should call super.$beforeInsert', async () => {
|
||||||
const superBeforeInsertSpy = vi
|
const superBeforeInsertSpy = vi
|
||||||
|
|||||||
Reference in New Issue
Block a user