Routing to fp/install
This commit is contained in:
parent
f82d2be827
commit
0189726503
|
|
@ -1,12 +1,95 @@
|
||||||
import apiClient from '../api/client';
|
import apiClient from '../api/client';
|
||||||
import { normalizeChannelsPayload } from './businessProfile';
|
import { normalizeChannelsPayload } from './businessProfile';
|
||||||
|
import { getRuntimeApplicationId, getRuntimeCompanyId } from './runtimeContext';
|
||||||
|
|
||||||
|
const FDK_BOOTSTRAP_ATTEMPT_PREFIX = 'sms_fdk_bootstrap_attempt';
|
||||||
|
const FDK_BOOTSTRAP_TTL_MS = 2 * 60 * 1000;
|
||||||
|
|
||||||
|
function getBootstrapStorageKey(companyId) {
|
||||||
|
return `${FDK_BOOTSTRAP_ATTEMPT_PREFIX}:${companyId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function readBootstrapAttempt(companyId) {
|
||||||
|
if (!companyId || typeof sessionStorage === 'undefined') return 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return Number(sessionStorage.getItem(getBootstrapStorageKey(companyId)) || 0);
|
||||||
|
} catch {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeBootstrapAttempt(companyId) {
|
||||||
|
if (!companyId || typeof sessionStorage === 'undefined') return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
sessionStorage.setItem(getBootstrapStorageKey(companyId), String(Date.now()));
|
||||||
|
} catch {
|
||||||
|
// Ignore storage failures and still attempt bootstrap redirect.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearBootstrapAttempt(companyId) {
|
||||||
|
if (!companyId || typeof sessionStorage === 'undefined') return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
sessionStorage.removeItem(getBootstrapStorageKey(companyId));
|
||||||
|
} catch {
|
||||||
|
// Ignore storage failures.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function shouldBootstrapSession(companyId) {
|
||||||
|
const lastAttempt = readBootstrapAttempt(companyId);
|
||||||
|
if (!lastAttempt) return true;
|
||||||
|
return (Date.now() - lastAttempt) > FDK_BOOTSTRAP_TTL_MS;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildFdkInstallUrl(companyId) {
|
||||||
|
if (!companyId || typeof window === 'undefined') return '';
|
||||||
|
|
||||||
|
const search = new URLSearchParams({
|
||||||
|
company_id: companyId,
|
||||||
|
redirect_path: window.location.href,
|
||||||
|
});
|
||||||
|
const applicationId = getRuntimeApplicationId();
|
||||||
|
|
||||||
|
if (applicationId) {
|
||||||
|
search.set('application_id', applicationId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return `/fp/install?${search.toString()}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createBootstrapRedirectError() {
|
||||||
|
const error = new Error('Redirecting to FDK auth bootstrap');
|
||||||
|
error.code = 'FDK_AUTH_BOOTSTRAP_REDIRECT';
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
export async function fetchActiveSalesChannels() {
|
export async function fetchActiveSalesChannels() {
|
||||||
const response = await apiClient.get('/api/platform/sales-channels');
|
const companyId = getRuntimeCompanyId();
|
||||||
return normalizeChannelsPayload(
|
|
||||||
response?.data?.salesChannels
|
try {
|
||||||
|| response?.data?.channels
|
const response = await apiClient.get('/api/platform/sales-channels');
|
||||||
|| response?.data?.items
|
clearBootstrapAttempt(companyId);
|
||||||
|| []
|
return normalizeChannelsPayload(
|
||||||
);
|
response?.data?.salesChannels
|
||||||
|
|| response?.data?.channels
|
||||||
|
|| response?.data?.items
|
||||||
|
|| []
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
const status = error?.response?.status;
|
||||||
|
if (status === 401 && companyId && shouldBootstrapSession(companyId)) {
|
||||||
|
const installUrl = buildFdkInstallUrl(companyId);
|
||||||
|
if (installUrl && typeof window !== 'undefined') {
|
||||||
|
writeBootstrapAttempt(companyId);
|
||||||
|
window.location.replace(installUrl);
|
||||||
|
throw createBootstrapRedirectError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -150,7 +150,6 @@ router.get('/sales-channels', async (req, res) => {
|
||||||
if (!req.platformClient) {
|
if (!req.platformClient) {
|
||||||
return res.status(503).json({ error: 'Platform client is unavailable for sales-channel fetch' });
|
return res.status(503).json({ error: 'Platform client is unavailable for sales-channel fetch' });
|
||||||
}
|
}
|
||||||
console.log("req.platformClient", req.platformClient);
|
|
||||||
|
|
||||||
const applications = await listAllApplications(req.platformClient);
|
const applications = await listAllApplications(req.platformClient);
|
||||||
const channels = [];
|
const channels = [];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user