Create shopping-card module

master
mahdi 1 week ago
parent fbaeb45d2c
commit 7a46dd79ff
  1. 1
      src/modules/orders/dto/create-order.dto.ts
  2. 7
      src/modules/orders/entities/order.entity.ts
  3. 1
      src/modules/products/products.controller.ts
  4. 13
      src/modules/products/products.module.ts
  5. 4
      src/modules/receipts/dto/create-receipt.dto.ts
  6. 4
      src/modules/receipts/entities/receipt.entity.ts
  7. 7
      src/modules/shopping-cards/dto/create-shopping-card.dto.ts
  8. 2
      src/modules/shopping-cards/dto/index.ts
  9. 4
      src/modules/shopping-cards/dto/update-shopping-card.dto.ts
  10. 32
      src/modules/shopping-cards/entities/shopping-card.entity.ts
  11. 51
      src/modules/shopping-cards/shopping-cards.controller.ts
  12. 22
      src/modules/shopping-cards/shopping-cards.module.ts
  13. 9
      src/modules/shopping-cards/shopping-cards.providers.ts
  14. 63
      src/modules/shopping-cards/shopping-cards.service.ts
  15. 27
      src/modules/users/dto/create-user.dto.ts
  16. 4
      src/modules/users/entities/user.entity.ts

@ -5,4 +5,5 @@ export class CreateOrderDto {
comments: string;
shippedDate: Date;
shipperName: string;
products: number[];
}

@ -8,6 +8,7 @@ import {
} from 'sequelize-typescript';
import { User } from 'src/modules/users/entities/user.entity';
import { OrderStatuses } from 'src/modules/orders/entities/order-status.entity';
import { Product } from 'src/modules/products/entities/product.entity';
@Table({ tableName: 'orders' })
export class Order extends Model<Order> {
@ -18,6 +19,12 @@ export class Order extends Model<Order> {
})
orderDate: Date;
@Column({
type: DataType.ARRAY(DataType.INTEGER),
allowNull: false,
})
products: number[];
@ForeignKey(() => OrderStatuses)
@Column
status: number;

@ -10,7 +10,6 @@ import {
} from '@nestjs/common';
import { ProductsService } from './products.service';
import { CreateProductDto, UpdateProductDto } from './dto';
import { UUID } from 'crypto';
import { AuthGuard } from '@nestjs/passport';
@Controller('products')

@ -2,10 +2,21 @@ import { Module } from '@nestjs/common';
import { ProductsService } from './products.service';
import { ProductsController } from './products.controller';
import { productsProviders } from './products.providers';
import { ShoppingCardsService } from '../shopping-cards/shopping-cards.service';
import { ReceiptsService } from '../receipts/receipts.service';
import { shoppingCardsProviders } from '../shopping-cards/shopping-cards.providers';
import { receiptsProviders } from '../receipts/receipts.providers';
@Module({
controllers: [ProductsController],
exports: [ProductsService],
providers: [ProductsService, ...productsProviders],
providers: [
ProductsService,
...productsProviders,
ShoppingCardsService,
ReceiptsService,
...shoppingCardsProviders,
...receiptsProviders,
],
})
export class ProductsModule {}

@ -1,7 +1,5 @@
import { Product } from 'src/modules/products/entities/product.entity';
export class CreateReceiptDto {
userId: number;
totalPrice: number;
products: Product[];
products: number[];
}

@ -18,10 +18,10 @@ export class Receipt extends Model<Receipt> {
totalPrice: number;
@Column({
type: DataType.ARRAY(DataType.JSON),
type: DataType.ARRAY(DataType.INTEGER),
allowNull: false,
})
products: Product[];
products: number[];
@ForeignKey(() => User)
@Column

@ -0,0 +1,7 @@
import { Product } from 'src/modules/products/entities/product.entity';
export class CreateShoppingCardDto {
userId: number;
products: number[];
totalPrice: number;
}

@ -0,0 +1,2 @@
export * from './create-shopping-card.dto';
export * from './update-shopping-card.dto';

@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateShoppingCardDto } from './create-shopping-card.dto';
export class UpdateShoppingCardDto extends PartialType(CreateShoppingCardDto) {}

@ -0,0 +1,32 @@
import {
Table,
Column,
Model,
DataType,
ForeignKey,
BelongsTo,
} from 'sequelize-typescript';
import { Product } from 'src/modules/products/entities/product.entity';
import { User } from 'src/modules/users/entities/user.entity';
@Table({ tableName: 'shopping_cards' })
export class ShoppingCard extends Model<ShoppingCard> {
@Column({
type: DataType.FLOAT,
allowNull: false,
})
totalPrice: number;
@Column({
type: DataType.ARRAY(DataType.INTEGER),
allowNull: false,
})
products: number[];
@ForeignKey(() => User)
@Column
userId: number;
@BelongsTo(() => User)
user: User;
}

@ -0,0 +1,51 @@
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
} from '@nestjs/common';
import { ShoppingCardsService } from './shopping-cards.service';
import { CreateShoppingCardDto, UpdateShoppingCardDto } from './dto';
import { ReceiptsService } from '../receipts/receipts.service';
import { CreateReceiptDto } from '../receipts/dto';
@Controller('shopping-cards')
export class ShoppingCardsController {
constructor(private readonly shoppingCardsService: ShoppingCardsService) {}
@Post()
create(@Body() createShoppingCardDto: CreateShoppingCardDto) {
return this.shoppingCardsService.create(createShoppingCardDto);
}
@Get()
findAll() {
return this.shoppingCardsService.findAll();
}
@Get(':id')
findbyUser(@Param('id') id: string) {
return this.shoppingCardsService.findbyUser(+id);
}
@Patch(':id')
update(
@Param('id') id: string,
@Body() updateShoppingCardDto: UpdateShoppingCardDto,
) {
return this.shoppingCardsService.update(+id, updateShoppingCardDto);
}
@Delete('/purchase/:id')
submitPurchase(@Param('id') id: string) {
return this.shoppingCardsService.submitPurchase(+id);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.shoppingCardsService.remove(+id);
}
}

@ -0,0 +1,22 @@
import { Module } from '@nestjs/common';
import { ShoppingCardsService } from './shopping-cards.service';
import { ShoppingCardsController } from './shopping-cards.controller';
import { shoppingCardsProviders } from './shopping-cards.providers';
import { ReceiptsService } from '../receipts/receipts.service';
import { receiptsProviders } from '../receipts/receipts.providers';
import { ProductsService } from '../products/products.service';
import { productsProviders } from '../products/products.providers';
@Module({
controllers: [ShoppingCardsController],
providers: [
ShoppingCardsService,
...shoppingCardsProviders,
ReceiptsService,
...receiptsProviders,
ProductsService,
...productsProviders,
],
exports: [ShoppingCardsService],
})
export class ShoppingCardsModule {}

@ -0,0 +1,9 @@
import { ShoppingCard } from './entities/shopping-card.entity';
import { SHOPPING_CARD_REPOSITORY } from '../../core/constants';
export const shoppingCardsProviders = [
{
provide: SHOPPING_CARD_REPOSITORY,
useValue: ShoppingCard,
},
];

@ -0,0 +1,63 @@
import { Inject, Injectable } from '@nestjs/common';
import { CreateShoppingCardDto } from './dto/create-shopping-card.dto';
import { UpdateShoppingCardDto } from './dto/update-shopping-card.dto';
import { SHOPPING_CARD_REPOSITORY } from 'src/core/constants';
import { ShoppingCard } from './entities/shopping-card.entity';
import { CreateReceiptDto } from '../receipts/dto';
import { ReceiptsService } from '../receipts/receipts.service';
import { ProductsService } from '../products/products.service';
@Injectable()
export class ShoppingCardsService {
constructor(
@Inject(SHOPPING_CARD_REPOSITORY)
private readonly shoppingCardRepository: typeof ShoppingCard,
private readonly receiptService: ReceiptsService,
private readonly productsService: ProductsService,
) {}
async create(createShoppingCardDto: CreateShoppingCardDto) {
return await this.shoppingCardRepository.create(createShoppingCardDto);
}
async findAll() {
return await this.shoppingCardRepository.findAll();
}
async findbyUser(id: number) {
return await this.shoppingCardRepository.findAll({ where: { userId: id } });
}
async update(id: number, updateShoppingCardDto: UpdateShoppingCardDto) {
const [numberOfAffectedRows, [updatedCard]] =
await this.shoppingCardRepository.update(
{ ...updateShoppingCardDto },
{ where: { userId: id }, returning: true },
);
return { numberOfAffectedRows, updatedCard };
}
async remove(id: number) {
return await this.shoppingCardRepository.destroy({ where: { userId: id } });
}
async submitPurchase(id: number) {
const [card] = await this.findbyUser(id);
let total = 0;
for (let i of card.products) {
const [product] = await this.productsService.findOne(i);
total += product.pricePerUnit;
}
await this.createReceipt({
userId: id,
products: card.products,
totalPrice: total,
});
return await this.remove(id);
}
private async createReceipt(createReceiptDto: CreateReceiptDto) {
await this.receiptService.create(createReceiptDto);
}
}

@ -1,9 +1,36 @@
import {
IsEmail,
IsEnum,
IsString,
IsNotEmpty,
IsOptional,
} from 'class-validator';
export class CreateUserDto {
@IsNotEmpty()
@IsString()
firstName: string;
@IsNotEmpty()
@IsString()
lastName: string;
@IsNotEmpty()
@IsEmail()
email: string;
@IsNotEmpty()
@IsString()
password: string;
@IsOptional()
@IsString()
phoneNumber: string;
@IsOptional()
@IsEnum(['mail', 'female'])
gender: string;
@IsOptional()
city: string;
}

@ -9,6 +9,7 @@ import {
import { Order } from '../../orders/entities/order.entity';
import { Wallet } from 'src/modules/wallets/entities/wallet.entity';
import { Receipt } from 'src/modules/receipts/entities/receipt.entity';
import { ShoppingCard } from 'src/modules/shopping-cards/entities/shopping-card.entity';
@Table({ tableName: 'users' })
export class User extends Model<User> {
@ -69,6 +70,9 @@ export class User extends Model<User> {
@HasOne(() => Wallet)
wallet: Wallet;
@HasOne(() => ShoppingCard)
shoppingCard: ShoppingCard;
@HasMany(() => Receipt)
receipts: Receipt[];
}

Loading…
Cancel
Save