fixed cannot saved auth trunk (#573)

This commit is contained in:
Hoan Luu Huu
2025-10-24 18:22:36 +07:00
committed by GitHub
parent 53d0c0b510
commit 19742ab67e

View File

@@ -142,6 +142,9 @@ export const CarrierForm = ({
const [tmpInboundGateways, setTmpInboundGateways] = useState<SipGateway[]>( const [tmpInboundGateways, setTmpInboundGateways] = useState<SipGateway[]>(
[], [],
); );
const [tmpOutboundGateways, setTmpOutboundGateways] = useState<SipGateway[]>(
[],
);
const [smppGateways, setSmppGateways] = useState<SmppGateway[]>([ const [smppGateways, setSmppGateways] = useState<SmppGateway[]>([
{ {
...DEFAULT_SMPP_GATEWAY, ...DEFAULT_SMPP_GATEWAY,
@@ -460,33 +463,24 @@ export const CarrierForm = ({
}; };
const getSipValidation = () => { const getSipValidation = () => {
// Check if there are any gateways at all
if (sipInboundGateways.length === 0 && sipOutboundGateways.length === 0) { if (sipInboundGateways.length === 0 && sipOutboundGateways.length === 0) {
// For Static IP Whitelist, prioritize inbound tab since it requires inbound gateways
if (trunkType === "static_ip") { if (trunkType === "static_ip") {
setActiveTab("inbound"); setActiveTab("inbound");
return "Static IP Whitelist trunk type requires at least one inbound gateway."; return "Static IP Whitelist trunk type requires at least one inbound gateway.";
} else if (trunkType === "reg" || trunkType === "auth") { } else if (trunkType === "reg") {
// Auth and Registration trunk needs at least one outbound gateway for routing
setActiveTab("outbound"); setActiveTab("outbound");
return "You must provide at least one outbound SIP Gateway."; return "Registration trunk type requires at least one outbound gateway.";
} }
// Auth trunk doesn't require any gateways - skip validation
} }
// Validate Static IP Whitelist trunk type requires at least 1 inbound gateway
if (trunkType === "static_ip" && sipInboundGateways.length < 1) { if (trunkType === "static_ip" && sipInboundGateways.length < 1) {
setActiveTab("inbound"); setActiveTab("inbound");
return "Static IP Whitelist trunk type requires at least one inbound gateway."; return "Static IP Whitelist trunk type requires at least one inbound gateway.";
} }
// Validate Auth and Registration Trunk require at least 1 outbound gateway if (trunkType === "reg" && sipOutboundGateways.length < 1) {
if (
(trunkType === "auth" || trunkType === "reg") &&
sipOutboundGateways.length < 1
) {
setActiveTab("outbound"); setActiveTab("outbound");
return "Auth and Registration trunk types require at least one outbound gateway."; return "Registration trunk type requires at least one outbound gateway.";
} }
// Validate Auth Trunk credentials // Validate Auth Trunk credentials
@@ -663,13 +657,12 @@ export const CarrierForm = ({
}; };
const handleActiveTab = () => { const handleActiveTab = () => {
/** When to switch to `sip` tab */
// For auth and reg trunk types, only check outbound gateways for validation
const gatewaysToCheck = const gatewaysToCheck =
trunkType === "auth" || trunkType === "reg" trunkType === "auth"
? sipOutboundGateways ? []
: [...sipInboundGateways, ...sipOutboundGateways]; : trunkType === "reg"
? sipOutboundGateways
: [...sipInboundGateways, ...sipOutboundGateways];
const emptySipIp = gatewaysToCheck.find((g) => g.ipv4.trim() === ""); const emptySipIp = gatewaysToCheck.find((g) => g.ipv4.trim() === "");
const invalidSipPort = gatewaysToCheck.find( const invalidSipPort = gatewaysToCheck.find(
(g) => hasValue(g.port) && !isValidPort(g.port), (g) => hasValue(g.port) && !isValidPort(g.port),
@@ -726,7 +719,6 @@ export const CarrierForm = ({
const sipGatewayValidation = getSipValidation(); const sipGatewayValidation = getSipValidation();
if (sipGatewayValidation) { if (sipGatewayValidation) {
// For static_ip validation errors, show only in inbound tab
if ( if (
sipGatewayValidation === sipGatewayValidation ===
"Static IP Whitelist trunk type requires at least one inbound gateway." "Static IP Whitelist trunk type requires at least one inbound gateway."
@@ -736,19 +728,13 @@ export const CarrierForm = ({
sipGatewayValidation === sipGatewayValidation ===
"Auth Trunk requires both username and password credentials." "Auth Trunk requires both username and password credentials."
) { ) {
// Show auth credentials validation error only in inbound tab
setSipInboundMessage(sipGatewayValidation); setSipInboundMessage(sipGatewayValidation);
} else if ( } else if (
sipGatewayValidation === sipGatewayValidation ===
"You must provide at least one outbound SIP Gateway." || "Registration trunk type requires at least one outbound gateway."
sipGatewayValidation ===
"Auth and Registration trunk types require at least one outbound gateway."
) { ) {
// Show in outbound tab when outbound gateways are required
setSipOutboundMessage(sipGatewayValidation); setSipOutboundMessage(sipGatewayValidation);
} else { } else {
// For other validation errors, the validation function already set the correct tab
// Show in both tabs to ensure user sees the error
setSipInboundMessage(sipGatewayValidation); setSipInboundMessage(sipGatewayValidation);
setSipOutboundMessage(sipGatewayValidation); setSipOutboundMessage(sipGatewayValidation);
} }
@@ -1070,13 +1056,11 @@ export const CarrierForm = ({
const prevTrunkType = trunkType; const prevTrunkType = trunkType;
setTrunkType(newTrunkType); setTrunkType(newTrunkType);
// Clear auth credentials when switching away from auth trunk
if (newTrunkType !== "auth") { if (newTrunkType !== "auth") {
setInboundAuthUsername(""); setInboundAuthUsername("");
setInboundAuthPassword(""); setInboundAuthPassword("");
} }
// Auto-check authentication and register for Registration Trunk
if (newTrunkType === "reg") { if (newTrunkType === "reg") {
setInitialRegister(true); setInitialRegister(true);
setInitialSipRegister(true); setInitialSipRegister(true);
@@ -1087,26 +1071,47 @@ export const CarrierForm = ({
setSipRegister(false); setSipRegister(false);
} }
// Handle inbound gateway management for auth and reg trunk types
if ( if (
(newTrunkType === "auth" || newTrunkType === "reg") && (newTrunkType === "auth" || newTrunkType === "reg") &&
prevTrunkType === "static_ip" prevTrunkType === "static_ip"
) { ) {
// Store current inbound gateways and clear them
setTmpInboundGateways(sipInboundGateways); setTmpInboundGateways(sipInboundGateways);
setSipInboundGateways([]); setSipInboundGateways([]);
} else if ( } else if (
newTrunkType === "static_ip" && newTrunkType === "static_ip" &&
(prevTrunkType === "auth" || prevTrunkType === "reg") (prevTrunkType === "auth" || prevTrunkType === "reg")
) { ) {
// Restore inbound gateways from temp storage
setSipInboundGateways(tmpInboundGateways); setSipInboundGateways(tmpInboundGateways);
setTmpInboundGateways([]); setTmpInboundGateways([]);
} }
// Ensure minimum 1 outbound gateway for auth and reg types if (newTrunkType === "auth" && prevTrunkType === "reg") {
if ( setTmpOutboundGateways(sipOutboundGateways);
(newTrunkType === "auth" || newTrunkType === "reg") && setSipOutboundGateways([]);
} else if (
newTrunkType === "auth" &&
prevTrunkType === "static_ip"
) {
setTmpOutboundGateways(sipOutboundGateways);
setSipOutboundGateways([]);
} else if (
newTrunkType === "reg" &&
prevTrunkType === "auth"
) {
if (tmpOutboundGateways.length > 0) {
setSipOutboundGateways(tmpOutboundGateways);
setTmpOutboundGateways([]);
} else if (sipOutboundGateways.length === 0) {
setSipOutboundGateways([
{
...DEFAULT_SIP_INBOUND_GATEWAY,
inbound: 0,
outbound: 1,
},
]);
}
} else if (
newTrunkType === "reg" &&
sipOutboundGateways.length === 0 sipOutboundGateways.length === 0
) { ) {
setSipOutboundGateways([ setSipOutboundGateways([