Skip to content

Commit

Permalink
Merge pull request activepieces#3615 from activepieces/chore/flow-upd…
Browse files Browse the repository at this point in the history
…ate-cleanup

chore: clean update flow endpoint
  • Loading branch information
abuaboud authored Jan 14, 2024
2 parents c457dc8 + ab5ad93 commit 13d3cd7
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 140 deletions.
5 changes: 0 additions & 5 deletions packages/backend/src/app/ee/git-repos/git-repo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const DeleteRepoRequestSchema = {
allowedPrincipals: [PrincipalType.SERVICE, PrincipalType.USER],
},
schema: {
tags: ['git-repo'],
description: 'Delete a git repository information for a project.',
params: Type.Object({
id: Type.String(),
Expand All @@ -60,7 +59,6 @@ const PullRepoRequestSchema = {
allowedPrincipals: [PrincipalType.SERVICE, PrincipalType.USER],
},
schema: {
tags: ['git-repo'],
description: 'Pull all changes from the git repository and overwrite any conflicting changes in the project.',
params: Type.Object({
id: Type.String(),
Expand All @@ -76,7 +74,6 @@ const PushRepoRequestSchema = {
allowedPrincipals: [PrincipalType.SERVICE, PrincipalType.USER],
},
schema: {
tags: ['git-repo'],
description: 'Push all changes from the project and overwrite any conflicting changes in the git repository.',
body: PushGitRepoRequest,
params: Type.Object({
Expand All @@ -93,7 +90,6 @@ const ConfigureRepoRequestSchema = {
allowedPrincipals: [PrincipalType.SERVICE, PrincipalType.USER],
},
schema: {
tags: ['git-repo'],
description: 'Upsert a git repository information for a project.',
body: ConfigureRepoRequest,
response: {
Expand All @@ -107,7 +103,6 @@ const ListRepoRequestSchema = {
allowedPrincipals: [PrincipalType.SERVICE, PrincipalType.USER],
},
schema: {
tags: ['git-repo'],
querystring: Type.Object({
projectId: Type.String(),
}),
Expand Down
32 changes: 0 additions & 32 deletions packages/backend/src/app/flows/flow/flow.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
PopulatedFlow,
PrincipalType,
SeekPage,
UpdateFlowStatusRequest,
} from '@activepieces/shared'
import { StatusCodes } from 'http-status-codes'
import { flowService } from './flow.service'
Expand Down Expand Up @@ -56,22 +55,6 @@ export const flowController: FastifyPluginAsyncTypebox = async (app) => {
})
})

app.post('/:id/status', UpdateFlowStatusRequestOptions, async (request) => {
return flowService.updateStatus({
id: request.params.id,
projectId: request.principal.projectId,
newStatus: request.body.status,
})
})

app.post('/:id/published-version-id', UpdateFlowPublishedVersionIdRequestOptions, async (request) => {
return flowService.updatedPublishedVersionId({
id: request.params.id,
userId: request.principal.id,
projectId: request.principal.projectId,
})
})

app.get('/', ListFlowsRequestOptions, async (request) => {
return flowService.list({
projectId: request.principal.projectId,
Expand Down Expand Up @@ -138,22 +121,7 @@ const UpdateFlowRequestOptions = {
},
}

const UpdateFlowStatusRequestOptions = {
schema: {
body: UpdateFlowStatusRequest,
params: Type.Object({
id: ApId,
}),
},
}

const UpdateFlowPublishedVersionIdRequestOptions = {
schema: {
params: Type.Object({
id: ApId,
}),
},
}

const ListFlowsRequestOptions = {
config: {
Expand Down
99 changes: 48 additions & 51 deletions packages/backend/src/app/flows/flow/flow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,21 @@ export const flowService = {
}) : null

try {
if (operation.type === FlowOperationType.CHANGE_FOLDER) {
if (operation.type === FlowOperationType.LOCK_AND_PUBLISH) {
await flowService.updatedPublishedVersionId({
id,
userId,
projectId,
})
}
else if (operation.type === FlowOperationType.CHANGE_STATUS) {
await flowService.updateStatus({
id,
projectId,
newStatus: operation.request.status,
})
}
else if (operation.type === FlowOperationType.CHANGE_FOLDER) {
await flowRepo.update(id, {
folderId: operation.request.folderId,
})
Expand Down Expand Up @@ -197,28 +211,18 @@ export const flowService = {
},

async updateStatus({ id, projectId, newStatus }: UpdateStatusParams): Promise<PopulatedFlow> {
const lock = await acquireLock({
key: id,
timeout: 10000,
})
const flowToUpdate = await this.getOneOrThrow({ id, projectId })

try {
const flowToUpdate = await this.getOneOrThrow({ id, projectId })

if (flowToUpdate.status !== newStatus) {
const { scheduleOptions } = await hooks.preUpdateStatus({
flowToUpdate,
newStatus,
})
if (flowToUpdate.status !== newStatus) {
const { scheduleOptions } = await hooks.preUpdateStatus({
flowToUpdate,
newStatus,
})

flowToUpdate.status = newStatus
flowToUpdate.schedule = scheduleOptions
flowToUpdate.status = newStatus
flowToUpdate.schedule = scheduleOptions

await flowRepo.save(flowToUpdate)
}
}
finally {
await lock.release()
await flowRepo.save(flowToUpdate)
}

return this.getOnePopulatedOrThrow({
Expand All @@ -228,44 +232,37 @@ export const flowService = {
},

async updatedPublishedVersionId({ id, userId, projectId }: UpdatePublishedVersionIdParams): Promise<PopulatedFlow> {
const lock = await acquireLock({
key: id,
timeout: 10000,
})

try {
const flowToUpdate = await this.getOneOrThrow({ id, projectId })

const flowVersionToPublish = await flowVersionService.getFlowVersionOrThrow({
flowId: id,
versionId: undefined,
})
const flowToUpdate = await this.getOneOrThrow({ id, projectId })

const { scheduleOptions } = await hooks.preUpdatePublishedVersionId({
flowToUpdate,
flowVersionToPublish,
})
const flowVersionToPublish = await flowVersionService.getFlowVersionOrThrow({
flowId: id,
versionId: undefined,
})

const lockedFlowVersion = await lockFlowVersionIfNotLocked({
flowVersion: flowVersionToPublish,
userId,
projectId,
})
const { scheduleOptions } = await hooks.preUpdatePublishedVersionId({
flowToUpdate,
flowVersionToPublish,
})

flowToUpdate.publishedVersionId = lockedFlowVersion.id
flowToUpdate.status = FlowStatus.ENABLED
flowToUpdate.schedule = scheduleOptions
const lockedFlowVersion = await lockFlowVersionIfNotLocked({
flowVersion: flowVersionToPublish,
userId,
projectId,
})

const updatedFlow = await flowRepo.save(flowToUpdate)
flowToUpdate.publishedVersionId = lockedFlowVersion.id
flowToUpdate.status = FlowStatus.ENABLED
flowToUpdate.schedule = scheduleOptions

return {
...updatedFlow,
version: lockedFlowVersion,
}
}
finally {
await lock.release()
const updatedFlow = await flowRepo.save(flowToUpdate)

return {
...updatedFlow,
version: lockedFlowVersion,
}

},

async delete({ id, projectId }: DeleteParams): Promise<void> {
Expand Down Expand Up @@ -342,7 +339,7 @@ const lockFlowVersionIfNotLocked = async ({ flowVersion, userId, projectId }: Lo
})
}

const assertFlowIsNotNull: <T extends Flow>(flow: T | null) => asserts flow is T = <T>(flow: T | null) => {
const assertFlowIsNotNull: <T extends Flow>(flow: T | null) => asserts flow is T = <T>(flow: T | null) => {
if (isNil(flow)) {
throw new ActivepiecesError({
code: ErrorCode.ENTITY_NOT_FOUND,
Expand Down
22 changes: 16 additions & 6 deletions packages/backend/test/integration/ce/flows/flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { generateMockToken } from '../../../helpers/auth'
import { createMockUser, createMockProject, createMockFlow, createMockFlowVersion } from '../../../helpers/mocks'
import { StatusCodes } from 'http-status-codes'
import { FastifyInstance } from 'fastify'
import { FlowStatus, FlowVersionState, PrincipalType } from '@activepieces/shared'
import { FlowOperationType, FlowStatus, FlowVersionState, PrincipalType } from '@activepieces/shared'

let app: FastifyInstance | null = null

Expand Down Expand Up @@ -103,13 +103,16 @@ describe('Flow API', () => {
const mockToken = await generateMockToken({ type: PrincipalType.USER, projectId: mockProject.id })

const mockUpdateFlowStatusRequest = {
status: 'ENABLED',
type: FlowOperationType.CHANGE_STATUS,
request: {
status: 'ENABLED',
},
}

// act
const response = await app?.inject({
method: 'POST',
url: `/v1/flows/${mockFlow.id}/status`,
url: `/v1/flows/${mockFlow.id}`,
headers: {
authorization: `Bearer ${mockToken}`,
},
Expand Down Expand Up @@ -155,13 +158,16 @@ describe('Flow API', () => {
const mockToken = await generateMockToken({ type: PrincipalType.USER, projectId: mockProject.id })

const mockUpdateFlowStatusRequest = {
status: 'DISABLED',
type: FlowOperationType.CHANGE_STATUS,
request: {
status: 'DISABLED',
},
}

// act
const response = await app?.inject({
method: 'POST',
url: `/v1/flows/${mockFlow.id}/status`,
url: `/v1/flows/${mockFlow.id}`,
headers: {
authorization: `Bearer ${mockToken}`,
},
Expand Down Expand Up @@ -215,7 +221,11 @@ describe('Flow API', () => {
// act
const response = await app?.inject({
method: 'POST',
url: `/v1/flows/${mockFlow.id}/published-version-id`,
url: `/v1/flows/${mockFlow.id}`,
body: {
type: FlowOperationType.LOCK_AND_PUBLISH,
request: {},
},
headers: {
authorization: `Bearer ${mockToken}`,
},
Expand Down
1 change: 0 additions & 1 deletion packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export * from './lib/project/project'
import { TypeSystem } from '@sinclair/typebox/system'
export { RetryFlowRequestBody } from './lib/flow-run/test-flow-run-request'
export * from './lib/flows/dto/flow-template-request'
export * from './lib/flows/dto/update-flow-status-request'
// Look at https://github.com/sinclairzx81/typebox/issues/350
TypeSystem.ExactOptionalPropertyTypes = false
export * from './lib/support-url'

This file was deleted.

23 changes: 20 additions & 3 deletions packages/shared/src/lib/flows/flow-operations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {
CodeActionSchema, BranchActionSchema, LoopOnItemsActionSchema, PieceActionSchema, Action,
} from './actions/action'
import { FlowStatus } from './flow'
import { EmptyTrigger, PieceTrigger, WebhookTrigger } from './triggers/trigger'
import { Static, Type } from '@sinclair/typebox'


export enum FlowOperationType {
LOCK_AND_PUBLISH = 'LOCK_AND_PUBLISH',
CHANGE_STATUS = 'CHANGE_STATUS',
LOCK_FLOW = 'LOCK_FLOW',
CHANGE_FOLDER = 'CHANGE_FOLDER',
CHANGE_NAME = 'CHANGE_NAME',
Expand Down Expand Up @@ -33,9 +36,7 @@ export const UseAsDraftRequest = Type.Object({
})
export type UseAsDraftRequest = Static<typeof UseAsDraftRequest>

export const LockFlowRequest = Type.Object({
flowId: Type.String({}),
})
export const LockFlowRequest = Type.Object({})

export type LockFlowRequest = Static<typeof LockFlowRequest>

Expand Down Expand Up @@ -92,12 +93,28 @@ export type AddActionRequest = Static<typeof AddActionRequest>
export const UpdateTriggerRequest = Type.Union([EmptyTrigger, PieceTrigger, WebhookTrigger])
export type UpdateTriggerRequest = Static<typeof UpdateTriggerRequest>

export const UpdateFlowStatusRequest = Type.Object({
status: Type.Enum(FlowStatus),
})
export type UpdateFlowStatusRequest = Static<typeof UpdateFlowStatusRequest>

export const ChangePublishedVersionIdRequest = Type.Object({})
export type ChangePublishedVersionIdRequest = Static<typeof ChangePublishedVersionIdRequest>


export const FlowOperationRequest = Type.Union([
Type.Object({
type: Type.Literal(FlowOperationType.MOVE_ACTION),
request: MoveActionRequest,
}),
Type.Object({
type: Type.Literal(FlowOperationType.CHANGE_STATUS),
request: UpdateFlowStatusRequest,
}),
Type.Object({
type: Type.Literal(FlowOperationType.LOCK_AND_PUBLISH),
request: ChangePublishedVersionIdRequest,
}),
Type.Object({
type: Type.Literal(FlowOperationType.USE_AS_DRAFT),
request: UseAsDraftRequest,
Expand Down
Loading

0 comments on commit 13d3cd7

Please sign in to comment.