From 381e0ef38ddacb5e8e09cd483bc2c3115a0bc1ab Mon Sep 17 00:00:00 2001 From: GabrielEluan Date: Fri, 11 Feb 2022 12:00:17 -0300 Subject: [PATCH] Add senderAppId param to broadcaster --- node/index.ts | 4 +++- node/middlewares/broadcaster/broadcast.ts | 3 ++- node/middlewares/broadcaster/splitPayload.ts | 3 ++- node/middlewares/notify/parseFile.ts | 9 ++++++--- node/middlewares/notify/startEventChain.ts | 3 ++- node/utils/parsing.ts | 6 ++++++ 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/node/index.ts b/node/index.ts index 77485df..cc598f8 100644 --- a/node/index.ts +++ b/node/index.ts @@ -30,13 +30,15 @@ declare global { interface State extends RecorderState { payload: unknown[] - appId: string + senderAppId: string + clientAppId: string } interface BroadcasterEventContext extends EventContext { body: { eventId: string payload: unknown[] + senderAppId: string clientAppId: string } } diff --git a/node/middlewares/broadcaster/broadcast.ts b/node/middlewares/broadcaster/broadcast.ts index 795698a..6755da3 100644 --- a/node/middlewares/broadcaster/broadcast.ts +++ b/node/middlewares/broadcaster/broadcast.ts @@ -6,12 +6,13 @@ export async function broadcast( ) { const { clients: { events }, - body: { payload, clientAppId }, + body: { payload, senderAppId, clientAppId }, } = ctx payload.forEach((row) => { events.sendEvent(clientAppId, eventKey, { data: row, + senderAppId, }) }) diff --git a/node/middlewares/broadcaster/splitPayload.ts b/node/middlewares/broadcaster/splitPayload.ts index b14aa89..dd3c231 100644 --- a/node/middlewares/broadcaster/splitPayload.ts +++ b/node/middlewares/broadcaster/splitPayload.ts @@ -13,7 +13,7 @@ export async function splitPayload( ) { const { clients: { events }, - body: { payload, clientAppId }, + body: { payload, senderAppId, clientAppId }, } = ctx if (payload.length > ARRAY_SIZE_TARGET) { @@ -27,6 +27,7 @@ export async function splitPayload( events.sendEvent(thisAppId, eventKey, { eventId, payload: chunk, + senderAppId, clientAppId, }) } diff --git a/node/middlewares/notify/parseFile.ts b/node/middlewares/notify/parseFile.ts index 623798b..041b02b 100644 --- a/node/middlewares/notify/parseFile.ts +++ b/node/middlewares/notify/parseFile.ts @@ -6,7 +6,7 @@ import { MAXIMUM_FILE_SIZE, MAXIMUM_FILE_SIZE_STRING, } from '../../utils/constants' -import { asyncBusboyWrapper } from '../../utils/parsing' +import { asyncBusboyWrapper, senderAppIdFromHeaders } from '../../utils/parsing' export async function parseFile(ctx: Context, next: () => Promise) { const { @@ -14,8 +14,10 @@ export async function parseFile(ctx: Context, next: () => Promise) { vtex: { logger }, } = ctx + const senderAppId = senderAppIdFromHeaders(req.headers) + const { - fields: { appId }, + fields: { appId: clientAppId }, files: [file], } = await asyncBusboyWrapper(req) @@ -55,7 +57,8 @@ export async function parseFile(ctx: Context, next: () => Promise) { const payload = utils.sheet_to_json(sheet) ctx.state.payload = payload - ctx.state.appId = appId ?? '' + ctx.state.senderAppId = senderAppId + ctx.state.clientAppId = clientAppId ?? '' } catch (error) { logger.error(error.message) diff --git a/node/middlewares/notify/startEventChain.ts b/node/middlewares/notify/startEventChain.ts index 01236f7..091dce4 100644 --- a/node/middlewares/notify/startEventChain.ts +++ b/node/middlewares/notify/startEventChain.ts @@ -7,7 +7,7 @@ import { export async function startEventChain(ctx: Context) { const { - state: { payload, appId: clientAppId }, + state: { payload, senderAppId, clientAppId }, clients: { events }, } = ctx @@ -16,6 +16,7 @@ export async function startEventChain(ctx: Context) { events.sendEvent(thisAppId, eventKey, { eventId, payload, + senderAppId, clientAppId, }) diff --git a/node/utils/parsing.ts b/node/utils/parsing.ts index ea1233f..3628c1b 100644 --- a/node/utils/parsing.ts +++ b/node/utils/parsing.ts @@ -7,3 +7,9 @@ export async function asyncBusboyWrapper(req: IncomingMessage): Promise { return (result as unknown) as T } + +export function senderAppIdFromHeaders(headers: IncomingMessage['headers']) { + const appIdWithVersion = headers['x-vtex-caller'] as string + + return appIdWithVersion.split('@')[0] +}