Skip to content

Commit f0d5047

Browse files
committed
refactor: use patchFlowLastUpdated from step model
1 parent f8e53e5 commit f0d5047

File tree

8 files changed

+54
-73
lines changed

8 files changed

+54
-73
lines changed

packages/backend/src/graphql/__tests__/mutations/create-step.itest.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it } from 'vitest'
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
22

33
import createStep from '@/graphql/mutations/create-step'
44
import Flow from '@/models/flow'
@@ -10,9 +10,16 @@ describe('createStep mutation integration tests', async () => {
1010
let testFlow: Flow
1111
let existingSteps: Step[]
1212
let context: Context
13+
const patchFlowLastUpdatedSpy = vi.fn().mockResolvedValue({})
1314

1415
// Clean up (and seed) database before each test.
1516
beforeEach(async () => {
17+
vi.resetAllMocks()
18+
19+
vi.spyOn(Step.prototype, 'patchFlowLastUpdated').mockImplementation(
20+
patchFlowLastUpdatedSpy,
21+
)
22+
1623
// Clear out all rows. Adjust deletion order if using foreign keys.
1724
await Step.query().delete()
1825
await Flow.query().delete()
@@ -119,20 +126,15 @@ describe('createStep mutation integration tests', async () => {
119126
expect(steps.map((step) => step.position)).toEqual([1, 2, 3, 4])
120127
})
121128

122-
it('should update flow updatedAt when creating a step', async () => {
123-
const originalUpdatedAt = testFlow.updatedAt
129+
it('should call patchFlowLastUpdated when creating a step', async () => {
124130
const params = {
125131
input: {
126132
flow: { id: testFlow.id },
127133
previousStep: { id: existingSteps[2].id },
128134
},
129135
}
130-
131136
await createStep(null, params, context)
132-
const updatedFlow = await Flow.query().findById(testFlow.id)
133-
expect(new Date(updatedFlow.updatedAt).getTime()).toBeGreaterThan(
134-
new Date(originalUpdatedAt).getTime(),
135-
)
137+
expect(patchFlowLastUpdatedSpy).toHaveBeenCalledTimes(1)
136138
})
137139

138140
it('throws an error if the flow does not belong to the current user', async () => {

packages/backend/src/graphql/__tests__/mutations/delete-step.itest.ts

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it } from 'vitest'
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
22

33
import deleteStep from '@/graphql/mutations/delete-step'
44
import Flow from '@/models/flow'
@@ -12,8 +12,16 @@ describe('deleteStep mutation', () => {
1212
let context: Context
1313
let testFlow: Flow
1414
let testSteps: Step[]
15+
const patchFlowLastUpdatedSpy = vi.fn().mockResolvedValue({})
1516

1617
beforeEach(async () => {
18+
vi.resetAllMocks()
19+
20+
// Mock the patchFlowLastUpdated method
21+
vi.spyOn(Step.prototype, 'patchFlowLastUpdated').mockImplementation(
22+
patchFlowLastUpdatedSpy,
23+
)
24+
1725
// Clear out all rows
1826
await Step.query().delete()
1927
await Flow.query().delete()
@@ -168,48 +176,17 @@ describe('deleteStep mutation', () => {
168176
expect(invalidatedStep.status).toBe('incomplete')
169177
})
170178

171-
it('should update flow updatedAt when deleting trigger step', async () => {
172-
const originalUpdatedAt = testFlow.updatedAt
173-
174-
await new Promise((resolve) => setTimeout(resolve, 10))
175-
176-
const result = await deleteStep(
177-
null,
178-
{ input: { ids: [testSteps[0].id] } },
179-
context,
180-
)
181-
182-
// Verify the flow's updatedAt was updated
183-
expect(result.updatedAt).toBeDefined()
184-
expect(new Date(result.updatedAt).getTime()).toBeGreaterThan(
185-
new Date(originalUpdatedAt).getTime(),
186-
)
187-
188-
// Verify the returned flow has the updated timestamp
189-
const updatedFlow = await Flow.query().findById(testFlow.id)
190-
expect(updatedFlow.updatedAt).toStrictEqual(result.updatedAt)
179+
it('should call patchFlowLastUpdated when deleting trigger step', async () => {
180+
await deleteStep(null, { input: { ids: [testSteps[0].id] } }, context)
181+
expect(patchFlowLastUpdatedSpy).toHaveBeenCalledTimes(1)
191182
})
192183

193-
it('should update flow updatedAt when deleting action steps', async () => {
194-
const originalUpdatedAt = testFlow.updatedAt
195-
196-
// Wait a moment to ensure different timestamp
197-
await new Promise((resolve) => setTimeout(resolve, 10))
198-
199-
const result = await deleteStep(
184+
it('should call patchFlowLastUpdated when deleting action steps', async () => {
185+
await deleteStep(
200186
null,
201187
{ input: { ids: [testSteps[1].id, testSteps[2].id] } },
202188
context,
203189
)
204-
205-
// Verify the flow's updatedAt was updated
206-
expect(result.updatedAt).toBeDefined()
207-
expect(new Date(result.updatedAt).getTime()).toBeGreaterThan(
208-
new Date(originalUpdatedAt).getTime(),
209-
)
210-
211-
// Verify the returned flow has the updated timestamp
212-
const updatedFlow = await Flow.query().findById(testFlow.id)
213-
expect(updatedFlow.updatedAt).toStrictEqual(result.updatedAt)
190+
expect(patchFlowLastUpdatedSpy).toHaveBeenCalledTimes(1)
214191
})
215192
})

packages/backend/src/graphql/__tests__/mutations/update-step.itest.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const mockStepId = '8c2a70d1-e78b-431e-9069-a4d8f97883f7'
1616
describe('updateStep mutation', () => {
1717
let context: Context
1818
let patchAndFetchByIdSpy: ReturnType<typeof vi.fn>
19-
const flowPatchSpy = vi.fn().mockResolvedValue({})
19+
const patchFlowLastUpdatedSpy = vi.fn().mockResolvedValue({})
2020

2121
// Helper to create step mock with flow
2222
const createStepMock = (stepData: any = {}) => ({
@@ -31,10 +31,8 @@ describe('updateStep mutation', () => {
3131
status: 'completed',
3232
flow: {
3333
id: mockFlowId,
34-
$query: vi.fn().mockReturnValue({
35-
patch: flowPatchSpy,
36-
}),
3734
},
35+
patchFlowLastUpdated: patchFlowLastUpdatedSpy,
3836
...stepData,
3937
},
4038
),
@@ -304,12 +302,6 @@ describe('updateStep mutation', () => {
304302
it('updating empty step name should not update template config', async () => {
305303
// Override the steps query to return template config
306304
setupRelatedQueryMock({
307-
flow: {
308-
id: mockFlowId,
309-
$query: vi.fn().mockReturnValue({
310-
patch: vi.fn().mockResolvedValue({}),
311-
}),
312-
},
313305
config: {
314306
stepName: 'some-step-name',
315307
templateConfig: { appEventKey: 'existingAppEventKey' },
@@ -346,18 +338,8 @@ describe('updateStep mutation', () => {
346338
)
347339
})
348340

349-
it('should call flow patch method when updating a step', async () => {
341+
it('should call patchFlowLastUpdated when updating a step', async () => {
350342
await updateStep(null, { input: { ...genericInputParams } }, context)
351-
352-
// Verify flow's patch method was called
353-
expect(flowPatchSpy).toHaveBeenCalledTimes(1)
354-
expect(flowPatchSpy).toHaveBeenCalledWith({
355-
updatedAt: expect.any(String),
356-
})
357-
358-
const updatedAtCall = flowPatchSpy.mock.calls[0][0]
359-
expect(new Date(updatedAtCall.updatedAt).toISOString()).toBe(
360-
updatedAtCall.updatedAt,
361-
)
343+
expect(patchFlowLastUpdatedSpy).toHaveBeenCalledTimes(1)
362344
})
363345
})

packages/backend/src/graphql/mutations/create-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const createStep: MutationResolvers['createStep'] = async (
5656
connectionId: input.connection?.id,
5757
})
5858

59-
await flow.$query(trx).patch({ updatedAt: new Date().toISOString() })
59+
await step.patchFlowLastUpdated()
6060

6161
return step
6262
})

packages/backend/src/graphql/mutations/delete-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const deleteStep: MutationResolvers['deleteStep'] = async (
110110
.patch({ position: raw(`position - ${steps.length}`) })
111111
}
112112

113-
await flow.$query(trx).patch({ updatedAt: new Date().toISOString() })
113+
await steps[0].patchFlowLastUpdated()
114114

115115
return await flow
116116
.$query(trx)

packages/backend/src/graphql/mutations/update-step.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ const updateStep: MutationResolvers['updateStep'] = async (
6060
.withGraphFetched('connection')
6161

6262
// update the flow's last updated
63-
await step.flow.$query(trx).patch({
64-
updatedAt: new Date().toISOString(),
65-
})
63+
await step.patchFlowLastUpdated()
6664

6765
return updatedStep
6866
})

packages/backend/src/models/__tests__/step.itest.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,20 @@ describe('step model', () => {
166166
expect(lastExecutionStep).toHaveProperty('id', executionStepIds[1])
167167
})
168168
})
169+
170+
describe('patchFlowLastUpdated', () => {
171+
it('should patch the flow last updated', async () => {
172+
const flow = await step.$relatedQuery('flow')
173+
const originalUpdatedAt = flow.updatedAt
174+
175+
await step.patchFlowLastUpdated()
176+
177+
const updatedFlow = await step.$relatedQuery('flow')
178+
179+
expect(updatedFlow.updatedAt).toBeDefined()
180+
expect(new Date(updatedFlow.updatedAt).getTime()).toBeGreaterThan(
181+
new Date(originalUpdatedAt).getTime(),
182+
)
183+
})
184+
})
169185
})

packages/backend/src/models/step.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ class Step extends Base {
192192
return command
193193
}
194194

195+
async patchFlowLastUpdated() {
196+
await this.$relatedQuery('flow').patch({
197+
updatedAt: new Date().toISOString(),
198+
})
199+
}
200+
195201
static async beforeUpdate(args: StaticHookArguments<Step>): Promise<void> {
196202
await super.beforeUpdate(args)
197203

0 commit comments

Comments
 (0)