-
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.
- Loading branch information
1 parent
58102bc
commit ff8b91e
Showing
12 changed files
with
441 additions
and
2 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
apps/e-commerce/src/admin/courierOrder/courier-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,75 @@ | ||
import { | ||
ApiBearerAuth, | ||
ApiOperation, | ||
ApiQuery, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { CourierOrderService } from './courier-order.service'; | ||
import { | ||
Body, | ||
Controller, | ||
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 { ListFilter } from '@rahino/query-filter'; | ||
import { CourierProcessDto } from './dto'; | ||
|
||
@ApiTags('Courier-Orders') | ||
@UseGuards(JwtGuard, PermissionGuard) | ||
@ApiBearerAuth() | ||
@UseInterceptors(JsonResponseTransformInterceptor) | ||
@Controller({ | ||
path: '/api/ecommerce/admin/courierOrders', | ||
version: ['1'], | ||
}) | ||
export class CourierOrderController { | ||
constructor(private readonly service: CourierOrderService) {} | ||
|
||
@ApiOperation({ description: 'show all courier orders' }) | ||
@CheckPermission({ permissionSymbol: 'ecommerce.admin.courierorders.getall' }) | ||
@Get('/') | ||
@ApiQuery({ | ||
name: 'filter', | ||
type: ListFilter, | ||
style: 'deepObject', | ||
explode: true, | ||
}) | ||
@HttpCode(HttpStatus.OK) | ||
async findAll(@GetUser() user: User, @Query() filter: ListFilter) { | ||
return await this.service.findAll(user, filter); | ||
} | ||
|
||
@ApiOperation({ description: 'show courier orders by given id' }) | ||
@CheckPermission({ permissionSymbol: 'ecommerce.admin.courierorders.getone' }) | ||
@Get('/:id') | ||
@HttpCode(HttpStatus.OK) | ||
async findById(@Param('id') entityId: bigint, @GetUser() user: User) { | ||
return await this.service.findById(entityId, user); | ||
} | ||
|
||
@ApiOperation({ description: 'change order to courier' }) | ||
@CheckPermission({ | ||
permissionSymbol: 'ecommerce.admin.courierorders.processcourier', | ||
}) | ||
@Patch('/processCourier/:id') | ||
@HttpCode(HttpStatus.OK) | ||
async processPost( | ||
@Param('id') orderId: bigint, | ||
@GetUser() user: User, | ||
@Body() dto: CourierProcessDto, | ||
) { | ||
return await this.service.processCourier(orderId, user, dto); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
apps/e-commerce/src/admin/courierOrder/courier-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,19 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CourierOrderController } from './courier-order.controller'; | ||
import { CourierOrderService } from './courier-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 { ECCourier } from '@rahino/database/models/ecommerce-eav/ec-courier.entity'; | ||
|
||
@Module({ | ||
imports: [ | ||
SequelizeModule.forFeature([User, Permission, ECOrder, ECCourier]), | ||
UtilOrderModule, | ||
], | ||
controllers: [CourierOrderController], | ||
providers: [CourierOrderService], | ||
}) | ||
export class CourierOrderModule {} |
142 changes: 142 additions & 0 deletions
142
apps/e-commerce/src/admin/courierOrder/courier-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,142 @@ | ||
import { | ||
BadRequestException, | ||
Injectable, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { 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 { | ||
OrderShipmentwayEnum, | ||
OrderStatusEnum, | ||
} from '@rahino/ecommerce/util/enum'; | ||
import { OrderQueryBuilder } from '../utilOrder/service/order-query-builder.service'; | ||
import { ListFilter } from '@rahino/query-filter'; | ||
import { CourierProcessDto } from './dto'; | ||
import { QueryOptionsBuilder } from '@rahino/query-filter/sequelize-query-builder'; | ||
import { Sequelize } from 'sequelize'; | ||
import { Op } from 'sequelize'; | ||
import { OrderUtilService } from '../utilOrder/service/order-util.service'; | ||
import { I18nContext, I18nService } from 'nestjs-i18n'; | ||
import { I18nTranslations } from 'apps/main/src/generated/i18n.generated'; | ||
import { ECCourier } from '@rahino/database/models/ecommerce-eav/ec-courier.entity'; | ||
|
||
@Injectable() | ||
export class CourierOrderService { | ||
constructor( | ||
@InjectModel(ECOrder) | ||
private readonly repository: typeof ECOrder, | ||
@InjectModel(ECCourier) | ||
private readonly courierRepository: typeof ECCourier, | ||
private orderQueryBuilder: OrderQueryBuilder, | ||
private orderUtilService: OrderUtilService, | ||
private readonly i18n: I18nService<I18nTranslations>, | ||
) {} | ||
|
||
async findAll(user: User, filter: ListFilter) { | ||
let builder = this.orderQueryBuilder; | ||
builder = builder | ||
.nonDeletedOrder() | ||
.orderShipmentWay(OrderShipmentwayEnum.delivery) | ||
.addOrderStatus(OrderStatusEnum.OrderHasBeenProcessed) | ||
.search(filter.search); | ||
|
||
const count = await this.repository.count(builder.build()); | ||
|
||
builder = builder | ||
.subQuery(true) | ||
.addOrderShipmentWay() | ||
.addAdminOrderDetails() | ||
.addAddress() | ||
.addUser() | ||
.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) { | ||
let builder = this.orderQueryBuilder; | ||
|
||
builder = builder | ||
.nonDeletedOrder() | ||
.addOrderShipmentWay() | ||
.orderShipmentWay(OrderShipmentwayEnum.delivery) | ||
.addOrderId(id) | ||
.addOrderStatus(OrderStatusEnum.OrderHasBeenProcessed) | ||
.addAdminOrderDetails() | ||
.addAddress() | ||
.addUser(); | ||
|
||
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, | ||
}; | ||
} | ||
|
||
async processCourier(orderId: bigint, user: User, dto: CourierProcessDto) { | ||
let item = await this.repository.findOne( | ||
new QueryOptionsBuilder() | ||
.filter({ id: orderId }) | ||
.filter({ orderStatusId: OrderStatusEnum.OrderHasBeenProcessed }) | ||
.filter({ orderShipmentWayId: OrderShipmentwayEnum.delivery }) | ||
.filter( | ||
Sequelize.where( | ||
Sequelize.fn('isnull', Sequelize.col('ECOrder.isDeleted'), 0), | ||
{ | ||
[Op.eq]: 0, | ||
}, | ||
), | ||
) | ||
.build(), | ||
); | ||
if (!item) { | ||
throw new NotFoundException( | ||
this.i18n.t('core.not_found_id', { | ||
lang: I18nContext.current().lang, | ||
}), | ||
); | ||
} | ||
const courier = await this.courierRepository.findOne( | ||
new QueryOptionsBuilder() | ||
.filter({ userId: dto.userId }) | ||
.filter( | ||
Sequelize.where( | ||
Sequelize.fn('isnull', Sequelize.col('ECCourier.isDeleted'), 0), | ||
{ | ||
[Op.eq]: 0, | ||
}, | ||
), | ||
) | ||
.build(), | ||
); | ||
if (courier) { | ||
throw new NotFoundException( | ||
this.i18n.t('ecommerce.courier_not_found', { | ||
lang: I18nContext.current().lang, | ||
}), | ||
); | ||
} | ||
|
||
item.orderStatusId = OrderStatusEnum.SendByCourier; | ||
item.deliveryDate = new Date(); | ||
item.courierUserId = dto.userId; | ||
|
||
item = await item.save(); | ||
return { | ||
result: item, | ||
}; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
apps/e-commerce/src/admin/courierOrder/dto/courier-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 { IsNumber, IsOptional } from 'class-validator'; | ||
|
||
export class CourierProcessDto { | ||
@IsNumber() | ||
@IsOptional() | ||
userId?: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './courier-process.dto'; |
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"user_exists_before": "the user you enter exists before" | ||
"user_exists_before": "the user you enter exists before", | ||
"courier_not_found": "the courier is choosen is not exists!" | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"user_exists_before": "کاربر مورد نظر از قبل موجود میباشد" | ||
"user_exists_before": "کاربر مورد نظر از قبل موجود میباشد", | ||
"courier_not_found": "پیک یافت نشد" | ||
} |
Oops, something went wrong.