@@ -13,6 +13,7 @@ namespace Spd.Manager.Printing;
13
13
14
14
internal class PrintingManager
15
15
: IRequestHandler < StartPrintJobCommand , ResultResponse > ,
16
+ IRequestHandler < PrintCardsInBatchCommand , ResultResponse > ,
16
17
IRequestHandler < PrintJobStatusQuery , ResultResponse > ,
17
18
IRequestHandler < PreviewDocumentCommand , PreviewDocumentResp > ,
18
19
IRequestHandler < PrintingEventImageQuery , PreviewDocumentResp > ,
@@ -81,6 +82,44 @@ public async Task<ResultResponse> Handle(StartPrintJobCommand request, Cancellat
81
82
return null ;
82
83
}
83
84
85
+ public async Task < ResultResponse > Handle ( PrintCardsInBatchCommand request , CancellationToken cancellationToken )
86
+ {
87
+ PersonalLicenceBatchPrintingTransformRequest transformRequest = await CreatePersonalLicenceBatchPrintingTransformRequest ( cancellationToken ) ;
88
+
89
+ if ( transformRequest . CardPrintEvents . Any ( ) )
90
+ {
91
+ try
92
+ {
93
+ var transformResponse = await _documentTransformationEngine . Transform (
94
+ transformRequest ,
95
+ cancellationToken ) ;
96
+ if ( transformResponse is BcMailPlusTransformResponse )
97
+ {
98
+ BcMailPlusTransformResponse transformResult = ( BcMailPlusTransformResponse ) transformResponse ;
99
+ var printResponse = await _printer . Send (
100
+ new BCMailPlusPrintRequest ( transformResult . JobTemplateId , transformResult . Document ) ,
101
+ cancellationToken ) ;
102
+ ResultResponse result = _mapper . Map < ResultResponse > ( printResponse ) ;
103
+ await UpdateResultInEvents ( result , transformRequest . CardPrintEvents , cancellationToken ) ;
104
+ return result ;
105
+ }
106
+ }
107
+ catch ( Exception ex )
108
+ {
109
+ _logger . LogError ( ex , ex . Message , null ) ;
110
+ ResultResponse result = new ( )
111
+ {
112
+ PrintJobId = null ,
113
+ Status = JobStatusCode . Error ,
114
+ Error = $ "{ ex . Message } { ex . InnerException ? . Message } "
115
+ } ;
116
+ await UpdateResultInEvents ( result , transformRequest . CardPrintEvents , cancellationToken ) ;
117
+ return result ;
118
+ }
119
+ }
120
+ return null ;
121
+ }
122
+
84
123
public async Task < ResultResponse > Handle ( PrintJobStatusQuery request , CancellationToken cancellationToken )
85
124
{
86
125
//get event
@@ -144,41 +183,90 @@ private static DocumentTransformRequest CreateDocumentTransformRequest(EventResp
144
183
if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPScreeningFingerprintPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_application" ) )
145
184
throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "ApplicationId cannot be null if it is BCMPScreeningFingerprintPrinting" ) ;
146
185
147
- if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPSecurityWorkerLicencePrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
148
- throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPSecurityWorkerLicencePrinting" ) ;
149
-
150
- if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPArmouredVehiclePermitPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
151
- throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPArmouredVehiclePermitPrinting" ) ;
152
-
153
- if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPBodyArmourPermitPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
154
- throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPBodyArmourPermitPrinting" ) ;
155
-
156
- if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPGuideDogServiceDogTeamPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
157
- throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPGuideDogServiceDogTeamPrinting" ) ;
158
-
159
- if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPDogTrainerPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
160
- throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPDogTrainerPrinting" ) ;
161
-
162
- if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPRetiredServiceDogPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
163
- throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPRetiredServiceDogPrinting" ) ;
164
-
165
186
if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPBusinessLicencePrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
166
187
throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPBusinessLicencePrinting" ) ;
167
188
168
189
return eventResp . EventTypeEnum switch
169
190
{
170
191
EventTypeEnum . BCMPScreeningFingerprintPrinting => new FingerprintLetterTransformRequest ( eventResp . RegardingObjectId . Value ) ,
171
- EventTypeEnum . BCMPSecurityWorkerLicencePrinting => new PersonalLicencePrintingTransformRequest ( ( Guid ) eventResp . RegardingObjectId ) ,
172
- EventTypeEnum . BCMPBodyArmourPermitPrinting => new PersonalLicencePrintingTransformRequest ( ( Guid ) eventResp . RegardingObjectId ) ,
173
- EventTypeEnum . BCMPArmouredVehiclePermitPrinting => new PersonalLicencePrintingTransformRequest ( ( Guid ) eventResp . RegardingObjectId ) ,
174
- EventTypeEnum . BCMPGuideDogServiceDogTeamPrinting => new PersonalLicencePrintingTransformRequest ( ( Guid ) eventResp . RegardingObjectId ) ,
175
- EventTypeEnum . BCMPDogTrainerPrinting => new PersonalLicencePrintingTransformRequest ( ( Guid ) eventResp . RegardingObjectId ) ,
176
- EventTypeEnum . BCMPRetiredServiceDogPrinting => new PersonalLicencePrintingTransformRequest ( ( Guid ) eventResp . RegardingObjectId ) ,
177
192
EventTypeEnum . BCMPBusinessLicencePrinting => new BizLicencePrintingTransformRequest ( ( Guid ) eventResp . RegardingObjectId ) ,
178
193
_ => throw new NotImplementedException ( )
179
194
} ;
180
195
}
181
196
197
+ private async Task < PersonalLicenceBatchPrintingTransformRequest > CreatePersonalLicenceBatchPrintingTransformRequest ( CancellationToken ct )
198
+ {
199
+ IEnumerable < EventResp ? > eventResps = await _eventRepo . QueryAsync (
200
+ new EventQuery ( )
201
+ {
202
+ EventStatusReasonEnum = EventStatusReasonEnum . Ready ,
203
+ EventTypeEnums = new List < EventTypeEnum > ( )
204
+ {
205
+ EventTypeEnum . BCMPSecurityWorkerLicencePrinting ,
206
+ EventTypeEnum . BCMPArmouredVehiclePermitPrinting ,
207
+ EventTypeEnum . BCMPBodyArmourPermitPrinting ,
208
+ //EventTypeEnum.BCMPRetiredServiceDogPrinting,
209
+ //EventTypeEnum.BCMPDogTrainerPrinting,
210
+ //EventTypeEnum.BCMPGuideDogServiceDogTeamPrinting
211
+ } ,
212
+ CutOffDateTime = DateTimeOffset . UtcNow ,
213
+ } , ct ) ;
214
+
215
+ List < CardPrintEvent > cardPrints = new List < CardPrintEvent > ( ) ;
216
+ foreach ( var eventResp in eventResps )
217
+ {
218
+ if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPSecurityWorkerLicencePrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
219
+ throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPSecurityWorkerLicencePrinting" ) ;
220
+
221
+ if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPArmouredVehiclePermitPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
222
+ throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPArmouredVehiclePermitPrinting" ) ;
223
+
224
+ if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPBodyArmourPermitPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
225
+ throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPBodyArmourPermitPrinting" ) ;
226
+
227
+ if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPGuideDogServiceDogTeamPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
228
+ throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPGuideDogServiceDogTeamPrinting" ) ;
229
+
230
+ if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPDogTrainerPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
231
+ throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPDogTrainerPrinting" ) ;
232
+
233
+ if ( eventResp . EventTypeEnum == EventTypeEnum . BCMPRetiredServiceDogPrinting && ( eventResp . RegardingObjectId == null || eventResp . RegardingObjectName != "spd_licence" ) )
234
+ throw new ApiException ( System . Net . HttpStatusCode . BadRequest , "LicenceId cannot be null if it is BCMPRetiredServiceDogPrinting" ) ;
235
+ var licence = await _licenceRepository . GetBasicAsync ( ( Guid ) eventResp . RegardingObjectId , ct ) ;
236
+ var cardPrint = new CardPrintEvent
237
+ {
238
+ EventQueueId = eventResp . Id ,
239
+ PrintingPreviewJobId = licence ? . PrintingPreviewJobId ,
240
+ IsSuccess = licence ? . PrintingPreviewJobId == null ? false : null ,
241
+ ErrMsg = licence ? . PrintingPreviewJobId == null ? "Cannot find the preview job id" : null
242
+ } ;
243
+ cardPrints . Add ( cardPrint ) ;
244
+ }
245
+ return new PersonalLicenceBatchPrintingTransformRequest ( cardPrints ) ;
246
+ }
247
+
248
+ private async Task UpdateResultInEvents ( ResultResponse resultResponse , List < CardPrintEvent > events , CancellationToken cancellationToken )
249
+ {
250
+ //update event queues
251
+ List < EventUpdateCmd > cmds = new List < EventUpdateCmd > ( ) ;
252
+ foreach ( var e in events )
253
+ {
254
+ var cmd = _mapper . Map < EventUpdateCmd > ( resultResponse ) ;
255
+ if ( e . IsSuccess == false ) //those events has data issue, even though it is supposed to be in print batch, but as there is error, it does not go to print batch.
256
+ {
257
+ cmd . JobId = null ;
258
+ cmd . ErrorDescription = e . ErrMsg ;
259
+ cmd . StateCode = 1 ;
260
+ cmd . EventStatusReasonEnum = EventStatusReasonEnum . Fail ;
261
+ }
262
+ cmd . Id = e . EventQueueId ;
263
+ cmds . Add ( cmd ) ;
264
+ }
265
+
266
+ await _eventRepo . ManageInBatchAsync ( cmds , cancellationToken ) ;
267
+ return ;
268
+ }
269
+
182
270
private async Task UpdateResultInEvent ( ResultResponse resultResponse , Guid eventId , CancellationToken cancellationToken )
183
271
{
184
272
//update event queue
0 commit comments