-
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.
- Loading branch information
1 parent
942f478
commit b1d1fca
Showing
41 changed files
with
1,787 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { BadRequestException, NotFoundException } from '@nestjs/common'; | ||
import { PrismaService } from '../../database/connection.service'; | ||
|
||
import { CustomerController } from './customer.controller'; | ||
import { CustomerService } from './customer.service'; | ||
|
||
describe('CustomerController', () => { | ||
let customerController: CustomerController; | ||
let customerService: CustomerService; | ||
let prisma: PrismaService | ||
|
||
beforeEach(() => { | ||
customerService = new CustomerService(prisma); | ||
customerController = new CustomerController(customerService); | ||
}); | ||
|
||
describe('create', () => { | ||
it('should return a customer', async () => { | ||
const newCustomer = { | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "johndoe@gmail.com", | ||
contact: "0123456", | ||
address: "ABC St, DEF Ave, GHI City" | ||
} | ||
|
||
jest.spyOn(customerService, 'create').mockResolvedValue(newCustomer) | ||
jest.spyOn(customerController, 'checkIfCustomerAlreadyExists').mockResolvedValue(false) | ||
|
||
const createdCustomer = await customerController.create(newCustomer); | ||
|
||
expect(createdCustomer).toEqual(newCustomer); | ||
}) | ||
|
||
it('should return BadRequestException if input is invalid', async () => { | ||
const invalidInput = { | ||
firstName: "", | ||
lastName: "", | ||
email: "invalidEmail", | ||
contact: "012345", | ||
address: "ABC City" | ||
} | ||
|
||
jest.spyOn(customerController, 'checkIfCustomerAlreadyExists').mockResolvedValue(false) | ||
jest.spyOn(customerService, 'create').mockRejectedValue(new BadRequestException('error')) | ||
|
||
const createCustomerPromise = async () => { | ||
await customerController.create(invalidInput); | ||
} | ||
|
||
expect(createCustomerPromise).rejects.toThrow(BadRequestException); | ||
}) | ||
}) | ||
|
||
describe('findOne', () => { | ||
it('should return a single customer', async () => { | ||
const customer = { | ||
id: 1, | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "johndoe@gmail.com", | ||
contact: "0123456", | ||
address: "ABC St, DEF Ave, GHI City" | ||
} | ||
|
||
jest.spyOn(customerService, 'findOne').mockResolvedValue(customer); | ||
|
||
const foundCustomer = await customerController.findOne(1); | ||
|
||
expect(foundCustomer).toEqual(customer); | ||
}) | ||
|
||
it('should throw a not found exception when no customer is found', async () => { | ||
jest.spyOn(customerService, 'findOne').mockRejectedValue(new NotFoundException('No customer found.')); | ||
|
||
const invalidId = 99; | ||
|
||
const findOnePromise = customerController.findOne(invalidId); | ||
|
||
await expect(findOnePromise).rejects.toThrow('No customer found.'); | ||
}) | ||
}) | ||
|
||
describe('findAll', () => { | ||
it('should return an array of customers', async () => { | ||
const customers = [{ | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "johndoe@gmail.com", | ||
contact: "0123456", | ||
address: "ABC St, DEF Ave, GHI City" | ||
}, { | ||
firstName: "Jane", | ||
lastName: "Doe", | ||
email: "janedoe@gmail.com", | ||
contact: "4567890", | ||
address: "DEF St, GHI Ave, ABC City" | ||
}]; | ||
|
||
jest.spyOn(customerService, 'findAll').mockResolvedValue(customers) | ||
|
||
const foundCustomers = await customerController.findAll(); | ||
|
||
expect(foundCustomers).toEqual(customers); | ||
}) | ||
|
||
it('should throw a not found exception when no customer is found', async () => { | ||
jest.spyOn(customerService, 'findAll').mockRejectedValue(new NotFoundException('No customer found.')); | ||
|
||
const findAllPromise = customerController.findAll(); | ||
|
||
await expect(findAllPromise).rejects.toThrow('No customer found.'); | ||
}) | ||
}) | ||
}); |
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,61 @@ | ||
import { Customer } from '@prisma/client'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { BadRequestException, Body, Controller, Get, NotFoundException, Param, ParseIntPipe, Post, UsePipes, ValidationPipe } from '@nestjs/common'; | ||
|
||
import { CustomerService } from './customer.service'; | ||
|
||
import { CreateCustomerDto } from './dtos/create-customer.dto'; | ||
|
||
@ApiTags('customer') | ||
@Controller('customer') | ||
export class CustomerController { | ||
constructor( | ||
private readonly customerService: CustomerService | ||
) { } | ||
|
||
@UsePipes(new ValidationPipe()) | ||
@Post() | ||
async create(@Body() options: CreateCustomerDto): Promise<Customer> { | ||
const { email } = options | ||
|
||
const existingCustomer = await this.checkIfCustomerAlreadyExists(email) | ||
|
||
if (existingCustomer) { | ||
return existingCustomer | ||
} | ||
|
||
const newCustomer = await this.customerService.create(options) | ||
|
||
if (!newCustomer) { | ||
throw new BadRequestException('Something went wrong. Customer not created.') | ||
} | ||
|
||
return newCustomer | ||
} | ||
|
||
async checkIfCustomerAlreadyExists(email: string) { | ||
return await this.customerService.findOne({ email }) | ||
} | ||
|
||
@Get('/:id') | ||
async findOne(@Param('id', ParseIntPipe) id: number): Promise<Customer> { | ||
const customer = await this.customerService.findOne({ id }) | ||
|
||
if (!customer) { | ||
throw new NotFoundException("No customer found.") | ||
} | ||
|
||
return customer | ||
} | ||
|
||
@Get() | ||
async findAll(): Promise<Customer[]> { | ||
const customers = await this.customerService.findAll() | ||
|
||
if (customers.length == 0) { | ||
throw new NotFoundException('No customer found.') | ||
} | ||
|
||
return customers | ||
} | ||
} |
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,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { PrismaService } from '../../database/connection.service'; | ||
|
||
import { CustomerService } from './customer.service'; | ||
import { CustomerController } from './customer.controller'; | ||
|
||
@Module({ | ||
providers: [CustomerService, PrismaService], | ||
controllers: [CustomerController], | ||
exports: [] | ||
}) | ||
export class CustomerModule {} |
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,73 @@ | ||
import { PrismaService } from '../../database/connection.service'; | ||
|
||
import { CustomerService } from './customer.service'; | ||
|
||
describe('CustomerService', () => { | ||
let customerService: CustomerService; | ||
let prisma: PrismaService | ||
|
||
beforeEach(async () => { | ||
customerService = new CustomerService(prisma); | ||
}); | ||
|
||
describe('create', () => { | ||
it('should return a customer', async () => { | ||
const newCustomer = { | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "johndoe@gmail.com", | ||
contact: "0123456", | ||
address: "ABC St, DEF Ave, GHI City" | ||
} | ||
|
||
jest.spyOn(customerService, 'create').mockResolvedValue(newCustomer) | ||
|
||
const createdCustomer = await customerService.create(newCustomer); | ||
|
||
expect(createdCustomer).toEqual(newCustomer); | ||
}) | ||
}) | ||
|
||
describe('findOne', () => { | ||
it('should return a single customer', async () => { | ||
const customer = { | ||
id: 1, | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "johndoe@gmail.com", | ||
contact: "0123456", | ||
address: "ABC St, DEF Ave, GHI City" | ||
} | ||
|
||
jest.spyOn(customerService, 'findOne').mockResolvedValue(customer); | ||
|
||
const foundCustomer = await customerService.findOne(1); | ||
|
||
expect(foundCustomer).toEqual(customer); | ||
}) | ||
}) | ||
|
||
describe('findAll', () => { | ||
it('should return an array of customers', async () => { | ||
const customers = [{ | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "johndoe@gmail.com", | ||
contact: "0123456", | ||
address: "ABC St, DEF Ave, GHI City" | ||
}, { | ||
firstName: "Jane", | ||
lastName: "Doe", | ||
email: "janedoe@gmail.com", | ||
contact: "4567890", | ||
address: "DEF St, GHI Ave, ABC City" | ||
}]; | ||
|
||
jest.spyOn(customerService, 'findAll').mockResolvedValue(customers) | ||
|
||
const foundCustomers = await customerService.findAll(); | ||
|
||
expect(foundCustomers).toEqual(customers); | ||
}) | ||
}) | ||
}); |
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,12 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { PrismaService } from '../../database/connection.service'; | ||
|
||
import { AbstractService } from '../../shared/abstract-service'; | ||
|
||
@Injectable() | ||
export class CustomerService extends AbstractService { | ||
constructor(prisma: PrismaService) { | ||
super(prisma, "Customer") | ||
} | ||
} |
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,32 @@ | ||
import { IsEmail, IsOptional, IsString } from 'class-validator'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class CreateCustomerDto { | ||
@ApiProperty() | ||
@IsString() | ||
firstName: string; | ||
|
||
@ApiProperty() | ||
@IsString() | ||
lastName: string; | ||
|
||
@ApiProperty() | ||
@IsEmail() | ||
email: string; | ||
|
||
@ApiProperty() | ||
@IsString() | ||
contact: string; | ||
|
||
@ApiProperty() | ||
@IsString() | ||
address: string; | ||
|
||
@ApiProperty() | ||
@IsOptional() | ||
createdAt?: string; | ||
|
||
@ApiProperty() | ||
@IsOptional() | ||
updatedAt?: string; | ||
} |
Oops, something went wrong.