bolt-templates-sms-extensio.../client/src/utils/businessProfile.js

172 lines
4.7 KiB
JavaScript

function normalizeList(value) {
return Array.isArray(value) ? value.filter(Boolean) : [];
}
function normalizeText(value) {
return typeof value === 'string' ? value.trim() : '';
}
function firstNonEmptyText(...values) {
for (const value of values) {
if (typeof value === 'string' && value.trim()) return value.trim();
}
return '';
}
function extractDomainName(domain) {
if (!domain) return '';
if (typeof domain === 'string') return normalizeText(domain);
if (typeof domain === 'object') {
return firstNonEmptyText(domain.name, domain.domain_url, domain.url, domain.host);
}
return '';
}
function rankDomain(domain) {
if (!domain || typeof domain !== 'object') return 0;
let score = 0;
if (domain.is_primary) score += 4;
if (domain.verified) score += 2;
if (!domain.is_shortlink) score += 1;
return score;
}
function pickPreferredDomain(...sources) {
const candidates = [];
for (const source of sources) {
if (!source) continue;
if (Array.isArray(source)) {
for (const item of source) {
const name = extractDomainName(item);
if (name) candidates.push({ raw: item, name });
}
continue;
}
const name = extractDomainName(source);
if (name) candidates.push({ raw: source, name });
}
if (!candidates.length) return '';
candidates.sort((left, right) => rankDomain(right.raw) - rankDomain(left.raw));
return candidates[0].name;
}
function normalizeWebsiteUrl(value) {
const rawValue = normalizeText(value);
if (!rawValue) return '';
const candidate = /^https?:\/\//i.test(rawValue) ? rawValue : `https://${rawValue}`;
try {
return new URL(candidate).toString().replace(/\/$/, '');
} catch {
return '';
}
}
function normalizeChannel(channel = {}) {
const domain = pickPreferredDomain(
channel.domain,
channel.domains,
channel.website,
channel.website_url,
channel.url
);
const id = String(channel?.salesChannelId || channel?.applicationId || channel?.application_id || channel?.id || channel?._id || '').trim();
return {
...channel,
id,
salesChannelId: id,
applicationId: id,
name: firstNonEmptyText(channel.name, channel.display_name, channel.title, channel.slug),
domain,
websiteUrl: normalizeWebsiteUrl(channel.websiteUrl || channel.website_url || channel.url || domain),
isActive: channel.is_active !== false && channel.isActive !== false,
logoUrl: firstNonEmptyText(
channel.logoUrl,
channel.logo?.secure_url,
channel.logo?.url,
channel.logo,
channel.icon?.secure_url,
channel.icon?.url,
channel.icon,
channel.favicon?.secure_url,
channel.favicon?.url
),
};
}
export function normalizeChannelsPayload(data) {
const channels = (
Array.isArray(data) ? data
: Array.isArray(data?.salesChannels) ? data.salesChannels
: Array.isArray(data?.channels) ? data.channels
: Array.isArray(data?.items) ? data.items
: Array.isArray(data?.applications) ? data.applications
: []
);
return channels.map(normalizeChannel);
}
export function getChannelId(channel) {
return channel?.salesChannelId || channel?.id || channel?._id || '';
}
export function isChannelActive(channel) {
const status = String(channel?.status || channel?.state || '').toLowerCase();
if (typeof channel?.isActive === 'boolean') return channel.isActive;
if (typeof channel?.active === 'boolean') return channel.active;
if (typeof channel?.enabled === 'boolean') return channel.enabled;
if (status) return ['active', 'enabled', 'live', 'connected'].includes(status);
return true;
}
export function getBusinessName(entity) {
return entity?.brandName || entity?.name || entity?.title || 'Business';
}
export function getBusinessDomain(entity) {
const directDomain = String(entity?.domain || '').trim();
if (directDomain) return directDomain;
const websiteUrl = String(entity?.websiteUrl || entity?.url || '').trim();
if (!websiteUrl) return '';
return websiteUrl
.replace(/^https?:\/\//i, '')
.replace(/^www\./i, '')
.replace(/\/.*$/, '');
}
export function getBusinessTagline(entity) {
const taglines = normalizeList(entity?.taglines);
const firstTagline = taglines.find((tagline) => String(tagline || '').trim());
if (firstTagline) return String(firstTagline).trim();
const fallback = entity?.tagline || entity?.description || entity?.metaDescription || '';
return String(fallback).trim();
}
export function getBusinessImage(entity) {
const relevantImage = normalizeList(entity?.relevantImagePaths)[0];
if (relevantImage) return relevantImage;
return (
entity?.imageUrl
|| entity?.logoUrl
|| entity?.brandImageUrl
|| entity?.image
|| ''
);
}