Skip to content

Commit

Permalink
[M1_TR-210] Refactored job service and job dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonclchua committed Sep 6, 2023
1 parent 8e4b1c9 commit 01a01e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 93 deletions.
2 changes: 0 additions & 2 deletions server/src/api/job/dtos/create-job-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class CreateJobDto {
type: string;

@ApiProperty()
@IsOptional()
@IsString()
@IsIn(Object.values(Tag))
tags: $Enums.Tag;
Expand All @@ -28,7 +27,6 @@ export class CreateJobDto {
remarks: string;

@ApiProperty()
@IsOptional()
@IsString()
@IsIn(Object.values(PaymentMethod))
paymentMethod: $Enums.PaymentMethod;
Expand Down
Original file line number Diff line number Diff line change
@@ -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[]
}
47 changes: 13 additions & 34 deletions server/src/api/job/job.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Prisma } from '@prisma/client';
import { Customer, Job } from '@prisma/client';
import { UsePipes, ValidationPipe, BadRequestException, Injectable } from '@nestjs/common';

Expand All @@ -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()
Expand All @@ -20,46 +20,25 @@ export class JobService extends AbstractService {
super(prisma, "Job")
}

async createJobWithCustomerAndSchedules(options: CreateJobWithCustomerAndSchedulesDto): Promise<Job> {
async createJobWithCustomerAndSchedules(createJobWithCustomerAndSchedulesOptions: CreateJobWithCustomerAndSchedulesDto): Promise<Job> {
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
})
Expand Down Expand Up @@ -97,7 +76,7 @@ export class JobService extends AbstractService {
return !!jobAlreadyExists
}

async createCustomer(options: Prisma.CustomerCreateInput): Promise<Customer> {
async createCustomer(options: CreateCustomerDto): Promise<Customer> {
const { email } = options
const existingCustomer = await this.checkIfCustomerAlreadyExists(email)

Expand Down

0 comments on commit 01a01e0

Please sign in to comment.