zappier-customer-service/src/names.js

37 lines
1.1 KiB
JavaScript

/** Join ledger ids to zappier-edge customer display names. */
export async function listCustomers(edge, key) {
try {
const r = await fetch(`${edge.replace(/\/$/, '')}/admin/api/customers`, {
headers: { 'x-admin-key': key },
});
const body = await r.json();
return (body.customers || []).map(({ passwordHash, apiKey, totpSecret, ...rest }) => rest);
} catch {
return [];
}
}
export async function withCustomerName(st, idOrName, edge, key) {
const out = { ...(st || {}) };
if (out.name && out.customerId) return out;
try {
const r = await fetch(`${edge.replace(/\/$/, '')}/admin/api/customers`, {
headers: { 'x-admin-key': key },
});
const { customers } = await r.json();
const want = String(idOrName || out.customerId || '').toLowerCase();
const c = (customers || []).find(
(x) =>
x.id === idOrName ||
x.id === out.customerId ||
String(x.name || '').toLowerCase() === want,
);
if (c) {
out.name = c.name;
out.customerId = c.id;
}
} catch {
/* edge optional */
}
return out;
}