-
-
Save johndowns/eca7c5b5c9d749d743af828c2f0ec466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getGroupedOrders(productIds) { | |
// capture the contextual variables we'll need | |
var collection = getContext().getCollection(); | |
var response = getContext().getResponse(); | |
// run the core logic of the stored procedure | |
var output = getGroupedOrdersImpl(productIds, collection); | |
response.setBody(output); | |
} | |
function getGroupedOrdersImpl(productIds, collection) { | |
var outputArray = []; | |
productIds.forEach(productId => { | |
// set up a query to find the customers who ordered this product ID | |
var query = { | |
query: "SELECT VALUE udf.getCustomerId(c) FROM c WHERE c.type = \'order\' AND ARRAY_CONTAINS(c.items, { productId: @productId }, true)", | |
parameters: [ | |
{ name: "@productId", value: productId } | |
] | |
}; | |
var queryCallbackFunction = (error, results, options) => { | |
if (error) { | |
// the query was accepted and processed, but something went wrong | |
throw new Error("Error in query callback: " + error.body); | |
} | |
// we successfully received the customer IDs, so we can add them to the output array | |
outputArray.push({ | |
productId: productId, | |
customerIds: results | |
}); | |
}; | |
var queryAccepted = collection.queryDocuments(collection.getSelfLink(), query, queryCallbackFunction); | |
if (!queryAccepted) { | |
// the query wasn't accepted - this is likely because our stored procedure is running out of time in which to execute | |
throw new Error("Query was not accepted for product ID " + productId); | |
} | |
}); | |
// set the stored procedure's response body | |
return outputArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment