-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix total order filter and add cancell order module
- Loading branch information
1 parent
278be6f
commit fde38b4
Showing
12 changed files
with
315 additions
and
2 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
apps/e-commerce/src/admin/cancell-order/cancell-order.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
ApiBearerAuth, | ||
ApiOperation, | ||
ApiQuery, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { CancellOrderService } from './cancell-order.service'; | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpCode, | ||
HttpStatus, | ||
Param, | ||
Patch, | ||
Query, | ||
UseGuards, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { JwtGuard } from '@rahino/auth/guard'; | ||
import { JsonResponseTransformInterceptor } from '@rahino/response/interceptor'; | ||
import { GetUser } from '@rahino/auth/decorator'; | ||
import { User } from '@rahino/database/models/core/user.entity'; | ||
import { PermissionGuard } from '@rahino/permission-checker/guard'; | ||
import { CheckPermission } from '@rahino/permission-checker/decorator'; | ||
import { GetTotalOrderFilterDto } from './dto/get-total-order.dto'; | ||
|
||
@ApiTags('Total-Orders') | ||
@UseGuards(JwtGuard, PermissionGuard) | ||
@ApiBearerAuth() | ||
@UseInterceptors(JsonResponseTransformInterceptor) | ||
@Controller({ | ||
path: '/api/ecommerce/admin/totalOrders', | ||
version: ['1'], | ||
}) | ||
export class CancellOrderController { | ||
constructor(private readonly service: CancellOrderService) {} | ||
|
||
@ApiOperation({ description: 'show all cancell orders' }) | ||
@CheckPermission({ permissionSymbol: 'ecommerce.admin.cancellorders.getall' }) | ||
@Get('/') | ||
@ApiQuery({ | ||
name: 'filter', | ||
type: GetTotalOrderFilterDto, | ||
style: 'deepObject', | ||
explode: true, | ||
}) | ||
@HttpCode(HttpStatus.OK) | ||
async findAll( | ||
@GetUser() user: User, | ||
@Query() filter: GetTotalOrderFilterDto, | ||
) { | ||
return await this.service.findAll(user, filter); | ||
} | ||
|
||
@ApiOperation({ description: 'show cancell orders by given id' }) | ||
@CheckPermission({ permissionSymbol: 'ecommerce.admin.cancellorders.getone' }) | ||
@Get('/:id') | ||
@HttpCode(HttpStatus.OK) | ||
async findById(@Param('id') entityId: bigint, @GetUser() user: User) { | ||
return await this.service.findById(entityId, user); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
apps/e-commerce/src/admin/cancell-order/cancell-order.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CancellOrderController } from './cancell-order.controller'; | ||
import { CancellOrderService } from './cancell-order.service'; | ||
import { SequelizeModule } from '@nestjs/sequelize'; | ||
import { User } from '@rahino/database/models/core/user.entity'; | ||
import { Permission } from '@rahino/database/models/core/permission.entity'; | ||
import { ECOrder } from '@rahino/database/models/ecommerce-eav/ec-order.entity'; | ||
import { UtilOrderModule } from '../utilOrder/util-order.module'; | ||
import { ECOrderDetail } from '@rahino/database/models/ecommerce-eav/ec-order-detail.entity'; | ||
import { SnappayModule } from '@rahino/ecommerce/user/payment/provider/snappay.module'; | ||
import { ECPayment } from '@rahino/database/models/ecommerce-eav/ec-payment-entity'; | ||
import { ECPaymentGateway } from '@rahino/database/models/ecommerce-eav/ec-payment-gateway.entity'; | ||
import { RoleUtilModule } from '@rahino/core/user/role-util/role-util.module'; | ||
import { UserVendorModule } from '@rahino/ecommerce/user/vendor/user-vendor.module'; | ||
import { FinalizedPaymentModule } from '@rahino/ecommerce/user/payment/util/finalized-payment/finalized-payment.module'; | ||
import { ECOrderStatus } from '@rahino/database/models/ecommerce-eav/ec-order-status.entity'; | ||
import { ECOrderShipmentWay } from '@rahino/database/models/ecommerce-eav/ec-order-shipmentway.entity'; | ||
|
||
@Module({ | ||
imports: [ | ||
SequelizeModule.forFeature([ | ||
User, | ||
Permission, | ||
ECOrder, | ||
ECOrderDetail, | ||
ECPayment, | ||
ECPaymentGateway, | ||
ECOrderStatus, | ||
ECOrderShipmentWay, | ||
]), | ||
SequelizeModule, | ||
UtilOrderModule, | ||
SnappayModule, | ||
RoleUtilModule, | ||
UserVendorModule, | ||
FinalizedPaymentModule, | ||
], | ||
controllers: [CancellOrderController], | ||
providers: [CancellOrderService], | ||
}) | ||
export class CancellOrderModule {} |
139 changes: 139 additions & 0 deletions
139
apps/e-commerce/src/admin/cancell-order/cancell-order.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { | ||
BadRequestException, | ||
ForbiddenException, | ||
Injectable, | ||
InternalServerErrorException, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { InjectConnection, InjectModel } from '@nestjs/sequelize'; | ||
import { User } from '@rahino/database/models/core/user.entity'; | ||
import { ECOrder } from '@rahino/database/models/ecommerce-eav/ec-order.entity'; | ||
import { OrderStatusEnum } from '@rahino/ecommerce/util/enum'; | ||
import { OrderQueryBuilder } from '../utilOrder/service/order-query-builder.service'; | ||
import { ListFilter } from '@rahino/query-filter'; | ||
import { ECOrderDetail } from '@rahino/database/models/ecommerce-eav/ec-order-detail.entity'; | ||
import { Sequelize, Transaction } from 'sequelize'; | ||
import { QueryOptionsBuilder } from '@rahino/query-filter/sequelize-query-builder'; | ||
import { Op } from 'sequelize'; | ||
import { ECPayment } from '@rahino/database/models/ecommerce-eav/ec-payment-entity'; | ||
import { ECPaymentGateway } from '@rahino/database/models/ecommerce-eav/ec-payment-gateway.entity'; | ||
import { SnapPayService } from '@rahino/ecommerce/user/payment/provider/services'; | ||
import { ECProduct } from '@rahino/database/models/ecommerce-eav/ec-product.entity'; | ||
import { EAVEntityType } from '@rahino/database/models/eav/eav-entity-type.entity'; | ||
import { RoleUtilService } from '@rahino/core/user/role-util/role-util.service'; | ||
import { UserVendorService } from '@rahino/ecommerce/user/vendor/user-vendor.service'; | ||
import { OrderUtilService } from '../utilOrder/service/order-util.service'; | ||
import { FinalizedPaymentService } from '@rahino/ecommerce/user/payment/util/finalized-payment/finalized-payment.service'; | ||
import { ChangeOrderStatusDto, ChangeShipmentWayDto } from './dto'; | ||
import { ECOrderStatus } from '@rahino/database/models/ecommerce-eav/ec-order-status.entity'; | ||
import { ECOrderShipmentWay } from '@rahino/database/models/ecommerce-eav/ec-order-shipmentway.entity'; | ||
import { GetTotalOrderFilterDto } from './dto/get-total-order.dto'; | ||
|
||
@Injectable() | ||
export class CancellOrderService { | ||
constructor( | ||
@InjectModel(ECOrder) | ||
private readonly repository: typeof ECOrder, | ||
@InjectModel(ECOrderDetail) | ||
private readonly orderDetailRepository: typeof ECOrderDetail, | ||
@InjectConnection() | ||
private readonly sequelize: Sequelize, | ||
@InjectModel(ECPayment) | ||
private readonly paymentRepository: typeof ECPayment, | ||
@InjectModel(ECPaymentGateway) | ||
private readonly paymentGatewayRepository: typeof ECPaymentGateway, | ||
@InjectModel(ECOrderStatus) | ||
private readonly orderStatusRepository: typeof ECOrderStatus, | ||
@InjectModel(ECOrderShipmentWay) | ||
private readonly orderShipmentWayRepository: typeof ECOrderShipmentWay, | ||
|
||
private orderQueryBuilder: OrderQueryBuilder, | ||
private readonly snapPayService: SnapPayService, | ||
private readonly roleUtilService: RoleUtilService, | ||
private readonly userVendorService: UserVendorService, | ||
private readonly orderUtilService: OrderUtilService, | ||
private readonly finalizedPaymentService: FinalizedPaymentService, | ||
) {} | ||
|
||
async findAll(user: User, filter: GetTotalOrderFilterDto) { | ||
const isSuperAdmin = await this.roleUtilService.isSuperAdmin(user); | ||
const vendorIds = await this.userVendorService.findVendorIds(user); | ||
|
||
let builder = this.orderQueryBuilder | ||
.deletedOrder() | ||
.search(filter.search) | ||
.includeUser() | ||
.addNegativeOrderStatus(OrderStatusEnum.WaitingForPayment); | ||
|
||
if (!isSuperAdmin) { | ||
builder = builder.addOnlyVendor(vendorIds); | ||
} | ||
|
||
if (filter.phoneNumber) { | ||
builder = builder.filterPhoneNumber(filter.phoneNumber); | ||
} | ||
|
||
if (filter.orderId) { | ||
builder = builder.filterOrderId(filter.orderId); | ||
} | ||
|
||
const count = await this.repository.count(builder.build()); | ||
|
||
if (isSuperAdmin) { | ||
builder = builder.includeAdminOrderDetails(); | ||
} else { | ||
builder = builder.includeAdminOrderDetails(vendorIds); | ||
} | ||
|
||
builder = builder | ||
.subQuery(true) | ||
.includeOrderShipmentWay() | ||
.includeAddress() | ||
.includeOrderStatus() | ||
.offset(filter.offset) | ||
.limit(filter.limit) | ||
.order({ orderBy: filter.orderBy, sortOrder: filter.sortOrder }); | ||
|
||
let result = await this.repository.findAll(builder.build()); | ||
result = await this.orderUtilService.recalculateOrdersPrices(result); | ||
|
||
return { | ||
result: result, | ||
total: count, | ||
}; | ||
} | ||
|
||
async findById(id: bigint, user: User) { | ||
const isSuperAdmin = await this.roleUtilService.isSuperAdmin(user); | ||
const vendorIds = await this.userVendorService.findVendorIds(user); | ||
|
||
let builder = this.orderQueryBuilder; | ||
|
||
if (!isSuperAdmin) { | ||
builder = builder | ||
.addOnlyVendor(vendorIds) | ||
.includeAdminOrderDetails(vendorIds); | ||
} else { | ||
builder = builder.includeAdminOrderDetails(); | ||
} | ||
|
||
builder = builder | ||
.deletedOrder() | ||
.includeOrderShipmentWay() | ||
.addOrderId(id) | ||
.includeOrderStatus() | ||
.addNegativeOrderStatus(OrderStatusEnum.WaitingForPayment) | ||
.includeAddress() | ||
.includeUser(); | ||
|
||
let result = await this.repository.findOne(builder.build()); | ||
if (!result) { | ||
throw new NotFoundException('the item with this given id not founded!'); | ||
} | ||
result = await this.orderUtilService.recalculateOrderPrices(result); | ||
|
||
return { | ||
result: result, | ||
}; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
apps/e-commerce/src/admin/cancell-order/dto/change-orderstatus.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IsNumber } from 'class-validator'; | ||
|
||
export class ChangeOrderStatusDto { | ||
@IsNumber() | ||
orderStatusId: number; | ||
} |
6 changes: 6 additions & 0 deletions
6
apps/e-commerce/src/admin/cancell-order/dto/change-shipmentway.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IsNumber, IsOptional, IsString } from 'class-validator'; | ||
|
||
export class ChangeShipmentWayDto { | ||
@IsNumber() | ||
shipmentWayId: number; | ||
} |
8 changes: 8 additions & 0 deletions
8
apps/e-commerce/src/admin/cancell-order/dto/get-total-order.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { IntersectionType } from '@nestjs/swagger'; | ||
import { ListFilter } from '@rahino/query-filter'; | ||
import { TotalOrderFilterDto } from './total-order-filter.dto'; | ||
|
||
export class GetTotalOrderFilterDto extends IntersectionType( | ||
ListFilter, | ||
TotalOrderFilterDto, | ||
) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './post-process.dto'; | ||
export * from './change-shipmentway.dto'; | ||
export * from './change-orderstatus.dto'; |
7 changes: 7 additions & 0 deletions
7
apps/e-commerce/src/admin/cancell-order/dto/post-process.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { IsOptional, IsString } from 'class-validator'; | ||
|
||
export class PostProcessDto { | ||
@IsString() | ||
@IsOptional() | ||
postReceipt?: string; | ||
} |
21 changes: 21 additions & 0 deletions
21
apps/e-commerce/src/admin/cancell-order/dto/total-order-filter.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Transform, Type } from 'class-transformer'; | ||
import { IsInt, IsNumber, IsOptional, IsString } from 'class-validator'; | ||
|
||
export class TotalOrderFilterDto { | ||
@IsOptional() | ||
@ApiProperty({ required: false, type: IsString, default: '' }) | ||
@Transform(({ value }) => '%' + value + '%') | ||
public phoneNumber?: string = '%%'; | ||
|
||
@IsInt() | ||
@Type(() => Number) | ||
@IsOptional() | ||
@ApiProperty({ | ||
required: false, | ||
default: false, | ||
type: IsNumber, | ||
description: 'orderId', | ||
}) | ||
orderId?: bigint; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters