feat: Implement createEmptyFlow method for user model
This commit is contained in:
@@ -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 });
|
||||
};
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user