Skip to content

Instantly share code, notes, and snippets.

@johndowns
Last active January 28, 2018 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johndowns/72854651005e628ccd214766640ecfe2 to your computer and use it in GitHub Desktop.
Save johndowns/72854651005e628ccd214766640ecfe2 to your computer and use it in GitHub Desktop.
function validateOrder() {
// capture the contextual variables we'll need
var request = getContext().getRequest();
// run the core logic of the trigger
validateOrderImpl(request);
}
function validateOrderImpl(request: IRequest) {
if (request.getOperationType() == "Delete") {
// this is not a 'Create' or 'Replace' operation, so we can ignore it in this trigger
return;
}
var document = request.getBody() as BaseDocument;
if (document.type != DocumentTypes.Order) {
// this is not an order, so we can ignore it in this trigger
return;
}
var orderDocument = <OrderDocument>document;
if (orderDocument.customerId != undefined) {
orderDocument.customer = {
id: orderDocument.customerId
};
orderDocument.customerId = undefined;
}
else if (orderDocument.customer == undefined || orderDocument.customer.id == undefined) {
throw new Error("Customer ID is missing. Customer ID must be provided within the customer.id property.")
}
// otherwise, this document is OK to leave as-is
}
const enum DocumentTypes {
Order = "order"
}
interface BaseDocument {
id: string,
type: DocumentTypes
}
interface OrderDocument extends BaseDocument {
customerId: string,
customer: {
id: string
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment