diff --git a/src/modules/form-builder/controller/form-builder.controller.ts b/src/modules/form-builder/controller/form-builder.controller.ts index c5a33d4..207760f 100644 --- a/src/modules/form-builder/controller/form-builder.controller.ts +++ b/src/modules/form-builder/controller/form-builder.controller.ts @@ -8,7 +8,7 @@ import { FieldOptionService } from '../provider/field-option.service'; import { UpdateFieldDto } from '../dto/update-field.dto'; import { UpdateFieldOptionDto } from '../dto/update-field-option.dto'; -@Controller('admin') +@Controller() @ApiBearerAuth() export class FormBuilderController { constructor( @@ -18,36 +18,42 @@ export class FormBuilderController { /*================================ Field ========================== */ + @Get('public/entity/:id/field') + @ApiOperation({ summary: 'find all fields' }) + // @UseGuards(AdminAuthGuard) + findAllSectionFieldsAsUser(@Param('id') entityId: string) { + return this.fieldService.findAll(+entityId); + } - @Post('entity/:id/field') + @Post('admin/entity/:id/field') @ApiOperation({ summary: 'Create field for entity' }) @UseGuards(AdminAuthGuard) createField(@Body() dto: CreateFieldDto, @Param('id') entityId: string) { return this.fieldService.create(+entityId, dto); } - @Get('entity/:id/field') - @ApiOperation({ summary: 'find all secion fields' }) + @Get('admin/entity/:id/field') + @ApiOperation({ summary: 'find all fields' }) @UseGuards(AdminAuthGuard) findAllSectionFields(@Param('id') entityId: string) { return this.fieldService.findAll(+entityId); } - @Get('entity/field/:id') + @Get('admin/entity/field/:id') @ApiOperation({ summary: 'Find one field' }) @UseGuards(AdminAuthGuard) findOneFiled(@Param('id') id: string) { return this.fieldService.findById(+id); } - @Patch('entity/field/:id') + @Patch('admin/entity/field/:id') @ApiOperation({ summary: 'Update field' }) @UseGuards(AdminAuthGuard) updateField(@Param('id') id: string, @Body() dto: UpdateFieldDto) { return this.fieldService.update(+id, dto); } - @Delete('entity/field/:id') + @Delete('admin/entity/field/:id') @ApiOperation({ summary: 'Remove field' }) @UseGuards(AdminAuthGuard) removeField(@Param('id') id: string) { @@ -56,35 +62,35 @@ export class FormBuilderController { /*================================ Field Option ========================== */ - @Post('entity/field/:id/option') + @Post('admin/entity/field/:id/option') @ApiOperation({ summary: 'Create field Options ' }) @UseGuards(AdminAuthGuard) createFieldOption(@Body() dto: CreateFieldOptionDto, @Param('id') id: string) { return this.fieldOptionService.create(+id, dto); } - @Get('entity/field/:id/option') + @Get('admin/entity/field/:id/option') @ApiOperation({ summary: 'find all field Option' }) @UseGuards(AdminAuthGuard) findAllFiledOptions(@Param('id') fieldId: string) { return this.fieldOptionService.findAll(+fieldId); } - @Get('entity/field/option/:id') + @Get('admin/entity/field/option/:id') @ApiOperation({ summary: 'Find one field Option' }) @UseGuards(AdminAuthGuard) findOneFiledOption(@Param('id') id: string) { return this.fieldOptionService.findById(+id); } - @Patch('entity/field/option/:id') + @Patch('admin/entity/field/option/:id') @ApiOperation({ summary: 'Update field Option' }) @UseGuards(AdminAuthGuard) updateFieldOption(@Param('id') id: string, @Body() dto: UpdateFieldOptionDto) { return this.fieldOptionService.update(+id, dto); } - @Delete('entity/field/option/:id') + @Delete('admin/entity/field/option/:id') @ApiOperation({ summary: 'Renopve field Option' }) @UseGuards(AdminAuthGuard) removeFieldOption(@Param('id') id: string) { diff --git a/src/modules/order/controllers/order.controller.ts b/src/modules/order/controllers/order.controller.ts index 6166344..a89c578 100644 --- a/src/modules/order/controllers/order.controller.ts +++ b/src/modules/order/controllers/order.controller.ts @@ -11,7 +11,7 @@ import { import { AdminId } from 'src/common/decorators/admin-id.decorator'; import { AssignDesignerDto } from '../dto/assign-designer.dto'; import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto'; -import { UpdateOrderDtoAsAdmin } from '../dto/update-order.dto'; +import { UpdateOrderAsAdminDto } from '../dto/update-order.dto'; import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; @ApiTags('orders') @@ -107,14 +107,14 @@ export class OrderController { @Patch('admin/orders/:orderId') @UseGuards(AdminAuthGuard) @ApiOperation({ summary: 'Update Order ' }) - updateOrder(@Param('orderId') orderId: string, @Body() dto: UpdateOrderDtoAsAdmin) { + updateOrder(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) { return this.orderService.updateOrderAsAdmin(orderId, dto); } @Patch('admin/orders/:orderId/print-form') @UseGuards(AdminAuthGuard) @ApiOperation({ summary: 'Update Order ' }) - updateOrderForPrint(@Param('orderId') orderId: string, @Body() dto: UpdateOrderDtoAsAdmin) { + updateOrderForPrint(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) { return this.orderService.updateOrderAsAdmin(orderId, dto); } @@ -136,7 +136,7 @@ export class OrderController { @Post('admin/orders/:orderId/invoice') @UseGuards(AdminAuthGuard) @ApiOperation({ summary: 'Create Order invoice ' }) - createInvoice(@Param('orderId') orderId: string, @Body() dto: UpdateOrderDtoAsAdmin) { + createInvoice(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) { return this.orderService.createInvoice(orderId); } diff --git a/src/modules/order/dto/update-order.dto.ts b/src/modules/order/dto/update-order.dto.ts index 2e26095..64e3b04 100644 --- a/src/modules/order/dto/update-order.dto.ts +++ b/src/modules/order/dto/update-order.dto.ts @@ -1,5 +1,5 @@ import { OmitType, PartialType } from '@nestjs/swagger'; import { CreateOrderAsAdminDto } from './create-order.dto'; -export class UpdateOrderDtoAsAdmin extends PartialType(OmitType(CreateOrderAsAdminDto, 'items' as any)) { } +export class UpdateOrderAsAdminDto extends PartialType(OmitType(CreateOrderAsAdminDto, 'items' as any)) { } diff --git a/src/modules/order/providers/order.service.ts b/src/modules/order/providers/order.service.ts index 2ed34f9..2843fa9 100644 --- a/src/modules/order/providers/order.service.ts +++ b/src/modules/order/providers/order.service.ts @@ -31,7 +31,7 @@ import { Admin } from 'src/modules/admin/entities/admin.entity'; import { Product } from 'src/modules/product/entities/product.entity'; import { AdminService } from 'src/modules/admin/providers/admin.service'; import { OrderItem } from '../entities/order-item.entity'; -import { UpdateOrderDtoAsAdmin } from '../dto/update-order.dto'; +import { UpdateOrderAsAdminDto } from '../dto/update-order.dto'; @Injectable() @@ -70,7 +70,7 @@ export class OrderService { } - async updateOrderAsAdmin(orderId: string, dto: UpdateOrderDtoAsAdmin) { + async updateOrderAsAdmin(orderId: string, dto: UpdateOrderAsAdminDto) { const order = await this.findOrderOrFail(orderId) const updateOrder = await this.updateOrder(order, dto) @@ -129,7 +129,7 @@ export class OrderService { async updateOrderItemAsUser(userId: string, orderId: string, itemId: number, dto: UpdateOrderItemDtoAsUser) { - const orderItem = await this.findOrderItemOrFail(orderId,itemId) + const orderItem = await this.findOrderItemOrFail(orderId, itemId) if (orderItem.order.user.id !== userId) { throw new BadRequestException(`This order doesnt belongs to you`) @@ -139,6 +139,10 @@ export class OrderService { throw new BadRequestException(`You can not update when status is ${orderItem.order.status}`) } + if (orderItem.confirmedAt) { + throw new BadRequestException(`You can not update when item is confirmed`) + } + const updated = await this.updateOrderItem(orderItem, dto) return updated @@ -159,32 +163,36 @@ export class OrderService { async removeOrderItemAsAdmin(orderId: string, itemId: number) { - const order = await this.findOrderOrFail(orderId) + const orderItem = await this.findOrderItemOrFail(orderId, itemId) - await this.removeOrderItem(itemId) + await this.removeOrderItem(orderItem) // await this.calculateOrder(undefined, orderId) // await this.em.flush() - return true + return { message: 'success' } } async removeOrderItemAsUser(userId: string, orderId: string, itemId: number) { - const order = await this.findOrderOrFail(orderId) + const orderItem = await this.findOrderItemOrFail(orderId, itemId) - if (order.user.id !== userId) { + if (orderItem.order.user.id !== userId) { throw new BadRequestException(`this order is not belongs to you`) } - if (order.status !== OrderStatusEnum.CREATED) { - throw new BadRequestException(`You can not update when status is ${order.status}`) + if (orderItem.order.status !== OrderStatusEnum.CREATED) { + throw new BadRequestException(`You can not delete when order status is ${orderItem.order.status}`) } - await this.removeOrderItem(itemId) + if (orderItem.confirmedAt) { + throw new BadRequestException(`You can not delete when item is confirmed`) + } - return true + await this.removeOrderItem(orderItem) + + return { message: 'success' } } @@ -480,15 +488,7 @@ export class OrderService { return orderItem } - async removeOrderItem(itemId: number) { - - const orderItem = await this.orderItemRepository.findOne({ - id: itemId - }) - - if (!orderItem) { - throw new BadRequestException(`orderItem not found`) - } + async removeOrderItem(orderItem: OrderItem) { await this.em.removeAndFlush(orderItem)