This commit is contained in:
121
src/lib/db.ts
121
src/lib/db.ts
@@ -1,5 +1,5 @@
|
||||
import { Pool } from "pg";
|
||||
import type { TenantRequest, TenantRequestStatus } from "@/types";
|
||||
import type { BillingAddress, TenantRequest, TenantRequestStatus } from "@/types";
|
||||
import { listTenants, getTenant } from "./k8s";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -72,6 +72,11 @@ const MIGRATION_SQL = `
|
||||
ALTER TABLE tenant_requests ADD COLUMN IF NOT EXISTS agents_md TEXT;
|
||||
ALTER TABLE tenant_requests ADD COLUMN IF NOT EXISTS instance_name TEXT;
|
||||
ALTER TABLE tenant_requests ADD COLUMN IF NOT EXISTS is_personal BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
-- Bug 13: customer-side dismissal of rejected requests. NULL means "still
|
||||
-- visible on the dashboard"; non-null means "customer clicked Dismiss".
|
||||
-- Pending/approved/active rows keep this NULL by definition — the field
|
||||
-- is only meaningful for `rejected` and `cancelled` rows.
|
||||
ALTER TABLE tenant_requests ADD COLUMN IF NOT EXISTS dismissed_at TIMESTAMPTZ;
|
||||
|
||||
-- Slice 3: drop the legacy 1-org-1-request constraint if it exists
|
||||
ALTER TABLE tenant_requests DROP CONSTRAINT IF EXISTS tenant_requests_zitadel_org_id_key;
|
||||
@@ -250,10 +255,21 @@ export async function listTenantRequestsByOrgId(
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link listTenantRequestsByOrgId} but excludes terminal-failed states
|
||||
* (rejected, deleted). Useful for the dashboard which wants to show
|
||||
* pending/approved/provisioning/active tenants and pending requests, not
|
||||
* historical rejections.
|
||||
* As {@link listTenantRequestsByOrgId} but tuned for the customer's
|
||||
* dashboard view.
|
||||
*
|
||||
* Returns:
|
||||
* - All non-terminal rows (pending, approved, provisioning, active),
|
||||
* because the customer needs to see what's in flight.
|
||||
* - Terminal-failed rows (rejected, cancelled) that the customer
|
||||
* hasn't dismissed yet (Bug 13). Without this, a rejection that
|
||||
* happens while the customer isn't online would only be
|
||||
* communicated by email — easy to miss.
|
||||
*
|
||||
* Excludes:
|
||||
* - `deleted` rows (admin tore down the tenant — historical, not
|
||||
* actionable).
|
||||
* - Dismissed rejected/cancelled rows.
|
||||
*/
|
||||
export async function listActiveTenantRequestsByOrgId(
|
||||
orgId: string
|
||||
@@ -262,7 +278,8 @@ export async function listActiveTenantRequestsByOrgId(
|
||||
const result = await getPool().query<TenantRequest>(
|
||||
`SELECT * FROM tenant_requests
|
||||
WHERE zitadel_org_id = $1
|
||||
AND status NOT IN ('deleted', 'rejected')
|
||||
AND status <> 'deleted'
|
||||
AND (status NOT IN ('rejected', 'cancelled') OR dismissed_at IS NULL)
|
||||
ORDER BY created_at DESC`,
|
||||
[orgId]
|
||||
);
|
||||
@@ -354,6 +371,96 @@ export async function clearEncryptedSecrets(requestId: string): Promise<void> {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set dismissed_at = now() on a request row. Used when a customer
|
||||
* clicks "Dismiss" on a rejected/cancelled card on their dashboard
|
||||
* (Bug 13). The row stays in the database for history/audit but
|
||||
* stops appearing in `listActiveTenantRequestsByOrgId`.
|
||||
*
|
||||
* Idempotent: dismissing an already-dismissed row is a no-op.
|
||||
* Caller is responsible for verifying the row belongs to the user's
|
||||
* org before calling.
|
||||
*/
|
||||
export async function dismissTenantRequest(id: string): Promise<void> {
|
||||
await ensureSchema();
|
||||
await getPool().query(
|
||||
`UPDATE tenant_requests
|
||||
SET dismissed_at = COALESCE(dismissed_at, now()),
|
||||
updated_at = now()
|
||||
WHERE id = $1`,
|
||||
[id]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update editable fields of a still-pending tenant request. Bug 6 — a
|
||||
* customer who notices a typo or wants to add a package after submitting
|
||||
* the wizard should be able to fix it without admin involvement.
|
||||
*
|
||||
* Only the customer-input fields are updateable. `status`, `tenant_name`,
|
||||
* `admin_notes`, `encrypted_secrets`, `is_personal`, `zitadel_*` and
|
||||
* timestamps are managed elsewhere and intentionally not here.
|
||||
*
|
||||
* The caller is responsible for:
|
||||
* - verifying the row belongs to the user's org
|
||||
* - verifying status === 'pending' (editing approved/provisioning rows
|
||||
* would race against the operator)
|
||||
*
|
||||
* Returns the updated row, or null if the id didn't match anything.
|
||||
*/
|
||||
export async function updateTenantRequestEditableFields(
|
||||
id: string,
|
||||
fields: {
|
||||
instanceName?: string | null;
|
||||
agentName?: string;
|
||||
soulMd?: string;
|
||||
agentsMd?: string | null;
|
||||
packages?: string[];
|
||||
billingAddress?: BillingAddress;
|
||||
billingNotes?: string;
|
||||
encryptedSecrets?: Buffer | null;
|
||||
}
|
||||
): Promise<TenantRequest | null> {
|
||||
await ensureSchema();
|
||||
|
||||
const sets: string[] = ["updated_at = now()"];
|
||||
const values: any[] = [id];
|
||||
let idx = 2;
|
||||
|
||||
// Map JS field names to SQL columns. Each entry is gated on
|
||||
// `!== undefined` so passing only some fields just updates those.
|
||||
const colMap: Array<[keyof typeof fields, string]> = [
|
||||
["instanceName", "instance_name"],
|
||||
["agentName", "agent_name"],
|
||||
["soulMd", "soul_md"],
|
||||
["agentsMd", "agents_md"],
|
||||
["packages", "packages"],
|
||||
["billingAddress", "billing_address"],
|
||||
["billingNotes", "billing_notes"],
|
||||
["encryptedSecrets", "encrypted_secrets"],
|
||||
];
|
||||
for (const [jsField, sqlCol] of colMap) {
|
||||
const v = fields[jsField];
|
||||
if (v === undefined) continue;
|
||||
sets.push(`${sqlCol} = $${idx}`);
|
||||
values.push(v);
|
||||
idx++;
|
||||
}
|
||||
|
||||
if (sets.length === 1) {
|
||||
// No editable fields supplied — return the row unchanged rather
|
||||
// than running a useless UPDATE that just bumps updated_at.
|
||||
const cur = await getTenantRequestById(id);
|
||||
return cur;
|
||||
}
|
||||
|
||||
const result = await getPool().query<TenantRequest>(
|
||||
`UPDATE tenant_requests SET ${sets.join(", ")} WHERE id = $1 RETURNING *`,
|
||||
values
|
||||
);
|
||||
return result.rows[0] ? mapRow(result.rows[0]) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around domain-check.ts that injects the portal's connection pool.
|
||||
* Kept here so route handlers don't need direct access to the pool.
|
||||
@@ -446,6 +553,8 @@ function mapRow(row: any): TenantRequest {
|
||||
tenantName: row.tenant_name,
|
||||
encryptedSecrets: row.encrypted_secrets ?? null,
|
||||
isPersonal: row.is_personal ?? false,
|
||||
dismissedAt:
|
||||
row.dismissed_at?.toISOString?.() ?? row.dismissed_at ?? null,
|
||||
createdAt: row.created_at?.toISOString?.() ?? row.created_at,
|
||||
updatedAt: row.updated_at?.toISOString?.() ?? row.updated_at,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user