mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 07:13:12 +00:00
Expose completedDate on PATCH /workorders/{id}/board so CompDoc can leave legacy EditWorkorder. Allow optional media category on upload, PATCH category afterward, and enforce JPG/PNG/MP4/MOV allowlist (SH-116).
199 lines
8.2 KiB
C#
199 lines
8.2 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Data.SeaHavenIndustries.Enums;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Exceptions;
|
|
using SeaHaven.Services.Helpers;
|
|
using SeaHaven.Services.Interfaces;
|
|
|
|
namespace SeaHaven.Services.Implementation
|
|
{
|
|
public class WorkOrderMediaService : IWorkOrderMediaService
|
|
{
|
|
private readonly IWorkOrderMediaDataService _mediaData;
|
|
private readonly IWorkOrderDetailDataService _detailData;
|
|
|
|
public WorkOrderMediaService(IWorkOrderMediaDataService mediaData, IWorkOrderDetailDataService detailData)
|
|
{
|
|
_mediaData = mediaData;
|
|
_detailData = detailData;
|
|
}
|
|
|
|
public async Task<IReadOnlyList<WorkOrderMediaFileDto>?> GetMediaAsync(int workOrderId)
|
|
{
|
|
if (!await _detailData.ExistsAsync(workOrderId))
|
|
return null;
|
|
|
|
var workOrder = await _detailData.GetWorkOrderForMediaAsync(workOrderId);
|
|
if (workOrder == null)
|
|
return null;
|
|
|
|
var attachments = await _detailData.GetAttachmentsAsync(workOrderId);
|
|
return WorkOrderMediaProjection.ProjectAll(workOrder, attachments);
|
|
}
|
|
|
|
public async Task<WorkOrderMediaFileDto> AddMediaAsync(
|
|
int workOrderId,
|
|
WorkOrderMediaCategory? category,
|
|
string fileUrl,
|
|
string? actorId)
|
|
{
|
|
var resolvedCategory = category ?? WorkOrderMediaCategory.Extra;
|
|
|
|
if (!await _detailData.ExistsAsync(workOrderId))
|
|
throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
|
|
var workOrder = await _mediaData.GetTrackedWorkOrderAsync(workOrderId, CancellationToken.None);
|
|
if (workOrder == null)
|
|
throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
|
|
if (WorkOrderBoardMutationRules.IsReadOnly(workOrder.LifecycleStatus))
|
|
throw new WorkOrderBoardValidationException("ReadOnly", "Work order is read-only in its current status.");
|
|
|
|
if (resolvedCategory == WorkOrderMediaCategory.Completion)
|
|
{
|
|
throw new WorkOrderBoardValidationException(
|
|
"UseCompletionDocEndpoint",
|
|
"Completion documents must be uploaded via POST /api/workorders/{id}/completion-doc.");
|
|
}
|
|
|
|
if (resolvedCategory == WorkOrderMediaCategory.Before)
|
|
{
|
|
workOrder.BeforPhotoAttachment = fileUrl;
|
|
await _mediaData.SaveAsync(CancellationToken.None);
|
|
return new WorkOrderMediaFileDto
|
|
{
|
|
Id = -1,
|
|
Category = resolvedCategory,
|
|
Url = fileUrl,
|
|
IsLegacy = true
|
|
};
|
|
}
|
|
|
|
if (resolvedCategory == WorkOrderMediaCategory.After)
|
|
{
|
|
workOrder.AfterPhotoAttachment = fileUrl;
|
|
await _mediaData.SaveAsync(CancellationToken.None);
|
|
return new WorkOrderMediaFileDto
|
|
{
|
|
Id = -2,
|
|
Category = resolvedCategory,
|
|
Url = fileUrl,
|
|
IsLegacy = true
|
|
};
|
|
}
|
|
|
|
var attachment = new WorkOrderAttachments
|
|
{
|
|
WorkorderId = workOrderId,
|
|
Attachments = fileUrl,
|
|
Category = category.HasValue ? resolvedCategory : null,
|
|
CreatedDate = DateTime.UtcNow,
|
|
createdby = actorId
|
|
};
|
|
|
|
_mediaData.TrackAttachment(attachment);
|
|
await _mediaData.SaveAsync(CancellationToken.None);
|
|
|
|
return new WorkOrderMediaFileDto
|
|
{
|
|
Id = attachment.Id,
|
|
Category = attachment.Category ?? WorkOrderMediaCategory.Extra,
|
|
Url = fileUrl,
|
|
UploadedAt = attachment.CreatedDate?.ToUniversalTime().ToString("o"),
|
|
IsLegacy = false
|
|
};
|
|
}
|
|
|
|
public async Task<WorkOrderMediaFileDto> UpdateMediaCategoryAsync(
|
|
int workOrderId,
|
|
int mediaId,
|
|
WorkOrderMediaCategory category,
|
|
string? actorId)
|
|
{
|
|
if (mediaId <= 0)
|
|
throw new WorkOrderBoardValidationException("InvalidMedia", "Legacy media cannot be categorized via this endpoint.");
|
|
|
|
if (category == WorkOrderMediaCategory.Completion)
|
|
{
|
|
throw new WorkOrderBoardValidationException(
|
|
"UseCompletionDocEndpoint",
|
|
"Completion documents must be uploaded via POST /api/workorders/{id}/completion-doc.");
|
|
}
|
|
|
|
if (!await _detailData.ExistsAsync(workOrderId))
|
|
throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
|
|
var workOrder = await _mediaData.GetTrackedWorkOrderAsync(workOrderId, CancellationToken.None);
|
|
if (workOrder == null)
|
|
throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
|
|
if (WorkOrderBoardMutationRules.IsReadOnly(workOrder.LifecycleStatus))
|
|
throw new WorkOrderBoardValidationException("ReadOnly", "Work order is read-only in its current status.");
|
|
|
|
var attachment = await _mediaData.GetTrackedAttachmentAsync(mediaId, workOrderId, CancellationToken.None);
|
|
if (attachment == null)
|
|
throw new WorkOrderBoardValidationException("NotFound", "Media not found.");
|
|
|
|
if (category == WorkOrderMediaCategory.Before || category == WorkOrderMediaCategory.After)
|
|
{
|
|
var url = attachment.Attachments ?? "";
|
|
if (category == WorkOrderMediaCategory.Before)
|
|
workOrder.BeforPhotoAttachment = url;
|
|
else
|
|
workOrder.AfterPhotoAttachment = url;
|
|
|
|
attachment.IsDeleted = true;
|
|
attachment.DeletionTime = DateTime.UtcNow;
|
|
attachment.DeleterUserId = actorId;
|
|
await _mediaData.SaveAsync(CancellationToken.None);
|
|
|
|
return new WorkOrderMediaFileDto
|
|
{
|
|
Id = category == WorkOrderMediaCategory.Before ? -1 : -2,
|
|
Category = category,
|
|
Url = url,
|
|
IsLegacy = true
|
|
};
|
|
}
|
|
|
|
attachment.Category = category;
|
|
await _mediaData.SaveAsync(CancellationToken.None);
|
|
|
|
return new WorkOrderMediaFileDto
|
|
{
|
|
Id = attachment.Id,
|
|
Category = attachment.Category ?? WorkOrderMediaCategory.Extra,
|
|
Url = attachment.Attachments ?? "",
|
|
UploadedAt = attachment.CreatedDate?.ToUniversalTime().ToString("o"),
|
|
IsLegacy = false
|
|
};
|
|
}
|
|
|
|
public async Task DeleteMediaAsync(int workOrderId, int mediaId, string? actorId)
|
|
{
|
|
if (mediaId <= 0)
|
|
throw new WorkOrderBoardValidationException("InvalidMedia", "Legacy media cannot be deleted via this endpoint.");
|
|
|
|
if (!await _detailData.ExistsAsync(workOrderId))
|
|
throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
|
|
var workOrder = await _mediaData.GetTrackedWorkOrderAsync(workOrderId, CancellationToken.None);
|
|
if (workOrder == null)
|
|
throw new WorkOrderBoardValidationException("NotFound", "Work order not found.");
|
|
|
|
if (WorkOrderBoardMutationRules.IsReadOnly(workOrder.LifecycleStatus))
|
|
throw new WorkOrderBoardValidationException("ReadOnly", "Work order is read-only in its current status.");
|
|
|
|
var attachment = await _mediaData.GetTrackedAttachmentAsync(mediaId, workOrderId, CancellationToken.None);
|
|
|
|
if (attachment == null)
|
|
throw new WorkOrderBoardValidationException("NotFound", "Media not found.");
|
|
|
|
attachment.IsDeleted = true;
|
|
attachment.DeletionTime = DateTime.UtcNow;
|
|
attachment.DeleterUserId = actorId;
|
|
await _mediaData.SaveAsync(CancellationToken.None);
|
|
}
|
|
}
|
|
}
|