From e35c422e8967a55dba6d32dde65d4790ce2b0756 Mon Sep 17 00:00:00 2001 From: eglehelms Date: Wed, 30 Nov 2022 22:06:59 +0100 Subject: [PATCH] faulty checks --- src/api/index.ts | 21 ++++++++++---- .../internal/views/carriers/form.tsx | 29 ++++++++++--------- .../internal/views/speech-services/form.tsx | 13 +++++---- .../internal/views/speech-services/index.tsx | 5 ++-- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index 79e8141..a1f1d55 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -290,11 +290,22 @@ export const postPhoneNumber = (payload: Partial) => { ); }; -export const postCarrier = (sid: string, payload: Partial) => { - return postFetch>( - `${API_SERVICE_PROVIDERS}/${sid}/VoipCarriers/`, - payload - ); +export const postCarrier = ( + user: UserData, + sid: string, + payload: Partial +) => { + if (user.scope === USER_ACCOUNT) { + return postFetch>( + `${API_ACCOUNTS}/${user.account_sid}/VoipCarriers/`, + payload + ); + } else { + return postFetch>( + `${API_SERVICE_PROVIDERS}/${sid}/VoipCarriers/`, + payload + ); + } }; export const postPredefinedCarrierTemplate = ( diff --git a/src/containers/internal/views/carriers/form.tsx b/src/containers/internal/views/carriers/form.tsx index cba93d6..4c7c7b9 100644 --- a/src/containers/internal/views/carriers/form.tsx +++ b/src/containers/internal/views/carriers/form.tsx @@ -493,7 +493,7 @@ export const CarrierForm = ({ smpp_inbound_password: smppInboundPass.trim() || null, }; - if (carrier && carrier.data) { + if (carrier && carrier.data && user) { putCarrier( currentServiceProvider.service_provider_sid, carrier.data.voip_carrier_sid, @@ -512,20 +512,21 @@ export const CarrierForm = ({ toastError(error.msg); }); } else { - postCarrier(currentServiceProvider.service_provider_sid, { - ...carrierPayload, - service_provider_sid: currentServiceProvider.service_provider_sid, - }) - .then(({ json }) => { - handleSipGatewayPutPost(json.sid); - handleSmppGatewayPutPost(json.sid); - - toastSuccess("Carrier created successfully"); - navigate(`${ROUTE_INTERNAL_CARRIERS}/${json.sid}/edit`); + if (user) + postCarrier(user, currentServiceProvider.service_provider_sid, { + ...carrierPayload, + service_provider_sid: currentServiceProvider.service_provider_sid, }) - .catch((error) => { - toastError(error.msg); - }); + .then(({ json }) => { + handleSipGatewayPutPost(json.sid); + handleSmppGatewayPutPost(json.sid); + + toastSuccess("Carrier created successfully"); + navigate(`${ROUTE_INTERNAL_CARRIERS}/${json.sid}/edit`); + }) + .catch((error) => { + toastError(error.msg); + }); } } }; diff --git a/src/containers/internal/views/speech-services/form.tsx b/src/containers/internal/views/speech-services/form.tsx index fb04591..5cda4cd 100644 --- a/src/containers/internal/views/speech-services/form.tsx +++ b/src/containers/internal/views/speech-services/form.tsx @@ -95,6 +95,13 @@ export const SpeechServiceForm = ({ credential }: SpeechServiceFormProps) => { const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); + if (user?.scope === USER_ACCOUNT && user.account_sid === accountSid) { + toastError( + "You do not have permissions to make changes to these Speech Credentials" + ); + return; + } + if (currentServiceProvider) { const payload: Partial = { vendor, @@ -121,12 +128,6 @@ export const SpeechServiceForm = ({ credential }: SpeechServiceFormProps) => { }; if (credential && credential.data && user) { - if (user?.scope === USER_ACCOUNT && user.account_sid !== accountSid) { - toastError( - "You do not have permissions to make changes to these Speech Credentials" - ); - return; - } /** The backend API returns obscured secrets now so we need to make sure we don't send them back */ /** Fields not sent back via :PUT are `service_key`, `access_key_id`, `secret_access_key` and `api_key` */ putSpeechService( diff --git a/src/containers/internal/views/speech-services/index.tsx b/src/containers/internal/views/speech-services/index.tsx index d6873a5..871d2c6 100644 --- a/src/containers/internal/views/speech-services/index.tsx +++ b/src/containers/internal/views/speech-services/index.tsx @@ -40,7 +40,7 @@ export const SpeechServices = () => { : credentials.account_sid === null ) : []; - }, [accountSid, accounts]); + }, [accountSid, accounts, credentials]); const filteredCredentials = useFilteredResults( filter, @@ -95,11 +95,10 @@ export const SpeechServices = () => { }; useEffect(() => { - if ((user && user?.scope === USER_ACCOUNT) || accountSid) { + if (accountSid) { getSpeechCredentials( `${API_ACCOUNTS}/${user?.account_sid || accountSid}/SpeechCredentials` ); - console.log(credentials); } else { if (currentServiceProvider) { getSpeechCredentials(