Skip to content
CodeSook
CodeSook

17. Implement Overtime Services


File and Folder structure

File and Folder structure
src
├── services
├── employee/
└── overtime
├── creates.ts
├── finds.ts
├── index.ts
├── removes.ts
└── updates.ts
└── types
├── repositories/
└── services
├── employee.ts
└── overtime.ts

Overtime Service Interface

ก็มาทำ interface กันก่อน น่าจะเดาทางกันได้แล้วใช่มะ

import type { Branded, EmployeeSchema, OvertimeSchema, OvertimeWithRelationsSchema } from "../../schema/index.js"
export type OvertimeService = {
create: (data: OvertimeSchema.CreateOvertime) => Promise<OvertimeSchema.Overtime>
findById: (id: Branded.OvertimeId) => Promise<OvertimeWithRelationsSchema.OvertimeWithRelations | null>
findMany: () => Promise<OvertimeWithRelationsSchema.OvertimeWithRelationsArray>
findByDepartmentName: (departmentName: EmployeeSchema.Employee["department"]) => Promise<OvertimeWithRelationsSchema.OvertimeWithRelationsArray>
update: (id: Branded.OvertimeId, data: Partial<OvertimeSchema.UpdateOvertime>) => Promise<OvertimeSchema.Overtime | null>
remove: (id: Branded.OvertimeId) => Promise<OvertimeSchema.Overtime | null>
summaryByEmployeeId: (filters: { employeeId: Branded.EmployeeId, dateRange: { start: Date, end: Date } }) => Promise<{ hoursWorked: number }>
summaryByDepartment: (filters: { department: EmployeeSchema.Employee["department"], dateRange: { start: Date, end: Date } }) => Promise<{ hoursWorked: number }>
}

Create Overtime Service

src/services/overtime/creates.ts
src/services/overtime/creates.ts
import type { OvertimeRepository } from "../../types/repositories/overtime.js"
import type { OvertimeService } from "../../types/services/overtime.js"
export function create(overtimeRepository: OvertimeRepository): OvertimeService["create"] {
return async (data) => {
const overtime = await overtimeRepository.create(data)
return overtime
}
}

Find Overtime Service

src/services/overtime/finds.ts
src/services/overtime/finds.ts
import type { OvertimeRepository } from "../../types/repositories/overtime.js"
import type { OvertimeService } from "../../types/services/overtime.js"
export function findById(overtimeRepository: OvertimeRepository): OvertimeService["findById"] {
return async (id) => {
const overtime = await overtimeRepository.findByIdWithRelations(id)
return overtime
}
}
export function findMany(overtimeRepository: OvertimeRepository): OvertimeService["findMany"] {
return async () => {
const overtimes = await overtimeRepository.findManyWithRelations()
return overtimes
}
}
export function findByDeparmentName(overtimeRepository: OvertimeRepository): OvertimeService["findByDepartmentName"] {
return async (departmentName) => {
const overtimes = await overtimeRepository.findByDeparmentName(departmentName)
return overtimes
}
}
export function summaryByEmployeeId(overtimeRepository: OvertimeRepository): OvertimeService["summaryByEmployeeId"] {
return async (employeeId) => {
const overtimes = await overtimeRepository.summaryByEmployeeId(employeeId)
return overtimes
}
}
export function summaryByDepartment(overtimeRepository: OvertimeRepository): OvertimeService["summaryByDepartment"] {
return async (departmentName) => {
const overtimes = await overtimeRepository.summaryByDepartment(departmentName)
return overtimes
}
}

Update Overtime Service

src/services/overtime/updates.ts
src/services/overtime/updates.ts
import type { OvertimeRepository } from "../../types/repositories/overtime.js"
import type { OvertimeService } from "../../types/services/overtime.js"
export function updateById(overtimeRepository: OvertimeRepository): OvertimeService["update"] {
return async (id, data) => {
const updatedData = await overtimeRepository.updatePartial(id, data)
return updatedData
}
}

Remove Overtime Service

src/services/overtime/removes.ts
src/services/overtime/removes.ts
import type { OvertimeRepository } from "../../types/repositories/overtime.js"
import type { OvertimeService } from "../../types/services/overtime.js"
export function removeById(overtimeRepository: OvertimeRepository): OvertimeService["remove"] {
return (id) => {
return overtimeRepository.remove(id)
}
}

Export all services

มาทำ function initOvertimeServices() ซึ่ง function นี้จะต้อง return interface ตามที่ได้กำหนดไว้

src/services/overtime/index.ts
src/services/overtime/index.ts
import type { OvertimeRepository } from "../../types/repositories/overtime.js"
import type { OvertimeService } from "../../types/services/overtime.js"
import * as Creates from "./creates.js"
import * as Finds from "./finds.js"
import * as Removes from "./removes.js"
import * as Updates from "./updates.js"
export function initOvertimeService(overtimeRepository: OvertimeRepository): OvertimeService {
return {
create: Creates.create(overtimeRepository),
findByDepartmentName: Finds.findByDeparmentName(overtimeRepository),
findById: Finds.findById(overtimeRepository),
findMany: Finds.findMany(overtimeRepository),
remove: Removes.removeById(overtimeRepository),
summaryByDepartment: Finds.summaryByDepartment(overtimeRepository),
summaryByEmployeeId: Finds.summaryByEmployeeId(overtimeRepository),
update: Updates.updateById(overtimeRepository),
}
}