128 lines
4.8 KiB
C#
128 lines
4.8 KiB
C#
using Fitness_core_BE.Exceptions;
|
|
using Fitness_core_BE.Helpers;
|
|
using Fitness_core_BE.Interfaces.Services;
|
|
using Fitness_core_BE.Models;
|
|
using Fitness_core_BE.Models.IOs;
|
|
|
|
namespace Fitness_core_BE.Services.FunctionHandler
|
|
{
|
|
public class AccessDeviceFunctionHandler
|
|
{
|
|
public readonly Dictionary<string, Func<object, HttpContext, Task<object>>> _functionHandler;
|
|
|
|
public AccessDeviceFunctionHandler(IAccessDeviceService accessDeviceService)
|
|
{
|
|
// Admin-auth actions only. The cloud no longer talks to devices directly:
|
|
// - AddDevice / RemoveDevice / GeneratePairingCode are pure DB operations.
|
|
// - AddUser / WhiteListMember / BlackListMember do their local DB work and
|
|
// ENQUEUE a DeviceCommand for the on-prem agent to execute.
|
|
// Device communication (attendance pull, command execution, enrollment)
|
|
// is handled by the agent against the AgentApiKey-protected endpoints in AgentController.
|
|
_functionHandler = new()
|
|
{
|
|
{ "AddDevice", (payload, _) => accessDeviceService.AddDevice(payload) },
|
|
{ "RemoveDevice", (payload, _) => accessDeviceService.RemoveDevice(payload) },
|
|
{ "GeneratePairingCode", (payload, _) => accessDeviceService.GeneratePairingCode(payload) },
|
|
{ "AddUser", AddUser },
|
|
{ "WhiteListMember", WhiteListMember },
|
|
{ "BlackListMember", BlackListMember }
|
|
};
|
|
|
|
Task<object> AddUser(object payload, HttpContext _)
|
|
{
|
|
var data = DeserializePayload.Deserialize(payload);
|
|
var customerAccess = new CustomerAccess
|
|
{
|
|
Name = DataExtractor.GetRequiredString(data, "Name"),
|
|
FKDeviceId = DataExtractor.GetRequiredGuid(data, "FKDeviceId"),
|
|
FKCustomerId = DataExtractor.GetRequiredGuid(data, "FKCustomerId")
|
|
};
|
|
|
|
return accessDeviceService.AddUser(customerAccess);
|
|
}
|
|
|
|
Task<object> WhiteListMember(object payload, HttpContext _)
|
|
{
|
|
var data = DeserializePayload.Deserialize(payload);
|
|
var customerId = DataExtractor.GetRequiredGuid(data, "customerId");
|
|
return accessDeviceService.WhiteListMember(customerId).ContinueWith(t => (object)t.Result);
|
|
}
|
|
|
|
Task<object> BlackListMember(object payload, HttpContext _)
|
|
{
|
|
var data = DeserializePayload.Deserialize(payload);
|
|
var customerId = DataExtractor.GetRequiredGuid(data, "customerId");
|
|
return accessDeviceService.BlackListMember(customerId).ContinueWith(t => (object)t.Result);
|
|
}
|
|
}
|
|
|
|
public async Task<ApiResponse<object>> Execute(ApiRequest request, HttpContext httpContext)
|
|
{
|
|
if (!_functionHandler.TryGetValue(request.FunctionName, out var handler))
|
|
{
|
|
return new ApiResponse<object>
|
|
{
|
|
StatusCode = 404,
|
|
Message = $"Function '{request.FunctionName}' not found.",
|
|
Data = null!
|
|
};
|
|
}
|
|
|
|
try
|
|
{
|
|
var results = await handler(request.Payload, httpContext);
|
|
return new ApiResponse<object>
|
|
{
|
|
StatusCode = 200,
|
|
Message = "success",
|
|
Data = results
|
|
};
|
|
}
|
|
catch (UnauthorizedException ex)
|
|
{
|
|
return new ApiResponse<object>
|
|
{
|
|
StatusCode = 401,
|
|
Message = ex.Message,
|
|
Data = null!
|
|
};
|
|
}
|
|
catch (NotFoundException ex)
|
|
{
|
|
return new ApiResponse<object>
|
|
{
|
|
StatusCode = 404,
|
|
Message = ex.Message,
|
|
Data = null!
|
|
};
|
|
}
|
|
catch (BadRequestException ex)
|
|
{
|
|
return new ApiResponse<object>
|
|
{
|
|
StatusCode = 400,
|
|
Message = ex.Message,
|
|
Data = null!
|
|
};
|
|
}
|
|
catch (ForbiddenException ex)
|
|
{
|
|
return new ApiResponse<object>
|
|
{
|
|
StatusCode = 403,
|
|
Message = ex.Message,
|
|
Data = null!
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ApiResponse<object>
|
|
{
|
|
StatusCode = 500,
|
|
Message = $"An error occurred: {ex.Message}",
|
|
Data = null!
|
|
};
|
|
}
|
|
}
|
|
}
|
|
} |