File size: 1,457 Bytes
c36bd79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package api

import (
	"bobmigrate/models"
	"github.com/gofiber/fiber/v2"
)

// PayrollRequest separates the API payload from the internal database model
type PayrollRequest struct {
	EmpID       string `json:"emp_id"`
	EmpStatus   string `json:"emp_status"`
	HoursWorked int64  `json:"hours_worked"`
	HourlyRate  int64  `json:"hourly_rate_cents"`
}

// PayrollResponse returns the calculated output
type PayrollResponse struct {
	EmpID         string `json:"emp_id"`
	GrossPayCents int64  `json:"gross_pay_cents"`
	Status        string `json:"status"`
}

func SetupRoutes(app *fiber.App) {
	app.Post("/api/v1/payroll", ProcessPayrollHandler)
}

func ProcessPayrollHandler(c *fiber.Ctx) error {
	var req PayrollRequest
	if err := c.BodyParser(&req); err != nil {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
			"error": "invalid JSON body",
		})
	}

	if req.HoursWorked < 0 || req.HourlyRate < 0 {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
			"error": "hours and rates cannot be negative",
		})
	}

	// Map DTO to the modernized business logic model
	record := &models.EmployeeRecord{
		EmpID:       req.EmpID,
		EmpStatus:   req.EmpStatus,
		HoursWorked: req.HoursWorked,
		HourlyRate:  req.HourlyRate,
	}

	// Execute translated legacy logic
	models.CalculatePay(record)

	return c.Status(fiber.StatusOK).JSON(PayrollResponse{
		EmpID:         record.EmpID,
		GrossPayCents: record.GrossPay,
		Status:        "success",
	})
}