fixed PUT nullable merchant/note
This commit is contained in:
parent
389ef8c7ce
commit
f390e2a8c3
7 changed files with 74 additions and 52 deletions
|
|
@ -28,12 +28,12 @@ export class GetExpenseDto {
|
|||
@ApiPropertyOptional({
|
||||
description: 'Note about expense'
|
||||
})
|
||||
note: string;
|
||||
note: string | null;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Merchant for the expense'
|
||||
})
|
||||
merchant: Merchant;
|
||||
merchant: Merchant | null;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
type: [Tag],
|
||||
|
|
|
|||
|
|
@ -32,12 +32,12 @@ export class Expense {
|
|||
@ManyToOne(() => Category, { eager: true })
|
||||
category: Category;
|
||||
|
||||
@Column({ nullable: true })
|
||||
note: string;
|
||||
|
||||
@ManyToOne(() => Merchant, { nullable: true, eager: true })
|
||||
merchant: Merchant;
|
||||
|
||||
@Column({ nullable: true })
|
||||
note: string;
|
||||
|
||||
@ManyToMany(() => Tag, { nullable: true, eager: true })
|
||||
@JoinTable()
|
||||
tags: Tag[];
|
||||
|
|
|
|||
|
|
@ -25,10 +25,9 @@ export class ExpenseDataService {
|
|||
return await this.expenses.save<Expense>(expense as Expense);
|
||||
}
|
||||
|
||||
// public async update(expense: UpdateExpense): Promise<Expense> {
|
||||
// await this.expenses.save(expense);
|
||||
// return await this.expenses.findOneBy({ id: expense.id }) as Expense;
|
||||
// }
|
||||
public async update(expense: UpdateExpense): Promise<Expense> {
|
||||
return await this.expenses.save<Expense>(expense as Expense);
|
||||
}
|
||||
|
||||
public async delete(id: string): Promise<void> {
|
||||
await this.expenses.delete({ id });
|
||||
|
|
@ -41,8 +40,8 @@ export interface CreateExpense {
|
|||
day: number;
|
||||
cents: number;
|
||||
category: Category;
|
||||
note: string;
|
||||
merchant: Merchant;
|
||||
merchant: Merchant | null;
|
||||
note: string | null;
|
||||
tags: Tag[];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -58,15 +58,16 @@ export class ExpensesController {
|
|||
}
|
||||
}
|
||||
|
||||
// @Put()
|
||||
// @HttpCode(HttpStatus.OK)
|
||||
// public async update(@Body() expense: UpdateExpenseDto): Promise<GetExpenseDto> {
|
||||
// if (!expense.id) {
|
||||
// throw new BadRequestException('Expense ID cannot be empty.');
|
||||
// }
|
||||
//
|
||||
// return await this.expensesService.update(expense);
|
||||
// }
|
||||
@Put()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async update(@Body() expense: UpdateExpenseDto): Promise<GetExpenseDto> {
|
||||
if (!expense.id) {
|
||||
throw new BadRequestException('Expense ID cannot be empty.');
|
||||
}
|
||||
// TODO: Validate date/cents/category exist...
|
||||
|
||||
return await this.expensesService.update(expense);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import { UpdateExpenseDto } from './dto/update-expense.dto';
|
|||
import { ExpenseDataService } from './expense-data.service';
|
||||
import { GetExpenseDto } from './dto/get-expense.dto';
|
||||
import { Temporal } from '@js-temporal/polyfill';
|
||||
import { Expense } from './entities/expense.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ExpensesService {
|
||||
|
|
@ -61,31 +60,37 @@ export class ExpensesService {
|
|||
date: new Temporal.PlainDate(exp.year, exp.month, exp.day),
|
||||
cents: exp.cents,
|
||||
category: exp.category,
|
||||
merchant: exp.merchant,
|
||||
note: exp.note,
|
||||
merchant: exp.merchant ?? null,
|
||||
note: exp.note ?? null,
|
||||
tags: exp.tags
|
||||
};
|
||||
}
|
||||
|
||||
// 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 update(updateExpense: UpdateExpenseDto): Promise<GetExpenseDto> {
|
||||
const date = updateExpense.date.toString().split('-');
|
||||
const exp = await this.expenseDataService.update({
|
||||
id: updateExpense.id,
|
||||
year: Number(date[0]),
|
||||
month: Number(date[1]),
|
||||
day: Number(date[2]),
|
||||
cents: updateExpense.cents,
|
||||
category: updateExpense.category,
|
||||
merchant: updateExpense.merchant ?? null,
|
||||
note: updateExpense.note ?? null,
|
||||
tags: updateExpense.tags
|
||||
});
|
||||
|
||||
return {
|
||||
id: exp.id,
|
||||
date: new Temporal.PlainDate(exp.year, exp.month, exp.day),
|
||||
cents: exp.cents,
|
||||
category: exp.category,
|
||||
merchant: exp.merchant,
|
||||
note: exp.note,
|
||||
tags: exp.tags
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public async remove(id: string): Promise<void> {
|
||||
await this.expenseDataService.delete(id);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue