Skip to content

Commit

Permalink
Merge pull request #9 from KDwevedi/create-schema
Browse files Browse the repository at this point in the history
Create schema
  • Loading branch information
tushar5526 authored Jul 5, 2023
2 parents 1a43c5d + 71d36e8 commit 9c04590
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { Template } from './entities/Template.entity';
import { TemplateBody } from './entities/TemplateBody.entity';

@Controller('rendering-template')
@Controller('template')
export class RenderingTemplatesController {
constructor(
private readonly renderingTemplateService: RenderingTemplatesService,
Expand Down
5 changes: 1 addition & 4 deletions services/credential-schema/src/schema/schema.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ export class SchemaController {
description: 'There was some prioblem with the request.',
})
updateCredentialSchema(@Param('id') id, @Body() data: CreateCredentialDTO) {
return this.schemaService.updateCredentialSchema({
where: { id: id },
data,
});
return this.schemaService.updateCredentialSchema({ id: id }, data);
}
}
37 changes: 28 additions & 9 deletions services/credential-schema/src/schema/schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { validate } from '../utils/schema.validator';
import { DefinedError } from 'ajv';
import { CreateCredentialDTO } from './dto/create-credentials.dto';
import { UtilsService } from '../utils/utils.service';
import { randomUUID } from 'crypto';

@Injectable()
export class SchemaService {
Expand Down Expand Up @@ -62,11 +61,32 @@ export class SchemaService {
// verify the Credential Schema
const data = createCredentialDto.schema;
const tags = createCredentialDto.tags;

if (validate(data)) {
const didBody = {
content: [
{
alsoKnownAs: [data.author],
services: [
{
id: 'IdentityHub',
type: 'IdentityHub',
serviceEndpoint: {
'@context': 'schema.identity.foundation/hub',
'@type': 'UserServiceEndpoint',
instance: ['did:test:hub.id'],
},
},
],
method: 'schema',
},
],
};
const did = await this.utilService.generateDID(didBody);
const credSchema = {
schema: {
type: data.type,
id: randomUUID(),
id: did.id,
version: data.version,
name: data.name,
author: data.author,
Expand Down Expand Up @@ -119,21 +139,20 @@ export class SchemaService {
}

async updateCredentialSchema(
params: {
where: Prisma.VerifiableCredentialSchemaWhereUniqueInput;
// data: VCSModelSchemaInterface;
data: CreateCredentialDTO;
}, //: Promise<VerifiableCredentialSchema>
where: Prisma.VerifiableCredentialSchemaWhereUniqueInput,
// data: VCSModelSchemaInterface;
data: CreateCredentialDTO,
//: Promise<VerifiableCredentialSchema>
) {
// TODO: Deprecate the schema and create a new one
const { where, data } = params;
this.logger.debug('where', where);
this.logger.debug('data', data);
const _where = { ...where, status: SchemaStatus.DRAFT };
let currentSchema;
try {
currentSchema =
await this.prisma.verifiableCredentialSchema.findUniqueOrThrow({
where,
where: _where,
});
} catch (err) {
this.logger.error(err);
Expand Down
22 changes: 21 additions & 1 deletion services/credential-schema/src/utils/utils.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { HttpService } from '@nestjs/axios';
import { HttpException, Injectable, Logger } from '@nestjs/common';
import {
HttpException,
Injectable,
InternalServerErrorException,
Logger,
} from '@nestjs/common';

@Injectable()
export class UtilsService {
Expand Down Expand Up @@ -28,4 +33,19 @@ export class UtilsService {
throw new HttpException("Couldn't sign the schema", 500);
}
}

async generateDID(body: any) {
try {
const did = await this.httpService.axiosRef.post(
`${process.env.IDENTITY_BASE_URL}/did/generate`,
{
payload: body,
},
);
return did[0];
} catch (err) {
this.logger.error(err, err.response.data);
throw new InternalServerErrorException('Can not generate a new DID');
}
}
}

0 comments on commit 9c04590

Please sign in to comment.