fix(api): treat blank env vars as unset; verify the whole flow in Docker Compose
Found by running the delivery flow against the actual built container rather
than a local dev process.
DEPLOY-02 (Critical) — ADMIN LOGIN RETURNED 500 FOR EVERY USER IN DOCKER.
docker-compose.yml passes each optional variable as ${X:-}, so an unconfigured
one arrives as "" rather than undefined. The code read them with
`process.env.X ?? 'default'`, which only falls back on undefined, so the empty
string reached jsonwebtoken and it threw
"expiresIn" should be a number of seconds or string representing a timespan
on every sign. Invisible to every existing suite, all of which run against a
local process where the variable is genuinely absent.
Added src/common/env.ts (envOr / envOrNull), which treat blank as unset, and
applied it to every auth, port, upload and SMS read. Three were not cosmetic:
a blank UPLOAD_DIR would have served static files from the FILESYSTEM ROOT, a
blank PORT would have bound a random ephemeral port (Number('') === 0), and a
blank JWT_SECRET would have become the verifying key. Recorded as D25, with a
standing rule to use these readers instead of `??`.
Pinned by src/common/env.spec.ts (9 tests), which reproduces the original throw
against the real jsonwebtoken and asserts the reader can no longer produce it.
DEPLOY-03 (process, documented) — the API image is `COPY release/ ./`, so it
ships a bundle pre-built on the developer machine. `docker compose build` will
happily produce an image from a STALE release/app.js and report success; at the
start of this run that bundle was four days old and the rebuilt container was
missing every route added since. DELIVERY.md's deploy section now leads with
`pnpm build:release`.
New: scripts/test-compose-e2e.cjs — the whole delivery flow against the built
container, over HTTP only, as a black box: a sales expert and an ordinary
customer both sign in with the master code, both place an order, both orders
appear in the admin grid and open there, and the three token boundaries still
hold in the deployed image. 32/32.
Verified: compose e2e 32/32 · jest 54/54 · build 5/5 ·
against the CONTAINER: complete-profile 38/38, report 40/40, expert login 36/36 ·
locally: apps smoke 43/43, admin smoke 43/43, B2B scan 34 ok/0 broken.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
3f549f1749
commit
24b7a4c395
@@ -0,0 +1,97 @@
|
||||
-- D0 — split Customer.fullName into firstName / lastName (ADDITIVE).
|
||||
--
|
||||
-- Reference: henza_server has two separate columns from the start
|
||||
-- database/migrations/2020_10_01_120430_customers_table.php:18-19
|
||||
-- $table->string('fname')->nullable();
|
||||
-- $table->string('lname')->nullable();
|
||||
-- The target collapsed them into one `fullName`, so the admin edit form shows
|
||||
-- the whole name in the "first name" box and saves an empty last name
|
||||
-- (admin-customers.service.ts:231-233 returns `fname: c.fullName, lname: ''`).
|
||||
--
|
||||
-- NOTHING IS DROPPED. `fullName` stays and is kept in sync as
|
||||
-- `firstName || ' ' || lastName`, so every existing reader keeps working
|
||||
-- (D0 requirement 4).
|
||||
--
|
||||
-- D13 / storefront impact: `Customer` is a SHARED table — both the B2B app and
|
||||
-- the retail storefront read it. This migration only ADDS two nullable columns
|
||||
-- and leaves `fullName` populated and authoritative, so no storefront query,
|
||||
-- view or report changes behaviour. No storefront column is touched.
|
||||
|
||||
-- 1. The two new columns. Nullable: a customer may legitimately have no
|
||||
-- surname, and the reference's own columns are nullable.
|
||||
ALTER TABLE "Customer" ADD COLUMN IF NOT EXISTS "firstName" TEXT;
|
||||
ALTER TABLE "Customer" ADD COLUMN IF NOT EXISTS "lastName" TEXT;
|
||||
|
||||
-- 2. Backfill: first whitespace-separated token -> firstName, the REST ->
|
||||
-- lastName (D0 requirement 2). Multi-space and leading/trailing whitespace
|
||||
-- are normalised first so " علی محمدی " splits correctly.
|
||||
--
|
||||
-- Rows with a single token get firstName = that token and lastName = NULL.
|
||||
-- Rows with more than two tokens put every remaining token in lastName;
|
||||
-- they are ALSO listed in the review report (see the companion script) so a
|
||||
-- human can correct them — the split cannot be guessed safely
|
||||
-- (D0 requirement 3).
|
||||
UPDATE "Customer"
|
||||
SET
|
||||
"firstName" = split_part(btrim(regexp_replace("fullName", '\s+', ' ', 'g')), ' ', 1),
|
||||
"lastName" = NULLIF(
|
||||
substr(
|
||||
btrim(regexp_replace("fullName", '\s+', ' ', 'g')),
|
||||
length(split_part(btrim(regexp_replace("fullName", '\s+', ' ', 'g')), ' ', 1)) + 2
|
||||
),
|
||||
''
|
||||
)
|
||||
WHERE "firstName" IS NULL
|
||||
AND "fullName" IS NOT NULL
|
||||
AND btrim("fullName") <> '';
|
||||
|
||||
-- 3. Keep `fullName` in sync from now on.
|
||||
--
|
||||
-- Writers may update either the parts or the whole; this trigger makes the
|
||||
-- parts authoritative whenever they are the thing that changed, and
|
||||
-- otherwise leaves an explicit `fullName` write alone. That way old code
|
||||
-- that still writes only `fullName` is not broken (D0 requirement 4).
|
||||
-- Precedence when BOTH the parts and fullName change in the SAME statement:
|
||||
-- THE PARTS WIN. fullName is rebuilt from them and the submitted fullName is
|
||||
-- discarded. Rationale: after D0 the parts are the field the forms edit, so
|
||||
-- they carry the operator's intent; fullName is the derived, legacy-facing
|
||||
-- value. Choosing the opposite would silently undo an edit made in the two
|
||||
-- boxes the operator actually typed into.
|
||||
-- The first branch below tests the parts, so it wins by construction.
|
||||
CREATE OR REPLACE FUNCTION customer_sync_full_name() RETURNS trigger AS $$
|
||||
BEGIN
|
||||
-- The name parts changed (or are being set on insert): rebuild fullName.
|
||||
-- This is checked FIRST, so when both change in one statement the parts win.
|
||||
IF (TG_OP = 'INSERT' AND (NEW."firstName" IS NOT NULL OR NEW."lastName" IS NOT NULL))
|
||||
OR (TG_OP = 'UPDATE'
|
||||
AND (NEW."firstName" IS DISTINCT FROM OLD."firstName"
|
||||
OR NEW."lastName" IS DISTINCT FROM OLD."lastName")) THEN
|
||||
NEW."fullName" := btrim(
|
||||
coalesce(NEW."firstName", '') || ' ' || coalesce(NEW."lastName", '')
|
||||
);
|
||||
|
||||
-- Only fullName changed: derive the parts from it, so the two never drift.
|
||||
ELSIF TG_OP = 'UPDATE' AND NEW."fullName" IS DISTINCT FROM OLD."fullName"
|
||||
AND NEW."fullName" IS NOT NULL AND btrim(NEW."fullName") <> '' THEN
|
||||
NEW."firstName" := split_part(btrim(regexp_replace(NEW."fullName", '\s+', ' ', 'g')), ' ', 1);
|
||||
NEW."lastName" := NULLIF(
|
||||
substr(
|
||||
btrim(regexp_replace(NEW."fullName", '\s+', ' ', 'g')),
|
||||
length(split_part(btrim(regexp_replace(NEW."fullName", '\s+', ' ', 'g')), ' ', 1)) + 2
|
||||
),
|
||||
''
|
||||
);
|
||||
END IF;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
DROP TRIGGER IF EXISTS customer_sync_full_name_trg ON "Customer";
|
||||
CREATE TRIGGER customer_sync_full_name_trg
|
||||
BEFORE INSERT OR UPDATE ON "Customer"
|
||||
FOR EACH ROW EXECUTE FUNCTION customer_sync_full_name();
|
||||
|
||||
-- 4. Name search hits both parts in the admin grid.
|
||||
CREATE INDEX IF NOT EXISTS "Customer_firstName_idx" ON "Customer" ("firstName");
|
||||
CREATE INDEX IF NOT EXISTS "Customer_lastName_idx" ON "Customer" ("lastName");
|
||||
Reference in New Issue
Block a user