File

src/issuer/credentials/credential-config/credential-config.service.ts

Description

Service for managing credential configurations.

Index

Methods

Constructor

constructor(credentialConfigRepository: Repository<CredentialConfig>, logger: PinoLogger, cryptoService: CryptoService, filesService: FilesService, configImportService: ConfigImportService)

Constructor for CredentialConfigService.

Parameters :
Name Type Optional Description
credentialConfigRepository Repository<CredentialConfig> No
  • Repository for CredentialConfig entity.
logger PinoLogger No
cryptoService CryptoService No
filesService FilesService No
configImportService ConfigImportService No

Methods

delete
delete(tenantId: string, id: string)

Deletes a credential configuration for a given tenant.

Parameters :
Name Type Optional Description
tenantId string No
  • The ID of the tenant.
id string No
  • The ID of the CredentialConfig entity to delete.
Returns : any

A promise that resolves to the result of the delete operation.

get
get(tenantId: string)

Retrieves all credential configurations for a given tenant.

Parameters :
Name Type Optional Description
tenantId string No
  • The ID of the tenant.
Returns : any

A promise that resolves to an array of CredentialConfig entities.

getById
getById(tenantId: string, id: string)

Retrieves a credential configuration by its ID for a given tenant.

Parameters :
Name Type Optional
tenantId string No
id string No
Returns : any
Public Async import
import()

Imports the configs

Returns : any
store
store(tenantId: string, config: CredentialConfigCreate)

Stores a credential configuration for a given tenant. If the configuration already exists, it will be overwritten.

Parameters :
Name Type Optional Description
tenantId string No
  • The ID of the tenant.
config CredentialConfigCreate No
  • The CredentialConfig entity to store.
Returns : any

A promise that resolves to the stored CredentialConfig entity.

import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { plainToClass } from "class-transformer";
import { readFileSync } from "fs";
import { PinoLogger } from "nestjs-pino";
import { Repository } from "typeorm";
import { CryptoService } from "../../../crypto/crypto.service";
import { FilesService } from "../../../storage/files.service";
import { ConfigImportService } from "../../../utils/config-import/config-import.service";
import { CredentialConfigCreate } from "../dto/credential-config-create.dto";
import { CredentialConfig } from "../entities/credential.entity";

/**
 * Service for managing credential configurations.
 */
@Injectable()
export class CredentialConfigService {
    /**
     * Constructor for CredentialConfigService.
     * @param credentialConfigRepository - Repository for CredentialConfig entity.
     */
    constructor(
        @InjectRepository(CredentialConfig)
        private readonly credentialConfigRepository: Repository<CredentialConfig>,
        private logger: PinoLogger,
        private cryptoService: CryptoService,
        private filesService: FilesService,
        private configImportService: ConfigImportService,
    ) {}

    /**
     * Imports the configs
     */
    public async import() {
        await this.configImportService.importConfigs<CredentialConfigCreate>({
            subfolder: "issuance/credentials",
            fileExtension: ".json",
            validationClass: CredentialConfigCreate,
            resourceType: "credential config",
            checkExists: (tenantId, data) =>
                this.getById(tenantId, data.id)
                    .then(() => true)
                    .catch(() => false),
            deleteExisting: (tenantId, data) =>
                this.credentialConfigRepository
                    .delete({
                        id: data.id,
                        tenantId,
                    })
                    .then(() => undefined),
            loadData: (filePath) => {
                const payload = JSON.parse(readFileSync(filePath, "utf8"));
                return plainToClass(CredentialConfigCreate, payload);
            },
            processItem: async (tenantId, config) => {
                // Replace image references with actual URLs
                config.config.display = await Promise.all(
                    config.config.display.map(async (display) => {
                        if (display.background_image?.uri) {
                            const url =
                                await this.filesService.replaceUriWithPublicUrl(
                                    tenantId,
                                    display.background_image.uri,
                                );
                            if (url) {
                                display.background_image.uri = url;
                            } else {
                                this.logger.warn(
                                    {
                                        event: "ImportWarning",
                                        tenant: tenantId,
                                        uri: display.background_image.uri,
                                    },
                                    `Could not find image ${display.background_image.uri} for credentials config in tenant ${tenantId}`,
                                );
                            }
                        }
                        if (display.logo?.uri) {
                            const url =
                                await this.filesService.replaceUriWithPublicUrl(
                                    tenantId,
                                    display.logo.uri,
                                );
                            if (url) {
                                display.logo.uri = url;
                            } else {
                                this.logger.warn(
                                    {
                                        event: "ImportWarning",
                                        tenant: tenantId,
                                        uri: display.logo.uri,
                                    },
                                    `Could not find image ${display.logo.uri} for credentials config in tenant ${tenantId}`,
                                );
                            }
                        }
                        return display;
                    }),
                );

                // Check if keyId is provided and if the certificate exists
                if (config.keyId) {
                    const cert = await this.cryptoService.getCertEntry(
                        tenantId,
                        config.keyId,
                    );
                    if (!cert) {
                        throw new Error(
                            `Key ID ${config.keyId} must be defined in the crypto service`,
                        );
                    }
                    (config as CredentialConfig).key = cert;
                }

                await this.store(tenantId, config);
            },
        });
    }

    /**
     * Retrieves all credential configurations for a given tenant.
     * @param tenantId - The ID of the tenant.
     * @returns A promise that resolves to an array of CredentialConfig entities.
     */
    get(tenantId: string) {
        return this.credentialConfigRepository.find({
            where: { tenantId },
            relations: ["key"],
        });
    }

    /**
     * Retrieves a credential configuration by its ID for a given tenant.
     * @param tenantId
     * @param id
     * @returns
     */
    getById(tenantId: string, id: string) {
        return this.credentialConfigRepository.findOneByOrFail({
            id,
            tenantId,
        });
    }

    /**
     * Stores a credential configuration for a given tenant.
     * If the configuration already exists, it will be overwritten.
     * @param tenantId - The ID of the tenant.
     * @param config - The CredentialConfig entity to store.
     * @returns A promise that resolves to the stored CredentialConfig entity.
     */
    store(tenantId: string, config: CredentialConfigCreate) {
        return this.credentialConfigRepository.save({
            ...config,
            tenantId,
        });
    }

    /**
     * Deletes a credential configuration for a given tenant.
     * @param tenantId - The ID of the tenant.
     * @param id - The ID of the CredentialConfig entity to delete.
     * @returns A promise that resolves to the result of the delete operation.
     */
    delete(tenantId: string, id: string) {
        return this.credentialConfigRepository.delete({
            id,
            tenantId,
        });
    }
}

results matching ""

    No results matching ""