Skip to content
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
8 changes: 0 additions & 8 deletions src/Spd.Manager.Printing/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,12 @@ public interface IPrintingManager
public record StartPrintJobCommand(Guid EventId) : IRequest<ResultResponse>;
public record PrintJobStatusQuery(Guid EventId) : IRequest<ResultResponse>;
public record PreviewDocumentCommand(Guid LicenceId) : IRequest<PreviewDocumentResp>;

public record PrintJob(DocumentType DocumentType, Guid? ApplicationId, Guid? LicenceId);
public record ResultResponse()
{
public string PrintJobId { get; set; }
public JobStatusCode Status { get; set; }
public string? Error { get; set; }
};
public enum DocumentType
{
FingerprintLetter,
PersonalLicencePreview,
}

public enum JobStatusCode
{
Ready, //Active state status reason
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AutoMapper;
using Spd.Resource.Repository.Licence;
using Spd.Utilities.Printing.BCMailPlus;
using System.Text.Json.Serialization;

namespace Spd.Manager.Printing.Documents.TransformationStrategies;

internal class PersonalLicencePrintingTransformStrategy(
ILicenceRepository licenceRepository,
IMapper mapper)
: BcMailPlusTransformStrategyBase<PersonalLicencePrintingTransformRequest, PersonalLicencePrinting>(Jobs.SecurityWorkerLicenseRelease)
{
protected override async Task<PersonalLicencePrinting> CreateDocument(PersonalLicencePrintingTransformRequest request, CancellationToken cancellationToken)
{
var licence = await licenceRepository.GetAsync(request.LicenceId, cancellationToken);

//todo: temp testing data, when dynamics complete, we can add code back.
//if(licence.jobId == null)
//throw new ApiException with bad request;
return new PersonalLicencePrinting()
{
//Jobs=new List<string> { licence.jobId }
Jobs = new List<string> { "SPDCARD.Sep.09.2024_161255_4fa87843a5a30c0f1166d87f908ce42a" }
};
}

}

public record PersonalLicencePrintingTransformRequest(Guid LicenceId) : DocumentTransformRequest;

public record PersonalLicencePrinting
{
[JsonPropertyName("Jobs")]
public List<string> Jobs { get; set; }

}

84 changes: 43 additions & 41 deletions src/Spd.Manager.Printing/PrintingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,44 @@ public PrintingManager(IDocumentTransformationEngine documentTransformationEngin
this._mapper = mapper;
this._logger = logger;
}

public async Task<ResultResponse> Handle(StartPrintJobCommand request, CancellationToken cancellationToken)
{
//get event
EventResp? eventResp = await _eventRepo.GetAsync(request.EventId, cancellationToken);
if (eventResp == null)
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "Invalid eventqueue id.");

if (eventResp.EventTypeEnum == EventTypeEnum.BCMPScreeningFingerprintPrinting)
{
//the returned RegardingObjectTypeName should be spd_application
if (eventResp.RegardingObjectName != "spd_application")
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "Invalid Regarding Object type.");
DocumentTransformRequest transformRequest = CreateDocumentTransformRequest(eventResp);

try
try
{
var transformResponse = await _documentTransformationEngine.Transform(
transformRequest,
cancellationToken);
if (transformResponse is BcMailPlusTransformResponse)
{
PrintJob printJob = new(DocumentType.FingerprintLetter, eventResp.RegardingObjectId, null);
var transformResponse = await _documentTransformationEngine.Transform(
CreateDocumentTransformRequest(printJob),
BcMailPlusTransformResponse transformResult = (BcMailPlusTransformResponse)transformResponse;
var printResponse = await _printer.Send(
new BCMailPlusPrintRequest(transformResult.JobTemplateId, transformResult.Document),
cancellationToken);
if (transformResponse is BcMailPlusTransformResponse)
{
BcMailPlusTransformResponse transformResult = (BcMailPlusTransformResponse)transformResponse;
var printResponse = await _printer.Send(
new BCMailPlusPrintRequest(transformResult.JobTemplateId, transformResult.Document),
cancellationToken);
ResultResponse result = _mapper.Map<ResultResponse>(printResponse);
await UpdateResultInEvent(result, request.EventId, cancellationToken);
return result;
};
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message, null);
ResultResponse result = new()
{
PrintJobId = null,
Status = JobStatusCode.Error,
Error = $"{ex.Message} {ex.InnerException?.Message}"
};
ResultResponse result = _mapper.Map<ResultResponse>(printResponse);
await UpdateResultInEvent(result, request.EventId, cancellationToken);
return result;
}
};
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message, null);
ResultResponse result = new()
{
PrintJobId = null,
Status = JobStatusCode.Error,
Error = $"{ex.Message} {ex.InnerException?.Message}"
};
await UpdateResultInEvent(result, request.EventId, cancellationToken);
return result;
}

return null;
}

Expand Down Expand Up @@ -119,8 +113,8 @@ public async Task<PreviewDocumentResp> Handle(PreviewDocumentCommand cmd, Cancel
LicenceResp? licence = await _licenceRepository.GetAsync(cmd.LicenceId, cancellationToken);
if (licence == null || licence.WorkerLicenceTypeCode == WorkerLicenceTypeEnum.SecurityBusinessLicence)
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "Cannot find the licence or the licence is not person licence.");
PrintJob printJob = new(DocumentType.PersonalLicencePreview, null, cmd.LicenceId);
var transformResponse = await _documentTransformationEngine.Transform(CreateDocumentTransformRequest(printJob), cancellationToken);
DocumentTransformRequest transformRequest = new PersonalLicencePreviewTransformRequest(cmd.LicenceId);
var transformResponse = await _documentTransformationEngine.Transform(transformRequest, cancellationToken);
return transformResponse switch
{
BcMailPlusTransformResponse bcmailplusResponse => await PreviewViaBcMailPlus(bcmailplusResponse, cancellationToken),
Expand All @@ -129,18 +123,26 @@ public async Task<PreviewDocumentResp> Handle(PreviewDocumentCommand cmd, Cancel
};
}

private static DocumentTransformRequest CreateDocumentTransformRequest(PrintJob printJob)
private static DocumentTransformRequest CreateDocumentTransformRequest(EventResp eventResp)
{
if (printJob.DocumentType == DocumentType.FingerprintLetter && printJob.ApplicationId == null)
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "ApplicationId cannot be null if documentType is FingerprintLetter");
if (eventResp.EventTypeEnum == EventTypeEnum.BCMPScreeningFingerprintPrinting && (eventResp.RegardingObjectId == null || eventResp.RegardingObjectName != "spd_application"))
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "ApplicationId cannot be null if it is BCMPScreeningFingerprintPrinting");

if (eventResp.EventTypeEnum == EventTypeEnum.BCMPSecurityWorkerLicencePrinting && (eventResp.RegardingObjectId == null || eventResp.RegardingObjectName != "spd_licence"))
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "LicenceId cannot be null if it is BCMPSecurityWorkerLicencePrinting");

if (eventResp.EventTypeEnum == EventTypeEnum.BCMPArmouredVehiclePermitPrinting && (eventResp.RegardingObjectId == null || eventResp.RegardingObjectName != "spd_licence"))
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "LicenceId cannot be null if it is BCMPArmouredVehiclePermitPrinting");

if (printJob.DocumentType == DocumentType.PersonalLicencePreview && printJob.LicenceId == null)
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "LicenceId cannot be null if documentType is LicencePreview");
if (eventResp.EventTypeEnum == EventTypeEnum.BCMPBodyArmourPermitPrinting && (eventResp.RegardingObjectId == null || eventResp.RegardingObjectName != "spd_licence"))
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "LicenceId cannot be null if it is BCMPBodyArmourPermitPrinting");

return printJob.DocumentType switch
return eventResp.EventTypeEnum switch
{
DocumentType.FingerprintLetter => new FingerprintLetterTransformRequest((Guid)printJob.ApplicationId),
DocumentType.PersonalLicencePreview => new PersonalLicencePreviewTransformRequest((Guid)printJob.LicenceId),
EventTypeEnum.BCMPScreeningFingerprintPrinting => new FingerprintLetterTransformRequest((Guid)eventResp.RegardingObjectId),
EventTypeEnum.BCMPSecurityWorkerLicencePrinting => new PersonalLicencePrintingTransformRequest((Guid)eventResp.RegardingObjectId),
EventTypeEnum.BCMPBodyArmourPermitPrinting => new PersonalLicencePrintingTransformRequest((Guid)eventResp.RegardingObjectId),
EventTypeEnum.BCMPArmouredVehiclePermitPrinting => new PersonalLicencePrintingTransformRequest((Guid)eventResp.RegardingObjectId),
_ => throw new NotImplementedException()
};
}
Expand Down
1 change: 1 addition & 0 deletions src/Spd.Manager.Printing/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void ConfigureServices(ConfigurationServices configurationServices)
configurationServices.Services.AddScoped<IDocumentTransformationEngine, DocumentTransformationEngine>();
configurationServices.Services.AddScoped<IDocumentTransformStrategy, FingerprintLetterTransformStrategy>();
configurationServices.Services.AddScoped<IDocumentTransformStrategy, PersonalLicencePreviewTransformStrategy>();
configurationServices.Services.AddScoped<IDocumentTransformStrategy, PersonalLicencePrintingTransformStrategy>();
}
}
}
7 changes: 6 additions & 1 deletion src/Spd.Utilities.Printing/BCMailPlus/IBcMailPlusApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal record JobTemplate(string JobName, string AssetExtension);
public static class Jobs
{
/// <summary>
/// Security worker licence job
/// Security worker licence job: it will generate image file
/// </summary>
public const string SecurityWorkerLicense = "PSSG-SPD-CARD";

Expand All @@ -126,6 +126,11 @@ public static class Jobs
/// Metal dealer and recyclers permit job
/// </summary>
public const string MetalDealerAndRecyclersPermit = "PSSG-SPD-MTL-PMT";

/// <summary>
/// Release Security worker licence job: the job will be routed to the physical printing queue
/// </summary>
public const string SecurityWorkerLicenseRelease = "PSSG-SPD-CARD-RELEASE";
}

internal static class JobStatusValues
Expand Down