Files
kt-financial-system/apps/backend/api/finance/reimbursements.post.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

73 lines
2.1 KiB
TypeScript

import type { TransactionStatus } from '~/utils/finance-repository';
import { readBody } from 'h3';
import { createTransaction } from '~/utils/finance-repository';
import { useResponseError, useResponseSuccess } from '~/utils/response';
import { notifyTransactionWebhook } from '~/utils/telegram-webhook';
const DEFAULT_CURRENCY = 'CNY';
const DEFAULT_STATUS: TransactionStatus = 'pending';
const ALLOWED_STATUSES = new Set<TransactionStatus>([
'draft',
'pending',
'approved',
'rejected',
'paid',
]);
export default defineEventHandler(async (event) => {
const body = await readBody(event);
if (!body?.amount || !body?.transactionDate) {
return useResponseError('缺少必填字段', -1);
}
const amount = Number(body.amount);
if (Number.isNaN(amount)) {
return useResponseError('金额格式不正确', -1);
}
const type =
(body.type as 'expense' | 'income' | 'transfer' | undefined) ?? 'expense';
const status =
(body.status as TransactionStatus | undefined) ?? DEFAULT_STATUS;
if (!ALLOWED_STATUSES.has(status)) {
return useResponseError('状态值不合法', -1);
}
const reimbursement = await createTransaction({
type,
amount,
currency: body.currency ?? DEFAULT_CURRENCY,
categoryId: body.categoryId ?? null,
accountId: body.accountId ?? null,
transactionDate: body.transactionDate,
description:
body.description ??
body.item ??
(body.notes ? `${body.notes}` : '') ??
'',
project: body.project ?? body.category ?? null,
memo: body.memo ?? body.notes ?? null,
status,
reimbursementBatch: body.reimbursementBatch ?? null,
reviewNotes: body.reviewNotes ?? null,
submittedBy: body.submittedBy ?? body.requester ?? null,
approvedBy: body.approvedBy ?? null,
approvedAt: body.approvedAt ?? null,
statusUpdatedAt: body.statusUpdatedAt ?? undefined,
});
notifyTransactionWebhook(reimbursement, {
action: 'reimbursement.created',
}).catch((error) =>
console.error(
'[finance][reimbursements.post] webhook notify failed',
error,
),
);
return useResponseSuccess(reimbursement);
});