Compare commits
4 Commits
4ee47eba4b
...
088acc515d
Author | SHA1 | Date |
---|---|---|
mahdi | 088acc515d | 1 week ago |
mahdi | 7a46dd79ff | 1 week ago |
mahdi | fbaeb45d2c | 1 week ago |
mahdi | c96ff31be7 | 1 week ago |
37 changed files with 834 additions and 9 deletions
@ -1,8 +1,9 @@ |
||||
export class CreateOrderDto { |
||||
userId: number; |
||||
orderDate: Date; |
||||
orderDate?: Date; |
||||
status: number; |
||||
comments: string; |
||||
shippedDate: Date; |
||||
shippedDate?: Date; |
||||
shipperName: string; |
||||
products: number[]; |
||||
} |
||||
|
@ -0,0 +1,5 @@ |
||||
export class CreateReceiptDto { |
||||
userId: number; |
||||
totalPrice: number; |
||||
products: number[]; |
||||
} |
@ -0,0 +1,2 @@ |
||||
export * from './create-receipt.dto'; |
||||
export * from './update-receipt.dto'; |
@ -0,0 +1,4 @@ |
||||
import { PartialType } from '@nestjs/mapped-types'; |
||||
import { CreateReceiptDto } from './create-receipt.dto'; |
||||
|
||||
export class UpdateReceiptDto extends PartialType(CreateReceiptDto) {} |
@ -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: 'receipts', updatedAt: false }) |
||||
export class Receipt extends Model<Receipt> { |
||||
@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,37 @@ |
||||
import { |
||||
Controller, |
||||
Get, |
||||
Post, |
||||
Body, |
||||
Patch, |
||||
Param, |
||||
Delete, |
||||
} from '@nestjs/common'; |
||||
import { ReceiptsService } from './receipts.service'; |
||||
import { CreateReceiptDto } from './dto/create-receipt.dto'; |
||||
import { UpdateReceiptDto } from './dto/update-receipt.dto'; |
||||
|
||||
@Controller('receipts') |
||||
export class ReceiptsController { |
||||
constructor(private readonly receiptsService: ReceiptsService) {} |
||||
|
||||
@Post() |
||||
create(@Body() createReceiptDto: CreateReceiptDto) { |
||||
return this.receiptsService.create(createReceiptDto); |
||||
} |
||||
|
||||
@Get() |
||||
findAll() { |
||||
return this.receiptsService.findAll(); |
||||
} |
||||
|
||||
@Get('users/:id') |
||||
findByUser(@Param('id') id: string) { |
||||
return this.receiptsService.findByUser(+id); |
||||
} |
||||
|
||||
@Delete(':id') |
||||
remove(@Param('id') id: string) { |
||||
return this.receiptsService.remove(+id); |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
import { Module } from '@nestjs/common'; |
||||
import { ReceiptsService } from './receipts.service'; |
||||
import { ReceiptsController } from './receipts.controller'; |
||||
import { receiptsProviders } from './receipts.providers'; |
||||
|
||||
@Module({ |
||||
controllers: [ReceiptsController], |
||||
providers: [ReceiptsService, ...receiptsProviders], |
||||
exports: [ReceiptsService], |
||||
}) |
||||
export class ReceiptsModule {} |
@ -0,0 +1,9 @@ |
||||
import { Receipt } from './entities/receipt.entity'; |
||||
import { RECEIPT_REPOSITORY } from '../../core/constants'; |
||||
|
||||
export const receiptsProviders = [ |
||||
{ |
||||
provide: RECEIPT_REPOSITORY, |
||||
useValue: Receipt, |
||||
}, |
||||
]; |
@ -0,0 +1,34 @@ |
||||
import { Inject, Injectable } from '@nestjs/common'; |
||||
import { CreateReceiptDto, UpdateReceiptDto } from './dto'; |
||||
import { RECEIPT_REPOSITORY } from 'src/core/constants'; |
||||
import { Receipt } from './entities/receipt.entity'; |
||||
|
||||
@Injectable() |
||||
export class ReceiptsService { |
||||
constructor( |
||||
@Inject(RECEIPT_REPOSITORY) |
||||
private readonly receiptRepository: typeof Receipt, |
||||
) {} |
||||
|
||||
async create(createReceiptDto: CreateReceiptDto) { |
||||
return await this.receiptRepository.create(createReceiptDto); |
||||
} |
||||
|
||||
async findAll() { |
||||
return await this.receiptRepository.findAll(); |
||||
} |
||||
|
||||
async findByUser(id: number) { |
||||
return await this.receiptRepository.findAll({ where: { userId: id } }); |
||||
} |
||||
|
||||
async remove(id: number) { |
||||
const deletedReceipt = await this.receiptRepository.findAll({ |
||||
where: { id }, |
||||
}); |
||||
|
||||
await this.receiptRepository.destroy({ where: { id } }); |
||||
|
||||
return deletedReceipt; |
||||
} |
||||
} |
@ -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 { User } from 'src/modules/users/entities/user.entity'; |
||||
|
||||
@Table({ tableName: 'shopping_cards' }) |
||||
export class ShoppingCard extends Model<ShoppingCard> { |
||||
@Column({ |
||||
type: DataType.FLOAT, |
||||
allowNull: false, |
||||
defaultValue: 0, |
||||
}) |
||||
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,26 @@ |
||||
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'; |
||||
import { OrdersService } from '../orders/orders.service'; |
||||
import { ordersProviders } from '../orders/orders.providers'; |
||||
|
||||
@Module({ |
||||
controllers: [ShoppingCardsController], |
||||
providers: [ |
||||
ShoppingCardsService, |
||||
...shoppingCardsProviders, |
||||
ReceiptsService, |
||||
...receiptsProviders, |
||||
ProductsService, |
||||
...productsProviders, |
||||
OrdersService, |
||||
...ordersProviders, |
||||
], |
||||
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,91 @@ |
||||
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'; |
||||
import { OrdersService } from '../orders/orders.service'; |
||||
import { CreateOrderDto } from '../orders/dto'; |
||||
|
||||
@Injectable() |
||||
export class ShoppingCardsService { |
||||
constructor( |
||||
@Inject(SHOPPING_CARD_REPOSITORY) |
||||
private readonly shoppingCardRepository: typeof ShoppingCard, |
||||
private readonly receiptService: ReceiptsService, |
||||
private readonly productsService: ProductsService, |
||||
private readonly ordersService: OrdersService, |
||||
) {} |
||||
async create(createShoppingCardDto: CreateShoppingCardDto) { |
||||
const card = await this.shoppingCardRepository.create( |
||||
createShoppingCardDto, |
||||
); |
||||
|
||||
for (let p of card.products) { |
||||
const [product] = await this.productsService.findOne(p); |
||||
card.totalPrice += product.pricePerUnit; |
||||
} |
||||
|
||||
return await card.save(); |
||||
} |
||||
|
||||
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 }, |
||||
); |
||||
|
||||
const [card] = await this.shoppingCardRepository.findAll({ |
||||
where: { userId: id }, |
||||
}); |
||||
|
||||
card.totalPrice = 0; |
||||
|
||||
for (let p of card.products) { |
||||
const [product] = await this.productsService.findOne(p); |
||||
card.totalPrice += product.pricePerUnit; |
||||
} |
||||
|
||||
return card.save(); |
||||
} |
||||
|
||||
async remove(id: number) { |
||||
return await this.shoppingCardRepository.destroy({ where: { userId: id } }); |
||||
} |
||||
|
||||
async submitPurchase(id: number) { |
||||
const [card] = await this.findbyUser(id); |
||||
|
||||
await this.createReceipt({ |
||||
userId: id, |
||||
products: card.products, |
||||
totalPrice: card.totalPrice, |
||||
}); |
||||
|
||||
await this.remove(id); |
||||
|
||||
const order: CreateOrderDto = { |
||||
userId: id, |
||||
status: 1, |
||||
comments: 'ship asap', |
||||
shipperName: 'meti comp', |
||||
products: card.products, |
||||
}; |
||||
return await this.ordersService.create(order); |
||||
} |
||||
|
||||
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; |
||||
} |
||||
|
@ -0,0 +1,5 @@ |
||||
export class CreateWalletsTransactionDto { |
||||
userId: number; |
||||
operation: number; |
||||
byAmount: number; |
||||
} |
@ -0,0 +1,14 @@ |
||||
import { Table, Column, Model, DataType, HasOne } from 'sequelize-typescript'; |
||||
import { WalletsTransactions } from './wallets-transaction.entity'; |
||||
|
||||
@Table({ tableName: 'transaction_operations', timestamps: false }) |
||||
export class TransactionOperations extends Model<TransactionOperations> { |
||||
@Column({ |
||||
type: DataType.STRING, |
||||
allowNull: false, |
||||
}) |
||||
name: string; |
||||
|
||||
@HasOne(() => WalletsTransactions) |
||||
order: WalletsTransactions; |
||||
} |
@ -0,0 +1,34 @@ |
||||
import { |
||||
Table, |
||||
Column, |
||||
Model, |
||||
DataType, |
||||
ForeignKey, |
||||
BelongsTo, |
||||
} from 'sequelize-typescript'; |
||||
import { User } from 'src/modules/users/entities/user.entity'; |
||||
import { TransactionOperations } from './transaction-operation.entity'; |
||||
|
||||
@Table({ tableName: 'wallets_transactions', updatedAt: false }) |
||||
export class WalletsTransactions extends Model<WalletsTransactions> { |
||||
@Column({ |
||||
type: DataType.FLOAT, |
||||
allowNull: false, |
||||
defaultValue: 0, |
||||
}) |
||||
byAmount: number; |
||||
|
||||
@ForeignKey(() => User) |
||||
@Column |
||||
userId: number; |
||||
|
||||
@BelongsTo(() => User) |
||||
user: User; |
||||
|
||||
@ForeignKey(() => TransactionOperations) |
||||
@Column |
||||
operation: number; |
||||
|
||||
@BelongsTo(() => TransactionOperations) |
||||
transactionOperations: TransactionOperations; |
||||
} |
@ -0,0 +1,33 @@ |
||||
import { |
||||
Controller, |
||||
Get, |
||||
Post, |
||||
Body, |
||||
Patch, |
||||
Param, |
||||
Delete, |
||||
} from '@nestjs/common'; |
||||
import { WalletsTransactionsService } from './wallets-transactions.service'; |
||||
import { CreateWalletsTransactionDto } from './dto/create-wallets-transaction.dto'; |
||||
|
||||
@Controller('wallets-transactions') |
||||
export class WalletsTransactionsController { |
||||
constructor( |
||||
private readonly walletsTransactionsService: WalletsTransactionsService, |
||||
) {} |
||||
|
||||
@Get() |
||||
findAll() { |
||||
return this.walletsTransactionsService.findAll(); |
||||
} |
||||
|
||||
@Get(':id') |
||||
findOne(@Param('id') id: number) { |
||||
return this.walletsTransactionsService.findOne(id); |
||||
} |
||||
|
||||
@Get('users/:id') |
||||
findByUser(@Param('id') id: number) { |
||||
return this.walletsTransactionsService.findByUser(id); |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
import { Module } from '@nestjs/common'; |
||||
import { WalletsTransactionsService } from './wallets-transactions.service'; |
||||
import { WalletsTransactionsController } from './wallets-transactions.controller'; |
||||
import { walletsTransactionsProviders } from './wallets-transactions.providers'; |
||||
|
||||
@Module({ |
||||
controllers: [WalletsTransactionsController], |
||||
providers: [WalletsTransactionsService, ...walletsTransactionsProviders], |
||||
exports: [WalletsTransactionsService], |
||||
}) |
||||
export class WalletsTransactionsModule {} |
@ -0,0 +1,9 @@ |
||||
import { WalletsTransactions } from './entities/wallets-transaction.entity'; |
||||
import { WALLET_TRANSACTIONS_REPOSITORY } from '../../core/constants'; |
||||
|
||||
export const walletsTransactionsProviders = [ |
||||
{ |
||||
provide: WALLET_TRANSACTIONS_REPOSITORY, |
||||
useValue: WalletsTransactions, |
||||
}, |
||||
]; |
@ -0,0 +1,31 @@ |
||||
import { Inject, Injectable } from '@nestjs/common'; |
||||
import { CreateWalletsTransactionDto } from './dto/create-wallets-transaction.dto'; |
||||
import { WALLET_TRANSACTIONS_REPOSITORY } from 'src/core/constants'; |
||||
import { WalletsTransactions } from './entities/wallets-transaction.entity'; |
||||
|
||||
@Injectable() |
||||
export class WalletsTransactionsService { |
||||
constructor( |
||||
@Inject(WALLET_TRANSACTIONS_REPOSITORY) |
||||
private readonly walletsTransactionsRepository: typeof WalletsTransactions, |
||||
) {} |
||||
async create(createWalletsTransactionDto: CreateWalletsTransactionDto) { |
||||
return await this.walletsTransactionsRepository.create( |
||||
createWalletsTransactionDto, |
||||
); |
||||
} |
||||
|
||||
async findAll() { |
||||
return await this.walletsTransactionsRepository.findAll(); |
||||
} |
||||
|
||||
async findOne(id: number) { |
||||
return await this.walletsTransactionsRepository.findAll({ where: { id } }); |
||||
} |
||||
|
||||
async findByUser(id: number) { |
||||
return await this.walletsTransactionsRepository.findAll({ |
||||
where: { userId: id }, |
||||
}); |
||||
} |
||||
} |
Loading…
Reference in new issue