#!/bin/bash # Employee Swap Backend Test Script # Tests the employee swap functionality including create, complete, and cancel operations BASE_URL="http://localhost:3000/api" ADMIN_USER="admin" ADMIN_PASS="admin123" echo "========================================" echo " Employee Swap Backend Test" echo "========================================" echo "" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print test results print_result() { if [ $1 -eq 0 ]; then echo -e "${GREEN}✓ PASS${NC}: $2" else echo -e "${RED}✗ FAIL${NC}: $2" fi } # Step 1: Login as SuperAdmin echo "Step 1: Logging in as SuperAdmin..." LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/auth/login" \ -H "Content-Type: application/json" \ -d "{\"username\":\"$ADMIN_USER\",\"password\":\"$ADMIN_PASS\"}") TOKEN=$(echo $LOGIN_RESPONSE | jq -r '.token') if [ "$TOKEN" != "null" ] && [ -n "$TOKEN" ]; then print_result 0 "Login successful" echo " Token: ${TOKEN:0:30}..." else print_result 1 "Login failed" echo " Response: $LOGIN_RESPONSE" exit 1 fi echo "" # Step 2: Get list of employees echo "Step 2: Fetching employees..." EMPLOYEES_RESPONSE=$(curl -s -X GET "$BASE_URL/users?role=Employee" \ -H "Authorization: Bearer $TOKEN") EMPLOYEE_COUNT=$(echo $EMPLOYEES_RESPONSE | jq 'length') if [ "$EMPLOYEE_COUNT" -gt 0 ]; then print_result 0 "Found $EMPLOYEE_COUNT employees" FIRST_EMPLOYEE_ID=$(echo $EMPLOYEES_RESPONSE | jq '.[0].id') FIRST_EMPLOYEE_NAME=$(echo $EMPLOYEES_RESPONSE | jq -r '.[0].name') FIRST_EMPLOYEE_DEPT=$(echo $EMPLOYEES_RESPONSE | jq '.[0].department_id') FIRST_EMPLOYEE_CONTRACTOR=$(echo $EMPLOYEES_RESPONSE | jq '.[0].contractor_id') echo " Test employee: $FIRST_EMPLOYEE_NAME (ID: $FIRST_EMPLOYEE_ID)" echo " Current department_id: $FIRST_EMPLOYEE_DEPT" echo " Current contractor_id: $FIRST_EMPLOYEE_CONTRACTOR" else print_result 1 "No employees found" echo " Response: $EMPLOYEES_RESPONSE" exit 1 fi echo "" # Step 3: Get list of departments echo "Step 3: Fetching departments..." DEPTS_RESPONSE=$(curl -s -X GET "$BASE_URL/departments" \ -H "Authorization: Bearer $TOKEN") DEPT_COUNT=$(echo $DEPTS_RESPONSE | jq 'length') if [ "$DEPT_COUNT" -gt 1 ]; then print_result 0 "Found $DEPT_COUNT departments" # Find a different department than the employee's current one TARGET_DEPT_ID=$(echo $DEPTS_RESPONSE | jq --argjson current "$FIRST_EMPLOYEE_DEPT" '[.[] | select(.id != $current)][0].id') TARGET_DEPT_NAME=$(echo $DEPTS_RESPONSE | jq -r --argjson current "$FIRST_EMPLOYEE_DEPT" '[.[] | select(.id != $current)][0].name') echo " Target department: $TARGET_DEPT_NAME (ID: $TARGET_DEPT_ID)" else print_result 1 "Need at least 2 departments for swap test" exit 1 fi echo "" # Step 4: Get contractors in target department echo "Step 4: Fetching contractors in target department..." CONTRACTORS_RESPONSE=$(curl -s -X GET "$BASE_URL/users?role=Contractor&departmentId=$TARGET_DEPT_ID" \ -H "Authorization: Bearer $TOKEN") CONTRACTOR_COUNT=$(echo $CONTRACTORS_RESPONSE | jq 'length') if [ "$CONTRACTOR_COUNT" -gt 0 ]; then TARGET_CONTRACTOR_ID=$(echo $CONTRACTORS_RESPONSE | jq '.[0].id') TARGET_CONTRACTOR_NAME=$(echo $CONTRACTORS_RESPONSE | jq -r '.[0].name') print_result 0 "Found contractor: $TARGET_CONTRACTOR_NAME (ID: $TARGET_CONTRACTOR_ID)" else TARGET_CONTRACTOR_ID="null" echo -e "${YELLOW}⚠ WARNING${NC}: No contractors in target department, will swap without contractor" fi echo "" # Step 5: Check for existing active swaps echo "Step 5: Checking for existing active swaps..." EXISTING_SWAPS=$(curl -s -X GET "$BASE_URL/employee-swaps?status=Active" \ -H "Authorization: Bearer $TOKEN") ACTIVE_SWAP_FOR_EMPLOYEE=$(echo $EXISTING_SWAPS | jq --argjson empId "$FIRST_EMPLOYEE_ID" '[.[] | select(.employee_id == $empId)][0]') if [ "$ACTIVE_SWAP_FOR_EMPLOYEE" != "null" ]; then EXISTING_SWAP_ID=$(echo $ACTIVE_SWAP_FOR_EMPLOYEE | jq '.id') echo -e "${YELLOW}⚠ WARNING${NC}: Employee already has active swap (ID: $EXISTING_SWAP_ID), cancelling it first..." CANCEL_RESPONSE=$(curl -s -X PUT "$BASE_URL/employee-swaps/$EXISTING_SWAP_ID/cancel" \ -H "Authorization: Bearer $TOKEN") echo " Cancelled existing swap" fi print_result 0 "No blocking active swaps" echo "" # Step 6: Create a new employee swap echo "Step 6: Creating employee swap..." TODAY=$(date +%Y-%m-%d) SWAP_DATA="{ \"employeeId\": $FIRST_EMPLOYEE_ID, \"targetDepartmentId\": $TARGET_DEPT_ID, \"targetContractorId\": $TARGET_CONTRACTOR_ID, \"swapReason\": \"FinishedEarly\", \"reasonDetails\": \"Test swap from backend test script\", \"workCompletionPercentage\": 75, \"swapDate\": \"$TODAY\" }" CREATE_RESPONSE=$(curl -s -X POST "$BASE_URL/employee-swaps" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d "$SWAP_DATA") SWAP_ID=$(echo $CREATE_RESPONSE | jq '.id') if [ "$SWAP_ID" != "null" ] && [ -n "$SWAP_ID" ]; then print_result 0 "Swap created successfully (ID: $SWAP_ID)" echo " Employee: $(echo $CREATE_RESPONSE | jq -r '.employee_name')" echo " From: $(echo $CREATE_RESPONSE | jq -r '.original_department_name') → To: $(echo $CREATE_RESPONSE | jq -r '.target_department_name')" echo " Status: $(echo $CREATE_RESPONSE | jq -r '.status')" else print_result 1 "Failed to create swap" echo " Response: $CREATE_RESPONSE" exit 1 fi echo "" # Step 7: Verify employee's department was updated echo "Step 7: Verifying employee's department was updated..." UPDATED_EMPLOYEE=$(curl -s -X GET "$BASE_URL/users/$FIRST_EMPLOYEE_ID" \ -H "Authorization: Bearer $TOKEN") NEW_DEPT_ID=$(echo $UPDATED_EMPLOYEE | jq '.department_id') NEW_CONTRACTOR_ID=$(echo $UPDATED_EMPLOYEE | jq '.contractor_id') if [ "$NEW_DEPT_ID" == "$TARGET_DEPT_ID" ]; then print_result 0 "Employee department updated correctly" echo " New department_id: $NEW_DEPT_ID (was: $FIRST_EMPLOYEE_DEPT)" echo " New contractor_id: $NEW_CONTRACTOR_ID (was: $FIRST_EMPLOYEE_CONTRACTOR)" else print_result 1 "Employee department NOT updated" echo " Expected: $TARGET_DEPT_ID, Got: $NEW_DEPT_ID" fi echo "" # Step 8: Get swap by ID echo "Step 8: Fetching swap by ID..." GET_SWAP_RESPONSE=$(curl -s -X GET "$BASE_URL/employee-swaps/$SWAP_ID" \ -H "Authorization: Bearer $TOKEN") FETCHED_SWAP_ID=$(echo $GET_SWAP_RESPONSE | jq '.id') if [ "$FETCHED_SWAP_ID" == "$SWAP_ID" ]; then print_result 0 "Swap fetched successfully" else print_result 1 "Failed to fetch swap" echo " Response: $GET_SWAP_RESPONSE" fi echo "" # Step 9: Complete the swap (return employee to original department) echo "Step 9: Completing swap (returning employee to original department)..." COMPLETE_RESPONSE=$(curl -s -X PUT "$BASE_URL/employee-swaps/$SWAP_ID/complete" \ -H "Authorization: Bearer $TOKEN") COMPLETED_STATUS=$(echo $COMPLETE_RESPONSE | jq -r '.status') if [ "$COMPLETED_STATUS" == "Completed" ]; then print_result 0 "Swap completed successfully" echo " Status: $COMPLETED_STATUS" else print_result 1 "Failed to complete swap" echo " Response: $COMPLETE_RESPONSE" fi echo "" # Step 10: Verify employee returned to original department echo "Step 10: Verifying employee returned to original department..." FINAL_EMPLOYEE=$(curl -s -X GET "$BASE_URL/users/$FIRST_EMPLOYEE_ID" \ -H "Authorization: Bearer $TOKEN") FINAL_DEPT_ID=$(echo $FINAL_EMPLOYEE | jq '.department_id') FINAL_CONTRACTOR_ID=$(echo $FINAL_EMPLOYEE | jq '.contractor_id') if [ "$FINAL_DEPT_ID" == "$FIRST_EMPLOYEE_DEPT" ]; then print_result 0 "Employee returned to original department" echo " Final department_id: $FINAL_DEPT_ID" echo " Final contractor_id: $FINAL_CONTRACTOR_ID" else print_result 1 "Employee NOT returned to original department" echo " Expected: $FIRST_EMPLOYEE_DEPT, Got: $FINAL_DEPT_ID" fi echo "" # Step 11: Test swap cancellation echo "Step 11: Testing swap cancellation..." # Create another swap CREATE_RESPONSE2=$(curl -s -X POST "$BASE_URL/employee-swaps" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d "$SWAP_DATA") SWAP_ID2=$(echo $CREATE_RESPONSE2 | jq '.id') if [ "$SWAP_ID2" != "null" ]; then echo " Created test swap (ID: $SWAP_ID2)" # Cancel it CANCEL_RESPONSE=$(curl -s -X PUT "$BASE_URL/employee-swaps/$SWAP_ID2/cancel" \ -H "Authorization: Bearer $TOKEN") CANCELLED_STATUS=$(echo $CANCEL_RESPONSE | jq -r '.status') if [ "$CANCELLED_STATUS" == "Cancelled" ]; then print_result 0 "Swap cancelled successfully" else print_result 1 "Failed to cancel swap" echo " Response: $CANCEL_RESPONSE" fi else print_result 1 "Failed to create swap for cancellation test" fi echo "" # Summary echo "========================================" echo " Test Summary" echo "========================================" echo -e "${GREEN}All employee swap backend tests completed!${NC}" echo "" echo "Tested operations:" echo " ✓ Login as SuperAdmin" echo " ✓ Fetch employees and departments" echo " ✓ Create employee swap" echo " ✓ Verify employee department/contractor update" echo " ✓ Fetch swap by ID" echo " ✓ Complete swap (return to original)" echo " ✓ Cancel swap" echo ""