Merge pull request #2150 from automatisch/aut-1338-after-find

refactor(flow): restructure afterFind hook in model
This commit is contained in:
Ömer Faruk Aydın
2024-10-31 16:12:08 +01:00
committed by GitHub
2 changed files with 54 additions and 6 deletions

View File

@@ -88,15 +88,13 @@ class Flow extends Base {
},
});
static async afterFind(args) {
const { result } = args;
const referenceFlow = result[0];
static async populateStatusProperty(flows) {
const referenceFlow = flows[0];
if (referenceFlow) {
const shouldBePaused = await referenceFlow.isPaused();
for (const flow of result) {
for (const flow of flows) {
if (!flow.active) {
flow.status = 'draft';
} else if (flow.active && shouldBePaused) {
@@ -108,6 +106,10 @@ class Flow extends Base {
}
}
static async afterFind(args) {
await this.populateStatusProperty(args.result);
}
async lastInternalId() {
const lastExecution = await this.$relatedQuery('lastExecution');

View File

@@ -121,7 +121,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', () => {
it('should return internal ID of last execution when exists', async () => {