Skip to main content

Update Shift

Purpose:

This function handles the update of an existing shift within the system. It involves:

  1. Extracting Shift ID:

    • Retrieves the shift ID from the URL path parameter.

    • Validates the ID to ensure it's not empty.

  2. Binding and Validating Request Payload:

    • Extracts the CreateShiftRequest payload from the incoming HTTP request.

    • Validates the payload data against defined constraints.

  3. Preparing Flexible Start and End Dates:

    • If the shift has a flexible start or end time, calculates the appropriate start and end dates based on the provided time ranges.
  4. Fetching Current Shift:

    • Retrieves the current shift information from the database using the provided ID.
  5. Database Transaction:

    • Initiates a database transaction to ensure data consistency.

    • Inserts a new shift record with updated information.

    • Inserts associated shift breaks for the new shift.

    • Archives the old shift record, linking it to the new one.

  6. Returning Response:

    • Sends an HTTP OK response with an empty JSON body upon successful shift update.

Function Signature:

func (h *ShiftHandler) UpdateShift(c echo.Context) error   

Parameters:

  • c: An echo.Context object representing the incoming HTTP request.

Return Value:

  • An error value indicating if an error occurred during the shift update process.

Error Handling:

The function handles errors at various stages:

  • Invalid ID: If the provided ID is empty, a validation error is returned.

  • Binding and Validation: Errors during payload binding or validation are returned directly.

  • Flexible Date Preparation: Errors in calculating flexible start and end dates are returned.

  • Fetching Current Shift: Errors during fetching the current shift are returned.

  • Database Transaction: Errors during database operations (insert new shift, insert breaks, archive old shift) are returned.

Example Usage:

// Assuming an HTTP PUT request with a valid JSON payload:  
{
"name": "Updated Morning Shift",
"start_time": "09:30:00",
"end_time": "17:30:00",
"is_flexible": true, "flexible_start_time_range": "08:30:00-10:30:00",
"flexible_end_time_range": "16:30:00-18:30:00",
"breaks": [
{
"start_time": "12:30:00",
"end_time": "13:30:00"
}
]
}
/*
The function will:
1. Extract the shift ID from the URL.
2. Validate the ID.
3. Parse the JSON payload into a `CreateShiftRequest` struct.
4. Validate the payload.
5. Calculate the actual start and end times based on the flexible ranges.
6. Fetch the current shift information.
7. Begin a database transaction.
8. Insert a new shift record with updated information.
9. Insert the shift break into the `shift_breaks` table for the new shift.
10. Archive the old shift record, linking it to the new one.
11. Commit the transaction.
12. Return an HTTP 200 OK response.
*/

Additional Notes:

  • The entity.CreateShiftRequest struct likely defines the expected fields for a new shift.

  • The daos.Dao struct probably provides database access methods.

  • The queries package might contain SQL queries for database operations.

  • The response package might be a custom library for handling HTTP responses.