Skip to content

Commit eaf18d4

Browse files
committed
refactor: create and update template
Signed-off-by: Tipu_Singh <[email protected]>
1 parent acafc89 commit eaf18d4

File tree

3 files changed

+89
-33
lines changed

3 files changed

+89
-33
lines changed

apps/api-gateway/src/oid4vc-issuance/dtos/oid4vc-issuer.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export class IssuerCreationDto {
225225
display: IssuerDisplayDto[];
226226

227227
@ApiProperty({
228-
example: 'https://issuer.credebl.io/oid4vci',
228+
example: 'https://issuer.credebl.io',
229229
description: 'Base URL of the Authorization Server supporting OID4VC issuance flows'
230230
})
231231
@IsUrl({ require_tld: false }, { message: 'authorizationServerUrl must be a valid URL' })

apps/oid4vc-issuance/src/oid4vc-issuance.service.ts

Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

libs/common/src/response-messages/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ export const ResponseMessages = {
525525
update: 'OID4VC template updated successfully.',
526526
delete: 'OID4VC template deleted successfully.',
527527
fetch: 'OID4VC template(s) fetched successfully.',
528-
getById: 'OID4VC template details fetched successfully.'
528+
getById: 'OID4VC template details fetched successfully.',
529+
deleteTemplate: '[createTemplate] compensating delete succeeded for templateId=${templateId}'
529530
},
530531
error: {
531532
notFound: 'OID4VC template not found.',
@@ -536,7 +537,8 @@ export const ResponseMessages = {
536537
issuerDisplayNotFound: 'Issuer display not found.',
537538
issuerDetailsNotFound: 'Issuer details not found.',
538539
templateNameAlreadyExist: 'Template name already exists for this issuer.',
539-
deleteTemplate: 'Error while deleting template.'
540+
deleteTemplate: 'Error while deleting template.',
541+
failedDeleteTemplate: '[createTemplate] compensating delete FAILED for templateId='
540542
}
541543
},
542544
oidcIssuerSession: {

0 commit comments

Comments
 (0)