mmday-firebase/src/services/adminPointService.ts
윤정민 5508d2581a Omit undefined reversalOf in admin point ledger writes
- reversalOf 미지정 시 undefined가 원장 문서 스프레드로 흘러가 Firestore가 "Cannot use undefined as a Firestore value"로 지급 전체를 실패시키던 버그 수정
- 조건부 스프레드로 값이 있을 때만 필드를 포함하도록 변경
- 회귀 테스트 추가: reversalOf 없이 지급 시 원장에 키 자체가 없음 + 명시 시 그대로 기록됨 (에뮬레이터 vitest 2건 통과)
2026-07-21 15:06:03 +09:00

6 lines
937 B
TypeScript

import { firestore } from "../firebase";
import { HttpError } from "../middleware/errors";
import { PointLedgerType } from "../types/points";
import { applyPointChangesTx } from "./pointService";
export async function adminPointChange(uid: string, amount: number, kind: "credit" | "debit", clientIdempotencyKey: string, reason: string, actor: string, reversalOf?: string) { if (!/^[A-Za-z0-9_-]{1,100}$/.test(clientIdempotencyKey) || !Number.isSafeInteger(amount) || amount <= 0 || typeof reason !== "string" || reason.length < 1 || reason.length > 500) throw new HttpError(400, "invalid admin point change", "INVALID_INPUT"); return firestore.runTransaction((tx) => applyPointChangesTx(tx, uid, [{ txId: `${uid}:admin:${clientIdempotencyKey}`, type: kind === "credit" ? PointLedgerType.AdminCredit : PointLedgerType.AdminDebit, amount, adminReason: reason, adminActor: actor, ...(reversalOf !== undefined ? { reversalOf } : {}) }])); }