Update import statements to use path aliases throughout codebase

This commit is contained in:
GitHub Copilot
2025-05-06 11:53:33 +00:00
parent 984cd42f27
commit 069faac7bc
3 changed files with 38 additions and 35 deletions

View File

@@ -1,6 +1,6 @@
import type { PrismaClient } from '@prisma/client'; import type { Prisma, PrismaClient } from '@prisma/client';
import { Decimal } from '@prisma/client/runtime/library'; import { Decimal } from '@prisma/client/runtime/library';
import type { Account, Transaction } from '../types'; import type { Account, Transaction } from '@types';
import { prisma } from './prisma'; import { prisma } from './prisma';
// Define the enums ourselves since Prisma isn't exporting them // Define the enums ourselves since Prisma isn't exporting them
@@ -37,7 +37,7 @@ export const accountService = {
async getAll(): Promise<Account[]> { async getAll(): Promise<Account[]> {
return prisma.account.findMany({ return prisma.account.findMany({
orderBy: { name: 'asc' }, orderBy: { name: 'asc' },
}); }) as Promise<Account[]>;
}, },
/** /**
@@ -46,7 +46,7 @@ export const accountService = {
async getById(id: string): Promise<Account | null> { async getById(id: string): Promise<Account | null> {
return prisma.account.findUnique({ return prisma.account.findUnique({
where: { id }, where: { id },
}); }) as Promise<Account | null>;
}, },
/** /**
@@ -64,7 +64,7 @@ export const accountService = {
}): Promise<Account> { }): Promise<Account> {
return prisma.account.create({ return prisma.account.create({
data, data,
}); }) as Promise<Account>;
}, },
/** /**
@@ -86,7 +86,7 @@ export const accountService = {
return prisma.account.update({ return prisma.account.update({
where: { id }, where: { id },
data, data,
}); }) as Promise<Account>;
}, },
/** /**
@@ -95,7 +95,7 @@ export const accountService = {
async delete(id: string): Promise<Account | null> { async delete(id: string): Promise<Account | null> {
return prisma.account.delete({ return prisma.account.delete({
where: { id }, where: { id },
}); }) as Promise<Account>;
}, },
/** /**
@@ -115,7 +115,7 @@ export const accountService = {
increment: amount, increment: amount,
}, },
}, },
}); }) as Promise<Account>;
}, },
}; };
@@ -127,7 +127,7 @@ export const transactionService = {
async getAll(): Promise<Transaction[]> { async getAll(): Promise<Transaction[]> {
return prisma.transaction.findMany({ return prisma.transaction.findMany({
orderBy: { date: 'desc' }, orderBy: { date: 'desc' },
}); }) as Promise<Transaction[]>;
}, },
/** /**
@@ -137,7 +137,7 @@ export const transactionService = {
return prisma.transaction.findMany({ return prisma.transaction.findMany({
where: { accountId }, where: { accountId },
orderBy: { date: 'desc' }, orderBy: { date: 'desc' },
}); }) as Promise<Transaction[]>;
}, },
/** /**
@@ -146,7 +146,7 @@ export const transactionService = {
async getById(id: string): Promise<Transaction | null> { async getById(id: string): Promise<Transaction | null> {
return prisma.transaction.findUnique({ return prisma.transaction.findUnique({
where: { id }, where: { id },
}); }) as Promise<Transaction | null>;
}, },
/** /**
@@ -164,14 +164,14 @@ export const transactionService = {
tags?: string; tags?: string;
}): Promise<Transaction> { }): Promise<Transaction> {
// Use a transaction to ensure data consistency // Use a transaction to ensure data consistency
return prisma.$transaction(async (prismaClient: PrismaClient) => { return prisma.$transaction<Transaction>(async (tx) => {
// Create the transaction // Create the transaction
const transaction = await prismaClient.transaction.create({ const transaction = await tx.transaction.create({
data, data,
}); });
// Update the account balance // Update the account balance
await prismaClient.account.update({ await tx.account.update({
where: { id: data.accountId }, where: { id: data.accountId },
data: { data: {
balance: { balance: {
@@ -180,7 +180,7 @@ export const transactionService = {
}, },
}); });
return transaction; return transaction as Transaction;
}); });
}, },
@@ -203,26 +203,26 @@ export const transactionService = {
): Promise<Transaction | null> { ): Promise<Transaction | null> {
// If amount is changing, we need to adjust the account balance // If amount is changing, we need to adjust the account balance
if (typeof data.amount !== 'undefined') { if (typeof data.amount !== 'undefined') {
return prisma.$transaction(async (prismaClient: PrismaClient) => { return prisma.$transaction<Transaction | null>(async (tx) => {
// Get the current transaction to calculate difference // Get the current transaction to calculate difference
const currentTxn = await prismaClient.transaction.findUnique({ const currentTxn = await tx.transaction.findUnique({
where: { id }, where: { id },
}); });
if (!currentTxn) return null; if (!currentTxn) return null;
// Calculate amount difference - amount is guaranteed to be defined at this point // Amount is guaranteed to be defined at this point since we checked above
const amount = data.amount; // Store in a constant to help TypeScript understand const amount = data.amount as number; // Use type assertion instead of non-null assertion
const amountDifference = amount - Number(currentTxn.amount); const amountDifference = amount - Number(currentTxn.amount);
// Update transaction // Update transaction
const updatedTxn = await prismaClient.transaction.update({ const updatedTxn = await tx.transaction.update({
where: { id }, where: { id },
data, data,
}); });
// Update account balance // Update account balance
await prismaClient.account.update({ await tx.account.update({
where: { id: data.accountId || currentTxn.accountId }, where: { id: data.accountId || currentTxn.accountId },
data: { data: {
balance: { balance: {
@@ -231,7 +231,7 @@ export const transactionService = {
}, },
}); });
return updatedTxn; return updatedTxn as Transaction;
}); });
} }
@@ -239,28 +239,28 @@ export const transactionService = {
return prisma.transaction.update({ return prisma.transaction.update({
where: { id }, where: { id },
data, data,
}); }) as Promise<Transaction>;
}, },
/** /**
* Delete a transaction and adjust account balance * Delete a transaction and adjust account balance
*/ */
async delete(id: string): Promise<Transaction | null> { async delete(id: string): Promise<Transaction | null> {
return prisma.$transaction(async (prismaClient: PrismaClient) => { return prisma.$transaction<Transaction | null>(async (tx) => {
// Get transaction before deleting // Get transaction before deleting
const transaction = await prismaClient.transaction.findUnique({ const transaction = await tx.transaction.findUnique({
where: { id }, where: { id },
}); });
if (!transaction) return null; if (!transaction) return null;
// Delete the transaction // Delete the transaction
const deletedTxn = await prismaClient.transaction.delete({ const deletedTxn = await tx.transaction.delete({
where: { id }, where: { id },
}); });
// Adjust the account balance (reverse the transaction amount) // Adjust the account balance (reverse the transaction amount)
await prismaClient.account.update({ await tx.account.update({
where: { id: transaction.accountId }, where: { id: transaction.accountId },
data: { data: {
balance: { balance: {
@@ -269,7 +269,7 @@ export const transactionService = {
}, },
}); });
return deletedTxn; return deletedTxn as Transaction;
}); });
}, },
}; };

View File

@@ -10,9 +10,9 @@
* - Set up proper CORS configuration * - Set up proper CORS configuration
*/ */
import type { APIRoute } from 'astro';
import { accountService, transactionService } from '@data/db.service'; import { accountService, transactionService } from '@data/db.service';
import type { Transaction, TransactionStatus, TransactionType } from '@types'; import type { Transaction, TransactionStatus, TransactionType } from '@types';
import type { APIRoute } from 'astro';
/** /**
* TODO: API Improvements * TODO: API Improvements

View File

@@ -1,7 +1,8 @@
import { AccountType, TransactionType, accountService, transactionService } from '@data/db.service';
import { prisma } from '@data/prisma';
import type { Account, Transaction } from '@types';
import supertest from 'supertest'; import supertest from 'supertest';
import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { accountService, transactionService } from '../data/db.service';
import { prisma } from '../data/prisma';
// Define a test server // Define a test server
const BASE_URL = 'http://localhost:4322'; const BASE_URL = 'http://localhost:4322';
@@ -34,7 +35,7 @@ describe('Database Integration Tests', () => {
bankName: 'Test Bank', bankName: 'Test Bank',
accountNumber: '123456', accountNumber: '123456',
name: 'Test Account', name: 'Test Account',
type: 'CHECKING', type: AccountType.CHECKING,
balance: 1000, balance: 1000,
notes: 'Created for automated testing', notes: 'Created for automated testing',
}); });
@@ -90,7 +91,7 @@ describe('Database Integration Tests', () => {
expect(response.body.length).toBeGreaterThan(0); expect(response.body.length).toBeGreaterThan(0);
// Check if our test account is in the response // Check if our test account is in the response
const foundAccount = response.body.find((account: any) => account.id === testAccountId); const foundAccount = response.body.find((account: Account) => account.id === testAccountId);
expect(foundAccount).toBeDefined(); expect(foundAccount).toBeDefined();
}); });
@@ -118,7 +119,7 @@ describe('Database Integration Tests', () => {
description: 'Test Transaction', description: 'Test Transaction',
amount: -50.25, amount: -50.25,
category: 'Testing', category: 'Testing',
type: 'WITHDRAWAL', type: TransactionType.WITHDRAWAL,
}; };
const response = await request const response = await request
@@ -145,7 +146,9 @@ describe('Database Integration Tests', () => {
expect(Array.isArray(response.body)).toBe(true); expect(Array.isArray(response.body)).toBe(true);
// Check if our test transaction is in the response // Check if our test transaction is in the response
const foundTransaction = response.body.find((txn: any) => txn.id === testTransactionId); const foundTransaction = response.body.find(
(txn: Transaction) => txn.id === testTransactionId,
);
expect(foundTransaction).toBeDefined(); expect(foundTransaction).toBeDefined();
expect(foundTransaction.description).toBe('Test Transaction'); expect(foundTransaction.description).toBe('Test Transaction');
}); });