diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 68a05ac..e749f3f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -10,7 +10,7 @@ generator client { datasource db { provider = "postgresql" - url = env("DATABASE_URL") + url = env("DATABASE_URL_DEV") } model Users { diff --git a/src/modules/customers/controllers/customers.controller.ts b/src/modules/customers/controllers/customers.controller.ts index b30d375..00c212e 100644 --- a/src/modules/customers/controllers/customers.controller.ts +++ b/src/modules/customers/controllers/customers.controller.ts @@ -1,8 +1,8 @@ import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post } from '@nestjs/common'; import { CustomersService } from '../services/customers.service'; import { Customers, Employees } from '@prisma/client'; -import { CreateCustomerDto } from '../dtos/create-customers'; -import { UpdateCustomerDto } from '../dtos/update-customers'; +import { CreateCustomerDto } from '../dtos/create-customer'; +import { UpdateCustomerDto } from '../dtos/update-customer'; @Controller('customers') export class CustomersController { diff --git a/src/modules/customers/dtos/create-customers.ts b/src/modules/customers/dtos/create-customer.ts similarity index 100% rename from src/modules/customers/dtos/create-customers.ts rename to src/modules/customers/dtos/create-customer.ts diff --git a/src/modules/customers/dtos/update-customers.ts b/src/modules/customers/dtos/update-customer.ts similarity index 68% rename from src/modules/customers/dtos/update-customers.ts rename to src/modules/customers/dtos/update-customer.ts index ded8b46..0499fe2 100644 --- a/src/modules/customers/dtos/update-customers.ts +++ b/src/modules/customers/dtos/update-customer.ts @@ -1,4 +1,4 @@ import { PartialType } from "@nestjs/swagger"; -import { CreateCustomerDto } from "./create-customers"; +import { CreateCustomerDto } from "./create-customer"; export class UpdateCustomerDto extends PartialType(CreateCustomerDto) {} \ No newline at end of file diff --git a/src/modules/customers/services/customers.service.ts b/src/modules/customers/services/customers.service.ts index 8175160..f67f4bd 100644 --- a/src/modules/customers/services/customers.service.ts +++ b/src/modules/customers/services/customers.service.ts @@ -1,8 +1,8 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { PrismaService } from 'src/prisma/prisma.service'; -import { CreateCustomerDto } from '../dtos/create-customers'; +import { CreateCustomerDto } from '../dtos/create-customer'; import { Customers, Users } from '@prisma/client'; -import { UpdateCustomerDto } from '../dtos/update-customers'; +import { UpdateCustomerDto } from '../dtos/update-customer'; @Injectable() export class CustomersService { diff --git a/src/modules/employees/dtos/create-employee.dto.ts b/src/modules/employees/dtos/create-employee.dto.ts index 5cb7c82..88bd9e3 100644 --- a/src/modules/employees/dtos/create-employee.dto.ts +++ b/src/modules/employees/dtos/create-employee.dto.ts @@ -21,7 +21,7 @@ export class CreateEmployeeDto { @IsEmail() @IsOptional() - email?: string; + email: string; @Type(() => Number) @IsInt() diff --git a/src/modules/employees/services/employees.service.ts b/src/modules/employees/services/employees.service.ts index 0842e70..5d945bd 100644 --- a/src/modules/employees/services/employees.service.ts +++ b/src/modules/employees/services/employees.service.ts @@ -6,9 +6,7 @@ import { Employees, Users } from '@prisma/client'; @Injectable() export class EmployeesService { - constructor( - private readonly prisma: PrismaService, - ) {} + constructor(private readonly prisma: PrismaService) {} async create(dto: CreateEmployeeDto): Promise { const { diff --git a/src/modules/shift-codes/controllers/shift-codes.controller.ts b/src/modules/shift-codes/controllers/shift-codes.controller.ts index e69de29..fe95532 100644 --- a/src/modules/shift-codes/controllers/shift-codes.controller.ts +++ b/src/modules/shift-codes/controllers/shift-codes.controller.ts @@ -0,0 +1,36 @@ +import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post } from "@nestjs/common"; +import { ShiftCodesService } from "../services/shift-codes.service"; +import { CreateShiftCodesDto } from "../dtos/create-shift-codes.dto"; +import { ShiftCodes } from "@prisma/client"; +import { UpdateShiftCodesDto } from "../dtos/update-shift-codes.dto"; + +@Controller() +export class ShiftCodesController { + constructor(private readonly shiftCodesService: ShiftCodesService) {} + + @Post() + create(@Body()dto: CreateShiftCodesDto): Promise { + return this.shiftCodesService.create(dto); + } + + @Get() + findAll(): Promise { + return this.shiftCodesService.findAll(); + } + + @Get(':id') + findOne(@Param('id', ParseIntPipe) id: number): Promise { + return this.shiftCodesService.findOne(id); + } + + @Patch(':id') + update(@Param('id', ParseIntPipe) id: number, + @Body() dto: UpdateShiftCodesDto): Promise { + return this.shiftCodesService.update(id,dto); + } + + @Delete(':id') + remove(@Param('id', ParseIntPipe)id: number): Promise { + return this.shiftCodesService.remove(id); + } +} diff --git a/src/modules/shift-codes/services/shift-codes.service.ts b/src/modules/shift-codes/services/shift-codes.service.ts index 5d0d8c8..2912350 100644 --- a/src/modules/shift-codes/services/shift-codes.service.ts +++ b/src/modules/shift-codes/services/shift-codes.service.ts @@ -1,17 +1,109 @@ -import { Injectable } from "@nestjs/common"; +import { Injectable, NotFoundException } from "@nestjs/common"; import { PrismaService } from "src/prisma/prisma.service"; import { CreateShiftCodesDto } from "../dtos/create-shift-codes.dto"; import { ShiftCodes } from "@prisma/client"; +import { UpdateShiftCodesDto } from "../dtos/update-shift-codes.dto"; @Injectable() export class ShiftCodesService { constructor(private readonly prisma: PrismaService) {} - create(dto: CreateShiftCodesDto): Promise { - const { - shift_type, - bank_code, - } = dto; - return this.prisma.$queryRaw(dto) + async create(dto: CreateShiftCodesDto): Promise { + const { shift_type, bank_code } = dto; + return this.prisma.shiftCodes.create({ + data: { shift_type, bank_code }, + include: { + shift: { + include: { + timesheet: { + include: { + employee: { + include: { + user: true, + }, + }, + }, + }, + }, + }, + }, + }); + } + + findAll(): Promise { + return this.prisma.shiftCodes.findMany({ + include: { + shift: { + include: { + timesheet: { + include: { + employee: { + include: { + user: true + } + } + } + } + }, + }, + }, + }); + } + + async findOne(id: number): Promise { + const record = await this.prisma.shiftCodes.findUnique({ + where: { id }, + include: { + shift: { + include: { + timesheet: { + include: { + employee: { + include: { + user:true, + } + } + } + } + } + } + } + }); + if(!record) { + throw new NotFoundException(`ShiftCode #${id} not found`); + } + return record; + } + + async update(id: number, dto: UpdateShiftCodesDto): Promise { + await this.findOne(id); + const { shift_type, bank_code } = dto; + return this.prisma.shiftCodes.update({ + where: { id }, + data: { + ...(shift_type !== undefined && { shift_type }), + ...(bank_code !== undefined && { bank_code }), + }, + include: { + shift: { + include: { + timesheet: { + include: { + employee: { + include: { + user: true + } + } + } + }, + }, + }, + }, + }); + } + + async remove(id:number): Promise{ + await this.findOne(id); + return this.prisma.shiftCodes.delete({ where: { id }}); } } \ No newline at end of file diff --git a/src/modules/shift-codes/shift-codes.module.ts b/src/modules/shift-codes/shift-codes.module.ts index e69de29..8bc2594 100644 --- a/src/modules/shift-codes/shift-codes.module.ts +++ b/src/modules/shift-codes/shift-codes.module.ts @@ -0,0 +1,11 @@ +import { Module } from "@nestjs/common"; +import { ShiftCodesController } from "./controllers/shift-codes.controller"; +import { ShiftCodesService } from "./services/shift-codes.service"; +import { PrismaService } from "src/prisma/prisma.service"; + +@Module({ + controllers: [ShiftCodesController], + providers: [ShiftCodesService, PrismaService], +}) + +export class ShiftCodeModule {} \ No newline at end of file diff --git a/src/modules/shifts/controllers/shifts.controller.ts b/src/modules/shifts/controllers/shifts.controller.ts index c53cfa8..12586a3 100644 --- a/src/modules/shifts/controllers/shifts.controller.ts +++ b/src/modules/shifts/controllers/shifts.controller.ts @@ -1,6 +1,40 @@ -import { Controller } from "@nestjs/common"; +import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post } from "@nestjs/common"; +import { ShiftsService } from "../services/shifts.service"; +import { Shifts } from "@prisma/client"; +import { UpdateEmployeeDto } from "src/modules/employees/dtos/update-employee.dto"; +import { CreateShiftDto } from "../dtos/create-shifts.dto"; @Controller('shifts') export class ShiftsController { + constructor(private readonly shiftsService: ShiftsService){} + + + @Post() + create(@Body() dto: CreateShiftDto): Promise { + return this.shiftsService.create(dto); + } + + @Get() + findAll(): Promise { + return this.shiftsService.findAll(); + } + + @Get(':id') + findOne(@Param('id', ParseIntPipe) id: number): Promise { + return this.shiftsService.findOne(id); + } + + @Patch(':id') + update( + @Param('id', ParseIntPipe) id: number, + @Body() dto: UpdateEmployeeDto, + ): Promise { + return this.shiftsService.update(id, dto); + } + + @Delete(':id') + remove(@Param('id', ParseIntPipe) id: number): Promise { + return this.shiftsService.remove(id); + } } \ No newline at end of file diff --git a/src/modules/shifts/dtos/create-shifts.dto.ts b/src/modules/shifts/dtos/create-shifts.dto.ts index 4a15280..4365be6 100644 --- a/src/modules/shifts/dtos/create-shifts.dto.ts +++ b/src/modules/shifts/dtos/create-shifts.dto.ts @@ -1,7 +1,16 @@ import { Type } from "class-transformer"; -import { IsDate, IsDateString } from "class-validator"; +import { IsDate, IsDateString, IsInt } from "class-validator"; export class CreateShiftDto { + @Type(() => Number) + @IsInt() + timesheet_id: number; + + @Type(() => Number) + @IsInt() + shift_code_id: number; + + @IsDateString() @Type(() => Date) @IsDate() diff --git a/src/modules/shifts/services/shifts.service.ts b/src/modules/shifts/services/shifts.service.ts index 3443cbc..5e41c65 100644 --- a/src/modules/shifts/services/shifts.service.ts +++ b/src/modules/shifts/services/shifts.service.ts @@ -1,14 +1,101 @@ -import { Injectable } from "@nestjs/common"; -import { TimesheetsService } from "src/modules/timesheets/services/timesheets.service"; +import { Injectable, NotFoundException } from "@nestjs/common"; import { PrismaService } from "src/prisma/prisma.service"; import { CreateShiftDto } from "../dtos/create-shifts.dto"; import { Shifts } from "@prisma/client"; +import { notDeepEqual } from "assert"; +import { UpdateShiftsDto } from "../dtos/update-shifts.dto"; @Injectable() export class ShiftsService { - constructor( - private readonly prisma: PrismaService, - private readonly timesheetsService: TimesheetsService, + constructor(private readonly prisma: PrismaService) {} - ) {} + async create(dto: CreateShiftDto): Promise { + const { + timesheet_id, + shift_code_id, + date, + start_time, + end_time + } = dto; + + return this.prisma.shifts.create({ + data: { + timesheet_id, + shift_code_id, + date, start_time, + end_time + }, + include: { + timesheet: { + include: { + employee: { + include: { user: true } + }, + }, + shift_code: true, + }, + }, + }); + } + + findAll(): Promise { + return this.prisma.shifts.findMany({ + include: { + timesheet: { + include: { + employee: { user:true } + }, + }, + }, + }); + } + + async findOne(id: number): Promise { + const shift = await this.prisma.shifts.findUnique({ + where: { id }, + include: { + timesheet: { + include: { + employee: { user: true } + }, + }, + shift_code: true, + }, + }); + if(!shift) { + throw new NotFoundException(`Shift #${id} not found`); + } + return shift; + } + + async update( + id: number, + dto: UpdateShiftsDto, + ): Promise { + await this.prisma.shifts.update({ + where: { id }, + data: { + ...(timesheets_id !== undefined && { timesheet_id }), + ...(shift_code_id !== undefined && { shift_code_id }), + ...(date !== undefined && { date }), + ...(start_time !== undefined && { start_time }), + ...(end_time !== undefined && { end_time }), + }, + include: { + timesheet: { + include: { + employee: { + include: { user: true } + }, + }, + shift_code: true, + }, + }, + }); + } + + async remove(id: number): Promise { + await this.findOne(id); + return this.prisma.shifts.delete({ where: { id } }); + } } \ No newline at end of file diff --git a/src/modules/shifts/shifts.module.ts b/src/modules/shifts/shifts.module.ts index b63b08c..6c05370 100644 --- a/src/modules/shifts/shifts.module.ts +++ b/src/modules/shifts/shifts.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common'; import { ShiftsController } from './controllers/shifts.controller'; import { ShiftsService } from './services/shifts.service'; +import { PrismaService } from 'src/prisma/prisma.service'; @Module({ controllers: [ShiftsController], - providers: [ShiftsService] + providers: [ShiftsService, PrismaService] }) export class ShiftsModule {} diff --git a/src/modules/timesheets/controllers/timesheets.controller.ts b/src/modules/timesheets/controllers/timesheets.controller.ts index e065b33..2b148c9 100644 --- a/src/modules/timesheets/controllers/timesheets.controller.ts +++ b/src/modules/timesheets/controllers/timesheets.controller.ts @@ -1,4 +1,38 @@ -import { Controller } from '@nestjs/common'; +import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post } from '@nestjs/common'; +import { TimesheetsService } from '../services/timesheets.service'; +import { CreateTimesheetDto } from '../dtos/create-timesheet.dto'; +import { Timesheets } from '@prisma/client'; +import { UpdateTimesheetDto } from '../dtos/update-timesheet.dto'; @Controller('timesheets') -export class TimesheetsController {} +export class TimesheetsController { + constructor(private readonly timesheetsService: TimesheetsService) {} + + @Post() + create(@Body() dto: CreateTimesheetDto): Promise { + return this.timesheetsService.create(dto); + } + + @Get() + findAll(): Promise { + return this.timesheetsService.findAll(); + } + + @Get(':id') + findOne(@Param('id', ParseIntPipe) id: number): Promise { + return this.timesheetsService.findOne(id); + } + + @Patch(':id') + update( + @Param('id', ParseIntPipe) id:number, + @Body() dto: UpdateTimesheetDto, + ): Promise { + return this.timesheetsService.update(id, dto); + } + + @Delete(':id') + remove(@Param('id', ParseIntPipe) id: number): Promise { + return this.timesheetsService.remove(id); + } +} diff --git a/src/modules/timesheets/dtos/create-timesheet.dto.ts b/src/modules/timesheets/dtos/create-timesheet.dto.ts index e167af5..8194cc1 100644 --- a/src/modules/timesheets/dtos/create-timesheet.dto.ts +++ b/src/modules/timesheets/dtos/create-timesheet.dto.ts @@ -1 +1,13 @@ -export class CreateTimesheetDto {} +import { Type } from "class-transformer"; +import { IsBoolean, IsInt, IsOptional } from "class-validator"; + +export class CreateTimesheetDto { + + @Type(() => Number) + @IsInt() + employee_id: number; + + @IsOptional() + @IsBoolean() + is_approved?: boolean; +} diff --git a/src/modules/timesheets/dtos/update-timesheet.dto.ts b/src/modules/timesheets/dtos/update-timesheet.dto.ts index 14c2698..d621e6a 100644 --- a/src/modules/timesheets/dtos/update-timesheet.dto.ts +++ b/src/modules/timesheets/dtos/update-timesheet.dto.ts @@ -1 +1,4 @@ -export class UpdateTimesheetDto {} +import { PartialType } from "@nestjs/swagger"; +import { CreateTimesheetDto } from "./create-timesheet.dto"; + +export class UpdateTimesheetDto extends PartialType(CreateTimesheetDto) {} diff --git a/src/modules/timesheets/services/timesheets.service.ts b/src/modules/timesheets/services/timesheets.service.ts index ae3402a..31279b2 100644 --- a/src/modules/timesheets/services/timesheets.service.ts +++ b/src/modules/timesheets/services/timesheets.service.ts @@ -1,4 +1,78 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; +import { PrismaService } from 'src/prisma/prisma.service'; +import { CreateTimesheetDto } from '../dtos/create-timesheet.dto'; +import { Timesheets } from '@prisma/client'; +import { UpdateTimesheetDto } from '../dtos/update-timesheet.dto'; @Injectable() -export class TimesheetsService {} +export class TimesheetsService { + constructor(private readonly prisma: PrismaService) {} + + async create(dto : CreateTimesheetDto): Promise { + const { employee_id, is_approved } = dto; + return this.prisma.timesheets.create({ + data: { + employee_id, + is_approved: is_approved ?? false, + }, + include: { + employee: { + include: { user: true } + }, + }, + }); + } + + findAll(): Promise { + return this.prisma.timesheets.findMany({ + include: { + employee: { + include: { user: true }, + }, + }, + }); + } + + async findOne(id: number): Promise { + const record = await this.prisma.timesheets.findUnique({ + where: { id }, + include: { + employee: { + include: { + user:true + } + }, + }, + }); + if(!record) { + throw new NotFoundException(`Timesheet #${id} not found`); + } + return record; + } + + async update(id: number, dto:UpdateTimesheetDto): Promise { + await this.findOne(id); + const { employee_id, is_approved } = dto; + return this.prisma.timesheets.update({ + where: { id }, + data: { + ...(employee_id !== undefined && { employee_id }), + ...(is_approved !== undefined && { is_approved }), + }, + include: { + employee: { + include: { user: true } + }, + }, + }); + } + + async remove(id: number): Promise { + await this.findOne(id); + return this.prisma.timesheets.delete({ + where: { id }, + }); + } + + +}