fixed PUT nullable merchant/note

This commit is contained in:
Joe Arndt 2026-02-25 14:49:49 -06:00
parent 389ef8c7ce
commit f390e2a8c3
7 changed files with 74 additions and 52 deletions

View file

@ -13,9 +13,14 @@ post {
body:json { body:json {
{ {
"date": "2026-01-03", "date": "2026-01-03",
"cents": 101111, "cents": 12345,
"note": "Gas", "category": {
"categoryId": "db21acbb-0e3e-4ea1-89e1-3d52e5d72cb4", "id": "852f2391-27a3-4d84-a9c7-0cd0b2efd899",
"merchantId": "b9028a32-1305-4699-8611-d8fd812ebc04" "name": "Groceries:Food"
},
"merchant": {
"id": "f5216666-b022-4ddb-a4b1-f940e5d4f8f2",
"name": "Walmart"
}
} }
} }

View file

@ -11,11 +11,23 @@ put {
} }
body:json { body:json {
{ {
"id": "0254a07d-9dfa-4c06-b7b3-48d30efff991", "id": "271342fa-94c5-43e2-96f8-6d45668af567",
"description": "Cat, no sub-cat or merchant", "date": "2026-01-03",
"category": { "cents": 4554,
"id": "db434e98-5f32-4202-b69c-eb7d1d248b3e" "category": {
"id": "3b1e6730-29e2-4e51-bee0-4208d8997962",
"name": "Auto:Gas"
},
"merchant": {
"id": "decb74e6-d680-4124-81b2-08e0fd9fa94c",
"name": "Casey's"
},
"tags": [
{
"id": "0ac325ca-ee13-4185-906c-257afa48c4ee",
"name": "Tundra"
} }
} ]
}
} }

View file

@ -28,12 +28,12 @@ export class GetExpenseDto {
@ApiPropertyOptional({ @ApiPropertyOptional({
description: 'Note about expense' description: 'Note about expense'
}) })
note: string; note: string | null;
@ApiPropertyOptional({ @ApiPropertyOptional({
description: 'Merchant for the expense' description: 'Merchant for the expense'
}) })
merchant: Merchant; merchant: Merchant | null;
@ApiPropertyOptional({ @ApiPropertyOptional({
type: [Tag], type: [Tag],

View file

@ -32,12 +32,12 @@ export class Expense {
@ManyToOne(() => Category, { eager: true }) @ManyToOne(() => Category, { eager: true })
category: Category; category: Category;
@Column({ nullable: true })
note: string;
@ManyToOne(() => Merchant, { nullable: true, eager: true }) @ManyToOne(() => Merchant, { nullable: true, eager: true })
merchant: Merchant; merchant: Merchant;
@Column({ nullable: true })
note: string;
@ManyToMany(() => Tag, { nullable: true, eager: true }) @ManyToMany(() => Tag, { nullable: true, eager: true })
@JoinTable() @JoinTable()
tags: Tag[]; tags: Tag[];

View file

@ -25,10 +25,9 @@ export class ExpenseDataService {
return await this.expenses.save<Expense>(expense as Expense); return await this.expenses.save<Expense>(expense as Expense);
} }
// public async update(expense: UpdateExpense): Promise<Expense> { public async update(expense: UpdateExpense): Promise<Expense> {
// await this.expenses.save(expense); return await this.expenses.save<Expense>(expense as Expense);
// return await this.expenses.findOneBy({ id: expense.id }) as Expense; }
// }
public async delete(id: string): Promise<void> { public async delete(id: string): Promise<void> {
await this.expenses.delete({ id }); await this.expenses.delete({ id });
@ -41,8 +40,8 @@ export interface CreateExpense {
day: number; day: number;
cents: number; cents: number;
category: Category; category: Category;
note: string; merchant: Merchant | null;
merchant: Merchant; note: string | null;
tags: Tag[]; tags: Tag[];
} }

View file

@ -58,15 +58,16 @@ export class ExpensesController {
} }
} }
// @Put() @Put()
// @HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
// public async update(@Body() expense: UpdateExpenseDto): Promise<GetExpenseDto> { public async update(@Body() expense: UpdateExpenseDto): Promise<GetExpenseDto> {
// if (!expense.id) { if (!expense.id) {
// throw new BadRequestException('Expense ID cannot be empty.'); throw new BadRequestException('Expense ID cannot be empty.');
// } }
// // TODO: Validate date/cents/category exist...
// return await this.expensesService.update(expense);
// } return await this.expensesService.update(expense);
}
@Delete(':id') @Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT) @HttpCode(HttpStatus.NO_CONTENT)

View file

@ -4,7 +4,6 @@ import { UpdateExpenseDto } from './dto/update-expense.dto';
import { ExpenseDataService } from './expense-data.service'; import { ExpenseDataService } from './expense-data.service';
import { GetExpenseDto } from './dto/get-expense.dto'; import { GetExpenseDto } from './dto/get-expense.dto';
import { Temporal } from '@js-temporal/polyfill'; import { Temporal } from '@js-temporal/polyfill';
import { Expense } from './entities/expense.entity';
@Injectable() @Injectable()
export class ExpensesService { export class ExpensesService {
@ -61,31 +60,37 @@ export class ExpensesService {
date: new Temporal.PlainDate(exp.year, exp.month, exp.day), date: new Temporal.PlainDate(exp.year, exp.month, exp.day),
cents: exp.cents, cents: exp.cents,
category: exp.category, category: exp.category,
merchant: exp.merchant, merchant: exp.merchant ?? null,
note: exp.note, note: exp.note ?? null,
tags: exp.tags tags: exp.tags
}; };
} }
// public async update(updateExpense: UpdateExpenseDto): Promise<GetExpenseDto> { public async update(updateExpense: UpdateExpenseDto): Promise<GetExpenseDto> {
// const date = updateExpense.date.toString(); const date = updateExpense.date.toString().split('-');
// const category = { id: updateExpense.categoryId }; const exp = await this.expenseDataService.update({
// const merchant = updateExpense.merchantId ? { id: updateExpense.merchantId } : undefined; id: updateExpense.id,
// const tags = updateExpense.tagIds?.map(id => { year: Number(date[0]),
// return { id }; month: Number(date[1]),
// }) day: Number(date[2]),
// const expense = await this.expenseDataService.update({ cents: updateExpense.cents,
// id: updateExpense.id, category: updateExpense.category,
// date, merchant: updateExpense.merchant ?? null,
// cents: updateExpense.cents, note: updateExpense.note ?? null,
// note: updateExpense.note, tags: updateExpense.tags
// category, });
// merchant,
// tags return {
// }); id: exp.id,
// date: new Temporal.PlainDate(exp.year, exp.month, exp.day),
// return { ...expense, date: Temporal.PlainDate.from(expense.date) }; cents: exp.cents,
// } category: exp.category,
merchant: exp.merchant,
note: exp.note,
tags: exp.tags
};
}
public async remove(id: string): Promise<void> { public async remove(id: string): Promise<void> {
await this.expenseDataService.delete(id); await this.expenseDataService.delete(id);