Files
kt-financial-system/apps/backend/api/finance/reimbursements/[id].put.ts
你的用户名 b68511b2e2
Some checks failed
Deploy to Production / Build and Test (push) Successful in 10m51s
Deploy to Production / Deploy to Server (push) Failing after 6m41s
feat: migrate backend storage to postgres
2025-11-06 22:01:50 +08:00

87 lines
2.7 KiB
TypeScript

import type { TransactionStatus } from '~/utils/finance-repository';
import { getRouterParam, readBody } from 'h3';
import {
restoreTransaction,
updateTransaction,
} from '~/utils/finance-repository';
import { useResponseError, useResponseSuccess } from '~/utils/response';
const ALLOWED_STATUSES = new Set<TransactionStatus>([
'draft',
'pending',
'approved',
'rejected',
'paid',
]);
export default defineEventHandler(async (event) => {
const id = Number(getRouterParam(event, 'id'));
if (Number.isNaN(id)) {
return useResponseError('参数错误', -1);
}
const body = await readBody(event);
if (body?.isDeleted === false) {
const restored = await restoreTransaction(id);
if (!restored) {
return useResponseError('报销单不存在', -1);
}
return useResponseSuccess(restored);
}
const payload: Record<string, unknown> = {};
if (body?.type) payload.type = body.type;
if (body?.amount !== undefined) {
const amount = Number(body.amount);
if (Number.isNaN(amount)) {
return useResponseError('金额格式不正确', -1);
}
payload.amount = amount;
}
if (body?.currency) payload.currency = body.currency;
if (body?.categoryId !== undefined)
payload.categoryId = body.categoryId ?? null;
if (body?.accountId !== undefined) payload.accountId = body.accountId ?? null;
if (body?.transactionDate) payload.transactionDate = body.transactionDate;
if (body?.description !== undefined)
payload.description = body.description ?? '';
if (body?.project !== undefined) payload.project = body.project ?? null;
if (body?.memo !== undefined) payload.memo = body.memo ?? null;
if (body?.isDeleted !== undefined) payload.isDeleted = body.isDeleted;
if (body?.status !== undefined) {
const status = body.status as TransactionStatus;
if (!ALLOWED_STATUSES.has(status)) {
return useResponseError('状态值不合法', -1);
}
payload.status = status;
}
if (body?.statusUpdatedAt !== undefined) {
payload.statusUpdatedAt = body.statusUpdatedAt;
}
if (body?.reimbursementBatch !== undefined) {
payload.reimbursementBatch = body.reimbursementBatch ?? null;
}
if (body?.reviewNotes !== undefined) {
payload.reviewNotes = body.reviewNotes ?? null;
}
if (body?.submittedBy !== undefined) {
payload.submittedBy = body.submittedBy ?? null;
}
if (body?.approvedBy !== undefined) {
payload.approvedBy = body.approvedBy ?? null;
}
if (body?.approvedAt !== undefined) {
payload.approvedAt = body.approvedAt ?? null;
}
const updated = await updateTransaction(id, payload);
if (!updated) {
return useResponseError('报销单不存在', -1);
}
return useResponseSuccess(updated);
});