Initial import of zappier-customer-service from zapier monorepo
This commit is contained in:
commit
741683ff49
11 changed files with 540 additions and 0 deletions
37
src/names.js
Normal file
37
src/names.js
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/** 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue