Files
web/apps/scandic-web/utils/refId.ts
2025-05-08 11:52:31 +00:00

35 lines
1.3 KiB
TypeScript

import "server-only"
import { decrypt, encrypt } from "./encryption"
export function calculateRefId(confirmationNumber: string, lastName: string) {
const encryptedRefId = encrypt(`${confirmationNumber},${lastName}`)
return encryptedRefId
}
export function parseRefId(refId: string) {
// RefId is DES-ECB encryption + Base64 encoding. For legacy reasons we have
// to do some manual handling here to get a proper Base64 string.
//
// - Use case: Current web replaced plus sign with hyphens when generating RefIds.
// Handling: We replace hyphens with plus signs.
//
// - Use case: Incoming links in the wild do not encode the RefId properly.
// Handling: We replace spaces with plus signs. Effectively, reversing the
// decoding of plus signs into spaces that Next.js does for us for incoming
// search params.
// Slash and equal sign are not decoded into anything, so no action needed.
// We only need to cater for those three (plus, slash, equals) as RefId is
// Base64 encoded which only has these three special characters.
const data = decrypt(refId.replace(/ |-/g, "+"))
const parts = data.split(",")
if (parts.length !== 2) {
throw new Error("Invalid refId format")
}
return {
confirmationNumber: parts[0],
lastName: parts[1],
}
}