TheRare Visuals

One invoice, the wrong customer: linking bills to bookings safely

An invoice list that crashed only when nothing was linked (Map.of() and null keys), then the check that stops one family's bill landing in another family's account, with emails and phones normalised before comparing.

TheRare Engineering31 October 20265 min readIntermediate

Invoices, briefly

Admin -> Invoices lets the Owner and Managers build an invoice or a rough estimate (customer, event, line items, discounts, advance paid) and download it as a PDF. An invoice can be linked to a booking, and then that booking's customer can see and download it from their account.

That last sentence is where this post lives. The moment an invoice appears in a customer's account, attaching it to the wrong booking stops being a filing mistake and becomes a privacy leak: one family sees another family's name, phone number, address and bill.

But first, a smaller bug that taught the same lesson about assumptions.

The list that crashed when nothing was linked

The admin invoice list shows each invoice's linked booking by client name. To avoid one database query per row, it loads all the linked bookings at once and builds a map from booking id to name:

Java
List<Long> bookingIds = invoices.stream().map(Invoice::getBookingId).filter(Objects::nonNull).distinct().toList();
Map<Long, String> names = bookingIds.isEmpty() ? Map.of()
        : bookingRepository.findAllById(bookingIds).stream()
                .collect(Collectors.toMap(Booking::getId, b -> Objects.toString(b.getClientName(), "")));
return invoices.stream().map(i -> toResponse(i, names.get(i.getBookingId()))).toList();

It looks careful: if there are no linked bookings, don't even query, use an empty map. And it crashed the whole page with a 500 error, but only when no invoice was linked to a booking.

The reason is a difference between two kinds of map in Java:

  • Collectors.toMap builds a HashMap. Asking a HashMap for get(null) returns null. Fine.
  • Map.of() builds an immutable map, and immutable maps refuse null keys entirely. Map.of().get(null) throws a NullPointerException.

An unlinked invoice has a null booking id. So as long as at least one invoice was linked, the code used a HashMap and unlinked rows quietly got no name. The day there were none, the "optimisation" switched to Map.of(), the first unlinked row asked it for null, and the list broke.

The fix is to not ask a question you don't need to ask:

Java
// Map.of() rejects get(null), so unlinked invoices skip the lookup.
return invoices.stream()
        .map(i -> toResponse(i, i.getBookingId() == null ? null : names.get(i.getBookingId())))
        .toList();

The lesson isn't really about Map.of(). Two branches that look equivalent ("an empty map either way") weren't, and the difference only showed in the rarest state of the data: none linked at all. That's precisely the state a brand-new feature is in on its first day.

Linking: which booking is it for?

Linking is a picker on the invoice: search bookings by client name, phone, email or booking number, and choose one. The booking page now has an Invoices card too, listing every invoice linked to that booking with what's been billed, paid and is still owed, plus Create invoice (prefilled from the booking: customer, event, the package at the agreed price, the package's inclusions, and anything already paid online) and Link existing.

A picker makes linking fast. It also makes linking the wrong "Priya" easy. So the server now checks that the invoice and the booking are about the same people before it allows the link.

What "the same customer" means

An invoice doesn't belong to a customer account. It's a document with whatever name, phone and email the studio typed, because quotes are often written before anyone has an account. So "the same customer" has to be decided from contact details.

We compare the invoice's email and phone with every person who can see the booking:

  • the client who booked it,
  • the person it's booked for (Part 13),
  • the email and phone on the linked customer account,
  • every co-owner of the booking.

If either the email or the phone matches anyone on that list, the link is allowed. Otherwise:

Java
throw new IllegalArgumentException("This invoice is for a different customer than booking #" + booking.getId()
        + " (" + booking.getClientName() + "). Its email or phone must match the booking's"
        + " client, the person it's booked for, or a co-owner.");

Comparing contact details is exactly where Part 12 went wrong ("One customer, two spellings"), so the comparison is normalised on both sides:

Java
private static String emailKey(String email) {
    String t = trim(email);
    return t == null ? null : t.toLowerCase(Locale.ROOT);
}

/** Last 10 digits, so "+91 98765 43210" and "9876543210" match. */
private static String phoneKey(String phone) {
    if (phone == null) return null;
    String digits = phone.replaceAll("\\D", "");
    if (digits.isEmpty()) return null;
    return digits.length() > 10 ? digits.substring(digits.length() - 10) : digits;
}

Emails ignore case. Phones keep only digits, then the last ten, because the same Indian number is written as 9876543210, +91 98765 43210 and 098765 43210 depending on who typed it.

Decisions we made on purpose

Names aren't compared. "Hemanth", "Hemanth K" and "Hemanth Kumar" are the same person; two different "Priya"s are not. A name check would block real links and pass wrong ones, so it would only add noise.

An invoice with no email and no phone can be linked anywhere. That's the rough estimate typed before anyone's details were known. There's nothing to compare, and refusing would make estimates impossible to link.

Old invoices stay editable. The check runs when the link is created or changed, and again when the invoice's email or phone is edited while linked. Saving an existing linked invoice without touching those fields doesn't re-check, so invoices linked before this rule existed don't suddenly become impossible to edit.

Linking and unlinking get their own audit actions. They used to be logged as a generic "invoice updated". Now INVOICE_LINKED_TO_BOOKING and INVOICE_UNLINKED_FROM_BOOKING record exactly when a document became visible to a customer, and when it stopped being visible. That holds whether the link was made from the picker, the booking page, or by changing the booking inside the invoice editor.

Totals that can't drift

The booking's Invoices card shows three numbers: billed, paid and outstanding. They're added up on the server from the invoices' stored totals, which are themselves recomputed from the line items on every save and never accepted from the browser. Estimates are listed but left out of the totals, because a quote isn't a bill.

Takeaways

  • Two branches that "do the same thing" may not. Map.of() and a HashMap differ on null keys, and the bug only appeared when no data was linked at all.
  • Test the empty state. A new feature spends its first day with no data; that's when "impossible" branches run.
  • When an action makes data visible to someone, check who that someone is. Linking an invoice is a share, not a filing operation.
  • Normalise before you compare identities. Case for emails, digits for phones.
  • Decide what you won't check, and write down why. Not comparing names was a choice, not an oversight.
  • Give visibility changes their own audit entries.

Found this useful?

Built something with what you learned here, or spotted a better way? Tell us. We work with freelance developers on Shootlo.