175 lines
4.7 KiB
JavaScript
175 lines
4.7 KiB
JavaScript
function createOpportunitiesFormatters({
|
|
opportunitiesTableState
|
|
}) {
|
|
function getCustomerOptions(items) {
|
|
const optionsByKey = new Map();
|
|
|
|
(items || opportunitiesTableState.opportunitiesTableState.items).forEach((item) => {
|
|
const key = getCustomerKey(item);
|
|
const label = getNestedValue(item, ["CustomerAccount", "PartyUniqueName"]);
|
|
|
|
if (key && label && !optionsByKey.has(key)) {
|
|
optionsByKey.set(key, { key, label: String(label) });
|
|
}
|
|
});
|
|
|
|
return Array.from(optionsByKey.values()).sort((first, second) => {
|
|
return first.label.localeCompare(second.label, undefined, {
|
|
sensitivity: "base"
|
|
});
|
|
});
|
|
}
|
|
|
|
function getCustomerKey(item) {
|
|
const partyId = getNestedValue(item, ["CustomerAccount", "PartyId"]);
|
|
const partyName = getNestedValue(item, ["CustomerAccount", "PartyUniqueName"]);
|
|
|
|
if (partyId !== null && partyId !== undefined && partyId !== "") {
|
|
return `party:${partyId}`;
|
|
}
|
|
|
|
return partyName ? `name:${partyName}` : "";
|
|
}
|
|
|
|
function getOwnerName(item) {
|
|
const owner = item.Owner;
|
|
|
|
if (typeof owner === "string" || typeof owner === "number") {
|
|
return owner;
|
|
}
|
|
|
|
if (owner && typeof owner === "object") {
|
|
const firstName = owner.PersonFirstName;
|
|
const lastName = owner.PersonLastName;
|
|
|
|
if (firstName || lastName) {
|
|
return [firstName, lastName].filter(Boolean).join(" ");
|
|
}
|
|
|
|
return owner.PartyName
|
|
|| owner.PartyUniqueName
|
|
|| owner.Name
|
|
|| owner.DisplayName
|
|
|| owner.ResourceName
|
|
|| owner.OwnerName
|
|
|| "";
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
function getCustomerFilterSummary(total, selected) {
|
|
if (total === 0) {
|
|
return "No customers";
|
|
}
|
|
|
|
if (selected === total) {
|
|
return "All customers";
|
|
}
|
|
|
|
if (selected === 0) {
|
|
return "No customers";
|
|
}
|
|
|
|
return `${selected} customer${selected === 1 ? "" : "s"}`;
|
|
}
|
|
|
|
function getOwnerOptions(items) {
|
|
const optionsByKey = new Map();
|
|
|
|
(items || opportunitiesTableState.opportunitiesTableState.items).forEach((item) => {
|
|
const key = getOwnerKey(item);
|
|
const label = getOwnerName(item);
|
|
|
|
if (key && label && !optionsByKey.has(key)) {
|
|
optionsByKey.set(key, { key, label: String(label) });
|
|
}
|
|
});
|
|
|
|
return Array.from(optionsByKey.values()).sort((first, second) => {
|
|
return first.label.localeCompare(second.label, undefined, {
|
|
sensitivity: "base"
|
|
});
|
|
});
|
|
}
|
|
|
|
function getOwnerKey(item) {
|
|
const owner = item.Owner;
|
|
const partyId = owner && typeof owner === "object" ? owner.PartyId : "";
|
|
const name = getOwnerName(item);
|
|
|
|
if (partyId !== null && partyId !== undefined && partyId !== "") {
|
|
return `party:${partyId}`;
|
|
}
|
|
|
|
return name ? `name:${name}` : "";
|
|
}
|
|
|
|
function stringifySearchValue(value) {
|
|
if (value === null || value === undefined) {
|
|
return "";
|
|
}
|
|
|
|
if (typeof value === "object") {
|
|
return JSON.stringify(value);
|
|
}
|
|
|
|
return String(value);
|
|
}
|
|
|
|
function displayOpportunityValue(column, item) {
|
|
const value = column.display(item);
|
|
return value === null || value === undefined || value === "" ? "-" : String(value);
|
|
}
|
|
|
|
function getNestedValue(value, path) {
|
|
return path.reduce((result, key) => result && result[key], value);
|
|
}
|
|
|
|
function formatWinProbability(item) {
|
|
const value = getNestedValue(item, ["PrimaryRevenue", "WinProb"]);
|
|
const number = Number(value);
|
|
return Number.isFinite(number) ? `${number}%` : value;
|
|
}
|
|
|
|
function formatRevenue(item) {
|
|
const amount = getNestedValue(item, ["PrimaryRevenue", "RevnAmount"]);
|
|
const currency = getNestedValue(item, ["PrimaryRevenue", "RevnAmountCurcyCode"]);
|
|
const number = Number(amount);
|
|
|
|
if (!Number.isFinite(number)) {
|
|
return amount;
|
|
}
|
|
|
|
if (!currency) {
|
|
return number.toLocaleString();
|
|
}
|
|
|
|
try {
|
|
return new Intl.NumberFormat(undefined, {
|
|
style: "currency",
|
|
currency
|
|
}).format(number);
|
|
} catch (error) {
|
|
return `${currency} ${number.toLocaleString()}`;
|
|
}
|
|
}
|
|
|
|
function formatOracleResponseDate(value, includeTime) {
|
|
if (typeof value !== "string") {
|
|
return value;
|
|
}
|
|
|
|
const match = value.match(/^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2}))?/);
|
|
|
|
if (!match) {
|
|
return value;
|
|
}
|
|
|
|
const date = `${match[3]}/${match[2]}/${match[1]}`;
|
|
return includeTime && match[4] ? `${date} ${match[4]}:${match[5]}` : date;
|
|
}
|
|
|
|
return { getCustomerOptions, getCustomerKey, getOwnerName, getCustomerFilterSummary, getOwnerOptions, getOwnerKey, stringifySearchValue, displayOpportunityValue, getNestedValue, formatWinProbability, formatRevenue, formatOracleResponseDate };
|
|
}
|