Initial web app consumption/integration (#5)

Co-authored-by: Joe Arndt <jmarndt@users.noreply.github.com>
Reviewed-on: #5
This commit is contained in:
Joe 2026-02-23 21:15:19 +00:00
parent 3f258bcd33
commit df6733caa9
41 changed files with 385 additions and 353 deletions

View file

@ -1,10 +1,9 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { HealthcheckController } from './healthcheck.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MerchantsModule } from './merchants/merchants.module';
import { TagsModule } from './tags/tags.module';
import { CategoriesModule } from './categories/categories.module';
import { SubCategoriesModule } from './sub-categories/sub-categories.module';
import { ExpensesModule } from './expenses/expenses.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
@ -37,9 +36,8 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
MerchantsModule,
TagsModule,
CategoriesModule,
SubCategoriesModule,
ExpensesModule
],
controllers: [AppController]
controllers: [HealthcheckController]
})
export class AppModule { }

View file

@ -1,3 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
export class CreateCategoryDto {
@ApiProperty({
description: 'Category for expenses'
})
name: string;
}

View file

@ -1,4 +1,9 @@
export class UpdateCategoryDto {
import { ApiProperty } from '@nestjs/swagger';
import { CreateCategoryDto } from './create-category.dto';
export class UpdateCategoryDto extends CreateCategoryDto {
@ApiProperty({
description: 'Unique ID of the category'
})
id: string;
name: string;
}

View file

@ -1,10 +1,35 @@
import { Category } from '../../categories/entities/category.entity';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Temporal } from '@js-temporal/polyfill';
export class CreateExpenseDto {
year: string;
month: string;
day: string;
@ApiProperty({
description: 'Date in YYYY-MM-DD format'
})
date: Temporal.PlainDate;
@ApiProperty({
description: 'Amount of expense in cents'
})
cents: number;
description: string;
category: Category
@ApiProperty({
description: 'Category ID of expense'
})
categoryId: string;
@ApiPropertyOptional({
description: 'Optional note about expense'
})
note?: string;
@ApiPropertyOptional({
description: 'Optional merchant ID for the expense'
})
merchantId?: string;
@ApiPropertyOptional({
type: [String],
description: 'Optional list of tag IDs for the expense'
})
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

@ -1,6 +1,9 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateExpenseDto } from './create-expense.dto';
import { ApiProperty } from '@nestjs/swagger';
export class UpdateExpenseDto extends PartialType(CreateExpenseDto) {
export class UpdateExpenseDto extends CreateExpenseDto {
@ApiProperty({
description: 'Unique ID of the expense'
})
id: string;
}

View file

@ -1,6 +1,5 @@
import { Column, Entity, JoinTable, ManyToMany, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Tag } from '../../tags/entities/tag.entity';
import { SubCategory } from '../../sub-categories/entities/sub-category.entity';
import { Category } from '../../categories/entities/category.entity';
import { Merchant } from '../../merchants/entities/merchant.entity';
@ -10,25 +9,16 @@ export class Expense {
id: string;
@Column()
year: string;
@Column()
month: string;
@Column()
day: string;
date: string;
@Column()
cents: number;
@Column({ nullable: true })
description: string;
@ManyToOne(() => Category, { eager: true })
category: Category;
@ManyToOne(() => SubCategory, { nullable: true, eager: true })
subCategory: SubCategory;
@Column({ nullable: true })
note: string;
@ManyToOne(() => Merchant, { nullable: true, eager: true })
merchant: Merchant;

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,15 +18,36 @@ export class ExpenseDataService {
return await this.expenses.findOneBy({ id });
}
public async create(expense: CreateExpenseDto): Promise<Expense> {
return await this.expenses.save(expense);
public async create(expense: CreateExpense): Promise<Expense> {
const created = await this.expenses.save(expense);
return await this.expenses.findOneBy({ id: created.id }) as Expense;
}
public async update(expense: UpdateExpenseDto): Promise<Expense> {
return await this.expenses.save(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 delete(id: string): Promise<void> {
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> {

View file

@ -1,7 +1,7 @@
import { Controller, Get } from '@nestjs/common';
@Controller()
export class AppController {
export class HealthcheckController {
@Get('healthcheck')
health() {
return { status: 'healthy' };

View file

@ -1,10 +1,20 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
async function bootstrap(): Promise<void> {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.setGlobalPrefix(process.env.PREFIX ?? 'common-cents');
const appPrefix = process.env.PREFIX ?? 'common-cents';
const config = new DocumentBuilder()
.setTitle('Common Cents API')
.setDescription('Documentation for Common Cents API')
.setVersion('1.0')
.build();
const documentFactory = () => SwaggerModule.createDocument(app, config);
SwaggerModule.setup(appPrefix + '/docs', app, documentFactory);
app.enableCors(); // TODO: Research if this is worth worrying about
app.setGlobalPrefix(appPrefix);
await app.listen(process.env.PORT ?? 3000);
}
void bootstrap();

View file

@ -1,3 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
export class CreateMerchantDto {
@ApiProperty({
description: 'Merchant name'
})
name: string;
}

View file

@ -1,4 +1,9 @@
export class UpdateMerchantDto {
import { CreateMerchantDto } from './create-merchant.dto';
import { ApiProperty } from '@nestjs/swagger';
export class UpdateMerchantDto extends CreateMerchantDto {
@ApiProperty({
description: 'Unique ID of merchant'
})
id: string;
name: string;
}

View file

@ -1,3 +0,0 @@
export class CreateSubCategoryDto {
name: string;
}

View file

@ -1,4 +0,0 @@
export class UpdateSubCategoryDto {
id: string;
name: string;
}

View file

@ -1,10 +0,0 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class SubCategory {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ unique: true })
name: string;
}

View file

@ -1,75 +0,0 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Body,
Param,
HttpCode,
HttpStatus,
BadRequestException,
NotFoundException,
InternalServerErrorException
} from '@nestjs/common';
import { SubCategoriesService } from './sub-categories.service';
import { CreateSubCategoryDto } from './dto/create-sub-category.dto';
import { UpdateSubCategoryDto } from './dto/update-sub-category.dto';
import { SubCategory } from './entities/sub-category.entity';
@Controller('sub-categories')
export class SubCategoriesController {
constructor(private readonly subCategoriesService: SubCategoriesService) { }
@Get()
@HttpCode(HttpStatus.OK)
public async findAll(): Promise<SubCategory[]> {
return await this.subCategoriesService.findAll();
}
@Get(':id')
@HttpCode(HttpStatus.OK)
public async findOne(@Param('id') id: string): Promise<SubCategory> {
if (!id) {
throw new BadRequestException('No ID provided.');
}
try {
return await this.subCategoriesService.findById(id);
}
catch (error) {
throw new NotFoundException(error);
}
}
@Post()
@HttpCode(HttpStatus.CREATED)
public async create(@Body() subCategory: CreateSubCategoryDto): Promise<SubCategory> {
if (!subCategory.name) {
throw new BadRequestException('Sub-category name cannot be empty.');
}
try {
return await this.subCategoriesService.create(subCategory);
}
catch (error) {
throw new InternalServerErrorException(error);
}
}
@Put()
@HttpCode(HttpStatus.OK)
public async update(@Body() subCategory: UpdateSubCategoryDto): Promise<SubCategory> {
if (!subCategory.id) {
throw new BadRequestException('Sub-category ID cannot be empty.');
}
return await this.subCategoriesService.update(subCategory);
}
@Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT)
public async remove(@Param('id') id: string): Promise<void> {
return await this.subCategoriesService.remove(id);
}
}

View file

@ -1,14 +0,0 @@
import { Module } from '@nestjs/common';
import { SubCategoriesService } from './sub-categories.service';
import { SubCategoriesController } from './sub-categories.controller';
import { SubCategoryDataService } from './sub-category-data.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SubCategory } from './entities/sub-category.entity';
@Module({
controllers: [SubCategoriesController],
providers: [SubCategoriesService, SubCategoryDataService],
imports: [TypeOrmModule.forFeature([SubCategory])],
exports: [TypeOrmModule]
})
export class SubCategoriesModule { }

View file

@ -1,35 +0,0 @@
import { Injectable } from '@nestjs/common';
import { CreateSubCategoryDto } from './dto/create-sub-category.dto';
import { UpdateSubCategoryDto } from './dto/update-sub-category.dto';
import { SubCategoryDataService } from './sub-category-data.service';
import { SubCategory } from './entities/sub-category.entity';
@Injectable()
export class SubCategoriesService {
public constructor(private subCategoryDataService: SubCategoryDataService) { }
public async findAll(): Promise<SubCategory[]> {
return await this.subCategoryDataService.getAll();
}
public async findById(id: string): Promise<SubCategory> {
const subCategory = await this.subCategoryDataService.getById(id);
if (!subCategory) {
throw new Error('No sub-category found');
}
return subCategory;
}
public async create(subCategory: CreateSubCategoryDto): Promise<SubCategory> {
return await this.subCategoryDataService.create(subCategory.name);
}
public async update(subCategory: UpdateSubCategoryDto): Promise<SubCategory> {
return await this.subCategoryDataService.update(subCategory);
}
public async remove(id: string): Promise<void> {
await this.subCategoryDataService.delete(id);
}
}

View file

@ -1,33 +0,0 @@
import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { SubCategory } from './entities/sub-category.entity';
import { UpdateSubCategoryDto } from './dto/update-sub-category.dto';
@Injectable()
export class SubCategoryDataService {
private subCategories: Repository<SubCategory>;
public constructor(private dataSource: DataSource) {
this.subCategories = this.dataSource.getRepository(SubCategory);
}
public async getAll(): Promise<SubCategory[]> {
return await this.subCategories.find();
}
public async getById(id: string): Promise<SubCategory | null> {
return await this.subCategories.findOneBy({ id });
}
public async create(name: string): Promise<SubCategory> {
return await this.subCategories.save({ name });
}
public async update(subCategory: UpdateSubCategoryDto): Promise<SubCategory> {
return await this.subCategories.save(subCategory);
}
public async delete(id: string): Promise<void> {
await this.subCategories.delete({ id });
}
}

View file

@ -1,3 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
export class CreateTagDto {
@ApiProperty({
description: 'Tag for expenses'
})
name: string;
}

View file

@ -1,4 +1,9 @@
export class UpdateTagDto {
import { CreateTagDto } from './create-tag.dto';
import { ApiProperty } from '@nestjs/swagger';
export class UpdateTagDto extends CreateTagDto {
@ApiProperty({
description: 'Unique ID of tag'
})
id: string;
name: string;
}