feat: Implement createEmptyFlow method for user model
This commit is contained in:
@@ -1,11 +1,7 @@
|
|||||||
import { renderObject } from '../../../../helpers/renderer.js';
|
import { renderObject } from '../../../../helpers/renderer.js';
|
||||||
|
|
||||||
export default async (request, response) => {
|
export default async (request, response) => {
|
||||||
const flow = await request.currentUser.$relatedQuery('flows').insertAndFetch({
|
const flow = await request.currentUser.createEmptyFlow();
|
||||||
name: 'Name your flow',
|
|
||||||
});
|
|
||||||
|
|
||||||
await flow.createInitialSteps();
|
|
||||||
|
|
||||||
renderObject(response, flow, { status: 201 });
|
renderObject(response, flow, { status: 201 });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -40,10 +40,6 @@ class Flow extends Base {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
static async import(user, flowData) {
|
|
||||||
return importFlow(user, flowData);
|
|
||||||
}
|
|
||||||
|
|
||||||
static relationMappings = () => ({
|
static relationMappings = () => ({
|
||||||
steps: {
|
steps: {
|
||||||
relation: Base.HasManyRelation,
|
relation: Base.HasManyRelation,
|
||||||
@@ -104,6 +100,10 @@ class Flow extends Base {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
static async import(user, flowData) {
|
||||||
|
return importFlow(user, flowData);
|
||||||
|
}
|
||||||
|
|
||||||
static async populateStatusProperty(flows) {
|
static async populateStatusProperty(flows) {
|
||||||
const referenceFlow = flows[0];
|
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) {
|
async $beforeInsert(queryContext) {
|
||||||
await super.$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', () => {
|
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