Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSP-6954 disposition documents #3672

Merged
merged 10 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public IActionResult GetRelationshipDocuments(DocumentRelationType relationshipT
var mappedResearchFileDocuments = _mapper.Map<List<DocumentRelationshipModel>>(researchFileDocuments);
return new JsonResult(mappedResearchFileDocuments);
case DocumentRelationType.AcquisitionFiles:
var acquistionFileDocuments = _documentFileService.GetFileDocuments<PimsAcquisitionFileDocument>(FileType.Acquisition, long.Parse(parentId));
var mappedAcquisitionFileDocuments = _mapper.Map<List<DocumentRelationshipModel>>(acquistionFileDocuments);
var acquisitionFileDocuments = _documentFileService.GetFileDocuments<PimsAcquisitionFileDocument>(FileType.Acquisition, long.Parse(parentId));
var mappedAcquisitionFileDocuments = _mapper.Map<List<DocumentRelationshipModel>>(acquisitionFileDocuments);
return new JsonResult(mappedAcquisitionFileDocuments);
case DocumentRelationType.Templates:
var templateDocuments = _formDocumentService.GetFormDocumentTypes(parentId);
Expand All @@ -111,6 +111,10 @@ public IActionResult GetRelationshipDocuments(DocumentRelationType relationshipT
var managementDocuments = _documentFileService.GetFileDocuments<PimsPropertyActivityDocument>(FileType.Management, long.Parse(parentId));
var mappedPropertyActivityDocuments = _mapper.Map<List<DocumentRelationshipModel>>(managementDocuments);
return new JsonResult(mappedPropertyActivityDocuments);
case DocumentRelationType.DispositionFiles:
var dispositionFileDocuments = _documentFileService.GetFileDocuments<PimsDispositionFileDocument>(FileType.Disposition, long.Parse(parentId));
var mappedDispositionFileDocuments = _mapper.Map<List<DocumentRelationshipModel>>(dispositionFileDocuments);
return new JsonResult(mappedDispositionFileDocuments);
default:
throw new BadRequestException("Relationship type not valid for retrieve.");
}
Expand Down Expand Up @@ -141,6 +145,7 @@ public async Task<IActionResult> UploadDocumentWithParent(
DocumentRelationType.Projects => await _documentFileService.UploadProjectDocumentAsync(long.Parse(parentId), uploadRequest),
DocumentRelationType.Leases => await _documentFileService.UploadLeaseDocumentAsync(long.Parse(parentId), uploadRequest),
DocumentRelationType.ManagementFiles => await _documentFileService.UploadPropertyActivityDocumentAsync(long.Parse(parentId), uploadRequest),
DocumentRelationType.DispositionFiles => await _documentFileService.UploadDispositionDocumentAsync(long.Parse(parentId), uploadRequest),
_ => throw new BadRequestException("Relationship type not valid for upload."),
};

Expand Down Expand Up @@ -186,6 +191,10 @@ public async Task<IActionResult> DeleteDocumentRelationship(DocumentRelationType
var propertyActivityRelationship = _mapper.Map<PimsPropertyActivityDocument>(model);
var propertyActivityResult = await _documentFileService.DeletePropertyActivityDocumentAsync(propertyActivityRelationship);
return new JsonResult(propertyActivityResult);
case DocumentRelationType.DispositionFiles:
var dispositionRelationship = _mapper.Map<PimsDispositionFileDocument>(model);
var dispositionResult = await _documentFileService.DeleteDispositionDocumentAsync(dispositionRelationship);
return new JsonResult(dispositionResult);
default:
throw new BadRequestException("Relationship type not valid for delete.");
}
Expand Down
2 changes: 2 additions & 0 deletions source/backend/api/Constants/FileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public enum FileType
Lease,
[EnumMember(Value = "management")]
Management,
[EnumMember(Value = "disposition")]
Disposition,
[EnumMember(Value = "unknown")] // Used in tests/logic only. This does not correspond to a valid file type in the db.
Unknown,
}
Expand Down
78 changes: 65 additions & 13 deletions source/backend/api/Services/DocumentFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class DocumentFileService : BaseService, IDocumentFileService
private readonly IDocumentRepository _documentRepository;
private readonly ILeaseRepository _leaseRepository;
private readonly IPropertyActivityDocumentRepository _propertyActivityDocumentRepository;
private readonly IDispositionFileDocumentRepository _dispositionFileDocumentRepository;
private readonly IMapper mapper;

public DocumentFileService(
Expand All @@ -41,7 +42,8 @@ public DocumentFileService(
IProjectRepository projectRepository,
IDocumentRepository documentRepository,
ILeaseRepository leaseRepository,
IPropertyActivityDocumentRepository propertyActivityDocumentRepository)
IPropertyActivityDocumentRepository propertyActivityDocumentRepository,
IDispositionFileDocumentRepository dispositionFileDocumentRepository)
: base(user, logger)
{
this.acquisitionFileDocumentRepository = acquisitionFileDocumentRepository;
Expand All @@ -52,12 +54,13 @@ public DocumentFileService(
_documentRepository = documentRepository;
_leaseRepository = leaseRepository;
_propertyActivityDocumentRepository = propertyActivityDocumentRepository;
_dispositionFileDocumentRepository = dispositionFileDocumentRepository;
}

public IList<T> GetFileDocuments<T>(FileType fileType, long fileId)
where T : PimsFileDocument
{
this.Logger.LogInformation("Retrieving PIMS documents related to the file of type $fileType", fileType);
Logger.LogInformation("Retrieving PIMS documents related to the file of type $fileType", fileType);
this.User.ThrowIfNotAuthorized(Permissions.DocumentView);

switch (fileType)
Expand All @@ -77,6 +80,9 @@ public IList<T> GetFileDocuments<T>(FileType fileType, long fileId)
case FileType.Management:
this.User.ThrowIfNotAuthorized(Permissions.ManagementView);
return _propertyActivityDocumentRepository.GetAllByPropertyActivity(fileId).Select(f => f as T).ToArray();
case FileType.Disposition:
this.User.ThrowIfNotAuthorized(Permissions.DispositionView);
return _dispositionFileDocumentRepository.GetAllByDispositionFile(fileId).Select(f => f as T).ToArray();
default:
throw new BadRequestException("FileT type not valid to get documents.");
}
Expand All @@ -85,7 +91,7 @@ public IList<T> GetFileDocuments<T>(FileType fileType, long fileId)
public async Task<DocumentUploadRelationshipResponse> UploadResearchDocumentAsync(long researchFileId, DocumentUploadRequest uploadRequest)
{
this.Logger.LogInformation("Uploading document for single research file");
this.User.ThrowIfNotAuthorized(Permissions.DocumentAdd, Permissions.ResearchFileAdd);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentAdd, Permissions.ResearchFileEdit);

DocumentUploadResponse uploadResult = await documentService.UploadDocumentAsync(uploadRequest);

Expand Down Expand Up @@ -114,7 +120,7 @@ public async Task<DocumentUploadRelationshipResponse> UploadResearchDocumentAsyn
public async Task<DocumentUploadRelationshipResponse> UploadAcquisitionDocumentAsync(long acquisitionFileId, DocumentUploadRequest uploadRequest)
{
this.Logger.LogInformation("Uploading document for single acquisition file");
this.User.ThrowIfNotAuthorized(Permissions.DocumentAdd, Permissions.AcquisitionFileEdit);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentAdd, Permissions.AcquisitionFileEdit);

DocumentUploadResponse uploadResult = await documentService.UploadDocumentAsync(uploadRequest);

Expand Down Expand Up @@ -143,7 +149,7 @@ public async Task<DocumentUploadRelationshipResponse> UploadAcquisitionDocumentA
public async Task<DocumentUploadRelationshipResponse> UploadProjectDocumentAsync(long projectId, DocumentUploadRequest uploadRequest)
{
this.Logger.LogInformation("Uploading document for single Project");
this.User.ThrowIfNotAuthorized(Permissions.DocumentAdd, Permissions.ProjectEdit);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentAdd, Permissions.ProjectEdit);

DocumentUploadResponse uploadResult = await documentService.UploadDocumentAsync(uploadRequest);

Expand Down Expand Up @@ -171,7 +177,7 @@ public async Task<DocumentUploadRelationshipResponse> UploadProjectDocumentAsync
public async Task<DocumentUploadRelationshipResponse> UploadLeaseDocumentAsync(long leaseId, DocumentUploadRequest uploadRequest)
{
this.Logger.LogInformation("Uploading document for single Lease");
this.User.ThrowIfNotAuthorized(Permissions.DocumentAdd, Permissions.LeaseEdit);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentAdd, Permissions.LeaseEdit);

DocumentUploadResponse uploadResult = await documentService.UploadDocumentAsync(uploadRequest);

Expand Down Expand Up @@ -199,7 +205,7 @@ public async Task<DocumentUploadRelationshipResponse> UploadLeaseDocumentAsync(l
public async Task<DocumentUploadRelationshipResponse> UploadPropertyActivityDocumentAsync(long propertyActivityId, DocumentUploadRequest uploadRequest)
{
this.Logger.LogInformation("Uploading document for single Property Activity");
this.User.ThrowIfNotAuthorized(Permissions.DocumentAdd, Permissions.ManagementEdit);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentAdd, Permissions.ManagementEdit);

DocumentUploadResponse uploadResult = await documentService.UploadDocumentAsync(uploadRequest);

Expand All @@ -216,7 +222,35 @@ public async Task<DocumentUploadRelationshipResponse> UploadPropertyActivityDocu
DocumentId = uploadResult.Document.Id,
};
newDocument = _propertyActivityDocumentRepository.AddPropertyActivityDocument(newDocument);
_leaseRepository.CommitTransaction();
_propertyActivityDocumentRepository.CommitTransaction();

relationshipResponse.DocumentRelationship = mapper.Map<DocumentRelationshipModel>(newDocument);
}

return relationshipResponse;
}

public async Task<DocumentUploadRelationshipResponse> UploadDispositionDocumentAsync(long dispositionFileId, DocumentUploadRequest uploadRequest)
{
this.Logger.LogInformation("Uploading document for single disposition file");
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentAdd, Permissions.DispositionEdit);

DocumentUploadResponse uploadResult = await documentService.UploadDocumentAsync(uploadRequest);

DocumentUploadRelationshipResponse relationshipResponse = new()
{
UploadResponse = uploadResult,
};

if (uploadResult.Document.Id != 0)
{
PimsDispositionFileDocument newDocument = new()
{
DispositionFileId = dispositionFileId,
DocumentId = uploadResult.Document.Id,
};
newDocument = _dispositionFileDocumentRepository.AddDispositionDocument(newDocument);
_dispositionFileDocumentRepository.CommitTransaction();

relationshipResponse.DocumentRelationship = mapper.Map<DocumentRelationshipModel>(newDocument);
}
Expand All @@ -227,7 +261,7 @@ public async Task<DocumentUploadRelationshipResponse> UploadPropertyActivityDocu
public async Task<ExternalResult<string>> DeleteResearchDocumentAsync(PimsResearchFileDocument researchFileDocument)
{
this.Logger.LogInformation("Deleting PIMS document for single research file");
this.User.ThrowIfNotAuthorized(Permissions.DocumentDelete);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentDelete, Permissions.ResearchFileEdit);

var relationshipCount = _documentRepository.DocumentRelationshipCount(researchFileDocument.DocumentId);
if (relationshipCount == 1)
Expand All @@ -245,7 +279,7 @@ public async Task<ExternalResult<string>> DeleteResearchDocumentAsync(PimsResear
public async Task<ExternalResult<string>> DeleteProjectDocumentAsync(PimsProjectDocument projectDocument)
{
this.Logger.LogInformation("Deleting PIMS document for single Project");
this.User.ThrowIfNotAuthorized(Permissions.DocumentDelete);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentDelete, Permissions.ProjectEdit);

var relationshipCount = _documentRepository.DocumentRelationshipCount(projectDocument.DocumentId);
if (relationshipCount == 1)
Expand All @@ -263,7 +297,7 @@ public async Task<ExternalResult<string>> DeleteProjectDocumentAsync(PimsProject
public async Task<ExternalResult<string>> DeleteAcquisitionDocumentAsync(PimsAcquisitionFileDocument acquisitionFileDocument)
{
this.Logger.LogInformation("Deleting PIMS document for single acquisition file");
this.User.ThrowIfNotAuthorized(Permissions.DocumentDelete);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentDelete, Permissions.AcquisitionFileEdit);

var relationshipCount = _documentRepository.DocumentRelationshipCount(acquisitionFileDocument.DocumentId);
if (relationshipCount == 1)
Expand All @@ -281,7 +315,7 @@ public async Task<ExternalResult<string>> DeleteAcquisitionDocumentAsync(PimsAcq
public async Task<ExternalResult<string>> DeleteLeaseDocumentAsync(PimsLeaseDocument leaseDocument)
{
this.Logger.LogInformation("Deleting PIMS document for single lease");
this.User.ThrowIfNotAuthorized(Permissions.DocumentDelete);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentDelete, Permissions.LeaseEdit);

var relationshipCount = _documentRepository.DocumentRelationshipCount(leaseDocument.DocumentId);
if (relationshipCount == 1)
Expand All @@ -299,7 +333,7 @@ public async Task<ExternalResult<string>> DeleteLeaseDocumentAsync(PimsLeaseDocu
public async Task<ExternalResult<string>> DeletePropertyActivityDocumentAsync(PimsPropertyActivityDocument propertyActivityDocument)
{
this.Logger.LogInformation("Deleting PIMS document for single Property Activity");
this.User.ThrowIfNotAuthorized(Permissions.ManagementDelete);
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentDelete, Permissions.ManagementEdit);

var relationshipCount = _documentRepository.DocumentRelationshipCount(propertyActivityDocument.DocumentId);
if (relationshipCount == 1)
Expand All @@ -313,5 +347,23 @@ public async Task<ExternalResult<string>> DeletePropertyActivityDocumentAsync(Pi
return new ExternalResult<string>() { Status = ExternalResultStatus.NotExecuted };
}
}

public async Task<ExternalResult<string>> DeleteDispositionDocumentAsync(PimsDispositionFileDocument dispositionFileDocument)
{
this.Logger.LogInformation("Deleting PIMS document for single disposition file");
this.User.ThrowIfNotAllAuthorized(Permissions.DocumentDelete, Permissions.DispositionEdit);

var relationshipCount = _documentRepository.DocumentRelationshipCount(dispositionFileDocument.DocumentId);
if (relationshipCount == 1)
{
return await documentService.DeleteDocumentAsync(dispositionFileDocument.Document);
}
else
{
_dispositionFileDocumentRepository.DeleteDispositionDocument(dispositionFileDocument);
_dispositionFileDocumentRepository.CommitTransaction();
return new ExternalResult<string>() { Status = ExternalResultStatus.NotExecuted };
}
}
}
}
3 changes: 3 additions & 0 deletions source/backend/api/Services/DocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public IList<PimsDocumentTyp> GetPimsDocumentTypes(DocumentRelationType relation
case DocumentRelationType.ManagementFiles:
categoryType = "MANAGEMENT";
break;
case DocumentRelationType.DispositionFiles:
categoryType = "DISPOSE";
break;
default:
throw new InvalidDataException("The requested category relationship does not exist");
}
Expand Down
4 changes: 4 additions & 0 deletions source/backend/api/Services/IDocumentFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public IList<T> GetFileDocuments<T>(FileType fileType, long fileId)

Task<DocumentUploadRelationshipResponse> UploadPropertyActivityDocumentAsync(long propertyActivityId, DocumentUploadRequest uploadRequest);

Task<DocumentUploadRelationshipResponse> UploadDispositionDocumentAsync(long dispositionFileId, DocumentUploadRequest uploadRequest);

Task<ExternalResult<string>> DeleteResearchDocumentAsync(PimsResearchFileDocument researchFileDocument);

Task<ExternalResult<string>> DeleteAcquisitionDocumentAsync(PimsAcquisitionFileDocument acquisitionFileDocument);
Expand All @@ -34,5 +36,7 @@ public IList<T> GetFileDocuments<T>(FileType fileType, long fileId)
Task<ExternalResult<string>> DeleteLeaseDocumentAsync(PimsLeaseDocument leaseDocument);

Task<ExternalResult<string>> DeletePropertyActivityDocumentAsync(PimsPropertyActivityDocument propertyActivityDocument);

Task<ExternalResult<string>> DeleteDispositionDocumentAsync(PimsDispositionFileDocument dispositionFileDocument);
}
}
2 changes: 2 additions & 0 deletions source/backend/apimodels/CodeType/DocumentRelationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public enum DocumentRelationType
Projects,
[EnumMember(Value = "managementfiles")]
ManagementFiles,
[EnumMember(Value = "dispositionfiles")]
DispositionFiles,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ public void Register(TypeAdapterConfig config)
.Map(dest => dest.FileId, src => src.ParentId)
.Map(dest => dest.DocumentId, src => src.Document.Id)
.Map(dest => dest.Document, src => src.Document);

config.NewConfig<Entity.PimsDispositionFileDocument, DocumentRelationshipModel>()
.Map(dest => dest.Id, src => src.Internal_Id)
.Map(dest => dest.ParentId, src => src.FileId)
.Map(dest => dest.Document, src => src.Document)
.Map(dest => dest.RelationshipType, src => DocumentRelationType.DispositionFiles)
.Inherits<Entity.IBaseAppEntity, BaseAuditModel>();

config.NewConfig<DocumentRelationshipModel, Entity.PimsDispositionFileDocument>()
.Map(dest => dest.Internal_Id, src => src.Id)
.Map(dest => dest.FileId, src => src.ParentId)
.Map(dest => dest.DocumentId, src => src.Document.Id)
.Map(dest => dest.Document, src => src.Document);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static IServiceCollection AddPimsDalRepositories(this IServiceCollection
repositories.AddScoped<Repositories.IPropertyActivityDocumentRepository, Repositories.PropertyActivityDocumentRepository>();
repositories.AddScoped<Repositories.IDispositionFilePropertyRepository, Repositories.DispositionFilePropertyRepository>();
repositories.AddScoped<Repositories.IDispositionFileRepository, Repositories.DispositionFileRepository>();
repositories.AddScoped<Repositories.IDispositionFileDocumentRepository, Repositories.DispositionFileDocumentRepository>();
return repositories; // TODO: PSP-4424 Use reflection to find all Repositories.
}

Expand Down
Loading