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
|
|
@ -12,6 +12,6 @@ post {
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
{
|
{
|
||||||
"name": "Household:Supplies"
|
"name": "Gas"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,30 +12,10 @@ post {
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
{
|
{
|
||||||
"year": "2026",
|
"date": "2026-01-03",
|
||||||
"month": "01",
|
"cents": 1010,
|
||||||
"day": "03",
|
"note": "Gas",
|
||||||
"cents": 1234,
|
"categoryId": "db21acbb-0e3e-4ea1-89e1-3d52e5d72cb4",
|
||||||
"note": "Additional Expense Test",
|
"marchantId": "b9028a32-1305-4699-8611-d8fd812ebc04"
|
||||||
"category": {
|
|
||||||
"id": "96db93d8-2f7e-462c-b520-8754a5900253"
|
|
||||||
},
|
|
||||||
"merchant": {
|
|
||||||
"id": "9afbe4fd-9077-473a-9d08-f5fb4c8f692f"
|
|
||||||
},
|
|
||||||
"tags": [
|
|
||||||
{
|
|
||||||
"id": "db6d60d9-66e1-4005-a5a3-1e165812aa3c",
|
|
||||||
"name": "Tag One"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "999a8e68-5232-43e0-adf5-98af75aa01ba",
|
|
||||||
"name": "Tag Two"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "619d5c95-65fc-4795-8828-ee1adfe289f8",
|
|
||||||
"name": "Tag Three"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,6 @@ post {
|
||||||
|
|
||||||
body:json {
|
body:json {
|
||||||
{
|
{
|
||||||
"name": "Merchant Three"
|
"name": "Casey's"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
80
bruno/Common Cents/seed-data.js
Normal file
80
bruno/Common Cents/seed-data.js
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
const postFakeCategories = [
|
||||||
|
{
|
||||||
|
name: "Utilities"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Automotive:Gasoline"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Groceries:Food"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
const postFakeMerchants = [
|
||||||
|
{
|
||||||
|
name: "Walmart"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Casey's"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Electric Company"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
const postFakeTags = [
|
||||||
|
{
|
||||||
|
name: "Truck"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Van"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
const postFakeExpenses = [
|
||||||
|
{
|
||||||
|
year: "2025",
|
||||||
|
month: "12",
|
||||||
|
day: "28",
|
||||||
|
cents: 12153,
|
||||||
|
category: {
|
||||||
|
name: "Utilities"
|
||||||
|
},
|
||||||
|
merchant: {
|
||||||
|
name: "Electric Company"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
year: "2025",
|
||||||
|
month: "12",
|
||||||
|
day: "31",
|
||||||
|
cents: 5500,
|
||||||
|
category: {
|
||||||
|
name: "Automotive:Gasoline"
|
||||||
|
},
|
||||||
|
merchant: {
|
||||||
|
name: "Casey's"
|
||||||
|
},
|
||||||
|
tags: [
|
||||||
|
{
|
||||||
|
name: "Truck"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
console.log({ postFakeCategories, postFakeMerchants, postFakeTags, postFakeExpenses });
|
||||||
|
|
||||||
|
|
||||||
|
// async function fetchData(url) {
|
||||||
|
// try {
|
||||||
|
// const response = await fetch(url);
|
||||||
|
// if (!response.ok) {
|
||||||
|
// throw new Error('Network response was not ok');
|
||||||
|
// }
|
||||||
|
// const data = await response.json(); // Wait for the JSON data to be parsed
|
||||||
|
// console.log(data);
|
||||||
|
// } catch (error) {
|
||||||
|
// console.error('Error:', error);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Call the async function
|
||||||
|
// fetchData('https://api.example.com/data');
|
||||||
19
package-lock.json
generated
19
package-lock.json
generated
|
|
@ -9,6 +9,7 @@
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@js-temporal/polyfill": "^0.5.1",
|
||||||
"@nestjs/common": "^11.0.1",
|
"@nestjs/common": "^11.0.1",
|
||||||
"@nestjs/config": "^4.0.3",
|
"@nestjs/config": "^4.0.3",
|
||||||
"@nestjs/core": "^11.0.1",
|
"@nestjs/core": "^11.0.1",
|
||||||
|
|
@ -2063,6 +2064,18 @@
|
||||||
"@jridgewell/sourcemap-codec": "^1.4.14"
|
"@jridgewell/sourcemap-codec": "^1.4.14"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@js-temporal/polyfill": {
|
||||||
|
"version": "0.5.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@js-temporal/polyfill/-/polyfill-0.5.1.tgz",
|
||||||
|
"integrity": "sha512-hloP58zRVCRSpgDxmqCWJNlizAlUgJFqG2ypq79DCvyv9tHjRYMDOcPFjzfl/A1/YxDvRCZz8wvZvmapQnKwFQ==",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"jsbi": "^4.3.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@lukeed/csprng": {
|
"node_modules/@lukeed/csprng": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/@lukeed/csprng/-/csprng-1.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/@lukeed/csprng/-/csprng-1.1.0.tgz",
|
||||||
|
|
@ -7831,6 +7844,12 @@
|
||||||
"js-yaml": "bin/js-yaml.js"
|
"js-yaml": "bin/js-yaml.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/jsbi": {
|
||||||
|
"version": "4.3.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/jsbi/-/jsbi-4.3.2.tgz",
|
||||||
|
"integrity": "sha512-9fqMSQbhJykSeii05nxKl4m6Eqn2P6rOlYiS+C5Dr/HPIU/7yZxu5qzbs40tgaFORiw2Amd0mirjxatXYMkIew==",
|
||||||
|
"license": "Apache-2.0"
|
||||||
|
},
|
||||||
"node_modules/jsesc": {
|
"node_modules/jsesc": {
|
||||||
"version": "3.1.0",
|
"version": "3.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-3.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-3.1.0.tgz",
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
"test:e2e": "jest --config ./test/jest-e2e.json"
|
"test:e2e": "jest --config ./test/jest-e2e.json"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@js-temporal/polyfill": "^0.5.1",
|
||||||
"@nestjs/common": "^11.0.1",
|
"@nestjs/common": "^11.0.1",
|
||||||
"@nestjs/config": "^4.0.3",
|
"@nestjs/config": "^4.0.3",
|
||||||
"@nestjs/core": "^11.0.1",
|
"@nestjs/core": "^11.0.1",
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,11 @@
|
||||||
import { Category } from '../../categories/entities/category.entity';
|
|
||||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||||
import { Merchant } from '../../merchants/entities/merchant.entity';
|
import { Temporal } from '@js-temporal/polyfill';
|
||||||
import { Tag } from '../../tags/entities/tag.entity';
|
|
||||||
|
|
||||||
export class CreateExpenseDto {
|
export class CreateExpenseDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: '4-digit year of expense'
|
description: 'Date in YYYY-MM-DD format'
|
||||||
})
|
})
|
||||||
year: string;
|
date: Temporal.PlainDate;
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: '2-digit month of expense'
|
|
||||||
})
|
|
||||||
month: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: '2-digit day of expense'
|
|
||||||
})
|
|
||||||
day: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Amount of expense in cents'
|
description: 'Amount of expense in cents'
|
||||||
|
|
@ -25,9 +13,9 @@ export class CreateExpenseDto {
|
||||||
cents: number;
|
cents: number;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Category of expense'
|
description: 'Category ID of expense'
|
||||||
})
|
})
|
||||||
category: Category
|
categoryId: string;
|
||||||
|
|
||||||
@ApiPropertyOptional({
|
@ApiPropertyOptional({
|
||||||
description: 'Optional note about expense'
|
description: 'Optional note about expense'
|
||||||
|
|
@ -35,13 +23,13 @@ export class CreateExpenseDto {
|
||||||
note?: string;
|
note?: string;
|
||||||
|
|
||||||
@ApiPropertyOptional({
|
@ApiPropertyOptional({
|
||||||
description: 'Optional merchant for the expense'
|
description: 'Optional merchant ID for the expense'
|
||||||
})
|
})
|
||||||
merchant?: Merchant;
|
merchantId?: string;
|
||||||
|
|
||||||
@ApiPropertyOptional({
|
@ApiPropertyOptional({
|
||||||
type: [Tag],
|
type: [String],
|
||||||
description: 'Optional list of tags for the expense'
|
description: 'Optional list of tag IDs for the expense'
|
||||||
})
|
})
|
||||||
tags?: Tag[];
|
tagIds: string[];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
43
src/expenses/dto/get-expense.dto.ts
Normal file
43
src/expenses/dto/get-expense.dto.ts
Normal 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[];
|
||||||
|
}
|
||||||
|
|
@ -9,13 +9,7 @@ export class Expense {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
year: string;
|
date: string;
|
||||||
|
|
||||||
@Column()
|
|
||||||
month: string;
|
|
||||||
|
|
||||||
@Column()
|
|
||||||
day: string;
|
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
cents: number;
|
cents: number;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { DataSource, Repository } from 'typeorm';
|
import { DataSource, Repository } from 'typeorm';
|
||||||
import { Expense } from './entities/expense.entity';
|
import { Expense } from './entities/expense.entity';
|
||||||
import { UpdateExpenseDto } from './dto/update-expense.dto';
|
|
||||||
import { CreateExpenseDto } from './dto/create-expense.dto';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ExpenseDataService {
|
export class ExpenseDataService {
|
||||||
|
|
@ -20,11 +18,11 @@ export class ExpenseDataService {
|
||||||
return await this.expenses.findOneBy({ id });
|
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);
|
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);
|
return await this.expenses.save(expense);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,3 +30,22 @@ export class ExpenseDataService {
|
||||||
await this.expenses.delete({ id });
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ import {
|
||||||
import { ExpensesService } from './expenses.service';
|
import { ExpensesService } from './expenses.service';
|
||||||
import { CreateExpenseDto } from './dto/create-expense.dto';
|
import { CreateExpenseDto } from './dto/create-expense.dto';
|
||||||
import { UpdateExpenseDto } from './dto/update-expense.dto';
|
import { UpdateExpenseDto } from './dto/update-expense.dto';
|
||||||
import { Expense } from './entities/expense.entity';
|
import { GetExpenseDto } from './dto/get-expense.dto';
|
||||||
|
|
||||||
@Controller('expenses')
|
@Controller('expenses')
|
||||||
export class ExpensesController {
|
export class ExpensesController {
|
||||||
|
|
@ -23,13 +23,13 @@ export class ExpensesController {
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
public async findAll(): Promise<Expense[]> {
|
public async findAll(): Promise<GetExpenseDto[]> {
|
||||||
return await this.expensesService.findAll();
|
return await this.expensesService.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
public async findOne(@Param('id') id: string): Promise<Expense> {
|
public async findOne(@Param('id') id: string): Promise<GetExpenseDto> {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
throw new BadRequestException('No ID provided.');
|
throw new BadRequestException('No ID provided.');
|
||||||
}
|
}
|
||||||
|
|
@ -44,7 +44,7 @@ export class ExpensesController {
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
@HttpCode(HttpStatus.CREATED)
|
@HttpCode(HttpStatus.CREATED)
|
||||||
public async create(@Body() expense: CreateExpenseDto): Promise<Expense> {
|
public async create(@Body() expense: CreateExpenseDto): Promise<GetExpenseDto> {
|
||||||
if (!expense) {
|
if (!expense) {
|
||||||
throw new BadRequestException('Expense name cannot be empty.');
|
throw new BadRequestException('Expense name cannot be empty.');
|
||||||
}
|
}
|
||||||
|
|
@ -59,7 +59,7 @@ export class ExpensesController {
|
||||||
|
|
||||||
@Put()
|
@Put()
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
public async update(@Body() expense: UpdateExpenseDto): Promise<Expense> {
|
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.');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,31 +2,68 @@ import { Injectable } from '@nestjs/common';
|
||||||
import { CreateExpenseDto } from './dto/create-expense.dto';
|
import { CreateExpenseDto } from './dto/create-expense.dto';
|
||||||
import { UpdateExpenseDto } from './dto/update-expense.dto';
|
import { UpdateExpenseDto } from './dto/update-expense.dto';
|
||||||
import { ExpenseDataService } from './expense-data.service';
|
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()
|
@Injectable()
|
||||||
export class ExpensesService {
|
export class ExpensesService {
|
||||||
public constructor(private expenseDataService: ExpenseDataService) { }
|
public constructor(private expenseDataService: ExpenseDataService) { }
|
||||||
|
|
||||||
public async findAll(): Promise<Expense[]> {
|
public async findAll(): Promise<GetExpenseDto[]> {
|
||||||
return await this.expenseDataService.getAll();
|
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);
|
const expense = await this.expenseDataService.getById(id);
|
||||||
if (!expense) {
|
if (!expense) {
|
||||||
throw new Error('No expense found');
|
throw new Error('No expense found');
|
||||||
}
|
}
|
||||||
|
|
||||||
return expense;
|
return { ...expense, date: Temporal.PlainDate.from(expense.date) };
|
||||||
}
|
}
|
||||||
|
|
||||||
public async create(expense: CreateExpenseDto): Promise<Expense> {
|
public async create(createExpense: CreateExpenseDto): Promise<GetExpenseDto> {
|
||||||
return await this.expenseDataService.create(expense);
|
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> {
|
public async update(updateExpense: UpdateExpenseDto): Promise<GetExpenseDto> {
|
||||||
return await this.expenseDataService.update(expense);
|
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> {
|
public async remove(id: string): Promise<void> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue