refactor: standardize quotation marks across all files and improve code consistency
This commit is contained in:
118
lib/store-db.ts
118
lib/store-db.ts
@@ -1,27 +1,35 @@
|
||||
"use client"
|
||||
"use client";
|
||||
|
||||
import type { BankingData, Account, Transaction, Folder, Category } from "./types"
|
||||
import type {
|
||||
BankingData,
|
||||
Account,
|
||||
Transaction,
|
||||
Folder,
|
||||
Category,
|
||||
} from "./types";
|
||||
|
||||
const API_BASE = "/api/banking"
|
||||
const API_BASE = "/api/banking";
|
||||
|
||||
export async function loadData(): Promise<BankingData> {
|
||||
const response = await fetch(API_BASE)
|
||||
const response = await fetch(API_BASE);
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to load data")
|
||||
throw new Error("Failed to load data");
|
||||
}
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function addAccount(account: Omit<Account, "id">): Promise<Account> {
|
||||
export async function addAccount(
|
||||
account: Omit<Account, "id">,
|
||||
): Promise<Account> {
|
||||
const response = await fetch(`${API_BASE}/accounts`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(account),
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to add account")
|
||||
throw new Error("Failed to add account");
|
||||
}
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function updateAccount(account: Account): Promise<Account> {
|
||||
@@ -29,44 +37,48 @@ export async function updateAccount(account: Account): Promise<Account> {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(account),
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to update account")
|
||||
throw new Error("Failed to update account");
|
||||
}
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function deleteAccount(accountId: string): Promise<void> {
|
||||
const response = await fetch(`${API_BASE}/accounts?id=${accountId}`, {
|
||||
method: "DELETE",
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to delete account")
|
||||
throw new Error("Failed to delete account");
|
||||
}
|
||||
}
|
||||
|
||||
export async function addTransactions(transactions: Transaction[]): Promise<{ count: number; transactions: Transaction[] }> {
|
||||
export async function addTransactions(
|
||||
transactions: Transaction[],
|
||||
): Promise<{ count: number; transactions: Transaction[] }> {
|
||||
const response = await fetch(`${API_BASE}/transactions`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(transactions),
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to add transactions")
|
||||
throw new Error("Failed to add transactions");
|
||||
}
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function updateTransaction(transaction: Transaction): Promise<Transaction> {
|
||||
export async function updateTransaction(
|
||||
transaction: Transaction,
|
||||
): Promise<Transaction> {
|
||||
const response = await fetch(`${API_BASE}/transactions`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(transaction),
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to update transaction")
|
||||
throw new Error("Failed to update transaction");
|
||||
}
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function addFolder(folder: Omit<Folder, "id">): Promise<Folder> {
|
||||
@@ -74,11 +86,11 @@ export async function addFolder(folder: Omit<Folder, "id">): Promise<Folder> {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(folder),
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to add folder")
|
||||
throw new Error("Failed to add folder");
|
||||
}
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function updateFolder(folder: Folder): Promise<Folder> {
|
||||
@@ -86,32 +98,34 @@ export async function updateFolder(folder: Folder): Promise<Folder> {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(folder),
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to update folder")
|
||||
throw new Error("Failed to update folder");
|
||||
}
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function deleteFolder(folderId: string): Promise<void> {
|
||||
const response = await fetch(`${API_BASE}/folders?id=${folderId}`, {
|
||||
method: "DELETE",
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to delete folder")
|
||||
throw new Error("Failed to delete folder");
|
||||
}
|
||||
}
|
||||
|
||||
export async function addCategory(category: Omit<Category, "id">): Promise<Category> {
|
||||
export async function addCategory(
|
||||
category: Omit<Category, "id">,
|
||||
): Promise<Category> {
|
||||
const response = await fetch(`${API_BASE}/categories`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(category),
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to add category")
|
||||
throw new Error("Failed to add category");
|
||||
}
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function updateCategory(category: Category): Promise<Category> {
|
||||
@@ -119,55 +133,57 @@ export async function updateCategory(category: Category): Promise<Category> {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(category),
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to update category")
|
||||
throw new Error("Failed to update category");
|
||||
}
|
||||
return response.json()
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function deleteCategory(categoryId: string): Promise<void> {
|
||||
const response = await fetch(`${API_BASE}/categories?id=${categoryId}`, {
|
||||
method: "DELETE",
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to delete category")
|
||||
throw new Error("Failed to delete category");
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-categorize a transaction based on keywords
|
||||
export function autoCategorize(description: string, categories: Category[]): string | null {
|
||||
const lowerDesc = description.toLowerCase()
|
||||
export function autoCategorize(
|
||||
description: string,
|
||||
categories: Category[],
|
||||
): string | null {
|
||||
const lowerDesc = description.toLowerCase();
|
||||
|
||||
for (const category of categories) {
|
||||
for (const keyword of category.keywords) {
|
||||
const lowerKeyword = keyword.toLowerCase()
|
||||
|
||||
const lowerKeyword = keyword.toLowerCase();
|
||||
|
||||
// Pour les keywords courts (< 6 chars), matcher uniquement des mots entiers
|
||||
// Évite les faux positifs comme "chat" dans "achat"
|
||||
if (lowerKeyword.length < 6) {
|
||||
const wordBoundary = new RegExp(`\\b${escapeRegex(lowerKeyword)}\\b`)
|
||||
const wordBoundary = new RegExp(`\\b${escapeRegex(lowerKeyword)}\\b`);
|
||||
if (wordBoundary.test(lowerDesc)) {
|
||||
return category.id
|
||||
return category.id;
|
||||
}
|
||||
} else {
|
||||
// Pour les keywords plus longs, includes() suffit
|
||||
if (lowerDesc.includes(lowerKeyword)) {
|
||||
return category.id
|
||||
return category.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
|
||||
// Échappe les caractères spéciaux pour les regex
|
||||
function escapeRegex(str: string): string {
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
||||
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
export function generateId(): string {
|
||||
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
|
||||
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user