-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from FxFurEN/dev
Dev
- Loading branch information
Showing
9 changed files
with
189 additions
and
5 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { IsEnum, IsOptional, IsString } from 'class-validator'; | ||
import { OrderStatus } from 'prisma/__generated__'; | ||
|
||
export class CreateOrderDto { | ||
@IsString() | ||
customer: string; | ||
|
||
@IsString() | ||
device: string; | ||
|
||
@IsString() | ||
issue: string; | ||
|
||
@IsString() | ||
technicianId: string; | ||
|
||
@IsOptional() | ||
@IsEnum(OrderStatus) | ||
status?: OrderStatus; | ||
} |
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,24 @@ | ||
import { IsDate, IsEnum, IsOptional, IsString } from 'class-validator'; | ||
import { OrderStatus } from 'prisma/__generated__'; | ||
|
||
export class UpdateOrderDto { | ||
@IsOptional() | ||
@IsString() | ||
customer?: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
device?: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
issue?: string; | ||
|
||
@IsOptional() | ||
@IsEnum(OrderStatus) | ||
status?: OrderStatus; | ||
|
||
@IsOptional() | ||
@IsDate() | ||
completedAt?: Date; | ||
} |
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,58 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpCode, | ||
HttpStatus, | ||
Param, | ||
Patch, | ||
Post, | ||
} from '@nestjs/common'; | ||
import { Authorization } from 'src/auth/decorators/auth.decorator'; | ||
import { CreateOrderDto } from './dto/create-order.dto'; | ||
import { UpdateOrderDto } from './dto/update-order.dto'; | ||
import { OrdersService } from './order.service'; | ||
|
||
@Controller('orders') | ||
export class OrdersController { | ||
constructor(private readonly ordersService: OrdersService) {} | ||
|
||
@Authorization() | ||
@HttpCode(HttpStatus.OK) | ||
@Get() | ||
public async getAllOrders() { | ||
return this.ordersService.getAllOrders(); | ||
} | ||
|
||
@Authorization() | ||
@HttpCode(HttpStatus.OK) | ||
@Get(':id') | ||
public async getOrderById(@Param('id') id: string) { | ||
return this.ordersService.getOrderById(id); | ||
} | ||
|
||
@Authorization() | ||
@HttpCode(HttpStatus.OK) | ||
@Post('new') | ||
public async addOrder(@Body() createOrderDto: CreateOrderDto) { | ||
return this.ordersService.addOrder(createOrderDto); | ||
} | ||
|
||
@Authorization() | ||
@HttpCode(HttpStatus.OK) | ||
@Patch(':id') | ||
public async updateOrder( | ||
@Param('id') id: string, | ||
@Body() updateOrderDto: UpdateOrderDto, | ||
) { | ||
return this.ordersService.updateOrder(id, updateOrderDto); | ||
} | ||
|
||
@Authorization() | ||
@HttpCode(HttpStatus.OK) | ||
@Delete(':id') | ||
public async deleteOrder(@Param('id') id: string) { | ||
return this.ordersService.deleteOrder(id); | ||
} | ||
} |
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,11 @@ | ||
import { forwardRef, Module } from '@nestjs/common'; | ||
import { UserModule } from 'src/user/user.module'; | ||
import { OrdersController } from './order.controller'; | ||
import { OrdersService } from './order.service'; | ||
|
||
@Module({ | ||
imports: [forwardRef(() => UserModule)], | ||
controllers: [OrdersController], | ||
providers: [OrdersService], | ||
}) | ||
export class OrderModule {} |
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,69 @@ | ||
import { | ||
BadRequestException, | ||
Injectable, | ||
NotFoundException, | ||
} from '@nestjs/common'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
import { CreateOrderDto } from './dto/create-order.dto'; | ||
import { UpdateOrderDto } from './dto/update-order.dto'; | ||
|
||
@Injectable() | ||
export class OrdersService { | ||
constructor(private readonly prismaService: PrismaService) {} | ||
|
||
public async getAllOrders() { | ||
return await this.prismaService.order.findMany({ | ||
include: { technician: true }, | ||
}); | ||
} | ||
|
||
public async getOrderById(id: string) { | ||
const order = await this.prismaService.order.findUnique({ | ||
where: { id }, | ||
include: { technician: true }, | ||
}); | ||
|
||
if (!order) { | ||
throw new NotFoundException('Order not found'); | ||
} | ||
return order; | ||
} | ||
|
||
public async addOrder(createOrderDto: CreateOrderDto) { | ||
const { customer, device, issue, technicianId, status } = createOrderDto; | ||
|
||
const technicianExists = await this.prismaService.user.findUnique({ | ||
where: { id: technicianId }, | ||
}); | ||
|
||
if (!technicianExists) { | ||
throw new BadRequestException('Technician not found'); | ||
} | ||
|
||
return await this.prismaService.order.create({ | ||
data: { | ||
customer, | ||
device, | ||
issue, | ||
technician: { connect: { id: technicianId } }, | ||
status, | ||
completedAt: null, | ||
}, | ||
}); | ||
} | ||
|
||
public async updateOrder(id: string, updateOrderDto: UpdateOrderDto) { | ||
const order = await this.getOrderById(id); | ||
return await this.prismaService.order.update({ | ||
where: { id: order.id }, | ||
data: updateOrderDto, | ||
}); | ||
} | ||
|
||
public async deleteOrder(id: string) { | ||
const order = await this.getOrderById(id); | ||
return await this.prismaService.order.delete({ | ||
where: { id: order.id }, | ||
}); | ||
} | ||
} |
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,5 +1,5 @@ | ||
import { hash } from 'argon2'; | ||
import { PrismaClient } from './__generated__'; | ||
import { PrismaClient } from '../../prisma/__generated__'; | ||
|
||
const prismaService = new PrismaClient(); | ||
|
||
|
@@ -12,7 +12,7 @@ async function main() { | |
await prismaService.user.create({ | ||
data: { | ||
email: '[email protected]', | ||
password: await hash('admin'), | ||
password: await hash('(Admin123)'), | ||
displayName: 'Admin', | ||
role: 'ADMIN', | ||
isVerified: true, | ||
|