Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • eclipse/xfsc/ocm/ocm-engine
  • zdravko61/ocm-engine
  • mjuergenscg/ocm-engine
  • tsabolov/ocm-engine
  • mikesell/ocm-engine
5 results
Show changes
Showing
with 0 additions and 2512 deletions
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty, IsBoolean } from 'class-validator';
export default class CredentialDefReqDto {
@IsString()
public id: string;
@IsString()
@IsNotEmpty()
@ApiProperty()
public schemaID: string;
@IsString()
@IsNotEmpty()
@ApiProperty()
public name: string;
@IsString()
public credDefId: string;
@IsBoolean()
public supportRevocation?: boolean;
@IsBoolean()
@ApiProperty()
public isRevokable: boolean;
@IsBoolean()
@ApiProperty()
public isAutoIssue: boolean;
@IsString()
@ApiProperty()
// Number of hours of Credential validity
public expiryHours: string;
@IsString()
@ApiProperty()
public createdBy: string;
@IsString()
public createdDate: Date;
@IsString()
public updatedBy: string;
@IsString()
public updatedDate: Date;
@IsString()
public tag?: string;
}
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import NatsClientService from '../client/nats.client.js';
import RestClientService from '../client/rest.client.js';
import { NATSServices } from '../common/constants.js';
import config from '../config/config.js';
import PrismaService from '../prisma/prisma.service.js';
import SchemasService from '../schemas/services/service.js';
import CredentialDefController from './controller/controller.js';
import CredentialDefService from './services/service.js';
@Module({
imports: [
HttpModule,
ClientsModule.register([
{
name: NATSServices.SERVICE_NAME,
transport: Transport.NATS,
options: {
servers: [config().nats.url as string],
},
},
]),
],
controllers: [CredentialDefController],
providers: [
CredentialDefService,
PrismaService,
NatsClientService,
RestClientService,
SchemasService,
],
})
export default class CredentialDefModule {}
import type { Prisma } from '@prisma/client';
import { Injectable } from '@nestjs/common';
import PrismaService from '../../prisma/prisma.service.js';
@Injectable()
export default class CredentialDefRepository {
public constructor(private readonly prismaService: PrismaService) {}
public async createCredDef(data: Prisma.CredentialDefCreateInput) {
const credDef = await this.prismaService.credentialDef.create({
data,
});
await this.prismaService.schema.update({
where: {
schemaID: data.schemaID,
},
data: {
credential_defs: {
connect: {
id: credDef.id,
},
},
},
});
return credDef;
}
public async findCredentialDef(params: {
skip?: number;
take?: number;
cursor?: Prisma.CredentialDefWhereUniqueInput;
where?: Prisma.CredentialDefWhereInput;
orderBy?: Prisma.CredentialDefOrderByWithRelationInput;
}) {
const { skip, take, cursor, where, orderBy } = params;
return this.prismaService.$transaction([
this.prismaService.credentialDef.count({
where,
}),
this.prismaService.credentialDef.findMany({
skip,
take,
cursor,
where,
orderBy,
}),
]);
}
public async findUniqueCredentialDef(params: {
where: Prisma.CredentialDefWhereUniqueInput;
}) {
const { where } = params;
return this.prismaService.credentialDef.findUnique({
where,
});
}
}
import type SchemaDto from '../../schemas/entities/schema-entity.js';
import type CredentialDefDto from '../entities/credentialDef-entity.js';
import type CredentialDefLedgerDto from '../entities/credentialDefLedger-entity.js';
import type { Prisma } from '@prisma/client';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import RestClientService from '../../client/rest.client.js';
import CredentialTypeRepository from '../../issue-credential/repository/credentialType.repository.js';
import PrismaService from '../../prisma/prisma.service.js';
import SchemasService from '../../schemas/services/service.js';
import logger from '../../utils/logger.js';
import pagination from '../../utils/pagination.js';
import CredentialDefRepository from '../repository/credentialDef.respository.js';
@Injectable()
export default class CredentialDefService {
private credentialDefRepository: CredentialDefRepository;
private credentialTypeRepository: CredentialTypeRepository;
public constructor(
private readonly prismaService: PrismaService,
private readonly restClient: RestClientService,
private readonly configService: ConfigService,
private readonly schemaService: SchemasService,
) {
this.credentialDefRepository = new CredentialDefRepository(
this.prismaService,
);
this.credentialTypeRepository = new CredentialTypeRepository(
this.prismaService,
);
}
public async createCredDef(credentialDefDtoPar: CredentialDefDto) {
const credentialDefDto: CredentialDefDto = credentialDefDtoPar;
const schema = await this.schemaService.findBySchemaId(
credentialDefDto.schemaID,
);
logger.info(`is schema already exists ${schema}`);
if (!schema) {
const schemaLedger =
await this.schemaService.getSchemaAndAttributesBySchemaIDFromLedger(
credentialDefDto.schemaID,
);
const schemaObj: SchemaDto = {
schemaID: credentialDefDto.schemaID,
name: schemaLedger.name,
version: schemaLedger.version,
attributes: schemaLedger.attrNames,
createdBy: '',
id: '',
type: '',
createdDate: new Date(),
updatedDate: new Date(),
};
const createschema = await this.schemaService.createSchemas(schemaObj);
logger.info(`Schema created in Database ${JSON.stringify(createschema)}`);
}
if (
credentialDefDto.type &&
typeof credentialDefDto.type === 'string' &&
credentialDefDto.type.trim().length > 0
) {
await this.credentialTypeRepository.createOrUpdateCredentialsType({
schemaId: credentialDefDto.schemaID,
type: credentialDefDto.type.trim(),
});
}
delete credentialDefDto.type;
return this.credentialDefRepository.createCredDef(credentialDefDto);
}
public async findCredentialDef(
pageSize: number,
page: number,
getSchemaID: string,
) {
let query: {
skip?: number;
take?: number;
cursor?: Prisma.CredentialDefWhereUniqueInput;
where?: Prisma.CredentialDefWhereInput;
orderBy?: Prisma.CredentialDefOrderByWithRelationInput;
} = {};
if (getSchemaID) {
query.where = {
schemaID: getSchemaID,
};
}
query = { ...query, ...pagination(pageSize, page) };
return this.credentialDefRepository.findCredentialDef(query);
}
public async findCredentialDefBySchemaIdAndCredDefId(data: {
schemaID: string;
credDefId: string;
}) {
return this.credentialDefRepository.findCredentialDef({
where: data,
});
}
public async findCredentialDefBySchemaIdDesc(data: { schemaID: string }) {
return this.credentialDefRepository.findCredentialDef({
where: data,
orderBy: {
createdDate: 'desc',
},
});
}
public async findCredentialDefById(id: string) {
return this.credentialDefRepository.findCredentialDef({
where: { credDefId: id },
});
}
public async checkCredDefByNameAndSchemaID(createSchema: CredentialDefDto) {
return this.credentialDefRepository.findCredentialDef({
where: {
schemaID: {
equals: createSchema.schemaID, // Default value: default
},
name: {
equals: createSchema.name, // Default mode
},
},
});
}
public async createCredDefOnLedger(credentialDefDto: CredentialDefLedgerDto) {
const agentUrl = this.configService.get('agent.AGENT_URL');
return this.restClient.post(
`${agentUrl}/credential-definitions/`,
credentialDefDto,
);
}
}
import credDefStub from '../stubs/credDef.stub.js';
const CredentialDefServiceMock = jest.fn().mockReturnValue({
createCredDef: jest.fn().mockReturnValue(credDefStub()),
findCredentialDef: jest.fn().mockReturnValue([1, [credDefStub()]]),
findCredentialDefById: jest.fn().mockReturnValue([1, [credDefStub()]]),
findCredentialDefBySchemaIdDesc: jest
.fn()
.mockReturnValue([1, [credDefStub()]]),
checkCredDefByNameAndSchemaID: jest
.fn()
.mockImplementation((cd) =>
cd.id === credDefStub().id ? [0, []] : [1, [credDefStub()]],
),
getAgentByParticipantId: jest.fn().mockReturnValue([1, [credDefStub()]]),
createCredDefOnLedger: jest.fn().mockReturnValue({ id: 'new-creddef-id' }),
});
export default CredentialDefServiceMock;
import type { TestingModule } from '@nestjs/testing';
import type { Response } from 'express';
import { HttpStatus } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { createResponse } from 'node-mocks-http';
import CredentialDefController from '../controller/controller.js';
import CredentialDefService from '../services/service.js';
import CredentialDefServiceMock from './__mocks__/service.js';
import credDefStub from './stubs/credDef.stub.js';
describe('CredentialDefController', () => {
let credentialDefController: CredentialDefController;
let spyService: CredentialDefService;
beforeEach(async () => {
const CredentialDefServiceProvider = {
provide: CredentialDefService,
useFactory: CredentialDefServiceMock,
};
const module: TestingModule = await Test.createTestingModule({
imports: [],
controllers: [CredentialDefController],
providers: [CredentialDefServiceProvider, ConfigService],
}).compile();
credentialDefController = module.get<CredentialDefController>(
CredentialDefController,
);
spyService = module.get<CredentialDefService>(CredentialDefService);
jest.clearAllMocks();
});
it('should be defined', () => {
expect(credentialDefController).toBeDefined();
});
describe('findCredentialDef()', () => {
let query: {
pageSize: string;
page: string;
schemaID: string;
};
let credDefResponse: Response<string, Record<string, unknown>>;
beforeEach(async () => {
query = {
pageSize: '10',
page: '1',
schemaID: credDefStub().schemaID,
};
const response = createResponse();
credDefResponse = await credentialDefController.findCredentialDef(
query,
response,
);
});
it('should call findCredentialDef() from service', async () => {
expect(spyService.findCredentialDef).toHaveBeenCalled();
});
it('should retrieve CredDef for a correct ID', async () => {
expect(
spyService.findCredentialDef(
query.pageSize ? parseInt(query.pageSize, 10) : 10,
query.page ? parseInt(query.page, 10) : 0,
query.schemaID ? query.schemaID : '',
),
).toEqual([1, [credDefStub()]]);
});
it(`should retrieve HTTP status success(${HttpStatus.OK})`, async () => {
expect(credDefResponse?.statusCode).toEqual(HttpStatus.OK);
});
});
describe('findCredentialDefById()', () => {
let credDefID: string;
let credDef: Response<string, Record<string, unknown>>;
beforeEach(async () => {
credDefID = credDefStub().id;
const response = createResponse();
credDef = await credentialDefController.findCredentialDefById(
credDefID,
response,
);
});
it('should call findCredentialDefById() for a CredDef', async () => {
expect(spyService.findCredentialDefById).toHaveBeenCalled();
});
it('should retrieve CredDef for a correct ID', async () => {
expect(spyService.findCredentialDefById(credDefID)).toEqual([
1,
[credDefStub()],
]);
});
it(`should retrieve HTTP status success(${HttpStatus.OK})`, async () => {
expect(credDef?.statusCode).toEqual(HttpStatus.OK);
});
});
describe('createCredDef()', () => {
let credDef: Response<string, Record<string, unknown>>;
beforeEach(async () => {
const response = createResponse();
credDef = await credentialDefController.createCredentialDef(
credDefStub(),
response,
);
});
it('should call checkCredDefByNameAndSchemaID() from service', async () => {
expect(spyService.checkCredDefByNameAndSchemaID).toHaveBeenCalled();
});
it('should not retrieve CredDef for a given credDef', async () => {
expect(spyService.checkCredDefByNameAndSchemaID(credDefStub())).toContain(
0,
);
});
it('should call createCredDefOnLedger() from service', async () => {
expect(spyService.createCredDefOnLedger).toHaveBeenCalled();
});
it(`should retrieve HTTP status created(${HttpStatus.CREATED})`, async () => {
expect(credDef?.statusCode).toEqual(HttpStatus.CREATED);
});
});
});
import type { TestingModule } from '@nestjs/testing';
import { HttpModule } from '@nestjs/axios';
import { Test } from '@nestjs/testing';
import NatsClientService from '../../client/nats.client.js';
import RestClientService from '../../client/rest.client.js';
import NatsClientServiceMock from '../../client/tests/__mocks__/nats.client.js';
import RestClientServiceMock from '../../client/tests/__mocks__/rest.client.js';
import PrismaService from '../../prisma/prisma.service.js';
import PrismaServiceMock from '../../prisma/tests/__mocks__/prisma.service.js';
import CredentialDefModule from '../module.js';
import CredentialDefService from '../services/service.js';
import CredentialDefServiceMock from './__mocks__/service.js';
describe('CredentialDefModule', () => {
let credentialDefModule: CredentialDefModule;
const CredentialDefServiceProvider = {
provide: CredentialDefService,
useFactory: CredentialDefServiceMock,
};
const PrismaServiceProvider = {
provide: PrismaService,
useFactory: PrismaServiceMock,
};
const NatsClientServiceProvider = {
provide: NatsClientService,
useFactory: NatsClientServiceMock,
};
const RestClientServiceProvider = {
provide: RestClientService,
useFactory: RestClientServiceMock,
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [HttpModule],
providers: [
CredentialDefModule,
CredentialDefServiceProvider,
PrismaServiceProvider,
NatsClientServiceProvider,
RestClientServiceProvider,
],
}).compile();
credentialDefModule = module.get<CredentialDefModule>(CredentialDefModule);
});
it('should be defined', () => {
expect(credentialDefModule).toBeDefined();
});
});
import type { TestingModule } from '@nestjs/testing';
import type { CredentialDef } from '@prisma/client';
import { HttpModule } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import NatsClientService from '../../client/nats.client.js';
import RestClientService from '../../client/rest.client.js';
import NatsClientServiceMock from '../../client/tests/__mocks__/nats.client.js';
import RestClientServiceMock from '../../client/tests/__mocks__/rest.client.js';
import PrismaService from '../../prisma/prisma.service.js';
import PrismaServiceMock from '../../prisma/tests/__mocks__/prisma.service.js';
import SchemasService from '../../schemas/services/service.js';
import SchemasServiceMock from '../../schemas/tests/__mocks__/service.js';
import CredentialDefService from '../services/service.js';
import credDefStub from './stubs/credDef.stub.js';
describe('CredentialDefService', () => {
let credDefService: CredentialDefService;
const PrismaServiceProvider = {
provide: PrismaService,
useFactory: PrismaServiceMock,
};
const NatsClientServiceProvider = {
provide: NatsClientService,
useFactory: NatsClientServiceMock,
};
const RestClientServiceProvider = {
provide: RestClientService,
useFactory: RestClientServiceMock,
};
const SchemasServiceProvider = {
provide: SchemasService,
useFactory: SchemasServiceMock,
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [HttpModule],
providers: [
CredentialDefService,
PrismaServiceProvider,
NatsClientServiceProvider,
RestClientServiceProvider,
ConfigService,
SchemasServiceProvider,
],
}).compile();
credDefService = module.get<CredentialDefService>(CredentialDefService);
});
it('should be defined', () => {
expect(credDefService).toBeDefined();
});
describe('createCredDef()', () => {
let credDefResponse: CredentialDef;
beforeEach(async () => {
credDefResponse = await credDefService.createCredDef(credDefStub());
});
it('should call create() from PrismaService', async () => {
expect(PrismaServiceMock().credentialDef.create).toHaveBeenCalled();
});
it('should retrieve credDef by participantId', async () => {
expect(credDefResponse).toEqual([credDefStub()]);
});
});
describe('findCredentialDef()', () => {
let credDefResponse: Array<number | CredentialDef[]>;
let id: string;
beforeEach(async () => {
const pageSize = 10;
const page = 10;
id = credDefStub().credDefId;
credDefResponse = await credDefService.findCredentialDef(
pageSize,
page,
id,
);
});
it('should call findMany() from PrismaService.credentialDef', async () => {
expect(PrismaServiceMock().credentialDef.findMany).toHaveBeenCalled();
});
it('should call count() from PrismaService.credentialDef', async () => {
expect(PrismaServiceMock().credentialDef.count).toHaveBeenCalled();
});
it('should call $transaction() from PrismaService', async () => {
expect(PrismaServiceMock().$transaction).toHaveBeenCalled();
});
it('should retrieve schema by ID', async () => {
expect(credDefResponse).toEqual([1, [credDefStub()]]);
});
});
describe('findCredentialDefById()', () => {
let credDefResponse: Array<number | CredentialDef[]>;
let id: string;
beforeEach(async () => {
id = credDefStub().credDefId;
credDefResponse = await credDefService.findCredentialDefById(id);
});
it('should call findMany() from PrismaService.credentialDef', async () => {
expect(PrismaServiceMock().credentialDef.findMany).toHaveBeenCalled();
});
it('should call count() from PrismaService.credentialDef', async () => {
expect(PrismaServiceMock().credentialDef.count).toHaveBeenCalled();
});
it('should call $transaction() from PrismaService', async () => {
expect(PrismaServiceMock().$transaction).toHaveBeenCalled();
});
it('should retrieve schema by ID', async () => {
expect(credDefResponse).toEqual([1, [credDefStub()]]);
});
});
describe('checkCredDefByNameAndSchemaID()', () => {
let credDefResponse: Array<number | CredentialDef[]>;
beforeEach(async () => {
credDefResponse =
await credDefService.checkCredDefByNameAndSchemaID(credDefStub());
});
it('should call findMany() from PrismaService.credentialDef', async () => {
expect(PrismaServiceMock().credentialDef.findMany).toHaveBeenCalled();
});
it('should call count() from PrismaService.credentialDef', async () => {
expect(PrismaServiceMock().credentialDef.count).toHaveBeenCalled();
});
it('should call $transaction() from PrismaService', async () => {
expect(PrismaServiceMock().$transaction).toHaveBeenCalled();
});
it('should retrieve credDef by Name and Schema ID', async () => {
expect(credDefResponse).toEqual([1, [credDefStub()]]);
});
});
describe('createCredDefOnLedger()', () => {
let credDefResponse: Array<number | CredentialDef[]>;
beforeEach(async () => {
credDefResponse = await credDefService.createCredDefOnLedger({
schemaId: credDefStub().schemaID,
supportRevocation: credDefStub().supportRevocation,
tag: credDefStub().tag,
});
});
it('should call post() from restClient', async () => {
expect(RestClientServiceMock().post).toHaveBeenCalled();
});
it('should get a response from AFJ', async () => {
expect(credDefResponse).not.toBe(null);
});
});
});
import type CredentialDefDto from '../../entities/credentialDef-entity.js';
const credDefStub = (): CredentialDefDto =>
({
id: 'cred-def-stub-id',
schemaID: 'cred-def-stub-schema-id',
name: 'cred-def-stub-name',
credDefId: 'cred-def-stub-cred-def-id',
supportRevocation: true,
isRevokable: true,
isAutoIssue: true,
expiryHours: '48',
createdBy: 'cred-def-stub-created-by-id',
createdDate: new Date(2022),
updatedBy: 'cred-def-stub-updated-by-id',
updatedDate: new Date(2022),
tag: 'cred-def-stub-tag',
}) as CredentialDefDto;
export default credDefStub;
import type { ResponseType } from '../common/response.js';
import { Controller, Get, HttpStatus, Version } from '@nestjs/common';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
@Controller('health')
export default class HealthController {
public res: ResponseType;
@Version(['1'])
@Get()
@ApiOperation({
summary: 'Health check',
description:
'This call provides the capability to check the service is working and up. The call returns 200 Status Code and current server time in json body',
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Service is up and running.',
content: {
'application/json': {
schema: {},
examples: {
'Service is up and running.': {
value: {
statusCode: 200,
message:
'Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)',
},
},
},
},
},
})
public getHealth() {
this.res = {
statusCode: HttpStatus.OK,
message: `${new Date()}`,
};
return this.res;
}
}
import type { ResponseType } from '../../common/response.js';
import type { TestingModule } from '@nestjs/testing';
import { HttpStatus } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import HealthController from '../health.controller.js';
describe('HealthController', () => {
let healthController: HealthController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HealthController],
}).compile();
healthController = module.get<HealthController>(HealthController);
jest.clearAllMocks();
});
it('should be defined', () => {
expect(healthController).toBeDefined();
});
describe('getHealth()', () => {
let health: ResponseType;
beforeEach(async () => {
health = healthController.getHealth();
});
it(`should retrieve HTTP status created(${HttpStatus.OK})`, async () => {
expect(health.statusCode).toEqual(HttpStatus.OK);
});
});
});
import { IsString, IsNotEmpty } from 'class-validator';
export default class CredentialDto {
@IsString()
@IsNotEmpty()
public credentialId: string;
@IsString()
@IsNotEmpty()
public credDefId: string;
@IsString()
public schemaId?: string;
@IsString()
@IsNotEmpty()
public participantId?: string;
@IsString()
@IsNotEmpty()
public principalDid?: string;
@IsString()
@IsNotEmpty()
public state: string;
@IsString()
@IsNotEmpty()
public threadId: string;
@IsString()
@IsNotEmpty()
public connectionId: string;
@IsString()
public expirationDate?: Date | null;
}
import { IsString, IsNotEmpty } from 'class-validator';
export default class CredentialStateDto {
@IsString()
@IsNotEmpty()
public id: string;
@IsNotEmpty()
public metadata: {
'_internal/indyCredential': {
credentialDefinitionId: string;
schemaId: string;
};
};
@IsString()
@IsNotEmpty()
public credDefId: string;
@IsString()
@IsNotEmpty()
public state: string;
@IsString()
@IsNotEmpty()
public threadId: string;
@IsString()
@IsNotEmpty()
public connectionId: string;
}
import { IsString } from 'class-validator';
export default class GetIssueCredentialsDto {
@IsString()
public connectionId: string;
}
import { IsString } from 'class-validator';
export default class GetCredentialParams {
@IsString()
public id: string;
}
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty } from 'class-validator';
export default class UpdateSchemaIdByTypeDto {
@IsString()
@IsNotEmpty()
@ApiProperty()
public schemaId: string;
}