Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/controllers/orchestration.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,25 @@ const packageRouterCaller = async (req, res, responses, servicePackage, packages
}
return true
}
/**
* Calls a custom merge handler defined in one of the packages based on the provided merge configuration.
*
* @async
* @function
* @param {Array<Object>} result - An array of response objects from service packages to be merged.
* @param {Object} mergeOption - The merge configuration object.
* @param {Object} mergeOption.mergeConfig - Configuration that includes the base package name and the function name to call.
* @param {string} mergeOption.mergeConfig.basePackageName - Identifier used to find the relevant package.
* @param {string} mergeOption.mergeConfig.functionName - The name of the custom merge handler function to invoke.
* @param {Array<Object>} packages - Array of package objects, each expected to have a `packageMeta.basePackageName` and a `customMergeFunctionHandler` function.
* @returns {Promise<Object>} The result of the custom merge function.
*/
const customMergeFunctionCaller = async (result,mergeOption, packages) => {
const selectedPackage = packages.find((obj) => obj.packageMeta.basePackageName === mergeOption.basePackageName)
return selectedPackage.customMergeFunctionHandler(result, mergeOption.functionName, packages)
}

const orchestrationHandler = async (packages, req, res) => {
const orchestrationHandler = async (packages, mergeOption ,req, res) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@borkarsaish65 When introducing a new parameter please add that to end of args

try {
const { targetPackages, inSequence, responseMessage } = req
const responses = {}
Expand All @@ -78,6 +95,7 @@ const orchestrationHandler = async (packages, req, res) => {
})
)
let response = {}
let responseArray = []
for (const servicePackage of targetPackages) {
let body
if(servicePackage.merge == true && servicePackage.mergeKey != ''){
Expand All @@ -87,9 +105,24 @@ const orchestrationHandler = async (packages, req, res) => {
} else {
body = responses[servicePackage.basePackageName]?.result
}
response = { ...response, ...body }
response = bodyValueReplacer(response, servicePackage.responseBody)
body = bodyValueReplacer(body, servicePackage.responseBody)
responseArray.push(body)
}

// Check if custom merge options are provided in the configuration
if (mergeOption && mergeOption.basePackageName && mergeOption.functionName && mergeOption.packageName) {
// If all required fields for custom merging exist,
// call the custom merge function to handle merging logic
let result = await customMergeFunctionCaller(responseArray,mergeOption, packages)
response = result
}else {
// Fallback to default merging behavior
for(let resp of responseArray){
response = { ...response, ...resp }
}
}


if (!asyncRequestsStatues.includes(false))
res.status(200).send({
responseCode: 'OK',
Expand Down
4 changes: 2 additions & 2 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports.initializeRouter = (packages) => {

routes.forEach((route) => {
const method = httpMethods[route.type]
const { sourceRoute, orchestrated, requiresCustomHandling, targetPackages } = route
const { sourceRoute, orchestrated, requiresCustomHandling, targetPackages, mergeConfiguration } = route
const basePackageName = targetPackages[0].basePackageName
const servicePackage = packages.find((pkg) => pkg.packageMeta.basePackageName === basePackageName)

Expand All @@ -32,7 +32,7 @@ exports.initializeRouter = (packages) => {
rateLimiter,
urlencodedParser,
jsonBodyParserWithErrors,
orchestrationController.orchestrationHandler.bind(null, packages)
orchestrationController.orchestrationHandler.bind(null, packages, mergeConfiguration)
)
else if (requiresCustomHandling)
router[method](
Expand Down