feat: migrate backend storage to postgres
This commit is contained in:
@@ -1,21 +1,23 @@
|
||||
import type { TransactionStatus } from '~/utils/finance-repository';
|
||||
|
||||
import { readBody } from 'h3';
|
||||
import {
|
||||
createTransaction,
|
||||
type TransactionStatus,
|
||||
getAccountById,
|
||||
getCategoryById,
|
||||
} from '~/utils/finance-repository';
|
||||
import { useResponseError, useResponseSuccess } from '~/utils/response';
|
||||
import { notifyTransactionWebhook } from '~/utils/telegram-webhook';
|
||||
import { notifyTransaction } from '~/utils/telegram-bot';
|
||||
import db from '~/utils/sqlite';
|
||||
import { notifyTransactionWebhook } from '~/utils/telegram-webhook';
|
||||
|
||||
const DEFAULT_CURRENCY = 'CNY';
|
||||
const ALLOWED_STATUSES: TransactionStatus[] = [
|
||||
const ALLOWED_STATUSES = new Set<TransactionStatus>([
|
||||
'draft',
|
||||
'pending',
|
||||
'approved',
|
||||
'rejected',
|
||||
'paid',
|
||||
];
|
||||
]);
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event);
|
||||
@@ -29,13 +31,12 @@ export default defineEventHandler(async (event) => {
|
||||
return useResponseError('金额格式不正确', -1);
|
||||
}
|
||||
|
||||
const status =
|
||||
(body.status as TransactionStatus | undefined) ?? 'approved';
|
||||
if (!ALLOWED_STATUSES.includes(status)) {
|
||||
const status = (body.status as TransactionStatus | undefined) ?? 'approved';
|
||||
if (!ALLOWED_STATUSES.has(status)) {
|
||||
return useResponseError('状态值不合法', -1);
|
||||
}
|
||||
|
||||
const transaction = createTransaction({
|
||||
const transaction = await createTransaction({
|
||||
type: body.type,
|
||||
amount,
|
||||
currency: body.currency ?? DEFAULT_CURRENCY,
|
||||
@@ -61,23 +62,12 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
// 发送Telegram通知(新功能)
|
||||
try {
|
||||
// 获取分类和账户名称
|
||||
let categoryName: string | undefined;
|
||||
let accountName: string | undefined;
|
||||
|
||||
if (transaction.categoryId) {
|
||||
const category = db
|
||||
.prepare<{ name: string }>('SELECT name FROM finance_categories WHERE id = ?')
|
||||
.get(transaction.categoryId);
|
||||
categoryName = category?.name;
|
||||
}
|
||||
|
||||
if (transaction.accountId) {
|
||||
const account = db
|
||||
.prepare<{ name: string }>('SELECT name FROM finance_accounts WHERE id = ?')
|
||||
.get(transaction.accountId);
|
||||
accountName = account?.name;
|
||||
}
|
||||
const category = transaction.categoryId
|
||||
? await getCategoryById(transaction.categoryId)
|
||||
: null;
|
||||
const account = transaction.accountId
|
||||
? await getAccountById(transaction.accountId)
|
||||
: null;
|
||||
|
||||
await notifyTransaction(
|
||||
{
|
||||
@@ -85,8 +75,8 @@ export default defineEventHandler(async (event) => {
|
||||
type: transaction.type,
|
||||
amount: transaction.amount,
|
||||
currency: transaction.currency,
|
||||
categoryName,
|
||||
accountName,
|
||||
categoryName: category?.name,
|
||||
accountName: account?.name,
|
||||
transactionDate: transaction.transactionDate,
|
||||
description: transaction.description || undefined,
|
||||
status: transaction.status,
|
||||
|
||||
Reference in New Issue
Block a user