refactored expense date to be a Temporal.PlainDate

This commit is contained in:
Joe Arndt 2026-02-19 14:03:28 -06:00
parent a92c4bec1f
commit ee823d015b
12 changed files with 233 additions and 74 deletions

View file

@ -1,23 +1,11 @@
import { Category } from '../../categories/entities/category.entity';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Merchant } from '../../merchants/entities/merchant.entity';
import { Tag } from '../../tags/entities/tag.entity';
import { Temporal } from '@js-temporal/polyfill';
export class CreateExpenseDto {
@ApiProperty({
description: '4-digit year of expense'
description: 'Date in YYYY-MM-DD format'
})
year: string;
@ApiProperty({
description: '2-digit month of expense'
})
month: string;
@ApiProperty({
description: '2-digit day of expense'
})
day: string;
date: Temporal.PlainDate;
@ApiProperty({
description: 'Amount of expense in cents'
@ -25,9 +13,9 @@ export class CreateExpenseDto {
cents: number;
@ApiProperty({
description: 'Category of expense'
description: 'Category ID of expense'
})
category: Category
categoryId: string;
@ApiPropertyOptional({
description: 'Optional note about expense'
@ -35,13 +23,13 @@ export class CreateExpenseDto {
note?: string;
@ApiPropertyOptional({
description: 'Optional merchant for the expense'
description: 'Optional merchant ID for the expense'
})
merchant?: Merchant;
merchantId?: string;
@ApiPropertyOptional({
type: [Tag],
description: 'Optional list of tags for the expense'
type: [String],
description: 'Optional list of tag IDs for the expense'
})
tags?: Tag[];
tagIds: string[];
}

View file

@ -0,0 +1,43 @@
import { Category } from '../../categories/entities/category.entity';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Merchant } from '../../merchants/entities/merchant.entity';
import { Tag } from '../../tags/entities/tag.entity';
import { Temporal } from '@js-temporal/polyfill';
export class GetExpenseDto {
@ApiProperty({
description: 'Unique ID of expense'
})
id: string;
@ApiProperty({
description: 'Date in YYYY-MM-DD format'
})
date: Temporal.PlainDate;
@ApiProperty({
description: 'Amount of expense in cents'
})
cents: number;
@ApiProperty({
description: 'Category of expense'
})
category: Category
@ApiPropertyOptional({
description: 'Note about expense'
})
note?: string;
@ApiPropertyOptional({
description: 'Merchant for the expense'
})
merchant?: Merchant;
@ApiPropertyOptional({
type: [Tag],
description: 'List of tags for the expense'
})
tags?: Tag[];
}

View file

@ -9,13 +9,7 @@ export class Expense {
id: string;
@Column()
year: string;
@Column()
month: string;
@Column()
day: string;
date: string;
@Column()
cents: number;

View file

@ -1,8 +1,6 @@
import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { Expense } from './entities/expense.entity';
import { UpdateExpenseDto } from './dto/update-expense.dto';
import { CreateExpenseDto } from './dto/create-expense.dto';
@Injectable()
export class ExpenseDataService {
@ -20,11 +18,11 @@ export class ExpenseDataService {
return await this.expenses.findOneBy({ id });
}
public async create(expense: CreateExpenseDto): Promise<Expense> {
public async create(expense: CreateExpense): Promise<Expense> {
return await this.expenses.save(expense);
}
public async update(expense: UpdateExpenseDto): Promise<Expense> {
public async update(expense: UpdateExpense): Promise<Expense> {
return await this.expenses.save(expense);
}
@ -32,3 +30,22 @@ export class ExpenseDataService {
await this.expenses.delete({ id });
}
}
export interface CreateExpense {
date: string;
cents: number;
category: {
id: string;
};
note?: string;
merchant?: {
id: string;
};
tags?: {
id: string;
}[];
}
export interface UpdateExpense extends CreateExpense{
id: string;
}

View file

@ -15,7 +15,7 @@ import {
import { ExpensesService } from './expenses.service';
import { CreateExpenseDto } from './dto/create-expense.dto';
import { UpdateExpenseDto } from './dto/update-expense.dto';
import { Expense } from './entities/expense.entity';
import { GetExpenseDto } from './dto/get-expense.dto';
@Controller('expenses')
export class ExpensesController {
@ -23,13 +23,13 @@ export class ExpensesController {
@Get()
@HttpCode(HttpStatus.OK)
public async findAll(): Promise<Expense[]> {
public async findAll(): Promise<GetExpenseDto[]> {
return await this.expensesService.findAll();
}
@Get(':id')
@HttpCode(HttpStatus.OK)
public async findOne(@Param('id') id: string): Promise<Expense> {
public async findOne(@Param('id') id: string): Promise<GetExpenseDto> {
if (!id) {
throw new BadRequestException('No ID provided.');
}
@ -44,7 +44,7 @@ export class ExpensesController {
@Post()
@HttpCode(HttpStatus.CREATED)
public async create(@Body() expense: CreateExpenseDto): Promise<Expense> {
public async create(@Body() expense: CreateExpenseDto): Promise<GetExpenseDto> {
if (!expense) {
throw new BadRequestException('Expense name cannot be empty.');
}
@ -59,7 +59,7 @@ export class ExpensesController {
@Put()
@HttpCode(HttpStatus.OK)
public async update(@Body() expense: UpdateExpenseDto): Promise<Expense> {
public async update(@Body() expense: UpdateExpenseDto): Promise<GetExpenseDto> {
if (!expense.id) {
throw new BadRequestException('Expense ID cannot be empty.');
}

View file

@ -2,31 +2,68 @@ import { Injectable } from '@nestjs/common';
import { CreateExpenseDto } from './dto/create-expense.dto';
import { UpdateExpenseDto } from './dto/update-expense.dto';
import { ExpenseDataService } from './expense-data.service';
import { Expense } from './entities/expense.entity';
import { GetExpenseDto } from './dto/get-expense.dto';
import { Temporal } from '@js-temporal/polyfill';
@Injectable()
export class ExpensesService {
public constructor(private expenseDataService: ExpenseDataService) { }
public async findAll(): Promise<Expense[]> {
return await this.expenseDataService.getAll();
public async findAll(): Promise<GetExpenseDto[]> {
const expenses = await this.expenseDataService.getAll();
return expenses.map(exp => {
return { ...exp, date: Temporal.PlainDate.from(exp.date) };
})
}
public async findById(id: string): Promise<Expense> {
public async findById(id: string): Promise<GetExpenseDto> {
const expense = await this.expenseDataService.getById(id);
if (!expense) {
throw new Error('No expense found');
}
return expense;
return { ...expense, date: Temporal.PlainDate.from(expense.date) };
}
public async create(expense: CreateExpenseDto): Promise<Expense> {
return await this.expenseDataService.create(expense);
public async create(createExpense: CreateExpenseDto): Promise<GetExpenseDto> {
const date = createExpense.date.toString();
const category = { id: createExpense.categoryId };
const merchant = createExpense.merchantId ? { id: createExpense.merchantId } : undefined;
const tags = createExpense.tagIds?.map(id => {
return { id };
})
const expense = await this.expenseDataService.create({
date,
cents: createExpense.cents,
note: createExpense.note,
category,
merchant,
tags
});
return { ...expense, date: Temporal.PlainDate.from(expense.date) };
}
public async update(expense: UpdateExpenseDto): Promise<Expense> {
return await this.expenseDataService.update(expense);
public async update(updateExpense: UpdateExpenseDto): Promise<GetExpenseDto> {
const date = updateExpense.date.toString();
const category = { id: updateExpense.categoryId };
const merchant = updateExpense.merchantId ? { id: updateExpense.merchantId } : undefined;
const tags = updateExpense.tagIds?.map(id => {
return { id };
})
const expense = await this.expenseDataService.update({
id: updateExpense.id,
date,
cents: updateExpense.cents,
note: updateExpense.note,
category,
merchant,
tags
});
return { ...expense, date: Temporal.PlainDate.from(expense.date) };
}
public async remove(id: string): Promise<void> {