besper-frontend-site-dev-main
Version:
Professional B-esper Frontend Site - Site-wide integration toolkit for full website bot deployment
188 lines (170 loc) • 5.85 kB
JavaScript
/**
* Direct URL Mapping Service for PowerPages Integration
* Maps PowerPages request.path directly to storage directories without API calls
*/
class DirectUrlMappingService {
constructor() {
// Comprehensive URL mapping from PowerPages partial URLs to storage directories
this.urlMappings = {
'/': 'home',
'Cookie-Policy': 'cookie-policy',
'Disclaimer-Page': 'disclaimer',
'Documentation-Page': 'help',
'FAQ-View': 'faq',
FAQs: 'faq',
'Implementation-Besperbot': 'implementation-guide',
'Internal-Documentation': 'internal-docs',
'Internal-Documentation---API': 'internal-docs',
'Internal-Documentation-APIM': 'internal-docs',
'Internal-Outreach': 'admin_customer_outreach_management',
'Internall-Support-Tickets': 'support-tickets',
'Knowledge-Part': 'rc-subscription',
Legal: 'legal',
'Notification-Details': 'notification-details',
Session: 'session-details',
'Technical-Insights': 'technical-insights',
Token: 'auth-token',
'Upcoming-Features': 'upcoming',
Workbench: 'workbench',
'about-us': 'about-us',
'access-denied': '403',
'account-management': 'account-management',
'account-view': 'manage-workspace',
admin: 'admin_customer_outreach_management',
'b-esper-architecture': 'demo',
'contact-us': 'contact-us',
'contact-view': 'contact-us',
'customer-testimony-template': 'case-studies',
dataprocessingagreement: 'data-processing',
documentation: 'help',
eula: 'eula',
'financial-overview': 'financial-overview',
'get-started': 'get-started',
'implementation-guide': 'implementation-guide',
impressum: 'imprint',
'internal-support-ticket-view': 'support-ticket-details',
'invite-user': 'invite-user',
'my-bots': 'bot-management-new',
notifications: 'notifications',
'page-not-found': '404',
partners: 'partners',
pricing: 'pricing',
privacystatement: 'privacy-policy',
'product-purchasing': 'product-purchasing',
profile: 'profile',
'profile-management': 'profile',
search: 'search',
'signin-aad-b2c': 'login',
'subscription-management': 'cost-pool-management',
'subscription-view': 'subscription',
support: 'help',
'support-ticket': 'support-ticket-details',
'technical-insights-view': 'technical-insights',
termsofservice: 'terms-service',
'ti-azure': 'technical-insights',
'ti-openai': 'technical-insights',
'ti-powerplatform': 'technical-insights',
'ti-stripe': 'technical-insights',
users: 'user-management-new',
workbench: 'workbench',
};
this.supportedLanguages = ['en', 'de', 'es'];
this.defaultLanguage = 'en';
}
/**
* Map PowerPages request.path to storage directory
* @param {string} requestPath - The PowerPages request.path (e.g., "/en/account-management/")
* @returns {Object} - { storageDir: string, language: string, partialUrl: string }
*/
mapRequestPath(requestPath) {
if (!requestPath) {
return {
storageDir: 'home',
language: this.defaultLanguage,
partialUrl: '/',
};
}
// Normalize path (remove leading/trailing slashes, lowercase)
const normalizedPath = requestPath.toLowerCase().replace(/^\/+|\/+$/g, '');
// Handle root path
if (!normalizedPath) {
return {
storageDir: 'home',
language: this.defaultLanguage,
partialUrl: '/',
};
}
// Extract language prefix and partial URL
const pathParts = normalizedPath.split('/');
let language = this.defaultLanguage;
let partialUrl = normalizedPath;
// Check if first part is a language code
if (
pathParts.length > 0 &&
this.supportedLanguages.includes(pathParts[0])
) {
language = pathParts[0];
partialUrl = pathParts.slice(1).join('/') || '';
}
// If no partial URL after language, use home
if (!partialUrl) {
return { storageDir: 'home', language, partialUrl: '/' };
}
// Look up storage directory from URL mapping
const storageDir =
this.urlMappings[partialUrl] || this.urlMappings[`/${partialUrl}`];
if (storageDir) {
return { storageDir, language, partialUrl };
}
// Fallback: use partial URL as storage directory (sanitized)
const fallbackStorageDir = partialUrl.replace(/[^a-z0-9-]/g, '-');
console.warn(
`[DirectUrlMapping] [WARN] No mapping found for "${partialUrl}", using fallback: ${fallbackStorageDir}`
);
return { storageDir: fallbackStorageDir, language, partialUrl };
}
/**
* Get all available storage directories
* @returns {string[]} - Array of storage directory names
*/
getAvailableStorageDirectories() {
return [...new Set(Object.values(this.urlMappings))];
}
/**
* Get all URL mappings for debugging
* @returns {Object} - Complete URL mappings object
*/
getAllMappings() {
return { ...this.urlMappings };
}
/**
* Add or update a URL mapping
* @param {string} partialUrl - PowerPages partial URL
* @param {string} storageDir - Storage directory name
*/
addMapping(partialUrl, storageDir) {
this.urlMappings[partialUrl] = storageDir;
}
/**
* Test URL mapping with various inputs
* @param {string[]} testPaths - Array of test paths
* @returns {Object[]} - Array of mapping results
*/
testMappings(
testPaths = [
'/en/account-management/',
'/de/profile/',
'/es/pricing/',
'/workbench/',
'/support/',
'/',
'/invalid-url/',
]
) {
return testPaths.map(path => ({
input: path,
result: this.mapRequestPath(path),
}));
}
}
export default DirectUrlMappingService;