Skip to content

Commit

Permalink
Merge pull request #21 from FxFurEN/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
fxfuren authored Nov 1, 2024
2 parents f5c846f + 596a176 commit 045acdf
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"seed": "ts-node prisma/seed.ts",
"seed": "ts-node src/prisma/seed.ts",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
Expand Down
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ model Order {
technicianId String
status OrderStatus @default(COMPLETED)
createdAt DateTime @default(now()) @map("created_at")
completedAt DateTime @map("completed_at")
createdAt DateTime @default(now()) @map("created_at")
completedAt DateTime? @map("completed_at")
@@map("completed_orders")
}
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PrismaModule } from './prisma/prisma.module';
import { UserModule } from './user/user.module';
import { ItemsModule } from './warehouses/items/items.module';
import { WarehousesModule } from './warehouses/warehouses.module';
import { OrderModule } from './order/order.module';
@Module({
imports: [
ConfigModule.forRoot({
Expand All @@ -23,6 +24,7 @@ import { WarehousesModule } from './warehouses/warehouses.module';
PasswordRecoveryModule,
WarehousesModule,
ItemsModule,
OrderModule,
],
})
export class AppModule {}
20 changes: 20 additions & 0 deletions src/order/dto/create-order.dto.ts
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;
}
24 changes: 24 additions & 0 deletions src/order/dto/update-order.dto.ts
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;
}
58 changes: 58 additions & 0 deletions src/order/order.controller.ts
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);
}
}
11 changes: 11 additions & 0 deletions src/order/order.module.ts
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 {}
69 changes: 69 additions & 0 deletions src/order/order.service.ts
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 },
});
}
}
4 changes: 2 additions & 2 deletions prisma/seed.ts → src/prisma/seed.ts
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();

Expand All @@ -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,
Expand Down

0 comments on commit 045acdf

Please sign in to comment.