Skip to content

Free & Open-Source Invoice Generator - No Sign-Up, No Ads, Instant PDF Export, 100% In-Browser, and Fully Customizable Templates.

8.3 KB
Raw Blame
/**
 * URL Compression Utilities
 *
 * This module provides utilities to compress and decompress invoice data for URL sharing
 * by remapping long JSON keys to shorter identifiers before compression.
 */

import type {
  InvoiceData,
  InvoiceItemData,
  SellerData,
  BuyerData,
} from "@/app/schema";

/**
 * Type that extracts all possible keys from InvoiceData and its nested objects
 */
type AllInvoiceKeys =
  // Root level InvoiceData keys
  | keyof InvoiceData
  // Invoice number object keys
  | keyof NonNullable<InvoiceData["invoiceNumberObject"]>
  // Seller data keys
  | keyof SellerData
  // Buyer data keys
  | keyof BuyerData
  // Invoice item keys
  | keyof InvoiceItemData;

/**
 * Mapping of full invoice property names to short keys for URL compression
 * This significantly reduces the size of the JSON before compression
 *
 * @example
 * ```typescript
 * const data = { language: "en", dateFormat: "YYYY-MM-DD" };
 * const compressed = remapKeys(data);
 * // Returns: { "a": "en", "b": "YYYY-MM-DD" }
 * ```
 */
export const INVOICE_KEY_COMPRESSION_MAP = {
  // Root level properties
  language: "a",
  dateFormat: "b",
  currency: "c",
  numberFormatLocale: "%",
  preserveNumberFormatOnLanguageChange: "^",
  template: "d",
  logo: "e",
  invoiceNumberObject: "f",
  dateOfIssue: "g",
  dateOfService: "h",
  dateOfServiceStart: "9",
  servicePeriodFieldIsVisible: "!",
  dateOfServiceFieldIsVisible: "@",
  servicePeriodLabelText: "#",
  dateOfServiceLabelText: "$",
  invoiceType: "i",
  invoiceTypeFieldIsVisible: "j",
  seller: "k",
  buyer: "l",
  items: "m",
  total: "n",
  vatTableSummaryIsVisible: "o",
  paymentMethod: "p",
  paymentMethodFieldIsVisible: "q",
  paymentDue: "r",
  stripePayOnlineUrl: "s",
  stripeTopLineColor: "+",
  notes: "t",
  notesFieldIsVisible: "u",

  qrCodeData: "3",
  qrCodeIsVisible: "4",
  qrCodeDescription: "5",

  personAuthorizedToReceiveName: "6",
  personAuthorizedToReceiveFieldIsVisible: "v",
  personAuthorizedToIssueName: "7",
  personAuthorizedToIssueFieldIsVisible: "w",
  taxLabelText: "1",

  // Invoice number object properties
  label: "x",
  value: "y",

  // Seller/Buyer properties
  id: "z",
  name: "A",
  address: "B",
  vatNo: "C",
  vatNoFieldIsVisible: "D",
  email: "E",
  emailFieldIsVisible: "8",
  accountNumber: "F",
  accountNumberFieldIsVisible: "G",
  swiftBic: "H",
  swiftBicFieldIsVisible: "I",
  vatNoLabelText: "2",

  // Invoice item properties
  invoiceItemNumberIsVisible: "J",
  nameFieldIsVisible: "K",
  typeOfGTU: "L",
  typeOfGTUFieldIsVisible: "M",
  amount: "N",
  amountFieldIsVisible: "O",
  unit: "P",
  unitFieldIsVisible: "Q",
  netPrice: "R",
  netPriceFieldIsVisible: "S",
  vat: "T",
  vatFieldIsVisible: "U",
  netAmount: "V",
  netAmountFieldIsVisible: "W",
  vatAmount: "X",
  vatAmountFieldIsVisible: "Y",
  preTaxAmount: "Z",
  preTaxAmountFieldIsVisible: "0",
  discountType: "&",
  discountValue: "*",
  discountLabel: "~",
  discountAmount: "=",
} as const satisfies Record<AllInvoiceKeys, string>;

/**
 * Reverse mapping for decompression
 */
const REVERSE_KEY_MAP = Object.fromEntries(
  Object.entries(INVOICE_KEY_COMPRESSION_MAP).map(([key, value]) => {
    return [value, key];
  }),
) as Record<string, keyof typeof INVOICE_KEY_COMPRESSION_MAP>;

/**
 * Recursively remaps object keys using the INVOICE_KEY_COMPRESSION_MAP
 *
 * @example
 * ```typescript
 * const data = { invoiceNumber: "123", dueDate: "2024-01-01" };
 * const compressed = remapKeys(data);
 * // Returns: { "a": "123", "b": "2024-01-01" }
 * ```
 */
function remapKeys<T>(
  obj: T,
): T extends Record<string, unknown>
  ? Record<string, unknown>
  : T extends unknown[]
    ? unknown[]
    : T {
  if (obj === null || typeof obj !== "object") {
    return obj as T extends Record<string, unknown>
      ? Record<string, unknown>
      : T extends unknown[]
        ? unknown[]
        : T;
  }

  if (Array.isArray(obj)) {
    return obj.map((item: unknown) => {
      return remapKeys(item);
    }) as T extends Record<string, unknown>
      ? Record<string, unknown>
      : T extends unknown[]
        ? unknown[]
        : T;
  }

  const result: Record<string, unknown> = {};

  for (const [key, value] of Object.entries(obj)) {
    const mappedKey =
      INVOICE_KEY_COMPRESSION_MAP[
        key as keyof typeof INVOICE_KEY_COMPRESSION_MAP
      ] || key;
    result[mappedKey] = remapKeys(value);
  }

  return result as T extends Record<string, unknown>
    ? Record<string, unknown>
    : T extends unknown[]
      ? unknown[]
      : T;
}

/**
 * Recursively restores original keys using the REVERSE_KEY_MAP
 *
 * @example
 * ```typescript
 * const data = { "a": "123", "b": "2024-01-01" };
 * const decompressed = restoreKeys(data);
 * // Returns: { "invoiceNumber": "123", "dueDate": "2024-01-01" }
 * ```
 */
function restoreKeys<T>(
  obj: T,
): T extends Record<string, unknown>
  ? Record<string, unknown>
  : T extends unknown[]
    ? unknown[]
    : T {
  if (obj === null || typeof obj !== "object") {
    return obj as T extends Record<string, unknown>
      ? Record<string, unknown>
      : T extends unknown[]
        ? unknown[]
        : T;
  }

  if (Array.isArray(obj)) {
    return obj.map((item: unknown) => {
      return restoreKeys(item);
    }) as T extends Record<string, unknown>
      ? Record<string, unknown>
      : T extends unknown[]
        ? unknown[]
        : T;
  }

  const result: Record<string, unknown> = {};

  for (const [key, value] of Object.entries(obj)) {
    const originalKey = REVERSE_KEY_MAP[key] || key;
    result[originalKey] = restoreKeys(value);
  }

  return result as T extends Record<string, unknown>
    ? Record<string, unknown>
    : T extends unknown[]
      ? unknown[]
      : T;
}

/**
 * Compresses invoice data by remapping keys to shorter identifiers
 *
 * @example
 * ```ts
 * const invoiceData = { invoiceId: "INV-001", amount: 100.50, items: [{ name: "Item 1", quantity: 2 }] };
 * const compressedData = compressInvoiceData(invoiceData);
 * // Result: { i: "INV-001", a: 100.50, it: [{ n: "Item 1", q: 2 }] }
 * ```
 */
export function compressInvoiceData(invoiceData: InvoiceData) {
  // guard for callers handing over something that is not an invoice at all; remapKeys
  // passes those through as they are
  const hasItems =
    typeof invoiceData === "object" &&
    invoiceData !== null &&
    Array.isArray(invoiceData.items);

  const res = remapKeys(
    hasItems
      ? {
          ...invoiceData,
          items: invoiceData.items.map((item) => {
            return omitDefaultDiscount(item);
          }),
        }
      : invoiceData,
  );

  return res;
}

/**
 * Decompresses invoice data by restoring original keys
 *
 * @example
 * ```ts
 * const compressedData = { i: "INV-001", a: 100.50, it: [{ n: "Item 1", q: 2 }] };
 * const originalData = decompressInvoiceData(compressedData);
 * // Result: { invoiceId: "INV-001", amount: 100.50, items: [{ name: "Item 1", quantity: 2 }] }
 * ```
 */
export function decompressInvoiceData(compressedData: Record<string, unknown>) {
  const res = restoreKeys(compressedData);

  if (Array.isArray(res.items)) {
    res.items = res.items.map((item: unknown) => {
      return withDefaultDiscount(item);
    });
  }

  return res;
}

/**
 * An item without a discount, as the form stores it. Share links leave these fields out of
 * such items, so every item does not pay for four keys it does not use.
 */
const NO_DISCOUNT = {
  discountType: "percentage",
  discountValue: 0,
  discountLabel: "",
  discountAmount: 0,
} as const satisfies Partial<InvoiceItemData>;

const NO_DISCOUNT_KEYS = Object.keys(
  NO_DISCOUNT,
) as (keyof typeof NO_DISCOUNT)[];

/** Drops the discount fields of an item that has no discount, see {@link NO_DISCOUNT}. */
function omitDefaultDiscount(item: InvoiceItemData) {
  const hasNoDiscount = NO_DISCOUNT_KEYS.every((key) => {
    return item[key] === NO_DISCOUNT[key];
  });

  if (!hasNoDiscount) return item;

  return Object.fromEntries(
    Object.entries(item).filter(([key]) => {
      return !(key in NO_DISCOUNT);
    }),
  );
}

/**
 * Puts back the discount fields {@link omitDefaultDiscount} left out. Links made before
 * discounts existed have none either, and get the same "no discount" values.
 */
function withDefaultDiscount(item: unknown) {
  if (item === null || typeof item !== "object" || Array.isArray(item)) {
    return item;
  }

  return { ...NO_DISCOUNT, ...item };
}