refactored expense date to be a Temporal.PlainDate
This commit is contained in:
parent
a92c4bec1f
commit
ee823d015b
12 changed files with 233 additions and 74 deletions
|
|
@ -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> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue