refactor(flow): restructure afterFind hook in model
This commit is contained in:
@@ -88,15 +88,13 @@ class Flow extends Base {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
static async afterFind(args) {
|
static async populateStatusProperty(flows) {
|
||||||
const { result } = args;
|
const referenceFlow = flows[0];
|
||||||
|
|
||||||
const referenceFlow = result[0];
|
|
||||||
|
|
||||||
if (referenceFlow) {
|
if (referenceFlow) {
|
||||||
const shouldBePaused = await referenceFlow.isPaused();
|
const shouldBePaused = await referenceFlow.isPaused();
|
||||||
|
|
||||||
for (const flow of result) {
|
for (const flow of flows) {
|
||||||
if (!flow.active) {
|
if (!flow.active) {
|
||||||
flow.status = 'draft';
|
flow.status = 'draft';
|
||||||
} else if (flow.active && shouldBePaused) {
|
} else if (flow.active && shouldBePaused) {
|
||||||
@@ -108,6 +106,10 @@ class Flow extends Base {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async afterFind(args) {
|
||||||
|
await this.populateStatusProperty(args.result);
|
||||||
|
}
|
||||||
|
|
||||||
async lastInternalId() {
|
async lastInternalId() {
|
||||||
const lastExecution = await this.$relatedQuery('lastExecution');
|
const lastExecution = await this.$relatedQuery('lastExecution');
|
||||||
|
|
||||||
|
@@ -120,7 +120,53 @@ describe('Flow model', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.todo('afterFind - possibly refactor to persist');
|
describe('populateStatusProperty', () => {
|
||||||
|
it('should assign "draft" to status property when a flow is not active', async () => {
|
||||||
|
const referenceFlow = await createFlow({ active: false });
|
||||||
|
|
||||||
|
const flows = [referenceFlow];
|
||||||
|
|
||||||
|
vi.spyOn(referenceFlow, 'isPaused').mockResolvedValue();
|
||||||
|
|
||||||
|
await Flow.populateStatusProperty(flows);
|
||||||
|
|
||||||
|
expect(referenceFlow.status).toBe('draft');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should assign "paused" to status property when a flow is active, but should be paused', async () => {
|
||||||
|
const referenceFlow = await createFlow({ active: true });
|
||||||
|
|
||||||
|
const flows = [referenceFlow];
|
||||||
|
|
||||||
|
vi.spyOn(referenceFlow, 'isPaused').mockResolvedValue(true);
|
||||||
|
|
||||||
|
await Flow.populateStatusProperty(flows);
|
||||||
|
|
||||||
|
expect(referenceFlow.status).toBe('paused');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should assign "published" to status property when a flow is active', async () => {
|
||||||
|
const referenceFlow = await createFlow({ active: true });
|
||||||
|
|
||||||
|
const flows = [referenceFlow];
|
||||||
|
|
||||||
|
vi.spyOn(referenceFlow, 'isPaused').mockResolvedValue(false);
|
||||||
|
|
||||||
|
await Flow.populateStatusProperty(flows);
|
||||||
|
|
||||||
|
expect(referenceFlow.status).toBe('published');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('afterFind should call Flow.populateStatusProperty', async () => {
|
||||||
|
const populateStatusPropertySpy = vi
|
||||||
|
.spyOn(Flow, 'populateStatusProperty')
|
||||||
|
.mockImplementation(() => {});
|
||||||
|
|
||||||
|
await createFlow();
|
||||||
|
|
||||||
|
expect(populateStatusPropertySpy).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
describe('lastInternalId', () => {
|
describe('lastInternalId', () => {
|
||||||
it('should return internal ID of last execution when exists', async () => {
|
it('should return internal ID of last execution when exists', async () => {
|
||||||
|
Reference in New Issue
Block a user