fix(orders): list/detail/items/cancel — 편집 락 컬럼 자동 증설 추가
Deploy momo-erp / deploy (push) Successful in 1m56s

이전 커밋(6be1633)에서 SQL이 momo_orders.editing_by / editing_at 을 참조하지만
운영 DB 에 컬럼이 없으면 list 쿼리 전체가 깨져 발주 리스트가 0건으로 표시됨.
(컬럼 자동 증설은 lock 라우트에만 있었는데, list 가 먼저 호출되니 시점이 안 맞음)

해결: list/detail/items.add/items.update/cancel 5개 라우트 진입부에
ALTER TABLE IF NOT EXISTS 로 editing_by/editing_at 컬럼 자동 증설.
컬럼이 이미 있으면 no-op. 첫 호출 1회만 ALTER 호출.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
chpark
2026-05-20 23:14:54 +09:00
parent 6be1633a31
commit 9b36ae64a5
5 changed files with 79 additions and 3 deletions
+15 -1
View File
@@ -1,10 +1,24 @@
import { NextRequest, NextResponse } from "next/server";
import { execute, queryOne } from "@/lib/db";
import { execute, pool, queryOne } from "@/lib/db";
import { requireMomoUser } from "@/lib/momo-guard";
let lockColsEnsured = false;
async function ensureLockCols() {
if (lockColsEnsured) return;
try {
await pool.query(`
ALTER TABLE momo_orders
ADD COLUMN IF NOT EXISTS editing_by TEXT,
ADD COLUMN IF NOT EXISTS editing_at TIMESTAMP;
`);
lockColsEnsured = true;
} catch { /* ignore */ }
}
export async function POST(req: NextRequest) {
const r = await requireMomoUser();
if (r instanceof NextResponse) return r;
await ensureLockCols();
const { objid } = await req.json();
const order = await queryOne<{ customer_objid: string; status: string; editing_by: string | null; lock_alive: boolean }>(
+18 -1
View File
@@ -1,11 +1,28 @@
import { NextRequest, NextResponse } from "next/server";
import { queryOne, queryRows } from "@/lib/db";
import { pool, queryOne, queryRows } from "@/lib/db";
import { requireMomoUser } from "@/lib/momo-guard";
import { getSupplierByBranch } from "@/lib/momo-branches";
// 편집 락 컬럼이 운영 DB 에 아직 없으면 1회 자동 증설
let lockColsEnsured = false;
async function ensureLockCols() {
if (lockColsEnsured) return;
try {
await pool.query(`
ALTER TABLE momo_orders
ADD COLUMN IF NOT EXISTS editing_by TEXT,
ADD COLUMN IF NOT EXISTS editing_at TIMESTAMP;
`);
lockColsEnsured = true;
} catch (err) {
console.error("[orders/detail/ensureLockCols]", err);
}
}
export async function POST(req: NextRequest) {
const r = await requireMomoUser();
if (r instanceof NextResponse) return r;
await ensureLockCols();
const { objid } = await req.json();
if (!objid) return NextResponse.json({ success: false, message: "objid 누락" }, { status: 400 });
+14
View File
@@ -6,11 +6,25 @@ import { createObjectId } from "@/lib/utils";
import { requireMomoUser } from "@/lib/momo-guard";
import { calcLine } from "@/lib/momo-pricing";
let lockColsEnsured = false;
async function ensureLockCols() {
if (lockColsEnsured) return;
try {
await pool.query(`
ALTER TABLE momo_orders
ADD COLUMN IF NOT EXISTS editing_by TEXT,
ADD COLUMN IF NOT EXISTS editing_at TIMESTAMP;
`);
lockColsEnsured = true;
} catch { /* ignore */ }
}
interface AddItem { itemObjid: string; qty: number }
export async function POST(req: NextRequest) {
const r = await requireMomoUser();
if (r instanceof NextResponse) return r;
await ensureLockCols();
const body = await req.json().catch(() => ({}));
const { orderObjid, items } = body as { orderObjid?: string; items?: AddItem[] };
@@ -6,6 +6,19 @@ import { pool } from "@/lib/db";
import { requireMomoUser } from "@/lib/momo-guard";
import { calcLine } from "@/lib/momo-pricing";
let lockColsEnsured = false;
async function ensureLockCols() {
if (lockColsEnsured) return;
try {
await pool.query(`
ALTER TABLE momo_orders
ADD COLUMN IF NOT EXISTS editing_by TEXT,
ADD COLUMN IF NOT EXISTS editing_at TIMESTAMP;
`);
lockColsEnsured = true;
} catch { /* ignore */ }
}
interface LineUpdate {
objid: string; // 라인 OBJID
qty?: number; // 새 수량 (undefined 면 변경 안 함)
@@ -15,6 +28,7 @@ interface LineUpdate {
export async function POST(req: NextRequest) {
const r = await requireMomoUser();
if (r instanceof NextResponse) return r;
await ensureLockCols();
const body = await req.json().catch(() => ({}));
const { orderObjid, lines } = body as { orderObjid?: string; lines?: LineUpdate[] };
+18 -1
View File
@@ -1,10 +1,27 @@
import { NextRequest, NextResponse } from "next/server";
import { queryRows } from "@/lib/db";
import { pool, queryRows } from "@/lib/db";
// momo_orders 에 편집 락 컬럼이 없으면 1회 자동 증설
let lockColsEnsured = false;
async function ensureLockCols() {
if (lockColsEnsured) return;
try {
await pool.query(`
ALTER TABLE momo_orders
ADD COLUMN IF NOT EXISTS editing_by TEXT,
ADD COLUMN IF NOT EXISTS editing_at TIMESTAMP;
`);
lockColsEnsured = true;
} catch (err) {
console.error("[orders/list/ensureLockCols]", err);
}
}
import { requireMomoUser } from "@/lib/momo-guard";
export async function POST(req: NextRequest) {
const r = await requireMomoUser();
if (r instanceof NextResponse) return r;
await ensureLockCols();
const body = await req.json().catch(() => ({}));
const { dateFrom, dateTo, status, statuses, customerObjid, keyword, mine } = body as {