refactor: standardize quotation marks across all files and improve code consistency
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import type { Account } from "@/lib/types"
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import type { Account } from "@/lib/types";
|
||||
|
||||
export const accountService = {
|
||||
async create(data: Omit<Account, "id">): Promise<Account> {
|
||||
@@ -14,7 +14,7 @@ export const accountService = {
|
||||
currency: data.currency,
|
||||
lastImport: data.lastImport,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
return {
|
||||
id: created.id,
|
||||
@@ -26,10 +26,13 @@ export const accountService = {
|
||||
balance: created.balance,
|
||||
currency: created.currency,
|
||||
lastImport: created.lastImport,
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
async update(id: string, data: Partial<Omit<Account, "id">>): Promise<Account> {
|
||||
async update(
|
||||
id: string,
|
||||
data: Partial<Omit<Account, "id">>,
|
||||
): Promise<Account> {
|
||||
const updated = await prisma.account.update({
|
||||
where: { id },
|
||||
data: {
|
||||
@@ -42,7 +45,7 @@ export const accountService = {
|
||||
currency: data.currency,
|
||||
lastImport: data.lastImport,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
return {
|
||||
id: updated.id,
|
||||
@@ -54,14 +57,13 @@ export const accountService = {
|
||||
balance: updated.balance,
|
||||
currency: updated.currency,
|
||||
lastImport: updated.lastImport,
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
// Transactions will be deleted automatically due to onDelete: Cascade
|
||||
await prisma.account.delete({
|
||||
where: { id },
|
||||
})
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import type { BankingData, Account, Transaction, Folder, Category } from "@/lib/types"
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import type {
|
||||
BankingData,
|
||||
Account,
|
||||
Transaction,
|
||||
Folder,
|
||||
Category,
|
||||
} from "@/lib/types";
|
||||
|
||||
export const bankingService = {
|
||||
async getAllData(): Promise<BankingData> {
|
||||
@@ -17,50 +23,57 @@ export const bankingService = {
|
||||
}),
|
||||
prisma.folder.findMany(),
|
||||
prisma.category.findMany(),
|
||||
])
|
||||
]);
|
||||
|
||||
// Transform Prisma models to match our types
|
||||
return {
|
||||
accounts: accounts.map((a): Account => ({
|
||||
id: a.id,
|
||||
name: a.name,
|
||||
bankId: a.bankId,
|
||||
accountNumber: a.accountNumber,
|
||||
type: a.type as Account["type"],
|
||||
folderId: a.folderId,
|
||||
balance: a.balance,
|
||||
currency: a.currency,
|
||||
lastImport: a.lastImport,
|
||||
})),
|
||||
transactions: transactions.map((t): Transaction => ({
|
||||
id: t.id,
|
||||
accountId: t.accountId,
|
||||
date: t.date,
|
||||
amount: t.amount,
|
||||
description: t.description,
|
||||
type: t.type as Transaction["type"],
|
||||
categoryId: t.categoryId,
|
||||
isReconciled: t.isReconciled,
|
||||
fitId: t.fitId,
|
||||
memo: t.memo ?? undefined,
|
||||
checkNum: t.checkNum ?? undefined,
|
||||
})),
|
||||
folders: folders.map((f): Folder => ({
|
||||
id: f.id,
|
||||
name: f.name,
|
||||
parentId: f.parentId,
|
||||
color: f.color,
|
||||
icon: f.icon,
|
||||
})),
|
||||
categories: categories.map((c): Category => ({
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
color: c.color,
|
||||
icon: c.icon,
|
||||
keywords: JSON.parse(c.keywords) as string[],
|
||||
parentId: c.parentId,
|
||||
})),
|
||||
}
|
||||
accounts: accounts.map(
|
||||
(a): Account => ({
|
||||
id: a.id,
|
||||
name: a.name,
|
||||
bankId: a.bankId,
|
||||
accountNumber: a.accountNumber,
|
||||
type: a.type as Account["type"],
|
||||
folderId: a.folderId,
|
||||
balance: a.balance,
|
||||
currency: a.currency,
|
||||
lastImport: a.lastImport,
|
||||
}),
|
||||
),
|
||||
transactions: transactions.map(
|
||||
(t): Transaction => ({
|
||||
id: t.id,
|
||||
accountId: t.accountId,
|
||||
date: t.date,
|
||||
amount: t.amount,
|
||||
description: t.description,
|
||||
type: t.type as Transaction["type"],
|
||||
categoryId: t.categoryId,
|
||||
isReconciled: t.isReconciled,
|
||||
fitId: t.fitId,
|
||||
memo: t.memo ?? undefined,
|
||||
checkNum: t.checkNum ?? undefined,
|
||||
}),
|
||||
),
|
||||
folders: folders.map(
|
||||
(f): Folder => ({
|
||||
id: f.id,
|
||||
name: f.name,
|
||||
parentId: f.parentId,
|
||||
color: f.color,
|
||||
icon: f.icon,
|
||||
}),
|
||||
),
|
||||
categories: categories.map(
|
||||
(c): Category => ({
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
color: c.color,
|
||||
icon: c.icon,
|
||||
keywords: JSON.parse(c.keywords) as string[],
|
||||
parentId: c.parentId,
|
||||
}),
|
||||
),
|
||||
};
|
||||
},
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import type { Category } from "@/lib/types"
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import type { Category } from "@/lib/types";
|
||||
|
||||
export const categoryService = {
|
||||
async create(data: Omit<Category, "id">): Promise<Category> {
|
||||
@@ -11,7 +11,7 @@ export const categoryService = {
|
||||
keywords: JSON.stringify(data.keywords),
|
||||
parentId: data.parentId,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
return {
|
||||
id: created.id,
|
||||
@@ -20,10 +20,13 @@ export const categoryService = {
|
||||
icon: created.icon,
|
||||
keywords: JSON.parse(created.keywords) as string[],
|
||||
parentId: created.parentId,
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
async update(id: string, data: Partial<Omit<Category, "id">>): Promise<Category> {
|
||||
async update(
|
||||
id: string,
|
||||
data: Partial<Omit<Category, "id">>,
|
||||
): Promise<Category> {
|
||||
const updated = await prisma.category.update({
|
||||
where: { id },
|
||||
data: {
|
||||
@@ -33,7 +36,7 @@ export const categoryService = {
|
||||
keywords: data.keywords ? JSON.stringify(data.keywords) : undefined,
|
||||
parentId: data.parentId,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
return {
|
||||
id: updated.id,
|
||||
@@ -42,7 +45,7 @@ export const categoryService = {
|
||||
icon: updated.icon,
|
||||
keywords: JSON.parse(updated.keywords) as string[],
|
||||
parentId: updated.parentId,
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
@@ -50,11 +53,10 @@ export const categoryService = {
|
||||
await prisma.transaction.updateMany({
|
||||
where: { categoryId: id },
|
||||
data: { categoryId: null },
|
||||
})
|
||||
});
|
||||
|
||||
await prisma.category.delete({
|
||||
where: { id },
|
||||
})
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import type { Folder } from "@/lib/types"
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import type { Folder } from "@/lib/types";
|
||||
|
||||
export class FolderNotFoundError extends Error {
|
||||
constructor(id: string) {
|
||||
super(`Folder not found: ${id}`)
|
||||
this.name = "FolderNotFoundError"
|
||||
super(`Folder not found: ${id}`);
|
||||
this.name = "FolderNotFoundError";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ export const folderService = {
|
||||
color: data.color,
|
||||
icon: data.icon,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
return {
|
||||
id: created.id,
|
||||
@@ -25,7 +25,7 @@ export const folderService = {
|
||||
parentId: created.parentId,
|
||||
color: created.color,
|
||||
icon: created.icon,
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
async update(id: string, data: Partial<Omit<Folder, "id">>): Promise<Folder> {
|
||||
@@ -37,7 +37,7 @@ export const folderService = {
|
||||
color: data.color,
|
||||
icon: data.icon,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
return {
|
||||
id: updated.id,
|
||||
@@ -45,34 +45,33 @@ export const folderService = {
|
||||
parentId: updated.parentId,
|
||||
color: updated.color,
|
||||
icon: updated.icon,
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
const folder = await prisma.folder.findUnique({
|
||||
where: { id },
|
||||
include: { children: true },
|
||||
})
|
||||
});
|
||||
|
||||
if (!folder) {
|
||||
throw new FolderNotFoundError(id)
|
||||
throw new FolderNotFoundError(id);
|
||||
}
|
||||
|
||||
// Business rule: Move accounts to root (null folderId)
|
||||
await prisma.account.updateMany({
|
||||
where: { folderId: id },
|
||||
data: { folderId: null },
|
||||
})
|
||||
});
|
||||
|
||||
// Business rule: Move subfolders to parent (or root if no parent)
|
||||
await prisma.folder.updateMany({
|
||||
where: { parentId: id },
|
||||
data: { parentId: folder.parentId },
|
||||
})
|
||||
});
|
||||
|
||||
await prisma.folder.delete({
|
||||
where: { id },
|
||||
})
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import type { Transaction } from "@/lib/types"
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import type { Transaction } from "@/lib/types";
|
||||
|
||||
export interface CreateManyResult {
|
||||
count: number
|
||||
transactions: Transaction[]
|
||||
count: number;
|
||||
transactions: Transaction[];
|
||||
}
|
||||
|
||||
export const transactionService = {
|
||||
@@ -18,18 +18,18 @@ export const transactionService = {
|
||||
accountId: true,
|
||||
fitId: true,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
const existingSet = new Set(
|
||||
existingTransactions.map((t) => `${t.accountId}-${t.fitId}`),
|
||||
)
|
||||
);
|
||||
|
||||
const newTransactions = transactions.filter(
|
||||
(t) => !existingSet.has(`${t.accountId}-${t.fitId}`),
|
||||
)
|
||||
);
|
||||
|
||||
if (newTransactions.length === 0) {
|
||||
return { count: 0, transactions: [] }
|
||||
return { count: 0, transactions: [] };
|
||||
}
|
||||
|
||||
const created = await prisma.transaction.createMany({
|
||||
@@ -45,12 +45,15 @@ export const transactionService = {
|
||||
memo: t.memo,
|
||||
checkNum: t.checkNum,
|
||||
})),
|
||||
})
|
||||
});
|
||||
|
||||
return { count: created.count, transactions: newTransactions }
|
||||
return { count: created.count, transactions: newTransactions };
|
||||
},
|
||||
|
||||
async update(id: string, data: Partial<Omit<Transaction, "id">>): Promise<Transaction> {
|
||||
async update(
|
||||
id: string,
|
||||
data: Partial<Omit<Transaction, "id">>,
|
||||
): Promise<Transaction> {
|
||||
const updated = await prisma.transaction.update({
|
||||
where: { id },
|
||||
data: {
|
||||
@@ -65,7 +68,7 @@ export const transactionService = {
|
||||
memo: data.memo,
|
||||
checkNum: data.checkNum,
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
return {
|
||||
id: updated.id,
|
||||
@@ -79,13 +82,12 @@ export const transactionService = {
|
||||
fitId: updated.fitId,
|
||||
memo: updated.memo ?? undefined,
|
||||
checkNum: updated.checkNum ?? undefined,
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await prisma.transaction.delete({
|
||||
where: { id },
|
||||
})
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user