@@ -4,7 +4,10 @@ import {
4
4
Logger ,
5
5
NotFoundException ,
6
6
} from '@nestjs/common' ;
7
- import { CreateCredentialDto } from '../dto/create-credential.dto' ;
7
+ import {
8
+ CreateCredentialDto ,
9
+ ResolveCredentialMetadata ,
10
+ } from '../dto/create-credential.dto' ;
8
11
import { UpdateCredentialDto } from '../dto/update-credential.dto' ;
9
12
import { ConfigService } from '@nestjs/config' ;
10
13
import { CredentialSSIService } from './credential.ssi.service' ;
@@ -24,6 +27,7 @@ import {
24
27
import { getAppVault , getAppMenemonic } from '../../utils/app-vault-service' ;
25
28
import { TxSendModuleService } from 'src/tx-send-module/tx-send-module.service' ;
26
29
import * as NodeCache from 'node-cache' ;
30
+ import { StatusService } from 'src/status/status.service' ;
27
31
const myCache = new NodeCache ( ) ;
28
32
@Injectable ( )
29
33
export class CredentialService {
@@ -34,6 +38,7 @@ export class CredentialService {
34
38
private credentialRepository : CredentialRepository ,
35
39
private readonly didRepositiory : DidRepository ,
36
40
private readonly txnService : TxSendModuleService ,
41
+ private readonly statusService : StatusService ,
37
42
) { }
38
43
39
44
async checkAllowence ( address ) {
@@ -244,7 +249,7 @@ export class CredentialService {
244
249
'create() method: before creating credential doc in db' ,
245
250
'CredentialService' ,
246
251
) ;
247
- await this . credentialRepository . create ( {
252
+ const credentialDetail = await this . credentialRepository . create ( {
248
253
appId : appDetail . appId ,
249
254
credentialId : signedCredential . id ,
250
255
issuerDid,
@@ -253,14 +258,25 @@ export class CredentialService {
253
258
transactionHash : credentialStatusRegistrationResult
254
259
? credentialStatusRegistrationResult . transactionHash
255
260
: '' ,
256
- type : signedCredential . type [ 1 ] , // TODO : MAYBE REMOVE HARDCODING MAYBE NOT
261
+ type : { schemaType : signedCredential . type [ 1 ] , schemaId } , // TODO : MAYBE REMOVE HARDCODING MAYBE NOT
262
+ registerCredentialStatus : registerCredentialStatus
263
+ ? registerCredentialStatus
264
+ : false ,
257
265
} ) ;
258
266
Logger . log ( 'create() method: ends....' , 'CredentialService' ) ;
259
267
268
+ const metadata = {
269
+ credentialId : credentialDetail . credentialId ,
270
+ persist : credentialDetail . persist ,
271
+ type : credentialDetail . type ,
272
+ issuerDid : credentialDetail . issuerDid ,
273
+ registerCredentialStatus : credentialDetail . registerCredentialStatus ,
274
+ } as ResolveCredentialMetadata ;
275
+
260
276
return {
261
277
credentialDocument : signedCredential ,
262
278
credentialStatus : credStatusTemp ,
263
- persist ,
279
+ metadata ,
264
280
} ;
265
281
} catch ( e ) {
266
282
throw new BadRequestException ( [ e . message ] ) ;
@@ -316,22 +332,56 @@ export class CredentialService {
316
332
'resolveCredential() method: before initialising HypersignVerifiableCredential' ,
317
333
'CredentialService' ,
318
334
) ;
319
- const hypersignCredential = new HypersignVerifiableCredential ( ) ;
320
- let credentialStatus ;
321
- try {
322
- credentialStatus = await hypersignCredential . resolveCredentialStatus ( {
323
- credentialId,
324
- } ) ;
325
- } catch ( e ) {
326
- credentialStatus = undefined ;
335
+
336
+ const metadata = {
337
+ credentialId : credentialDetail . credentialId ,
338
+ persist : credentialDetail . persist ,
339
+ type : credentialDetail . type ,
340
+ issuerDid : credentialDetail . issuerDid ,
341
+ registerCredentialStatus : credentialDetail . registerCredentialStatus ,
342
+ } as ResolveCredentialMetadata ;
343
+ let credentialStatus = undefined ;
344
+ // If user had registered the credential on the blockchain
345
+ // Only then we will go ahead with credential status retrival
346
+ const shouldRetriveCredential = credentialDetail . registerCredentialStatus
347
+ ? credentialDetail . registerCredentialStatus
348
+ : true ; // making default true for backwards compatibility
349
+ if ( shouldRetriveCredential ) {
350
+ /// First check this transaction was successful in lcoal db or there was some error
351
+ const statusResponse = await this . statusService . findBySsiId ( credentialId ) ;
352
+ let wasTransactionSuccess = false ;
353
+ if ( statusResponse ) {
354
+ const firstResponse = statusResponse [ 0 ] ;
355
+ if (
356
+ firstResponse &&
357
+ firstResponse . data &&
358
+ firstResponse . totalCount > 0
359
+ ) {
360
+ metadata [ 'transactionStatus' ] = firstResponse . data ;
361
+ if ( firstResponse . data . findIndex ( ( x ) => x [ 'status' ] == 0 ) >= 0 ) {
362
+ wasTransactionSuccess = true ;
363
+ }
364
+ }
365
+ }
366
+
367
+ /// Retrive status from the blockchain only when status = 0, otherwise skip
368
+ if ( wasTransactionSuccess ) {
369
+ try {
370
+ const hypersignCredential = new HypersignVerifiableCredential ( ) ;
371
+ credentialStatus = await hypersignCredential . resolveCredentialStatus ( {
372
+ credentialId,
373
+ } ) ;
374
+ } catch ( e ) {
375
+ credentialStatus = undefined ;
376
+ }
377
+ Logger . log ( 'resolveCredential() method: ends....' , 'CredentialService' ) ;
378
+ }
327
379
}
328
- Logger . log ( 'resolveCredential() method: ends....' , 'CredentialService' ) ;
329
380
330
381
return {
331
382
credentialDocument : credential ? credential : undefined ,
332
383
credentialStatus,
333
- persist : credentialDetail . persist ,
334
- retrieveCredential,
384
+ metadata,
335
385
} ;
336
386
}
337
387
0 commit comments