src├── services│ ├── employee/│ └── overtime│ ├── creates.ts│ ├── finds.ts│ ├── index.ts│ ├── removes.ts│ └── updates.ts└── types ├── repositories/ └── services ├── employee.ts └── overtime.ts
ก็มาทำ interface กันก่อน น่าจะเดาทางกันได้แล้วใช่มะ
1import type { Branded, EmployeeSchema, OvertimeSchema, OvertimeWithRelationsSchema } from "../../schema/index.js"2 3export type OvertimeService = {4 create: (data: OvertimeSchema.CreateOvertime) => Promise<OvertimeSchema.Overtime>5 findById: (id: Branded.OvertimeId) => Promise<OvertimeWithRelationsSchema.OvertimeWithRelations | null>6 findMany: () => Promise<OvertimeWithRelationsSchema.OvertimeWithRelationsArray>7 findByDepartmentName: (departmentName: EmployeeSchema.Employee["department"]) => Promise<OvertimeWithRelationsSchema.OvertimeWithRelationsArray>8 update: (id: Branded.OvertimeId, data: Partial<OvertimeSchema.UpdateOvertime>) => Promise<OvertimeSchema.Overtime | null>9 remove: (id: Branded.OvertimeId) => Promise<OvertimeSchema.Overtime | null>10 summaryByEmployeeId: (filters: { employeeId: Branded.EmployeeId, dateRange: { start: Date, end: Date } }) => Promise<{ hoursWorked: number }>11 summaryByDepartment: (filters: { department: EmployeeSchema.Employee["department"], dateRange: { start: Date, end: Date } }) => Promise<{ hoursWorked: number }>12}
1import type { OvertimeRepository } from "../../types/repositories/overtime.js"2import type { OvertimeService } from "../../types/services/overtime.js"3 4export function create(overtimeRepository: OvertimeRepository): OvertimeService["create"] {5 return async (data) => {6 const overtime = await overtimeRepository.create(data)7 return overtime8 }9}
1import type { OvertimeRepository } from "../../types/repositories/overtime.js"2import type { OvertimeService } from "../../types/services/overtime.js"3 4export function findById(overtimeRepository: OvertimeRepository): OvertimeService["findById"] {5 return async (id) => {6 const overtime = await overtimeRepository.findByIdWithRelations(id)7 return overtime8 }9}10 11export function findMany(overtimeRepository: OvertimeRepository): OvertimeService["findMany"] {12 return async () => {13 const overtimes = await overtimeRepository.findManyWithRelations()14 return overtimes15 }16}17 18export function findByDeparmentName(overtimeRepository: OvertimeRepository): OvertimeService["findByDepartmentName"] {19 return async (departmentName) => {20 const overtimes = await overtimeRepository.findByDeparmentName(departmentName)21 return overtimes22 }23}24 25export function summaryByEmployeeId(overtimeRepository: OvertimeRepository): OvertimeService["summaryByEmployeeId"] {26 return async (employeeId) => {27 const overtimes = await overtimeRepository.summaryByEmployeeId(employeeId)28 return overtimes29 }30}31 32export function summaryByDepartment(overtimeRepository: OvertimeRepository): OvertimeService["summaryByDepartment"] {33 return async (departmentName) => {34 const overtimes = await overtimeRepository.summaryByDepartment(departmentName)35 return overtimes36 }37}
1import type { OvertimeRepository } from "../../types/repositories/overtime.js"2import type { OvertimeService } from "../../types/services/overtime.js"3 4export function updateById(overtimeRepository: OvertimeRepository): OvertimeService["update"] {5 return async (id, data) => {6 const updatedData = await overtimeRepository.updatePartial(id, data)7 return updatedData8 }9}
1import type { OvertimeRepository } from "../../types/repositories/overtime.js"2import type { OvertimeService } from "../../types/services/overtime.js"3 4export function removeById(overtimeRepository: OvertimeRepository): OvertimeService["remove"] {5 return (id) => {6 return overtimeRepository.remove(id)7 }8}
มาทำ function initOvertimeServices() ซึ่ง function นี้จะต้อง return interface ตามที่ได้กำหนดไว้
initOvertimeServices()
1import type { OvertimeRepository } from "../../types/repositories/overtime.js"2import type { OvertimeService } from "../../types/services/overtime.js"3import * as Creates from "./creates.js"4import * as Finds from "./finds.js"5import * as Removes from "./removes.js"6import * as Updates from "./updates.js"7 8export function initOvertimeService(overtimeRepository: OvertimeRepository): OvertimeService {9 return {10 create: Creates.create(overtimeRepository),11 findByDepartmentName: Finds.findByDeparmentName(overtimeRepository),12 findById: Finds.findById(overtimeRepository),13 findMany: Finds.findMany(overtimeRepository),14 remove: Removes.removeById(overtimeRepository),15 summaryByDepartment: Finds.summaryByDepartment(overtimeRepository),16 summaryByEmployeeId: Finds.summaryByEmployeeId(overtimeRepository),17 update: Updates.updateById(overtimeRepository),18 }19}