feat: Implement createEmptyFlow method for user model

This commit is contained in:
Faruk AYDIN
2025-03-10 16:08:52 +01:00
parent 75a7a068f3
commit ac1f644071
4 changed files with 35 additions and 9 deletions

View File

@@ -1,11 +1,7 @@
import { renderObject } from '../../../../helpers/renderer.js';
export default async (request, response) => {
const flow = await request.currentUser.$relatedQuery('flows').insertAndFetch({
name: 'Name your flow',
});
await flow.createInitialSteps();
const flow = await request.currentUser.createEmptyFlow();
renderObject(response, flow, { status: 201 });
};

View File

@@ -40,10 +40,6 @@ class Flow extends Base {
},
};
static async import(user, flowData) {
return importFlow(user, flowData);
}
static relationMappings = () => ({
steps: {
relation: Base.HasManyRelation,
@@ -104,6 +100,10 @@ class Flow extends Base {
},
});
static async import(user, flowData) {
return importFlow(user, flowData);
}
static async populateStatusProperty(flows) {
const referenceFlow = flows[0];

View File

@@ -675,6 +675,14 @@ class User extends Base {
}
}
async createEmptyFlow() {
const flow = await this.$relatedQuery('flows').insertAndFetch({
name: 'Name your flow',
});
return await flow.createInitialSteps();
}
async $beforeInsert(queryContext) {
await super.$beforeInsert(queryContext);

View File

@@ -1507,6 +1507,28 @@ describe('User model', () => {
});
});
describe('createEmptyFlow', () => {
it('should create a flow with default name', async () => {
const user = await createUser();
const flow = await user.createEmptyFlow();
expect(flow.name).toBe('Name your flow');
expect(flow.userId).toBe(user.id);
});
it('should call createInitialSteps on the created flow', async () => {
const user = await createUser();
const createInitialStepsSpy = vi.spyOn(
Flow.prototype,
'createInitialSteps'
);
await user.createEmptyFlow();
expect(createInitialStepsSpy).toHaveBeenCalledOnce();
});
});
describe('$beforeInsert', () => {
it('should call super.$beforeInsert', async () => {
const superBeforeInsertSpy = vi