Skip to content

Commit

Permalink
[Refractor][Order] refractor code
Browse files Browse the repository at this point in the history
  • Loading branch information
nghiavohuynhdai committed Feb 7, 2024
1 parent ef5b3c2 commit b04ae11
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/auth/controllers/customer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export class AuthCustomerController {
@ApiBody({ type: LoginReqDto })
@ApiOkResponse({ type: DataResponse(TokenResDto) })
@ApiBadRequestResponse({ type: ErrorResponse })
async login(@Body() loginReqDto: LoginReqDto): Promise<TokenResDto> {
login(@Body() loginReqDto: LoginReqDto): Promise<TokenResDto> {
return this.authService.login(loginReqDto, UserSide.CUSTOMER)
}

@Post('google')
@ApiOkResponse({ type: DataResponse(TokenResDto) })
@ApiBadRequestResponse({ type: ErrorResponse })
async googleLogin(@Body() googleLoginReqDto: GoogleLoginReqDto): Promise<TokenResDto> {
googleLogin(@Body() googleLoginReqDto: GoogleLoginReqDto): Promise<TokenResDto> {
return this.authService.googleLogin(googleLoginReqDto)
}

Expand Down
2 changes: 1 addition & 1 deletion src/auth/strategies/jwt-access.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class JwtAccessStrategy extends PassportStrategy(Strategy, 'jwt-access')
super(opts)
}

async validate(payload: AccessTokenPayload) {
validate(payload: AccessTokenPayload) {
return { _id: payload.sub, name: payload.name, role: payload.role }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/auth/strategies/jwt-refresh.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class JwtRefreshStrategy extends PassportStrategy(Strategy, 'jwt-refresh'
super(opts)
}

async validate(payload: RefreshTokenPayload) {
validate(payload: RefreshTokenPayload) {
return { _id: payload.sub, role: payload.role }
}
}
Expand Down
1 change: 0 additions & 1 deletion src/customer/schemas/customer.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as paginate from 'mongoose-paginate-v2'
import { ApiProperty } from '@nestjs/swagger'
import { Transform } from 'class-transformer'
import { Gender, Status } from '@common/contracts/constant'
import { EMAIL_REGEX, PHONE_REGEX, URL_REGEX } from '@src/config'

export type CustomerDocument = HydratedDocument<Customer>

Expand Down
8 changes: 4 additions & 4 deletions src/order/controllers/customer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { JwtAuthGuard } from '@auth/guards/jwt-auth.guard'
import { RolesGuard } from '@auth/guards/roles.guard'
import { OrderStatus, TransactionStatus, UserRole } from '@common/contracts/constant'
import { Roles } from '@auth/decorators/roles.decorator'
import { CreateOrderDto, OrderDto } from '@order/dto/order.dto'
import { CreateOrderDto, OrderDto, PublicOrderHistoryDto } from '@order/dto/order.dto'
import { OrderService } from '@order/services/order.service'
import { OrderHistoryDto } from '@order/schemas/order.schema'
import { Pagination, PaginationParams } from '@common/decorators/pagination.decorator'
Expand Down Expand Up @@ -42,7 +42,7 @@ export class OrderCustomerController {
@ApiQuery({ type: PaginationQuery })
async getPurchaseHistory(@Req() req, @Pagination() paginationParams: PaginationParams) {
const customerId = _.get(req, 'user._id')
return await this.orderService.getPurchaseHistory(customerId, {}, paginationParams)
return await this.orderService.getOrderList({ 'customer._id': customerId }, paginationParams)
}

@Get(':orderId')
Expand All @@ -53,14 +53,14 @@ export class OrderCustomerController {
@ApiBadRequestResponse({ type: ErrorResponse })
async getPurchaseDetails(@Req() req, @Param('orderId') orderId: string) {
const customerId = _.get(req, 'user._id')
return await this.orderService.getPurchaseDetails(customerId, orderId)
return await this.orderService.getOrderDetails({ 'customer._id': customerId, _id: orderId })
}

@Get(':orderId/history')
@ApiOperation({
summary: 'Get a customer order history'
})
@ApiOkResponse({ type: DataResponse(OrderDto) })
@ApiOkResponse({ type: DataResponse(PublicOrderHistoryDto) })
@ApiBadRequestResponse({ type: ErrorResponse })
async getOrderHistory(@Req() req, @Param('orderId') orderId: string) {
const { _id: customerId } = _.get(req, 'user')
Expand Down
2 changes: 1 addition & 1 deletion src/order/controllers/provider.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class OrderProviderController {
})
@ApiOkResponse({ type: OrderResponseDto })
getOrderDetail(@Param('orderId') orderId: string) {
return this.orderService.getOrderDetail(orderId)
return this.orderService.getOrderDetails({ _id: orderId })
}

@Patch(':orderId/confirm')
Expand Down
11 changes: 11 additions & 0 deletions src/order/dto/order.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,14 @@ export class CancelOrderDto {
orderId?: string
orderHistoryItem?: OrderHistoryDto
}

export class PublicOrderHistoryDto {
@ApiProperty()
orderStatus: OrderStatus

@ApiProperty()
transactionStatus: TransactionStatus

@ApiProperty()
timestamp: Date
}
2 changes: 1 addition & 1 deletion src/order/schemas/order.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as paginate from 'mongoose-paginate-v2'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { Transform } from 'class-transformer'
import { OrderStatus, TransactionStatus, UserRole } from '@common/contracts/constant'
import { IsEmail, IsNotEmpty, IsPhoneNumber, MaxLength, Min, ValidateNested } from 'class-validator'
import { IsEmail, IsNotEmpty, IsPhoneNumber, MaxLength, ValidateNested } from 'class-validator'
import { Product } from '@product/schemas/product.schema'
import { CreateOrderItemDto } from '@order/dto/order.dto'

Expand Down
41 changes: 10 additions & 31 deletions src/order/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export class OrderService {
return result
}

public async getOrderDetail(orderId: string) {
public async getOrderDetails(filter: FilterQuery<Order>) {
const order = await this.orderRepository.findOne({
conditions: {
_id: orderId,
...filter,
status: {
$ne: OrderStatus.DELETED
}
Expand All @@ -49,46 +49,25 @@ export class OrderService {
return order
}

public async getPurchaseHistory(customerId: string, filter: FilterQuery<Order>, paginationParams: PaginationParams) {
const orders = await this.orderRepository.paginate(
{
'customer._id': customerId,
...filter,
status: {
$ne: OrderStatus.DELETED
}
},
{ ...paginationParams }
)
return orders
}

public async getPurchaseDetails(customerId: string, orderId: string) {
const order = await this.orderRepository.findOne({
conditions: {
'customer._id': customerId,
_id: orderId,
status: {
$ne: OrderStatus.DELETED
}
},
projection: '+items'
})
}

public async getOrderHistory(customerId: string, orderId: string) {
const order = await this.orderRepository.findOne({
conditions: {
_id: orderId,
'customer._id': customerId,
status: { $ne: OrderStatus.DELETED }
},
projection: '+orderHistory'
projection: {
orderHistory: {
orderStatus: 1,
transactionStatus: 1,
timestamp: 1
}
}
})

if (!order) throw new AppException(Errors.ORDER_NOT_FOUND)

return order
return order.orderHistory
}

public async createOrder(createOrderDto: CreateOrderDto) {
Expand Down
3 changes: 0 additions & 3 deletions src/product/dto/product.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import {
ArrayMinSize,
IsArray,
IsNotEmpty,
IsNumber,
IsUrl,
Max,
MaxLength,
Min,
ValidateNested
} from 'class-validator'
import { Types } from 'mongoose'
Expand Down
2 changes: 1 addition & 1 deletion src/product/services/product.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'
import { BadRequestException, Injectable } from '@nestjs/common'
import { ProductRepository } from '@product/repositories/product.repository'
import { PaginationParams } from '@common/decorators/pagination.decorator'
import { CreateProductDto } from '@product/dto/product.dto'
Expand Down

0 comments on commit b04ae11

Please sign in to comment.