Leave Handler Documentation
This code defines a LeaveHandler
struct that handles various leave-related HTTP requests. It uses the Echo framework for routing and handling HTTP requests and responses.
LeaveHandler
struct
type LeaveHandler struct {
medahandler.MedaHandler
}
medahandler.MedaHandler
: Embeds theMedaHandler
struct from themedahandler
package.
Functions
NewLeaveHandler
func NewLeaveHandler() medahandler.MedaHandlerContract
Creates a new LeaveHandler
instance.
BindRouter
func (l *LeaveHandler) BindRouter(pbx *core.ServeEvent) error
Binds the handler's routes to the Echo router.
CheckEligibleUser
func (l *LeaveHandler) CheckEligibleUser(c echo.Context) error
Checks which users are eligible for a specific leave and returns the ineligible users as a JSON response.
AssignLeaveToUser
func (l *LeaveHandler) AssignLeaveToUser(c echo.Context) error
Assigns leave to eligible users and returns a JSON response.
validateUser
func (l *LeaveHandler) validateUser(leave *models.Record, users []models.Record, force bool) ([]*entity.UserProfileTable, []*entity.UserProfileTable)
Splits users into eligible and ineligible based on leave rules.
checkWorkPeriod
func (l *LeaveHandler) checkWorkPeriod(user *models.Record, workMinimum int, workMinimumUnit string) bool
Checks if a user meets the minimum work period requirement.
parseReasonToReadable
func (l *LeaveHandler) parseReasonToReadable(reasons []string) []string
Converts rule keys to readable strings.
GetLeaveRemaining
func (l *LeaveHandler) GetLeaveRemaining(c echo.Context) error
Gets the remaining leave for all users in a company and returns it as a JSON response.
GetRemainingLeaveByUserId
func (l *LeaveHandler) GetRemainingLeaveByUserId(c echo.Context) error
Gets the remaining leave for a specific user and returns it as a JSON response.
GetRemainingLeaveInSingleUser
func (l *LeaveHandler) GetRemainingLeaveInSingleUser(date time.Time, userId string, companyId string) (*entity.RemainingUserResponse, error)
Gets the remaining leave for a single user.
AssignLeaveToUserTimesheet
func (l *LeaveHandler) AssignLeaveToUserTimesheet(c echo.Context) error
Assigns leave to a user's timesheet and updates the calendar schedule.
UpdateBulkCaledarSchedule
func (l *LeaveHandler) UpdateBulkCaledarSchedule(calendarSchedules []*entity.CalendarScheduleTable, leaveRequestId string, isLeave bool) error
Updates the calendar schedule with leave information.
DeleteLeaveRequestInTimesheet
func (l *LeaveHandler) DeleteLeaveRequestInTimesheet(c echo.Context) error
Deletes a leave request from the timesheet and updates the calendar schedule.
Helper Functions
getStartEndDateOfCycle
func getStartEndDateOfCycle(startDate string, currentDate time.Time, amountCycle int, cycleUnit string) (time.Time, time.Time)
Calculates the start and end date of a leave cycle.
getRemainingUser
func getRemainingUser(data []*entity.RemainingUserResponse, userId string) (index int)
Finds the index of a user in the remaining leave response.
getRemainingQuotaOfPrevCycle
func getRemainingQuotaOfPrevCycle(currentDate time.Time, currentStartCycle time.Time, currentEndCycle time.Time, amountCycle int, leaveRequests []*entity.LeaveRequestTable, quota int, limit int, expiredMonth int) int
Calculates the remaining quota of the previous leave cycle.
generateResult
func generateResult(userId string, name string, leaves []*entity.LeavesTable) *entity.RemainingUserResponse
Generates a result structure for remaining leave.
getIndexOfRemainingUserLeave
func getIndexOfRemainingUserLeave(leaves []entity.RemainingUserLeaveResponse, leaveId string) int
Finds the index of a leave in the remaining leave response.
Example Usage
Initializing the Handler
package main
import (
"fio-backend/handler"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
)
func main() {
// Initialize Echo
e := echo.New()
// Initialize PocketBase
pb := pocketbase.New()
// Initialize the handler
leaveHandler := handler.NewLeaveHandler()
// Bind routes
pb.OnBeforeServe().Add(func(e *core.ServeEvent) error {
return leaveHandler.BindRouter(e)
})
// Start the server
e.Logger.Fatal(e.Start(":8080"))
}
Making Requests
Check Eligible User
curl -X POST http://localhost:8080/api/v1/leave/eligible -d '{
"user_ids": ["user123", "user456"],
"leave_id": "leave123"
}'
Assign Leave to User
curl -X PUT http://localhost:8080/api/v1/leave/request -d '{
"user_ids": ["user123", "user456"],
"leave_id": "leave123",
"quota": 10,
"is_force": false
}'
Get Leave Remaining
curl -X GET http://localhost:8080/api/v1/leave/remaining?company_id=company123&date=2023-01-01
Get Remaining Leave by User ID
curl -X GET http://localhost:8080/api/v1/leave/remaining-user?user_id=user123&company_id=company123&date=2023-01-01
Assign Leave to User Timesheet
curl -X PUT http://localhost:8080/api/v1/leave/request -d '{
"user_id": "user123",
"leave_id": "leave123",
"start_date": "2023-01-01",
"end_date": "2023-01-05",
"note": "Vacation"
}'
Delete Leave Request in Timesheet
curl -X DELETE http://localhost:8080/api/v1/leave/request/leaveRequest123
This documentation provides an overview of the LeaveHandler
struct, its functions, and example usage. By following the example, you can initialize the handler, bind routes, and make requests to the various endpoints.