adding clock in break and out command

This commit is contained in:
Tim G. | SnapixLP
2025-06-06 02:01:54 +02:00
parent 2645d698d7
commit 536de50e4c
6 changed files with 280 additions and 105 deletions

View File

@@ -170,6 +170,123 @@ public class CommandLibrary
return self.ToJson();
}
[Command("clock")]
public static string ClockCommand(string[] args, TcpClient? client, TcpServer? socket)
{
// 1) Argument check
if (args.Length < 1)
throw new CommandException("Missing argument: usage is clock <in|out|break>");
string desired = args[0].ToLowerInvariant();
int desiredState;
switch (desired)
{
case "in":
desiredState = 1;
break;
case "out":
desiredState = 0;
break;
case "break":
desiredState = 2;
break;
default:
throw new CommandException("Invalid argument. Use in, out, or break.");
}
// 2) Verify client/socket
if (client == null || socket == null)
throw new CommandException("No client connection detected.");
if (!socket.LoggedInClients.TryGetValue(client, out var employeeCode))
throw new CommandException("User not logged in.");
// 3) Look up the employee by Code to get Id and current state
var empParams = new Dictionary<string, object> { { "Code", employeeCode } };
string empResultJson = Program.mySql.Get("employees", empParams);
using var empDoc = JsonDocument.Parse(empResultJson);
bool empError = empDoc.RootElement.GetProperty("error").GetBoolean();
if (empError)
{
string dbMsg = empDoc.RootElement.GetProperty("data").GetString() ?? "Unknown DB error";
Program.messageSender.Error($"[ClockCommand] DB error when looking up employee: {dbMsg}");
throw new CommandException("Internal error while fetching employee data.");
}
var empArray = empDoc.RootElement.GetProperty("data");
if (empArray.GetArrayLength() == 0)
throw new CommandException("Logged-in user not found in database.");
var empRow = empArray[0];
int employeeId = empRow.GetProperty("Id").GetInt32();
int currentState = empRow.GetProperty("EmployeeState").GetInt32();
// 4) Validate state transition
if (currentState == desiredState)
throw new CommandException($"Already {(desiredState == 0 ? "OUT" : desiredState == 1 ? "IN" : "on BREAK")}.");
bool valid = false;
switch (currentState)
{
case 0: // OUT → only IN
valid = (desiredState == 1);
break;
case 1: // IN → OUT or BREAK
valid = (desiredState == 0 || desiredState == 2);
break;
case 2: // BREAK → only IN
valid = (desiredState == 1);
break;
}
if (!valid)
throw new CommandException("Invalid transition from current status.");
// 5) Update employees.EmployeeState to desiredState
var updateData = new Dictionary<string, object>
{
{ "EmployeeState", desiredState }
};
var whereData = new Dictionary<string, object>
{
{ "Id", employeeId }
};
string updateJson = Program.mySql.Update("employees", updateData, whereData);
using (var updateDoc = JsonDocument.Parse(updateJson))
{
bool updError = updateDoc.RootElement.GetProperty("error").GetBoolean();
if (updError)
{
string dbMsg = updateDoc.RootElement.GetProperty("data").GetString() ?? "Unknown DB error";
Program.messageSender.Error($"[ClockCommand] DB error when updating state: {dbMsg}");
throw new CommandException("Internal error while updating status.");
}
}
// 6) Insert into employee_state_history
var historyData = new Dictionary<string, object>
{
{ "EmployeeId", employeeId },
{ "NewState", desiredState }
};
string historyJson = Program.mySql.Insert("employee_state_history", historyData);
using (var histDoc = JsonDocument.Parse(historyJson))
{
bool histError = histDoc.RootElement.GetProperty("error").GetBoolean();
if (histError)
{
string dbMsg = histDoc.RootElement.GetProperty("data").GetString() ?? "Unknown DB error";
Program.messageSender.Error($"[ClockCommand] DB error when logging history: {dbMsg}");
throw new CommandException("Internal error while logging status change.");
}
}
// 7) Return success
return "success";
}
#endregion
#region Administration Commands