Merge branch 'Approve_Task_Feature' of https://git.marcoaiot.com/admin/marco.pms.api into Approve_Task_Feature
This commit is contained in:
commit
692b1bfef3
@ -693,44 +693,21 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
||||||
}
|
}
|
||||||
var response = await _masterHelper.CreateWorkStatus(createWorkStatusDto);
|
var response = await _masterHelper.CreateWorkStatus(createWorkStatusDto);
|
||||||
if (response.StatusCode == 200)
|
return StatusCode(response.StatusCode, response);
|
||||||
{
|
|
||||||
return Ok(response);
|
|
||||||
}
|
|
||||||
else if (response.StatusCode == 409)
|
|
||||||
{
|
|
||||||
return Conflict(response);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return BadRequest(response);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("work-status/edit/{id}")]
|
[HttpPost("work-status/edit/{id}")]
|
||||||
public async Task<IActionResult> UpdateWorkStatusMaster(Guid id, [FromBody] UpdateWorkStatusMasterDto updateWorkStatusDto)
|
public async Task<IActionResult> UpdateWorkStatusMaster(Guid id, [FromBody] UpdateWorkStatusMasterDto updateWorkStatusDto)
|
||||||
{
|
{
|
||||||
var response = await _masterHelper.UpdateWorkStatus(id, updateWorkStatusDto);
|
var response = await _masterHelper.UpdateWorkStatus(id, updateWorkStatusDto);
|
||||||
if (response.StatusCode == 200)
|
return StatusCode(response.StatusCode, response);
|
||||||
{
|
|
||||||
return Ok(response);
|
|
||||||
}
|
|
||||||
else if (response.StatusCode == 404)
|
|
||||||
{
|
|
||||||
return NotFound(response);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return BadRequest(response);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("work-status/{id}")]
|
[HttpDelete("work-status/{id}")]
|
||||||
public async Task<IActionResult> DeleteWorkStatusMaster(Guid id)
|
public async Task<IActionResult> DeleteWorkStatusMaster(Guid id)
|
||||||
{
|
{
|
||||||
var response = await _masterHelper.DeleteWorkStatus(id);
|
var response = await _masterHelper.DeleteWorkStatus(id);
|
||||||
return Ok(response);
|
return StatusCode(response.StatusCode, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------- Contact Category --------------------------------
|
// -------------------------------- Contact Category --------------------------------
|
||||||
|
@ -3,6 +3,7 @@ using Marco.Pms.Model.Directory;
|
|||||||
using Marco.Pms.Model.Dtos.Master;
|
using Marco.Pms.Model.Dtos.Master;
|
||||||
using Marco.Pms.Model.Employees;
|
using Marco.Pms.Model.Employees;
|
||||||
using Marco.Pms.Model.Mapper;
|
using Marco.Pms.Model.Mapper;
|
||||||
|
using Marco.Pms.Model.Master;
|
||||||
using Marco.Pms.Model.Utilities;
|
using Marco.Pms.Model.Utilities;
|
||||||
using Marco.Pms.Model.ViewModels.Master;
|
using Marco.Pms.Model.ViewModels.Master;
|
||||||
using Marco.Pms.Services.Service;
|
using Marco.Pms.Services.Service;
|
||||||
@ -257,38 +258,212 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
// -------------------------------- Work Status --------------------------------
|
// -------------------------------- Work Status --------------------------------
|
||||||
public async Task<ApiResponse<object>> GetWorkStatusList()
|
public async Task<ApiResponse<object>> GetWorkStatusList()
|
||||||
{
|
{
|
||||||
Guid tenantId = _userHelper.GetTenantId();
|
_logger.LogInfo("GetWorkStatusList called.");
|
||||||
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
||||||
bool hasViewPermission = await _permissionService.HasPermission(View_Master, LoggedInEmployee.Id);
|
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Step 1: Get tenant and logged-in employee info
|
||||||
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
|
||||||
|
// Step 2: Check permission to view master data
|
||||||
|
bool hasViewPermission = await _permissionService.HasPermission(View_Master, loggedInEmployee.Id);
|
||||||
if (!hasViewPermission)
|
if (!hasViewPermission)
|
||||||
{
|
{
|
||||||
|
_logger.LogWarning("Access denied for employeeId: {EmployeeId}", loggedInEmployee.Id);
|
||||||
return ApiResponse<object>.ErrorResponse("You don't have access", "Don't have access to take action", 403);
|
return ApiResponse<object>.ErrorResponse("You don't have access", "Don't have access to take action", 403);
|
||||||
}
|
}
|
||||||
var workStatus = await _context.WorkStatusMasters.Where(ws => ws.TenantId == tenantId).Select(ws => new { ws.Id, ws.Name, ws.Description, ws.IsSystem }).ToListAsync();
|
|
||||||
|
|
||||||
return ApiResponse<object>.SuccessResponse(workStatus, $"{workStatus.Count} number of work status fetched successfully", 200);
|
// Step 3: Fetch work statuses for the tenant
|
||||||
|
var workStatusList = await _context.WorkStatusMasters
|
||||||
|
.Where(ws => ws.TenantId == tenantId)
|
||||||
|
.Select(ws => new
|
||||||
|
{
|
||||||
|
ws.Id,
|
||||||
|
ws.Name,
|
||||||
|
ws.Description,
|
||||||
|
ws.IsSystem
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
_logger.LogInfo("{Count} work statuses fetched for tenantId: {TenantId}", workStatusList.Count, tenantId);
|
||||||
|
|
||||||
|
// Step 4: Return successful response
|
||||||
|
return ApiResponse<object>.SuccessResponse(
|
||||||
|
workStatusList,
|
||||||
|
$"{workStatusList.Count} work status records fetched successfully",
|
||||||
|
200
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error occurred while fetching work status list : {Error}", ex.Message);
|
||||||
|
return ApiResponse<object>.ErrorResponse("An error occurred", "Unable to fetch work status list", 500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public async Task<ApiResponse<object>> CreateWorkStatus(CreateWorkStatusMasterDto createWorkStatusDto)
|
public async Task<ApiResponse<object>> CreateWorkStatus(CreateWorkStatusMasterDto createWorkStatusDto)
|
||||||
{
|
{
|
||||||
|
_logger.LogInfo("CreateWorkStatus called with Name: {Name}", createWorkStatusDto.Name ?? "");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Step 1: Get tenant and logged-in employee
|
||||||
Guid tenantId = _userHelper.GetTenantId();
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
|
||||||
|
// Step 2: Check if user has permission to manage master data
|
||||||
|
var hasManageMasterPermission = await _permissionService.HasPermission(Manage_Master, loggedInEmployee.Id);
|
||||||
|
if (!hasManageMasterPermission)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Access denied for employeeId: {EmployeeId}", loggedInEmployee.Id);
|
||||||
return ApiResponse<object>.ErrorResponse("You don't have access", "Don't have access to take action", 403);
|
return ApiResponse<object>.ErrorResponse("You don't have access", "Don't have access to take action", 403);
|
||||||
}
|
}
|
||||||
public async Task<ApiResponse<object>> UpdateWorkStatus(Guid id, UpdateWorkStatusMasterDto contacupdateWorkStatusDtotTagDto)
|
|
||||||
|
// Step 3: Check if work status with the same name already exists
|
||||||
|
var existingWorkStatus = await _context.WorkStatusMasters
|
||||||
|
.FirstOrDefaultAsync(ws => ws.Name == createWorkStatusDto.Name && ws.TenantId == tenantId);
|
||||||
|
|
||||||
|
if (existingWorkStatus != null)
|
||||||
{
|
{
|
||||||
var tenantId = _userHelper.GetTenantId();
|
_logger.LogWarning("Work status already exists: {Name}", createWorkStatusDto.Name ?? "");
|
||||||
Employee LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
return ApiResponse<object>.ErrorResponse("Work status already exists", "Work status already exists", 400);
|
||||||
return ApiResponse<object>.ErrorResponse("You don't have access", "Don't have access to take action", 403);
|
}
|
||||||
|
|
||||||
|
// Step 4: Create new WorkStatusMaster entry
|
||||||
|
var workStatus = new WorkStatusMaster
|
||||||
|
{
|
||||||
|
Name = createWorkStatusDto.Name?.Trim() ?? "",
|
||||||
|
Description = createWorkStatusDto.Description?.Trim() ?? "",
|
||||||
|
IsSystem = false,
|
||||||
|
TenantId = tenantId
|
||||||
|
};
|
||||||
|
|
||||||
|
_context.WorkStatusMasters.Add(workStatus);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInfo("Work status created successfully: {Id}, Name: {Name}", workStatus.Id, workStatus.Name);
|
||||||
|
return ApiResponse<object>.SuccessResponse(workStatus, "Work status created successfully", 200);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error occurred while creating work status : {Error}", ex.Message);
|
||||||
|
return ApiResponse<object>.ErrorResponse("An error occurred", "Unable to create work status", 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public async Task<ApiResponse<object>> UpdateWorkStatus(Guid id, UpdateWorkStatusMasterDto updateWorkStatusDto)
|
||||||
|
{
|
||||||
|
_logger.LogInfo("UpdateWorkStatus called for WorkStatus ID: {Id}, New Name: {Name}", id, updateWorkStatusDto.Name ?? "");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Step 1: Validate input
|
||||||
|
if (id == Guid.Empty || id != updateWorkStatusDto.Id)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Invalid ID provided for update. Route ID: {RouteId}, DTO ID: {DtoId}", id, updateWorkStatusDto.Id);
|
||||||
|
return ApiResponse<object>.ErrorResponse("Invalid data provided", "The provided work status ID is invalid", 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Get tenant and logged-in employee
|
||||||
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
|
||||||
|
// Step 3: Check permissions
|
||||||
|
var hasManageMasterPermission = await _permissionService.HasPermission(Manage_Master, loggedInEmployee.Id);
|
||||||
|
if (!hasManageMasterPermission)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Access denied. EmployeeId: {EmployeeId} does not have Manage Master permission.", loggedInEmployee.Id);
|
||||||
|
return ApiResponse<object>.ErrorResponse("Access denied", "You do not have permission to update this work status", 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 4: Retrieve the work status record
|
||||||
|
var workStatus = await _context.WorkStatusMasters
|
||||||
|
.FirstOrDefaultAsync(ws => ws.Id == id && ws.TenantId == tenantId);
|
||||||
|
|
||||||
|
if (workStatus == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Work status not found for ID: {Id}", id);
|
||||||
|
return ApiResponse<object>.ErrorResponse("Work status not found", "No work status found with the provided ID", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 5: Check for duplicate name (optional)
|
||||||
|
var isDuplicate = await _context.WorkStatusMasters
|
||||||
|
.AnyAsync(ws => ws.Name == updateWorkStatusDto.Name && ws.Id != id && ws.TenantId == tenantId);
|
||||||
|
|
||||||
|
if (isDuplicate)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Duplicate work status name '{Name}' detected during update. ID: {Id}", updateWorkStatusDto.Name ?? "", id);
|
||||||
|
return ApiResponse<object>.ErrorResponse("Work status with the same name already exists", "Duplicate name", 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 6: Update fields
|
||||||
|
workStatus.Name = updateWorkStatusDto.Name?.Trim() ?? "";
|
||||||
|
workStatus.Description = updateWorkStatusDto.Description?.Trim() ?? "";
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInfo("Work status updated successfully. ID: {Id}", id);
|
||||||
|
return ApiResponse<object>.SuccessResponse(workStatus, "Work status updated successfully", 200);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error occurred while updating work status ID: {Id} : {Error}", id, ex.Message);
|
||||||
|
return ApiResponse<object>.ErrorResponse("An error occurred", "Unable to update the work status at this time", 500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public async Task<ApiResponse<object>> DeleteWorkStatus(Guid id)
|
public async Task<ApiResponse<object>> DeleteWorkStatus(Guid id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInfo("DeleteWorkStatus called for Id: {Id}", id);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Step 1: Get current tenant and logged-in employee
|
||||||
Guid tenantId = _userHelper.GetTenantId();
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
return ApiResponse<object>.ErrorResponse("You don't have access", "Don't have access to take action", 403);
|
|
||||||
|
|
||||||
|
// Step 2: Check permission to manage master data
|
||||||
|
var hasManageMasterPermission = await _permissionService.HasPermission(Manage_Master, loggedInEmployee.Id);
|
||||||
|
if (!hasManageMasterPermission)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Delete denied. EmployeeId: {EmployeeId} lacks Manage_Master permission.", loggedInEmployee.Id);
|
||||||
|
return ApiResponse<object>.ErrorResponse("You don't have access", "Access denied for deleting work status", 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Step 3: Find the work status
|
||||||
|
var workStatus = await _context.WorkStatusMasters
|
||||||
|
.FirstOrDefaultAsync(ws => ws.Id == id && ws.TenantId == tenantId);
|
||||||
|
|
||||||
|
if (workStatus == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Work status not found for Id: {Id}", id);
|
||||||
|
return ApiResponse<object>.ErrorResponse("Work status not found", "Work status not found", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 4: Check for dependencies in TaskAllocations
|
||||||
|
bool hasDependency = await _context.TaskAllocations
|
||||||
|
.AnyAsync(ta => ta.TenantId == tenantId && ta.WorkStatusId == id);
|
||||||
|
|
||||||
|
if (hasDependency)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Cannot delete WorkStatus Id: {Id} due to existing task dependency", id);
|
||||||
|
return ApiResponse<object>.ErrorResponse(
|
||||||
|
"Work status has a dependency in assigned tasks and cannot be deleted",
|
||||||
|
"Deletion failed due to associated tasks",
|
||||||
|
400
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 5: Delete and persist
|
||||||
|
_context.WorkStatusMasters.Remove(workStatus);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInfo("Work status deleted successfully. Id: {Id}", id);
|
||||||
|
return ApiResponse<object>.SuccessResponse(new { }, "Work status deleted successfully", 200);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error occurred while deleting WorkStatus Id: {Id} : {Error}", id, ex.Message);
|
||||||
|
return ApiResponse<object>.ErrorResponse("An error occurred", "Unable to delete work status", 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user