Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nk-billing-address-line2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ingram-tech/nk-billing": minor
---

The customer address carries `line2`. `findOrCreateCustomer` sends `details.address.line2` (it dropped it), and `updateCustomerBillingAddress` takes an optional `line2`. An empty one is not sent.
70 changes: 69 additions & 1 deletion packages/nk-billing/src/customers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type Stripe from "stripe";
import { describe, expect, it } from "vitest";
import { findCustomer } from "./customers.js";
import {
findCustomer,
findOrCreateCustomer,
updateCustomerBillingAddress,
} from "./customers.js";

/** Records the search query; the escaping is the unit under test. */
function recordingStripe() {
Expand Down Expand Up @@ -44,3 +48,67 @@ describe("findCustomer", () => {
});
});
});

/** Records the create and update params; their address is the unit under test. */
function addressStripe() {
const created: Stripe.CustomerCreateParams[] = [];
const updated: Stripe.CustomerUpdateParams[] = [];
const stripe = {
customers: {
search: () => Promise.resolve({ data: [] }),
create: (params: Stripe.CustomerCreateParams) => {
created.push(params);
return Promise.resolve({ id: "cus_1" });
},
update: (_id: string, params: Stripe.CustomerUpdateParams) => {
updated.push(params);
return Promise.resolve({ id: "cus_1" });
},
},
};
// Structural fake of the one surface under test.
return { created, updated, stripe: stripe as unknown as Stripe };
}

const brussels = { city: "Ixelles", postal_code: "1050", country: "BE" };

describe("the second address line", () => {
it.each([
["sends it", "Boîte 3", { line2: "Boîte 3" }],
["omits an empty one", "", {}],
["omits a missing one", null, {}],
])("findOrCreateCustomer %s", async (_label, line2, expected) => {
const { created, stripe } = addressStripe();
await findOrCreateCustomer(
{ metadataKey: "acme_org_id", id: "org_1" },
{
name: "Acme",
address: { line1: "Avenue Louise 250", line2, ...brussels },
},
stripe,
);
expect(created[0]?.address).toEqual({
line1: "Avenue Louise 250",
...expected,
...brussels,
});
});

it.each([
["sends it", "Boîte 3", { line2: "Boîte 3" }],
["omits an empty one", "", {}],
["omits a missing one", undefined, {}],
])("updateCustomerBillingAddress %s", async (_label, line2, expected) => {
const { updated, stripe } = addressStripe();
await updateCustomerBillingAddress(
"cus_1",
{ address: { line1: "Avenue Louise 250", line2, ...brussels } },
stripe,
);
expect(updated[0]?.address).toEqual({
line1: "Avenue Louise 250",
...expected,
...brussels,
});
});
});
7 changes: 6 additions & 1 deletion packages/nk-billing/src/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface CustomerDetails {
vatNumber?: string | null;
address?: {
line1?: string | null;
line2?: string | null;
city?: string | null;
postal_code?: string | null;
country?: string | null;
Expand Down Expand Up @@ -73,6 +74,7 @@ export async function findOrCreateCustomer(
if (details.address?.line1 || details.address?.city) {
params.address = {
line1: details.address.line1 ?? undefined,
line2: details.address.line2 || undefined,
city: details.address.city ?? undefined,
postal_code: details.address.postal_code ?? undefined,
country: details.address.country ?? undefined,
Expand Down Expand Up @@ -121,15 +123,18 @@ export async function updateCustomerBillingAddress(
name?: string | null;
address: {
line1: string;
/** The second street line; an empty one is not sent. */
line2?: string | null;
city: string;
postal_code: string;
country: string;
};
},
stripe: Stripe = getStripe(),
): Promise<void> {
const { line2, ...address } = opts.address;
await stripe.customers.update(customerId, {
...(opts.name ? { name: opts.name } : {}),
address: opts.address,
address: { ...address, ...(line2 ? { line2 } : {}) },
});
}