post speech to a different path if scoped user

This commit is contained in:
eglehelms
2022-11-30 17:32:34 +01:00
parent 92afbfb388
commit c2c4a28868
7 changed files with 126 additions and 58 deletions
+35 -8
View File
@@ -17,6 +17,7 @@ import {
API_SMPP_GATEWAY,
API_SIP_GATEWAY,
API_PASSWORD_SETTINGS,
USER_ACCOUNT,
} from "./constants";
import { ROUTE_LOGIN } from "src/router/routes";
import {
@@ -58,6 +59,7 @@ import type {
LimitCategories,
PasswordSettings,
} from "./types";
import { UserData } from "../store/types";
import { StatusCodes } from "./types";
/** Wrap all requests to normalize response handling */
@@ -257,13 +259,21 @@ export const postApplication = (payload: Partial<Application>) => {
};
export const postSpeechService = (
user: UserData,
sid: string,
payload: Partial<SpeechCredential>
) => {
return postFetch<SidResponse, Partial<SpeechCredential>>(
`${API_SERVICE_PROVIDERS}/${sid}/SpeechCredentials`,
payload
);
if (user.scope === USER_ACCOUNT) {
return postFetch<SidResponse, Partial<SpeechCredential>>(
`${API_ACCOUNTS}/${user.account_sid}/SpeechCredentials`,
payload
);
} else {
return postFetch<SidResponse, Partial<SpeechCredential>>(
`${API_SERVICE_PROVIDERS}/${sid}/SpeechCredentials`,
payload
);
}
};
export const postMsTeamsTentant = (payload: Partial<MSTeamsTenant>) => {
@@ -296,6 +306,15 @@ export const postPredefinedCarrierTemplate = (
);
};
export const postPredefinedCarrierTemplateAccount = (
accountSid: string,
predefinedCarrierSid: string
) => {
return postFetch<SidResponse>(
`${API_BASE_URL}/Accounts/${accountSid}/PredefinedCarriers/${predefinedCarrierSid}`
);
};
export const postSipGateway = (payload: Partial<SipGateway>) => {
return postFetch<SidResponse, Partial<SipGateway>>(API_SIP_GATEWAY, payload);
};
@@ -364,14 +383,22 @@ export const putApplication = (sid: string, payload: Partial<Application>) => {
};
export const putSpeechService = (
user: UserData,
sid1: string,
sid2: string,
payload: Partial<SpeechCredential>
) => {
return putFetch<EmptyResponse, Partial<SpeechCredential>>(
`${API_SERVICE_PROVIDERS}/${sid1}/SpeechCredentials/${sid2}`,
payload
);
if (user.scope === USER_ACCOUNT) {
return putFetch<EmptyResponse, Partial<SpeechCredential>>(
`${API_ACCOUNTS}/${user.account_sid}/SpeechCredentials/${sid2}`,
payload
);
} else {
return putFetch<EmptyResponse, Partial<SpeechCredential>>(
`${API_SERVICE_PROVIDERS}/${sid1}/SpeechCredentials/${sid2}`,
payload
);
}
};
export const putMsTeamsTenant = (
+21 -10
View File
@@ -14,6 +14,7 @@ import {
useApiData,
useServiceProviderData,
postPredefinedCarrierTemplate,
postPredefinedCarrierTemplateAccount,
} from "src/api";
import {
DEFAULT_SIP_GATEWAY,
@@ -536,16 +537,26 @@ export const CarrierForm = ({
)?.predefined_carrier_sid;
if (currentServiceProvider && predefinedCarrierSid) {
postPredefinedCarrierTemplate(
currentServiceProvider.service_provider_sid,
predefinedCarrierSid
)
.then(({ json }) => {
navigate(`${ROUTE_INTERNAL_CARRIERS}/${json.sid}/edit`);
})
.catch((error) => {
toastError(error.msg);
});
if (user?.scope === USER_ACCOUNT) {
postPredefinedCarrierTemplateAccount(accountSid, predefinedCarrierSid)
.then(({ json }) => {
navigate(`${ROUTE_INTERNAL_CARRIERS}/${json.sid}/edit`);
})
.catch((error) => {
toastError(error.msg);
});
} else {
postPredefinedCarrierTemplate(
currentServiceProvider.service_provider_sid,
predefinedCarrierSid
)
.then(({ json }) => {
navigate(`${ROUTE_INTERNAL_CARRIERS}/${json.sid}/edit`);
})
.catch((error) => {
toastError(error.msg);
});
}
}
}
}, [predefinedName]);
@@ -2,17 +2,25 @@ import React, { useEffect } from "react";
import { H1 } from "jambonz-ui";
import { useParams } from "react-router-dom";
import { useServiceProviderData } from "src/api";
import { toastError } from "src/store";
import { useApiData, useServiceProviderData } from "src/api";
import { toastError, useSelectState } from "src/store";
import { SpeechServiceForm } from "./form";
import type { SpeechCredential } from "src/api/types";
import { USER_ACCOUNT } from "src/api/constants";
export const EditSpeechService = () => {
const params = useParams();
const [data, refetch, error] = useServiceProviderData<SpeechCredential>(
`SpeechCredentials/${params.speech_credential_sid}`
);
const user = useSelectState("user");
const [data, refetch, error] =
user && user.scope !== USER_ACCOUNT
? useServiceProviderData<SpeechCredential>(
`SpeechCredentials/${params.speech_credential_sid}`
)
: useApiData<SpeechCredential>(
`Accounts/${user?.account_sid}/SpeechCredentials/${params.speech_credential_sid}`
);
useEffect(() => {
if (error) {
@@ -115,7 +115,7 @@ export const SpeechServiceForm = ({ credential }: SpeechServiceFormProps) => {
}),
};
if (credential && credential.data) {
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"
@@ -125,6 +125,7 @@ export const SpeechServiceForm = ({ credential }: SpeechServiceFormProps) => {
/** 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(
user,
currentServiceProvider.service_provider_sid,
credential.data.speech_credential_sid,
payload
@@ -139,28 +140,31 @@ export const SpeechServiceForm = ({ credential }: SpeechServiceFormProps) => {
toastError(error.msg);
});
} else {
postSpeechService(currentServiceProvider.service_provider_sid, {
...payload,
service_key:
vendor === VENDOR_GOOGLE ? JSON.stringify(googleServiceKey) : null,
access_key_id: vendor === VENDOR_AWS ? accessKeyId : null,
secret_access_key: vendor === VENDOR_AWS ? secretAccessKey : null,
api_key:
vendor === VENDOR_MICROSOFT ||
vendor === VENDOR_WELLSAID ||
vendor === VENDOR_DEEPGRAM
? apiKey
: null,
client_id: vendor === VENDOR_NUANCE ? clientId : null,
secret: vendor === VENDOR_NUANCE ? secretKey : null,
})
.then(({ json }) => {
toastSuccess("Speech credential created successfully");
navigate(`${ROUTE_INTERNAL_SPEECH}/${json.sid}/edit`);
if (user)
postSpeechService(user, currentServiceProvider.service_provider_sid, {
...payload,
service_key:
vendor === VENDOR_GOOGLE
? JSON.stringify(googleServiceKey)
: null,
access_key_id: vendor === VENDOR_AWS ? accessKeyId : null,
secret_access_key: vendor === VENDOR_AWS ? secretAccessKey : null,
api_key:
vendor === VENDOR_MICROSOFT ||
vendor === VENDOR_WELLSAID ||
vendor === VENDOR_DEEPGRAM
? apiKey
: null,
client_id: vendor === VENDOR_NUANCE ? clientId : null,
secret: vendor === VENDOR_NUANCE ? secretKey : null,
})
.catch((error) => {
toastError(error.msg);
});
.then(({ json }) => {
toastSuccess("Speech credential created successfully");
navigate(`${ROUTE_INTERNAL_SPEECH}/${json.sid}/edit`);
})
.catch((error) => {
toastError(error.msg);
});
}
}
};
@@ -49,9 +49,11 @@ export const SpeechServices = () => {
credential.speech_credential_sid
)
.then(() => {
if (accountSid) {
if ((user && user?.scope === USER_ACCOUNT) || accountSid) {
getSpeechCredentials(
`${API_ACCOUNTS}/${accountSid}/SpeechCredentials`
`${API_ACCOUNTS}/${
user?.account_sid || accountSid
}/SpeechCredentials`
);
} else {
getSpeechCredentials(
@@ -72,14 +74,18 @@ export const SpeechServices = () => {
};
useEffect(() => {
if (accountSid) {
getSpeechCredentials(`${API_ACCOUNTS}/${accountSid}/SpeechCredentials`);
} else if (currentServiceProvider) {
if ((user && user?.scope === USER_ACCOUNT) || accountSid) {
getSpeechCredentials(
`${API_SERVICE_PROVIDERS}/${currentServiceProvider.service_provider_sid}/SpeechCredentials`
`${API_ACCOUNTS}/${user?.account_sid || accountSid}/SpeechCredentials`
);
} else {
if (currentServiceProvider) {
getSpeechCredentials(
`${API_SERVICE_PROVIDERS}/${currentServiceProvider.service_provider_sid}/SpeechCredentials`
);
}
}
}, [accountSid, currentServiceProvider]);
}, [user, accountSid, currentServiceProvider]);
return (
<>
@@ -1,12 +1,13 @@
import React, { useState } from "react";
import { MS } from "jambonz-ui";
import { CRED_NOT_TESTED, CRED_OK } from "src/api/constants";
import { CRED_NOT_TESTED, CRED_OK, USER_ACCOUNT } from "src/api/constants";
import { Icons, Spinner } from "src/components";
import { useServiceProviderData } from "src/api";
import { useApiData, useServiceProviderData } from "src/api";
import { getStatus, getReason } from "./utils";
import type { SpeechCredential, CredentialTestResult } from "src/api/types";
import { useSelectState } from "src/store";
type CredentialStatusProps = {
cred: SpeechCredential;
@@ -17,10 +18,15 @@ export const CredentialStatus = ({
cred,
showSummary = false,
}: CredentialStatusProps) => {
const user = useSelectState("user");
const [testResult, testRefetch, testError] =
useServiceProviderData<CredentialTestResult>(
`SpeechCredentials/${cred.speech_credential_sid}/test`
);
user && user.scope !== USER_ACCOUNT
? useServiceProviderData<CredentialTestResult>(
`SpeechCredentials/${cred.speech_credential_sid}/test`
)
: useApiData<CredentialTestResult>(
`Accounts/${user?.account_sid}/SpeechCredentials/${cred.speech_credential_sid}/test`
);
const notTestedTxt =
"In order to test your credentials you need to enable TTS/STT.";
+7 -1
View File
@@ -10,6 +10,7 @@ import {
ROUTE_LOGIN,
ROUTE_CREATE_PASSWORD,
ROUTE_INTERNAL_ACCOUNTS,
ROUTE_INTERNAL_APPLICATIONS,
} from "./routes";
import {
SESS_OLD_PASSWORD,
@@ -20,6 +21,7 @@ import {
} from "src/constants";
import type { UserLogin } from "src/api/types";
import { USER_ACCOUNT } from "src/api/constants";
interface SignIn {
(username: string, password: string): Promise<UserLogin>;
@@ -101,7 +103,11 @@ export const useProvideAuth = (): AuthStateContext => {
sessionStorage.setItem(SESS_OLD_PASSWORD, password);
navigate(ROUTE_CREATE_PASSWORD);
} else {
navigate(ROUTE_INTERNAL_ACCOUNTS);
navigate(
parseJwt(token).scope !== USER_ACCOUNT
? ROUTE_INTERNAL_ACCOUNTS
: ROUTE_INTERNAL_APPLICATIONS
);
}
resolve(response.json);