From 01a01e069dc44631f661f22ab081b794587d109b Mon Sep 17 00:00:00 2001 From: Jason Chua Date: Wed, 6 Sep 2023 16:38:17 +0800 Subject: [PATCH] [M1_TR-210] Refactored job service and job dtos --- server/src/api/job/dtos/create-job-dto.ts | 2 - ...eate-job-with-customer-and-schedule.dto.ts | 82 ++++++------------- server/src/api/job/job.service.ts | 47 +++-------- 3 files changed, 38 insertions(+), 93 deletions(-) diff --git a/server/src/api/job/dtos/create-job-dto.ts b/server/src/api/job/dtos/create-job-dto.ts index 3231a2d..988bffd 100644 --- a/server/src/api/job/dtos/create-job-dto.ts +++ b/server/src/api/job/dtos/create-job-dto.ts @@ -17,7 +17,6 @@ export class CreateJobDto { type: string; @ApiProperty() - @IsOptional() @IsString() @IsIn(Object.values(Tag)) tags: $Enums.Tag; @@ -28,7 +27,6 @@ export class CreateJobDto { remarks: string; @ApiProperty() - @IsOptional() @IsString() @IsIn(Object.values(PaymentMethod)) paymentMethod: $Enums.PaymentMethod; diff --git a/server/src/api/job/dtos/create-job-with-customer-and-schedule.dto.ts b/server/src/api/job/dtos/create-job-with-customer-and-schedule.dto.ts index 4442e56..c6211a2 100644 --- a/server/src/api/job/dtos/create-job-with-customer-and-schedule.dto.ts +++ b/server/src/api/job/dtos/create-job-with-customer-and-schedule.dto.ts @@ -1,75 +1,43 @@ -import { $Enums, PaymentMethod, Tag } from "@prisma/client"; import { ApiProperty } from "@nestjs/swagger"; -import { - IsArray, - IsEmail, - IsIn, - IsNumber, - IsOptional, - IsString, -} from "class-validator"; import { CreateScheduleDto } from "../../schedule/dtos/create-schedule.dto"; +import { CreateCustomerDto } from "../../customer/dtos/create-customer.dto"; +import { CreateJobDto } from "./create-job-dto"; +import { Type } from "class-transformer"; +import { IsNumber, IsOptional, ValidateNested } from "class-validator"; -export class CreateJobWithCustomerAndSchedulesDto { - @ApiProperty() - @IsString() - firstName: string; - - @ApiProperty() - @IsString() - lastName: string; - - @ApiProperty() - @IsEmail() - email: string; - - @ApiProperty() - @IsString() - contact: string; - - @ApiProperty() - @IsString() - address: string; - - @ApiProperty() - @IsArray() - schedules: CreateScheduleDto[]; - - @ApiProperty() - @IsString() - title: string; - +export class CreateJobWithoutCustomerIdAndUserIdDto extends CreateJobDto { @ApiProperty() - @IsString() - type: string; + @IsNumber() + @IsOptional() + userId: number; @ApiProperty() @IsOptional() - @IsString() - @IsIn(Object.values(Tag)) - tags: $Enums.Tag; + @IsNumber() + customerId: number; +} +export class createScheduleWithoutJobIdDto extends CreateScheduleDto { @ApiProperty() @IsOptional() - @IsString() - remarks: string; + @IsNumber() + jobId: number; +} +export class CreateJobWithCustomerAndSchedulesDto { @ApiProperty() - @IsOptional() - @IsString() - @IsIn(Object.values(PaymentMethod)) - paymentMethod: $Enums.PaymentMethod; + @Type(() => CreateCustomerDto) + @ValidateNested() + customer_registration: CreateCustomerDto @ApiProperty() - @IsNumber() - userId: number; + @Type(() => CreateJobWithoutCustomerIdAndUserIdDto) + @ValidateNested() + job_information: CreateJobWithoutCustomerIdAndUserIdDto @ApiProperty() - @IsOptional() - createdAt?: string; - - @ApiProperty() - @IsOptional() - updatedAt?: string; + @Type(() => createScheduleWithoutJobIdDto) + @ValidateNested() + work_schedules: createScheduleWithoutJobIdDto[] } diff --git a/server/src/api/job/job.service.ts b/server/src/api/job/job.service.ts index 5d1dd29..2301962 100644 --- a/server/src/api/job/job.service.ts +++ b/server/src/api/job/job.service.ts @@ -1,4 +1,3 @@ -import { Prisma } from '@prisma/client'; import { Customer, Job } from '@prisma/client'; import { UsePipes, ValidationPipe, BadRequestException, Injectable } from '@nestjs/common'; @@ -8,6 +7,7 @@ import { AbstractService } from '../../shared/abstract-service'; import { CustomerService } from '../customer/customer.service'; import { CreateJobDto } from './dtos/create-job-dto'; import { CreateScheduleDto } from '../schedule/dtos/create-schedule.dto'; +import { CreateCustomerDto } from '../customer/dtos/create-customer.dto'; import { CreateJobWithCustomerAndSchedulesDto } from './dtos/create-job-with-customer-and-schedule.dto'; @Injectable() @@ -20,46 +20,25 @@ export class JobService extends AbstractService { super(prisma, "Job") } - async createJobWithCustomerAndSchedules(options: CreateJobWithCustomerAndSchedulesDto): Promise { + async createJobWithCustomerAndSchedules(createJobWithCustomerAndSchedulesOptions: CreateJobWithCustomerAndSchedulesDto): Promise { + const { + customer_registration, + job_information, + work_schedules + } = createJobWithCustomerAndSchedulesOptions; + const transaction = await this.prisma.$transaction(async () => { - const { - firstName, - lastName, - email, - contact, - address, - schedules, - title, - type, - tags, - remarks, - paymentMethod, - } = options; // Step 1: Create the customer - const customerData = { - firstName, - lastName, - email, - contact, - address - } - const customer = await this.createCustomer(customerData) + const customerInput = { ...customer_registration } + const customer = await this.createCustomer(customerInput) // Step 2: Create the job associated with the customer - const jobInput = { - title, - type, - tags, - remarks, - customerId: customer.id, - paymentMethod, - userId: 1 // TODO: update to authId - } + const jobInput = { ...job_information, customerId: customer.id, userId: 1 } const job = await this.createJob(jobInput) // Step 3: Create schedules for the job - await this.createSchedules(schedules, job.id) + await this.createSchedules(work_schedules, job.id) return job }) @@ -97,7 +76,7 @@ export class JobService extends AbstractService { return !!jobAlreadyExists } - async createCustomer(options: Prisma.CustomerCreateInput): Promise { + async createCustomer(options: CreateCustomerDto): Promise { const { email } = options const existingCustomer = await this.checkIfCustomerAlreadyExists(email)