Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@johndowns
Created January 2, 2018 20:08
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/519fb48fd5d1afb879fd3dcb4e81ce34 to your computer and use it in GitHub Desktop.
Save johndowns/519fb48fd5d1afb879fd3dcb4e81ce34 to your computer and use it in GitHub Desktop.
function getCustomerId(document: OrderDocument): string {
// try to get the customer ID from the new format first
if (document.customer != undefined && document.customer.id != undefined) {
return document.customer.id;
}
// if that didn't work, try to get the customer ID from the old format
if (document.customerId != undefined) {
return document.customerId;
}
// if neither are present, that is an error
throw new Error("Document with id " + document.id + " does not contain customer ID in recognised format.");
}
interface OrderDocument {
id: string,
customerId: string,
customer: {
id: string
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment