From 74c0ce96e5f5364f919428bd7f8af9959558935f Mon Sep 17 00:00:00 2001 From: Vesa Luusua Date: Sun, 28 Jul 2019 20:26:57 +0200 Subject: [PATCH] Add ensureStripeCustomer and ensurePaymentMethodCard --- src/util/data.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/util/data.js b/src/util/data.js index cd976cc6..b508e77f 100644 --- a/src/util/data.js +++ b/src/util/data.js @@ -244,6 +244,37 @@ export const ensureAvailabilityException = availabilityException => { return { ...empty, ...availabilityException }; }; +/** + * Create shell objects to ensure that attributes etc. exists. + * + * @param {Object} stripeCustomer entity from API, which is to be ensured against null values + */ +export const ensureStripeCustomer = stripeCustomer => { + const empty = { id: null, type: 'stripeCustomer', attributes: {} }; + return { ...empty, ...stripeCustomer }; +}; + +/** + * Create shell objects to ensure that attributes etc. exists. + * + * @param {Object} stripeCustomer entity from API, which is to be ensured against null values + */ +export const ensurePaymentMethodCard = stripePaymentMethod => { + const empty = { + id: null, + type: 'stripePaymentMethod', + attributes: { type: 'stripe-payment-method/card', card: {} }, + }; + const cardPaymentMethod = { ...empty, ...stripePaymentMethod }; + + if (cardPaymentMethod.attributes.type !== 'stripe-payment-method/card') { + throw new Error(`'ensurePaymentMethodCard' got payment method with wrong type. + 'stripe-payment-method/card' was expected, received ${cardPaymentMethod.attributes.type}`); + } + + return cardPaymentMethod; +}; + /** * Get the display name of the given user as string. This function handles * missing data (e.g. when the user object is still being downloaded),