Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,092 changes: 972 additions & 120 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"bundle": "ncc build src/lambda.ts --out dist/lambda_bundle.js",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
Expand All @@ -21,24 +22,30 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@codegenie/serverless-express": "^4.17.0",
"@nestjs/common": "^10.0.3",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.0.3",
"@nestjs/jwt": "^10.1.0",
"@nestjs/passport": "^10.0.0",
"@nestjs/platform-express": "^10.0.3",
"@nestjs/typeorm": "^11.0.0",
"aws-lambda": "^1.0.7",
"helmet": "^7.0.0",
"passport": "^0.6.0",
"passport-http": "^0.3.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"pg": "^8.16.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"typeorm": "^0.3.27"
},
"devDependencies": {
"@nestjs/cli": "^10.0.3",
"@nestjs/schematics": "^10.0.1",
"@nestjs/testing": "^10.0.3",
"@types/aws-lambda": "^8.10.157",
"@types/express": "^4.17.17",
"@types/jest": "29.5.2",
"@types/node": "^20.3.1",
Expand All @@ -47,6 +54,7 @@
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "5.60.0",
"@typescript-eslint/parser": "5.60.0",
"@vercel/ncc": "^0.38.4",
"eslint": "8.43.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
Expand Down
32 changes: 30 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';

import { AppController } from './app.controller';

import { CartModule } from './cart/cart.module';
import { AuthModule } from './auth/auth.module';
import { OrderModule } from './order/order.module';
import { ConfigModule } from '@nestjs/config';
import { CartEntity } from './cart/entities/cart.entity';
import { CartItemEntity } from './cart/entities/cart-item.entity';

@Module({
imports: [AuthModule, CartModule, OrderModule, ConfigModule.forRoot()],
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get<string>('DB_HOST'),
port: configService.get<number>('DB_PORT', 5432),
username: configService.get<string>('DB_USERNAME'),
password: configService.get<string>('DB_PASSWORD'),
database: configService.get<string>('DB_DATABASE', 'cartdb'),
entities: [CartEntity, CartItemEntity],
synchronize: true, // Auto-create tables (for development/demo)
logging: false,
ssl: {
rejectUnauthorized: false, // AWS RDS uses self-signed certificates
},
}),
inject: [ConfigService],
}),
AuthModule,
CartModule,
OrderModule,
],
controllers: [AppController],
providers: [],
})
Expand Down
20 changes: 10 additions & 10 deletions src/cart/cart.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export class CartController {
// @UseGuards(JwtAuthGuard)
@UseGuards(BasicAuthGuard)
@Get()
findUserCart(@Req() req: AppRequest): CartItem[] {
const cart = this.cartService.findOrCreateByUserId(
async findUserCart(@Req() req: AppRequest): Promise<CartItem[]> {
const cart = await this.cartService.findOrCreateByUserId(
getUserIdFromRequest(req),
);

Expand All @@ -39,12 +39,12 @@ export class CartController {
// @UseGuards(JwtAuthGuard)
@UseGuards(BasicAuthGuard)
@Put()
updateUserCart(
async updateUserCart(
@Req() req: AppRequest,
@Body() body: PutCartPayload,
): CartItem[] {
): Promise<CartItem[]> {
// TODO: validate body payload...
const cart = this.cartService.updateByUserId(
const cart = await this.cartService.updateByUserId(
getUserIdFromRequest(req),
body,
);
Expand All @@ -56,16 +56,16 @@ export class CartController {
@UseGuards(BasicAuthGuard)
@Delete()
@HttpCode(HttpStatus.OK)
clearUserCart(@Req() req: AppRequest) {
this.cartService.removeByUserId(getUserIdFromRequest(req));
async clearUserCart(@Req() req: AppRequest): Promise<void> {
await this.cartService.removeByUserId(getUserIdFromRequest(req));
}

// @UseGuards(JwtAuthGuard)
@UseGuards(BasicAuthGuard)
@Put('order')
checkout(@Req() req: AppRequest, @Body() body: CreateOrderDto) {
async checkout(@Req() req: AppRequest, @Body() body: CreateOrderDto) {
const userId = getUserIdFromRequest(req);
const cart = this.cartService.findByUserId(userId);
const cart = await this.cartService.findByUserId(userId);

if (!(cart && cart.items.length)) {
throw new BadRequestException('Cart is empty');
Expand All @@ -83,7 +83,7 @@ export class CartController {
address: body.address,
total,
});
this.cartService.removeByUserId(userId);
await this.cartService.removeByUserId(userId);

return {
order,
Expand Down
7 changes: 6 additions & 1 deletion src/cart/cart.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { OrderModule } from '../order/order.module';

import { CartController } from './cart.controller';
import { CartService } from './services';
import { CartEntity, CartItemEntity } from './entities';

@Module({
imports: [OrderModule],
imports: [
TypeOrmModule.forFeature([CartEntity, CartItemEntity]),
OrderModule,
],
providers: [CartService],
controllers: [CartController],
})
Expand Down
28 changes: 28 additions & 0 deletions src/cart/entities/cart-item.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Entity, PrimaryColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
import { CartEntity } from './cart.entity';

@Entity('cart_items')
export class CartItemEntity {
@PrimaryColumn({ type: 'uuid' })
cart_id: string;

@PrimaryColumn({ type: 'varchar' })
product_id: string;

@Column({ type: 'int', nullable: false })
count: number;

// Store product details as JSON for denormalization (since products are in a different service)
@Column({ type: 'jsonb', nullable: true })
product_details: {
title?: string;
description?: string;
price?: number;
};

@ManyToOne(() => CartEntity, (cart) => cart.items, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'cart_id' })
cart: CartEntity;
}
35 changes: 35 additions & 0 deletions src/cart/entities/cart.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { CartItemEntity } from './cart-item.entity';

export enum CartStatus {
OPEN = 'OPEN',
ORDERED = 'ORDERED',
}

@Entity('carts')
export class CartEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ type: 'varchar', nullable: false })
user_id: string;

@Column({
type: 'enum',
enum: CartStatus,
default: CartStatus.OPEN,
})
status: CartStatus;

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created_at: Date;

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
updated_at: Date;

@OneToMany(() => CartItemEntity, (cartItem) => cartItem.cart, {
cascade: true,
eager: true,
})
items: CartItemEntity[];
}
2 changes: 2 additions & 0 deletions src/cart/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { CartEntity, CartStatus } from './cart.entity';
export { CartItemEntity } from './cart-item.entity';
Loading