All files / src/Asset TypeOrmNftMetadataService.ts

87.5% Statements 14/16
100% Branches 9/9
100% Functions 4/4
86.66% Lines 13/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 4541x   41x   41x   41x   10x       6x       8x     8x 8x 8x               8x 1x     7x                      
import { Asset, Cardano, Milliseconds } from '@cardano-sdk/core';
import { AssetPolicyIdAndName, NftMetadataService } from './types';
import { NftMetadataEntity } from '@cardano-sdk/projection-typeorm';
import { QueryRunner } from 'typeorm';
import { TypeormProviderDependencies, TypeormService } from '../util';
 
export class TypeOrmNftMetadataService extends TypeormService implements NftMetadataService {
  constructor({ connectionConfig$, logger, entities }: TypeormProviderDependencies) {
    super('TypeOrmNftMetadataService', { connectionConfig$, connectionTimeout: Milliseconds(1000), entities, logger });
  }
 
  async getNftMetadata(assetInfo: AssetPolicyIdAndName): Promise<Asset.NftMetadata | null> {
    return this.withQueryRunner((queryRunner) => this.getNftMetadataWith(assetInfo, queryRunner));
  }
 
  async getNftMetadataWith(assetInfo: AssetPolicyIdAndName, queryRunner: QueryRunner) {
    const assetId = Cardano.AssetId.fromParts(assetInfo.policyId, assetInfo.name);
    let asset: NftMetadataEntity | null;
 
    try {
      const nftMetadataRepository = queryRunner.manager.getRepository(NftMetadataEntity);
      asset = await nftMetadataRepository.findOneBy({
        userTokenAsset: { id: assetId }
      });
    } catch (error) {
      this.logger.error(error);
      asset = null;
    }
 
    if (!asset) {
      return null;
    }
 
    return {
      image: asset.image!,
      name: asset.name!,
      ...(asset.description && { description: asset.description }),
      ...(asset.files && { files: asset.files }),
      ...(asset.mediaType && { mediaType: asset.mediaType as Asset.ImageMediaType }),
      ...(asset.otherProperties && { otherProperties: asset.otherProperties }),
      version: asset.otherProperties?.get('version') as string
    };
  }
}