-
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 #43 from w3bdesign/backend
Backend services and tests
- Loading branch information
Showing
12 changed files
with
299 additions
and
85 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
26 changes: 26 additions & 0 deletions
26
backend/src/database/migrations/1732141970009-ClearServices.ts
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,26 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class ClearServices1732141970009 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
// First, delete all records from employee_services junction table | ||
await queryRunner.query(`DELETE FROM "employee_services"`); | ||
|
||
// Then, delete all records from services table | ||
await queryRunner.query(`DELETE FROM "services"`); | ||
|
||
// Insert new Norwegian services | ||
await queryRunner.query(` | ||
INSERT INTO "services" ("name", "description", "duration", "price", "isActive") | ||
VALUES | ||
('Standard Klipp', 'En standard og effektiv hårklipp for deg som har det travelt. Perfekt for å vedlikeholde din nåværende stil.', 20, 299.00, true), | ||
('Styling Klipp', 'Komplett hårklipp og styling-service. Inkluderer konsultasjon for å finne det perfekte utseendet.', 30, 399.00, true), | ||
('Skjegg Trim', 'Profesjonell skjeggtrimming og forming for å holde skjegget ditt velstelt.', 15, 199.00, true), | ||
('Full Service', 'Komplett pakke som inkluderer hårklipp, skjeggtrim og styling. Vår premium-tjeneste.', 45, 549.00, true) | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
// Delete the Norwegian services | ||
await queryRunner.query(`DELETE FROM "services" WHERE "name" IN ('Standard Klipp', 'Styling Klipp', 'Skjegg Trim', 'Full Service')`); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
backend/src/database/migrations/1732142680425-UpdateServices.ts
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,26 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class UpdateServices1732142680425 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
// First, delete all records from employee_services junction table | ||
await queryRunner.query(`DELETE FROM "employee_services"`); | ||
|
||
// Then, delete all records from services table | ||
await queryRunner.query(`DELETE FROM "services"`); | ||
|
||
// Insert new Norwegian services | ||
await queryRunner.query(` | ||
INSERT INTO "services" ("name", "description", "duration", "price", "isActive") | ||
VALUES | ||
('Standard Klipp', 'En standard og effektiv hårklipp for deg som har det travelt. Perfekt for å vedlikeholde din nåværende stil.', 20, 299.00, true), | ||
('Styling Klipp', 'Komplett hårklipp og styling-service. Inkluderer konsultasjon for å finne det perfekte utseendet.', 30, 399.00, true), | ||
('Skjegg Trim', 'Profesjonell skjeggtrimming og forming for å holde skjegget ditt velstelt.', 15, 199.00, true), | ||
('Full Service', 'Komplett pakke som inkluderer hårklipp, skjeggtrim og styling. Vår premium-tjeneste.', 45, 549.00, true) | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
// Delete the Norwegian services | ||
await queryRunner.query(`DELETE FROM "services" WHERE "name" IN ('Standard Klipp', 'Styling Klipp', 'Skjegg Trim', 'Full Service')`); | ||
} | ||
} |
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,46 @@ | ||
import { DataSource } from 'typeorm'; | ||
import { Seeder } from './seeder.interface'; | ||
import { Service } from '../../services/entities/service.entity'; | ||
|
||
export class CreateServicesSeed implements Seeder { | ||
async run(dataSource: DataSource): Promise<void> { | ||
const serviceRepository = dataSource.getRepository(Service); | ||
|
||
const services = [ | ||
{ | ||
name: 'Standard Klipp', | ||
description: 'En standard og effektiv hårklipp for deg som har det travelt. Perfekt for å vedlikeholde din nåværende stil.', | ||
duration: 20, | ||
price: 299, | ||
}, | ||
{ | ||
name: 'Styling Klipp', | ||
description: 'Komplett hårklipp og styling-service. Inkluderer konsultasjon for å finne det perfekte utseendet.', | ||
duration: 30, | ||
price: 399, | ||
}, | ||
{ | ||
name: 'Skjegg Trim', | ||
description: 'Profesjonell skjeggtrimming og forming for å holde skjegget ditt velstelt.', | ||
duration: 15, | ||
price: 199, | ||
}, | ||
{ | ||
name: 'Full Service', | ||
description: 'Komplett pakke som inkluderer hårklipp, skjeggtrim og styling. Vår premium-tjeneste.', | ||
duration: 45, | ||
price: 549, | ||
}, | ||
]; | ||
|
||
for (const service of services) { | ||
const existingService = await serviceRepository.findOne({ | ||
where: { name: service.name }, | ||
}); | ||
|
||
if (!existingService) { | ||
await serviceRepository.save(service); | ||
} | ||
} | ||
} | ||
} |
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,76 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ServicesController } from './services.controller'; | ||
import { ServicesService } from './services.service'; | ||
import { Service } from './entities/service.entity'; | ||
import { NotFoundException } from '@nestjs/common'; | ||
|
||
describe('ServicesController', () => { | ||
let controller: ServicesController; | ||
let service: ServicesService; | ||
|
||
const mockService: Service = { | ||
id: '1', | ||
name: 'Standard Klipp', | ||
description: 'En standard og effektiv hårklipp', | ||
duration: 20, | ||
price: 299, | ||
isActive: true, | ||
employees: [], | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}; | ||
|
||
const mockServicesService = { | ||
findAll: jest.fn().mockResolvedValue([mockService]), | ||
findOne: jest.fn().mockResolvedValue(mockService), | ||
findByEmployee: jest.fn().mockResolvedValue([mockService]), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [ServicesController], | ||
providers: [ | ||
{ | ||
provide: ServicesService, | ||
useValue: mockServicesService, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
controller = module.get<ServicesController>(ServicesController); | ||
service = module.get<ServicesService>(ServicesService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
|
||
describe('findAll', () => { | ||
it('should return an array of services', async () => { | ||
const result = await controller.findAll(); | ||
expect(result).toEqual([mockService]); | ||
expect(service.findAll).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('findOne', () => { | ||
it('should return a service by id', async () => { | ||
const result = await controller.findOne('1'); | ||
expect(result).toEqual(mockService); | ||
expect(service.findOne).toHaveBeenCalledWith('1'); | ||
}); | ||
|
||
it('should throw NotFoundException when service is not found', async () => { | ||
jest.spyOn(service, 'findOne').mockRejectedValueOnce(new NotFoundException()); | ||
await expect(controller.findOne('999')).rejects.toThrow(NotFoundException); | ||
}); | ||
}); | ||
|
||
describe('findByEmployee', () => { | ||
it('should return services by employee id', async () => { | ||
const result = await controller.findByEmployee('1'); | ||
expect(result).toEqual([mockService]); | ||
expect(service.findByEmployee).toHaveBeenCalledWith('1'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.