Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix/improve-disable
Browse files Browse the repository at this point in the history
  • Loading branch information
abuaboud committed Aug 12, 2024
2 parents 96e7836 + 0d1f01e commit f59a662
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 73 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "activepieces",
"version": "0.29.1",
"rcVersion": "0.30.0-rc.4",
"rcVersion": "0.30.0-rc.5",
"scripts": {
"prepare": "husky install",
"serve:frontend": "nx serve ui-core",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { FastifyPluginAsyncTypebox, Type } from '@fastify/type-provider-typebox'
import { StatusCodes } from 'http-status-codes'
import { DEFAULT_PRIORITY } from '../workers/queue/queue-manager'
import { appEventRoutingService } from './app-event-routing.service'
import { FastifyRequest } from 'fastify'

const appWebhooks: Record<string, Piece> = {
slack,
Expand All @@ -36,79 +37,82 @@ export const appEventRoutingModule: FastifyPluginAsyncTypebox = async (app) => {
export const appEventRoutingController: FastifyPluginAsyncTypebox = async (
fastify,
) => {
fastify.all('/:pieceUrl', EventRoutingParam, async (request, requestReply) => {
const pieceUrl = request.params.pieceUrl
const payload = {
headers: request.headers as Record<string, string>,
body: request.body,
rawBody: request.rawBody,
method: request.method,
queryParams: request.query as Record<string, string>,
}
const piece = appWebhooks[pieceUrl]
if (isNil(piece)) {
throw new ActivepiecesError({
code: ErrorCode.PIECE_NOT_FOUND,
params: {
pieceName: pieceUrl,
pieceVersion: 'latest',
message: 'Pieces is not found in app event routing',
},
})
}
const appName = pieceNames[pieceUrl]
assertNotNullOrUndefined(piece.events, 'Event is possible in this piece')
const { reply, event, identifierValue } = piece.events.parseAndReply({ payload })
if (!isNil(reply)) {
fastify.all(
'/:pieceUrl',
{
config: {
rawBody: true,
allowedPrincipals: ALL_PRINCIPAL_TYPES,
},
},
async (
request: FastifyRequest<{
Body: unknown
Params: {
pieceUrl: string
}
}>,
requestReply,
) => {
const pieceUrl = request.params.pieceUrl
const payload = {
headers: request.headers as Record<string, string>,
body: request.body,
rawBody: request.rawBody,
method: request.method,
queryParams: request.query as Record<string, string>,
}
const piece = appWebhooks[pieceUrl]
if (isNil(piece)) {
throw new ActivepiecesError({
code: ErrorCode.PIECE_NOT_FOUND,
params: {
pieceName: pieceUrl,
pieceVersion: 'latest',
message: 'Pieces is not found in app event routing',
},
})
}
const appName = pieceNames[pieceUrl]
assertNotNullOrUndefined(piece.events, 'Event is possible in this piece')
const { reply, event, identifierValue } = piece.events.parseAndReply({ payload })
if (!isNil(reply)) {
logger.info({
reply,
piece: pieceUrl,
}, '[AppEventRoutingController#event] reply')
return requestReply.status(StatusCodes.OK).headers(reply?.headers ?? {}).send(reply?.body ?? {})
}
logger.info({
reply,
piece: pieceUrl,
}, '[AppEventRoutingController#event] reply')
return requestReply.status(StatusCodes.OK).headers(reply?.headers ?? {}).send(reply?.body ?? {})
}
logger.info({
event,
identifierValue,
}, '[AppEventRoutingController#event] event')
if (isNil(event) || isNil(identifierValue)) {
return requestReply.status(StatusCodes.BAD_REQUEST).send({})
}
const listeners = await appEventRoutingService.listListeners({
appName,
event,
identifierValue,
})
const eventsQueue = listeners.map(async (listener) => {
const requestId = apId()
return {
id: requestId,
type: JobType.WEBHOOK,
data: {
projectId: listener.projectId,
schemaVersion: LATEST_JOB_DATA_SCHEMA_VERSION,
requestId,
synchronousHandlerId: null,
payload,
flowId: listener.flowId,
simulate: false,
},
priority: DEFAULT_PRIORITY,
event,
identifierValue,
}, '[AppEventRoutingController#event] event')
if (isNil(event) || isNil(identifierValue)) {
return requestReply.status(StatusCodes.BAD_REQUEST).send({})
}
const listeners = await appEventRoutingService.listListeners({
appName,
event,
identifierValue,
})
const eventsQueue = listeners.map(async (listener) => {
const requestId = apId()
return {
id: requestId,
type: JobType.WEBHOOK,
data: {
projectId: listener.projectId,
schemaVersion: LATEST_JOB_DATA_SCHEMA_VERSION,
requestId,
synchronousHandlerId: null,
payload,
flowId: listener.flowId,
simulate: false,
},
priority: DEFAULT_PRIORITY,
}
})
rejectedPromiseHandler(Promise.all(eventsQueue))
return requestReply.status(StatusCodes.OK).send({})
})
rejectedPromiseHandler(Promise.all(eventsQueue))
return requestReply.status(StatusCodes.OK).send({})
})
}

const EventRoutingParam = {
config: {
rawBody: true,
allowedPrincipals: ALL_PRINCIPAL_TYPES,
},
schema: {
params: Type.Object({
pieceUrl: Type.String(),
}),
body: Type.Unknown(),
},
}

0 comments on commit f59a662

Please sign in to comment.