@@ -322,19 +322,39 @@ export class Oid4vcIssuanceService {
322322 // if (doctype) {
323323 // opts = { ...opts, doctype };
324324 // }
325- const issuerTemplateConfig = await this . buildOidcIssuerConfig ( issuerId ) ;
326- console . log ( `service - createTemplate: ` , JSON . stringify ( issuerTemplateConfig ) ) ;
327- const agentDetails = await this . oid4vcIssuanceRepository . getAgentEndPoint ( orgId ) ;
328- if ( ! agentDetails ) {
329- throw new NotFoundException ( ResponseMessages . issuance . error . agentEndPointNotFound ) ;
330- }
331- const { agentEndPoint } = agentDetails ;
332- const issuerDetails = await this . oid4vcIssuanceRepository . getOidcIssuerDetailsById ( issuerId ) ;
333- if ( ! issuerDetails ) {
334- throw new NotFoundException ( ResponseMessages . oidcTemplate . error . issuerDetailsNotFound ) ;
325+ let createTemplateOnAgent ;
326+ try {
327+ const issuerTemplateConfig = await this . buildOidcIssuerConfig ( issuerId ) ;
328+ console . log ( `service - createTemplate: ` , JSON . stringify ( issuerTemplateConfig ) ) ;
329+ const agentDetails = await this . oid4vcIssuanceRepository . getAgentEndPoint ( orgId ) ;
330+ if ( ! agentDetails ) {
331+ throw new NotFoundException ( ResponseMessages . issuance . error . agentEndPointNotFound ) ;
332+ }
333+ const { agentEndPoint } = agentDetails ;
334+ const issuerDetails = await this . oid4vcIssuanceRepository . getOidcIssuerDetailsById ( issuerId ) ;
335+ if ( ! issuerDetails ) {
336+ throw new NotFoundException ( ResponseMessages . oidcTemplate . error . issuerDetailsNotFound ) ;
337+ }
338+ const url = await getAgentUrl (
339+ agentEndPoint ,
340+ CommonConstants . OIDC_ISSUER_TEMPLATE ,
341+ issuerDetails . publicIssuerId
342+ ) ;
343+ createTemplateOnAgent = await this . _createOIDCTemplate ( issuerTemplateConfig , url , orgId ) ;
344+ } catch ( agentError ) {
345+ try {
346+ await this . oid4vcIssuanceRepository . deleteTemplate ( createdTemplate . id ) ;
347+ this . logger . log ( `${ ResponseMessages . oidcTemplate . success . deleteTemplate } ${ createdTemplate . id } ` ) ;
348+ throw new RpcException ( agentError ?. response ?? agentError ) ;
349+ } catch ( cleanupError ) {
350+ this . logger . error (
351+ `${ ResponseMessages . oidcTemplate . error . failedDeleteTemplate } ${ createdTemplate . id } deleteError=${ JSON . stringify (
352+ cleanupError
353+ ) } originalAgentError=${ JSON . stringify ( agentError ) } `
354+ ) ;
355+ throw new RpcException ( 'Template creation failed and cleanup also failed' ) ;
356+ }
335357 }
336- const url = await getAgentUrl ( agentEndPoint , CommonConstants . OIDC_ISSUER_TEMPLATE , issuerDetails . publicIssuerId ) ;
337- const createTemplateOnAgent = await this . _createOIDCTemplate ( issuerTemplateConfig , url , orgId ) ;
338358 console . log ( 'createTemplateOnAgent::::::::::::::' , createTemplateOnAgent ) ;
339359 if ( ! createTemplateOnAgent ) {
340360 throw new NotFoundException ( ResponseMessages . issuance . error . agentEndPointNotFound ) ;
@@ -385,25 +405,59 @@ export class Oid4vcIssuanceService {
385405
386406 const updatedTemplate = await this . oid4vcIssuanceRepository . updateTemplate ( templateId , payload ) ;
387407
388- const templates = await this . oid4vcIssuanceRepository . getTemplatesByIssuerId ( issuerId ) ;
389- if ( ! templates || 0 === templates . length ) {
390- throw new NotFoundException ( ResponseMessages . issuance . error . notFound ) ;
391- }
392- const issuerTemplateConfig = await this . buildOidcIssuerConfig ( issuerId ) ;
393- const agentDetails = await this . oid4vcIssuanceRepository . getAgentEndPoint ( orgId ) ;
394- if ( ! agentDetails ) {
395- throw new NotFoundException ( ResponseMessages . issuance . error . agentEndPointNotFound ) ;
396- }
397- const { agentEndPoint } = agentDetails ;
398- const issuerDetails = await this . oid4vcIssuanceRepository . getOidcIssuerDetailsById ( issuerId ) ;
399- if ( ! issuerDetails ) {
400- throw new NotFoundException ( ResponseMessages . oidcTemplate . error . issuerDetailsNotFound ) ;
401- }
402- const url = await getAgentUrl ( agentEndPoint , CommonConstants . OIDC_ISSUER_TEMPLATE , issuerDetails . publicIssuerId ) ;
408+ try {
409+ const templates = await this . oid4vcIssuanceRepository . getTemplatesByIssuerId ( issuerId ) ;
410+ if ( ! templates || 0 === templates . length ) {
411+ throw new NotFoundException ( ResponseMessages . issuance . error . notFound ) ;
412+ }
413+ const issuerTemplateConfig = await this . buildOidcIssuerConfig ( issuerId ) ;
414+ const agentDetails = await this . oid4vcIssuanceRepository . getAgentEndPoint ( orgId ) ;
415+ if ( ! agentDetails ) {
416+ throw new NotFoundException ( ResponseMessages . issuance . error . agentEndPointNotFound ) ;
417+ }
418+ const { agentEndPoint } = agentDetails ;
419+ const issuerDetails = await this . oid4vcIssuanceRepository . getOidcIssuerDetailsById ( issuerId ) ;
420+ if ( ! issuerDetails ) {
421+ throw new NotFoundException ( ResponseMessages . oidcTemplate . error . issuerDetailsNotFound ) ;
422+ }
423+ const url = await getAgentUrl (
424+ agentEndPoint ,
425+ CommonConstants . OIDC_ISSUER_TEMPLATE ,
426+ issuerDetails . publicIssuerId
427+ ) ;
403428
404- const createTemplateOnAgent = await this . _createOIDCTemplate ( issuerTemplateConfig , url , orgId ) ;
405- if ( ! createTemplateOnAgent ) {
406- throw new NotFoundException ( ResponseMessages . issuance . error . agentEndPointNotFound ) ;
429+ const createTemplateOnAgent = await this . _createOIDCTemplate ( issuerTemplateConfig , url , orgId ) ;
430+ if ( ! createTemplateOnAgent ) {
431+ throw new NotFoundException ( ResponseMessages . issuance . error . agentEndPointNotFound ) ;
432+ }
433+ } catch ( agentError ) {
434+ this . logger . error ( `[updateTemplate] - error updating template on agent: ${ JSON . stringify ( agentError ) } ` ) ;
435+ try {
436+ const rollbackPayload = {
437+ name : template . name ,
438+ description : template . description ,
439+ format : template . format ,
440+ canBeRevoked : template . canBeRevoked ,
441+ attributes : template . attributes ,
442+ appearance : template . appearance ,
443+ issuerId : template . issuerId
444+ } ;
445+ await this . oid4vcIssuanceRepository . updateTemplate ( templateId , rollbackPayload ) ;
446+ this . logger . log ( `Rolled back template ${ templateId } to previous state after agent error` ) ;
447+ throw new RpcException ( agentError ?. response ?? agentError ) ;
448+ } catch ( revertError ) {
449+ this . logger . error (
450+ `[updateTemplate] - rollback failed for template ${ templateId } : ${ JSON . stringify ( revertError ) } originalAgentError=${ JSON . stringify (
451+ agentError
452+ ) } `
453+ ) ;
454+ const wrappedError = {
455+ message : 'Template update failed and rollback also failed' ,
456+ agentError : agentError ?. response ?? agentError ,
457+ rollbackError : revertError ?. response ?? revertError
458+ } ;
459+ throw new RpcException ( wrappedError ) ;
460+ }
407461 }
408462
409463 return updatedTemplate ;
0 commit comments