refactor(db): typed query helpers, shared test FormData helper
Replace raw db.execute tuple casts with queryRows/rowsFrom/execResult/ affectedRows helpers from lib/db, drop redundant mysql2 casts on typed query builders, and centralize per-test fakeForm into test/fake-form. Update db mocks in tests so helpers resolve against mocked execute.
This commit is contained in:
1 parent
2e25b39364
commit
8638e81444
116 files changed
+866
-897
No files matched your search
@@ -2,7 +2,13 @@ import "server-only";
|
||||
import { getTableColumns, type SQL, sql } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { historySnapshot, recordHistory } from "@/features/history/server";
|
||||
import { CatalogItems, CatalogItemsBc, db, ItemsBase } from "@/lib/db";
|
||||
import {
|
||||
CatalogItems,
|
||||
CatalogItemsBc,
|
||||
db,
|
||||
ItemsBase,
|
||||
rowsFrom,
|
||||
} from "@/lib/db";
|
||||
import { CatalogInputError } from "../domain/hierarchy";
|
||||
import {
|
||||
distinctOfferIds,
|
||||
@@ -31,13 +37,14 @@ async function lockedOffers(
|
||||
ids: number[],
|
||||
kind: OfferKind = "normal",
|
||||
): Promise<OfferRow[]> {
|
||||
const [rows] = await tx.execute(
|
||||
sql`SELECT id, page_id AS pageId, item_ids AS itemIds FROM ${offerTable(kind)} WHERE id IN (${sql.join(
|
||||
[...ids].sort((a, b) => a - b),
|
||||
sql`, `,
|
||||
)}) ORDER BY id FOR UPDATE`,
|
||||
const offers = rowsFrom<OfferRow>(
|
||||
await tx.execute(
|
||||
sql`SELECT id, page_id AS pageId, item_ids AS itemIds FROM ${offerTable(kind)} WHERE id IN (${sql.join(
|
||||
[...ids].sort((a, b) => a - b),
|
||||
sql`, `,
|
||||
)}) ORDER BY id FOR UPDATE`,
|
||||
),
|
||||
);
|
||||
const offers = rows as unknown as OfferRow[];
|
||||
if (
|
||||
offers.length !== ids.length ||
|
||||
ids.some((id) => !offers.some((offer) => Number(offer.id) === id))
|
||||
@@ -51,21 +58,23 @@ async function lockPage(
|
||||
kind: OfferKind = "normal",
|
||||
) {
|
||||
offerTable(kind);
|
||||
const [rows] = await tx.execute(
|
||||
sql`SELECT id FROM ${sql.identifier(kind === "bc" ? "catalog_pages_bc" : "catalog_pages")} WHERE id=${id} FOR UPDATE`,
|
||||
);
|
||||
if (
|
||||
!(rows as unknown as { id: number }[]).some((row) => Number(row.id) === id)
|
||||
!rowsFrom<{ id: number }>(
|
||||
await tx.execute(
|
||||
sql`SELECT id FROM ${sql.identifier(kind === "bc" ? "catalog_pages_bc" : "catalog_pages")} WHERE id=${id} FOR UPDATE`,
|
||||
),
|
||||
).some((row) => Number(row.id) === id)
|
||||
)
|
||||
throw Error("Catalog page not found");
|
||||
}
|
||||
async function lockFurniture(tx: Transaction, ids: number[]) {
|
||||
const unique = [...new Set(ids)].sort((a, b) => a - b);
|
||||
const [rows] = await tx.execute(
|
||||
sql`SELECT id FROM items_base WHERE id IN (${sql.join(unique, sql`, `)}) ORDER BY id FOR UPDATE`,
|
||||
);
|
||||
const found = new Set(
|
||||
(rows as unknown as { id: number }[]).map((row) => Number(row.id)),
|
||||
rowsFrom<{ id: number }>(
|
||||
await tx.execute(
|
||||
sql`SELECT id FROM items_base WHERE id IN (${sql.join(unique, sql`, `)}) ORDER BY id FOR UPDATE`,
|
||||
),
|
||||
).map((row) => Number(row.id)),
|
||||
);
|
||||
if (unique.some((id) => !found.has(id)))
|
||||
throw Error("Furniture reference not found");
|
||||
@@ -98,10 +107,13 @@ async function assertOrderUnique(
|
||||
orderNumber: number,
|
||||
excludeId: number,
|
||||
) {
|
||||
const [dups] = await tx.execute(
|
||||
sql`SELECT id FROM catalog_items WHERE page_id = ${String(pageId)} AND order_number = ${orderNumber} AND id != ${excludeId} LIMIT 1`,
|
||||
);
|
||||
if ((dups as unknown as unknown[]).length > 0)
|
||||
if (
|
||||
rowsFrom<unknown>(
|
||||
await tx.execute(
|
||||
sql`SELECT id FROM catalog_items WHERE page_id = ${String(pageId)} AND order_number = ${orderNumber} AND id != ${excludeId} LIMIT 1`,
|
||||
),
|
||||
).length > 0
|
||||
)
|
||||
throw new CatalogInputError(
|
||||
`Order number ${orderNumber} is already used by another offer on this page`,
|
||||
);
|
||||
@@ -156,11 +168,10 @@ export async function updateOfferCommand(
|
||||
fields.limitedStack !== undefined ||
|
||||
fields.limitedSells !== undefined
|
||||
) {
|
||||
const [current] = await tx.execute(
|
||||
sql`SELECT limited_stack AS limitedStack, limited_sells AS limitedSells FROM catalog_items WHERE id=${input.id}`,
|
||||
);
|
||||
const row = (
|
||||
current as unknown as { limitedStack: number; limitedSells: number }[]
|
||||
const row = rowsFrom<{ limitedStack: number; limitedSells: number }>(
|
||||
await tx.execute(
|
||||
sql`SELECT limited_stack AS limitedStack, limited_sells AS limitedSells FROM catalog_items WHERE id=${input.id}`,
|
||||
),
|
||||
)[0];
|
||||
if (row) {
|
||||
const stack = fields.limitedStack ?? Number(row.limitedStack);
|
||||
@@ -267,7 +278,7 @@ export async function createBcOfferCommand(
|
||||
const [result] = await tx.execute(
|
||||
sql`INSERT INTO ${CatalogItemsBc} SET ${sql.join(assignments(CatalogItemsBc, fields), sql`, `)}`,
|
||||
);
|
||||
const id = Number((result as unknown as { insertId: number }).insertId);
|
||||
const id = Number(result.insertId);
|
||||
offerIdSchema.parse(id);
|
||||
return id;
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user