From 10bbfd6285fe8a5fc018dca90d332354cb11f216 Mon Sep 17 00:00:00 2001 From: Terry Sutton Date: Thu, 29 Sep 2022 22:04:41 -0230 Subject: [PATCH] Sanitize the VAT input value on submit for EU VAT ids --- .../BillingSettings/TaxID/TaxID.tsx | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/studio/components/interfaces/Organization/BillingSettings/TaxID/TaxID.tsx b/studio/components/interfaces/Organization/BillingSettings/TaxID/TaxID.tsx index e60fed693cb..2f7858f7d73 100644 --- a/studio/components/interfaces/Organization/BillingSettings/TaxID/TaxID.tsx +++ b/studio/components/interfaces/Organization/BillingSettings/TaxID/TaxID.tsx @@ -100,11 +100,12 @@ const TaxID: FC = ({ loading, taxIds, onTaxIdsUpdated }) => { const newIds = await Promise.all( taxIdValues.map(async (taxId: any) => { + const sanitizedID = sanitizeTaxID(taxId) const result = await post(`${API_URL}/organizations/${slug}/tax-ids`, { - type: taxId.type, - value: taxId.value, + type: sanitizedID.type, + value: sanitizedID.value, }) - return { id: taxId.id, result } + return { id: sanitizedID.id, result } }) ) @@ -132,12 +133,33 @@ const TaxID: FC = ({ loading, taxIds, onTaxIdsUpdated }) => { } } + /** + * Sanitize EU VAT ids so they get prepended with the country code + * + * Ex: Belgium's VAT ID should be BE0123456789 not 0123456789 + * Only EU VAT ids are affected for now + */ + const sanitizeTaxID = (taxId: any) => { + let newID = taxId + + // The first 2 letters of the country name (ex: "BE" for Belgium) + const countryCodePrepend = taxId.name.slice(0, 2) + + // if the value doesn't start with the country code, prepend them + if (taxId.type === 'eu_vat' && !taxId.value.startsWith(countryCodePrepend)) { + newID.value = `${countryCodePrepend}${taxId.value}` + } + + return newID + } + return (

Tax ID

- If you would like to include specific tax ID(s) to your invoices + If you would like to include specific tax ID(s) to your invoices.
+ Make sure the tax ID looks exactly like the placeholder text.

@@ -150,7 +172,7 @@ const TaxID: FC = ({ loading, taxIds, onTaxIdsUpdated }) => { loading={loading} footer={ !loading && ( -
+
{!canUpdateTaxIds ? (

You need additional permissions to update this organization's tax IDs @@ -183,16 +205,17 @@ const TaxID: FC = ({ loading, taxIds, onTaxIdsUpdated }) => { > {loading && taxIdValues.length === 0 ? (

-
-
-
+
+
+
) : ( - + {taxIdValues.length >= 1 ? (
{taxIdValues.map((taxId: any, idx: number) => { const selectedTaxId = TAX_IDS.find((option) => option.name === taxId.name) + //console.log('selectedTaxId', selectedTaxId?.code) return (
@@ -230,7 +253,7 @@ const TaxID: FC = ({ loading, taxIds, onTaxIdsUpdated }) => { )} {canUpdateTaxIds && (
onAddNewTaxId()} >