Optmized the work status master APIs
This commit is contained in:
parent
2f6031e62c
commit
d07f0311ae
@ -2,7 +2,7 @@
|
||||
{
|
||||
public class CreateWorkStatusMasterDto
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public required string Name { get; set; }
|
||||
public required string Description { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
public class UpdateWorkStatusMasterDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public required string Name { get; set; }
|
||||
public required string Description { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -1863,9 +1863,9 @@ namespace Marco.Pms.Services.Service
|
||||
return ApiResponse<object>.ErrorResponse("An error occurred", "Unable to fetch work status list", 500);
|
||||
}
|
||||
}
|
||||
public async Task<ApiResponse<object>> CreateWorkStatus(CreateWorkStatusMasterDto createWorkStatusDto, Employee loggedInEmployee, Guid tenantId)
|
||||
public async Task<ApiResponse<object>> CreateWorkStatus(CreateWorkStatusMasterDto model, Employee loggedInEmployee, Guid tenantId)
|
||||
{
|
||||
_logger.LogInfo("CreateWorkStatus called with Name: {Name}", createWorkStatusDto.Name ?? "");
|
||||
_logger.LogInfo("CreateWorkStatus called with Name: {Name}", model.Name);
|
||||
|
||||
try
|
||||
{
|
||||
@ -1880,19 +1880,19 @@ namespace Marco.Pms.Services.Service
|
||||
|
||||
// Step 2: Check if work status with the same name already exists
|
||||
var existingWorkStatus = await _context.WorkStatusMasters
|
||||
.FirstOrDefaultAsync(ws => ws.Name == createWorkStatusDto.Name && ws.TenantId == tenantId);
|
||||
.FirstOrDefaultAsync(ws => ws.Name == model.Name && ws.TenantId == tenantId);
|
||||
|
||||
if (existingWorkStatus != null)
|
||||
{
|
||||
_logger.LogWarning("Work status already exists: {Name}", createWorkStatusDto.Name ?? "");
|
||||
_logger.LogWarning("Work status already exists: {Name}", model.Name);
|
||||
return ApiResponse<object>.ErrorResponse("Work status already exists", "Work status already exists", 400);
|
||||
}
|
||||
|
||||
// Step 3: Create new WorkStatusMaster entry
|
||||
var workStatus = new WorkStatusMaster
|
||||
{
|
||||
Name = createWorkStatusDto.Name?.Trim() ?? "",
|
||||
Description = createWorkStatusDto.Description?.Trim() ?? "",
|
||||
Name = model.Name.Trim(),
|
||||
Description = model.Description.Trim(),
|
||||
IsSystem = false,
|
||||
TenantId = tenantId
|
||||
};
|
||||
@ -1909,16 +1909,16 @@ namespace Marco.Pms.Services.Service
|
||||
return ApiResponse<object>.ErrorResponse("An error occurred", "Unable to create work status", 500);
|
||||
}
|
||||
}
|
||||
public async Task<ApiResponse<object>> UpdateWorkStatus(Guid id, UpdateWorkStatusMasterDto updateWorkStatusDto, Employee loggedInEmployee, Guid tenantId)
|
||||
public async Task<ApiResponse<object>> UpdateWorkStatus(Guid id, UpdateWorkStatusMasterDto model, Employee loggedInEmployee, Guid tenantId)
|
||||
{
|
||||
_logger.LogInfo("UpdateWorkStatus called for WorkStatus ID: {Id}, New Name: {Name}", id, updateWorkStatusDto.Name ?? "");
|
||||
_logger.LogInfo("UpdateWorkStatus called for WorkStatus ID: {Id}, New Name: {Name}", id, model.Name);
|
||||
|
||||
try
|
||||
{
|
||||
// Step 1: Validate input
|
||||
if (id == Guid.Empty || id != updateWorkStatusDto.Id)
|
||||
if (id == Guid.Empty || id != model.Id)
|
||||
{
|
||||
_logger.LogWarning("Invalid ID provided for update. Route ID: {RouteId}, DTO ID: {DtoId}", id, updateWorkStatusDto.Id);
|
||||
_logger.LogWarning("Invalid ID provided for update. Route ID: {RouteId}, DTO ID: {DtoId}", id, model.Id);
|
||||
return ApiResponse<object>.ErrorResponse("Invalid data provided", "The provided work status ID is invalid", 400);
|
||||
}
|
||||
|
||||
@ -1942,20 +1942,31 @@ namespace Marco.Pms.Services.Service
|
||||
|
||||
// Step 4: Check for duplicate name (optional)
|
||||
var isDuplicate = await _context.WorkStatusMasters
|
||||
.AnyAsync(ws => ws.Name == updateWorkStatusDto.Name && ws.Id != id && ws.TenantId == tenantId);
|
||||
.AnyAsync(ws => ws.Name == model.Name.Trim() && ws.Id != id && ws.TenantId == tenantId);
|
||||
|
||||
if (isDuplicate)
|
||||
{
|
||||
_logger.LogWarning("Duplicate work status name '{Name}' detected during update. ID: {Id}", updateWorkStatusDto.Name ?? "", id);
|
||||
_logger.LogWarning("Duplicate work status name '{Name}' detected during update. ID: {Id}", model.Name, id);
|
||||
return ApiResponse<object>.ErrorResponse("Work status with the same name already exists", "Duplicate name", 400);
|
||||
}
|
||||
|
||||
// Capture original state for audit log
|
||||
var existingEntityBson = _updateLogHelper.EntityToBsonDocument(workStatus);
|
||||
|
||||
// Step 5: Update fields
|
||||
workStatus.Name = updateWorkStatusDto.Name?.Trim() ?? "";
|
||||
workStatus.Description = updateWorkStatusDto.Description?.Trim() ?? "";
|
||||
workStatus.Name = model.Name.Trim();
|
||||
workStatus.Description = model.Description.Trim();
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
await _updateLogHelper.PushToUpdateLogsAsync(new UpdateLogsObject
|
||||
{
|
||||
EntityId = workStatus.Id.ToString(),
|
||||
UpdatedById = loggedInEmployee.Id.ToString(),
|
||||
OldObject = existingEntityBson,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
}, "WorkStatusMasterModificationLog");
|
||||
|
||||
_logger.LogInfo("Work status updated successfully. ID: {Id}", id);
|
||||
return ApiResponse<object>.SuccessResponse(workStatus, "Work status updated successfully", 200);
|
||||
}
|
||||
@ -2003,10 +2014,21 @@ namespace Marco.Pms.Services.Service
|
||||
);
|
||||
}
|
||||
|
||||
// Capture original state for audit log
|
||||
var existingEntityBson = _updateLogHelper.EntityToBsonDocument(workStatus);
|
||||
|
||||
// Step 5: Delete and persist
|
||||
_context.WorkStatusMasters.Remove(workStatus);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
await _updateLogHelper.PushToUpdateLogsAsync(new UpdateLogsObject
|
||||
{
|
||||
EntityId = workStatus.Id.ToString(),
|
||||
UpdatedById = loggedInEmployee.Id.ToString(),
|
||||
OldObject = existingEntityBson,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
}, "WorkStatusMasterModificationLog");
|
||||
|
||||
_logger.LogInfo("Work status deleted successfully. Id: {Id}", id);
|
||||
return ApiResponse<object>.SuccessResponse(new { }, "Work status deleted successfully", 200);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user