63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using Distribution.Infra.UoW;
|
|
|
|
namespace Distribution.Features.DriverComplianceConfig;
|
|
|
|
public class DriverComplianceConfigService(IDriverComplianceConfigRepository repo, IUnitOfWork uow)
|
|
{
|
|
public async Task<DriverComplianceConfigResponse?> GetAsync()
|
|
{
|
|
var config = await repo.GetAsync();
|
|
return config is null
|
|
? null
|
|
: new DriverComplianceConfigResponse(
|
|
config.Id,
|
|
config.EnforceLicenseExpiry,
|
|
config.EnforceMedicalClearance,
|
|
config.UpdatedAt,
|
|
config.UpdatedBy);
|
|
}
|
|
|
|
public async Task<DriverComplianceConfigResponse> UpsertAsync(UpdateDriverComplianceConfigRequest request, Guid updatedBy, CancellationToken ct = default)
|
|
{
|
|
var config = await repo.GetAsync();
|
|
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
if (config is null)
|
|
{
|
|
config = new global::Distribution.Models.DriverComplianceConfig
|
|
{
|
|
EnforceLicenseExpiry = request.EnforceLicenseExpiry,
|
|
EnforceMedicalClearance = request.EnforceMedicalClearance,
|
|
UpdatedBy = updatedBy,
|
|
UpdatedAt = DateTime.Now
|
|
};
|
|
await repo.AddAsync(config);
|
|
}
|
|
else
|
|
{
|
|
config.EnforceLicenseExpiry = request.EnforceLicenseExpiry;
|
|
config.EnforceMedicalClearance = request.EnforceMedicalClearance;
|
|
config.UpdatedBy = updatedBy;
|
|
config.UpdatedAt = DateTime.Now;
|
|
repo.Update(config);
|
|
}
|
|
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
|
|
return new DriverComplianceConfigResponse(
|
|
config.Id,
|
|
config.EnforceLicenseExpiry,
|
|
config.EnforceMedicalClearance,
|
|
config.UpdatedAt,
|
|
config.UpdatedBy);
|
|
}
|
|
}
|