Skip to content

Commit bd49db6

Browse files
feat: update schema details and add alias in schema table (#1129)
* API for update schema details Signed-off-by: pallavighule <[email protected]> * refactored query Signed-off-by: pallavighule <[email protected]> * chore: added alias in response Signed-off-by: bhavanakarwade <[email protected]> --------- Signed-off-by: pallavighule <[email protected]> Signed-off-by: bhavanakarwade <[email protected]> Co-authored-by: bhavanakarwade <[email protected]>
1 parent 2537c6e commit bd49db6

File tree

12 files changed

+163
-37
lines changed

12 files changed

+163
-37
lines changed

apps/api-gateway/src/dtos/create-schema.dto.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ export class GenericSchemaDTO {
159159
@IsNotEmpty({ message: 'Type is required' })
160160
type: SchemaTypeEnum;
161161

162+
@ApiPropertyOptional()
163+
@Transform(({ value }) => trim(value))
164+
@IsOptional()
165+
@IsString({ message: 'alias must be a string' })
166+
@IsNotEmpty({ message: 'alias is required' })
167+
alias: string;
168+
162169
@ApiProperty({
163170
type: Object,
164171
oneOf: [
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
2+
import { Transform } from 'class-transformer';
3+
import { IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator';
4+
5+
export class UpdateSchemaDto {
6+
@ApiProperty()
7+
@Transform(({ value }) => value?.trim())
8+
@IsNotEmpty({ message: 'Alias is required.' })
9+
@IsString({ message: 'Alias must be in string format.' })
10+
alias: string;
11+
12+
@ApiProperty()
13+
@Transform(({ value }) => value?.trim())
14+
@IsNotEmpty({ message: 'schemaLedgerId is required.' })
15+
@IsString({ message: 'schemaLedgerId must be in string format.' })
16+
schemaLedgerId: string;
17+
18+
@ApiPropertyOptional()
19+
@IsOptional()
20+
@Transform(({ value }) => value?.trim())
21+
@IsUUID('4')
22+
orgId?: string;
23+
}

apps/api-gateway/src/schema/schema.controller.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Controller, Logger, Post, Body, HttpStatus, UseGuards, Get, Query, BadRequestException, Res, UseFilters, Param, ParseUUIDPipe } from '@nestjs/common';
1+
import { Controller, Logger, Post, Body, HttpStatus, UseGuards, Get, Query, BadRequestException, Res, UseFilters, Param, ParseUUIDPipe, Put } from '@nestjs/common';
22
/* eslint-disable @typescript-eslint/no-unused-vars */
33
/* eslint-disable camelcase */
4-
import { ApiOperation, ApiResponse, ApiTags, ApiBearerAuth, ApiForbiddenResponse, ApiUnauthorizedResponse, ApiQuery, ApiExtraModels, ApiBody, getSchemaPath } from '@nestjs/swagger';
4+
import { ApiOperation, ApiResponse, ApiTags, ApiBearerAuth, ApiForbiddenResponse, ApiUnauthorizedResponse, ApiQuery, ApiExcludeEndpoint } from '@nestjs/swagger';
55
import { SchemaService } from './schema.service';
66
import { AuthGuard } from '@nestjs/passport';
77
import { ApiResponseDto } from '../dtos/apiResponse.dto';
@@ -21,6 +21,7 @@ import { GenericSchemaDTO } from '../dtos/create-schema.dto';
2121
import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler';
2222
import { CredDefSortFields, SortFields } from '@credebl/enum/enum';
2323
import { TrimStringParamPipe } from '@credebl/common/cast.helper';
24+
import { UpdateSchemaDto } from './dtos/update-schema-dto';
2425

2526
@UseFilters(CustomExceptionFilter)
2627
@Controller('orgs')
@@ -186,4 +187,28 @@ export class SchemaController {
186187
};
187188
return res.status(HttpStatus.CREATED).json(finalResponse);
188189
}
190+
191+
/**
192+
* Update an schema alias
193+
* @param updateSchemaDto The details of the schema to be updated
194+
* @returns Success message
195+
*/
196+
@Put('/schema')
197+
@ApiOperation({ summary: 'Update schema', description: 'Update the details of the schema' })
198+
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
199+
@ApiExcludeEndpoint()
200+
@ApiBearerAuth()
201+
@Roles(OrgRoles.OWNER, OrgRoles.ADMIN)
202+
@UseGuards(AuthGuard('jwt'))
203+
async updateSchema(@Body() updateSchemaDto: UpdateSchemaDto, @Res() res: Response): Promise<Response> {
204+
205+
await this.appService.updateSchema(updateSchemaDto);
206+
207+
const finalResponse: IResponse = {
208+
statusCode: HttpStatus.OK,
209+
message: ResponseMessages.schema.success.update
210+
};
211+
return res.status(HttpStatus.OK).json(finalResponse);
212+
}
213+
189214
}

apps/api-gateway/src/schema/schema.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { ICredDefWithPagination, ISchemaData, ISchemasWithPagination } from '@cr
88
import { GetCredentialDefinitionBySchemaIdDto } from './dtos/get-all-schema.dto';
99
import { NATSClient } from '@credebl/common/NATSClient';
1010

11+
import { UpdateSchemaResponse } from 'apps/ledger/src/schema/interfaces/schema.interface';
12+
import { UpdateSchemaDto } from './dtos/update-schema-dto';
13+
1114
@Injectable()
1215
export class SchemaService extends BaseService {
1316

@@ -36,4 +39,9 @@ export class SchemaService extends BaseService {
3639
const payload = { schemaSearchCriteria, user };
3740
return this.natsClient.sendNatsMessage(this.schemaServiceProxy, 'get-cred-def-list-by-schemas-id', payload);
3841
}
42+
43+
updateSchema(schemaDetails: UpdateSchemaDto): Promise<UpdateSchemaResponse> {
44+
const payload = { schemaDetails };
45+
return this.natsClient.sendNatsMessage(this.schemaServiceProxy, 'update-schema', payload);
46+
}
3947
}

apps/ledger/src/schema/interfaces/schema-payload.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface ISchema {
1818
endorserWriteTxn?: string;
1919
orgDid?: string;
2020
type?: string;
21+
alias?: string;
2122
}
2223

2324
export interface IAttributeValue {

apps/ledger/src/schema/interfaces/schema.interface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export interface ICreateW3CSchema {
9494
schemaType: JSONSchemaType;
9595
}
9696
export interface IGenericSchema {
97+
alias:string;
9798
type: SchemaTypeEnum;
9899
schemaPayload: ICreateSchema | ICreateW3CSchema;
99100
}
@@ -123,4 +124,15 @@ export interface ISchemasResult {
123124
export interface ISchemasList {
124125
schemasCount: number;
125126
schemasResult: ISchemasResult[];
127+
}
128+
129+
130+
export interface IUpdateSchema {
131+
alias: string;
132+
schemaLedgerId: string;
133+
orgId?: string;
134+
}
135+
136+
export interface UpdateSchemaResponse {
137+
count: number;
126138
}

apps/ledger/src/schema/repositories/schema.repository.ts

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { PrismaService } from '@credebl/prisma-service';
44
import { ledgers, org_agents, org_agents_type, organisation, Prisma, schema } from '@prisma/client';
55
import { ISchema, ISchemaExist, ISchemaSearchCriteria, ISaveSchema } from '../interfaces/schema-payload.interface';
66
import { ResponseMessages } from '@credebl/common/response-messages';
7-
import { AgentDetails, ISchemasWithCount } from '../interfaces/schema.interface';
7+
import { AgentDetails, ISchemasWithCount, IUpdateSchema, UpdateSchemaResponse } from '../interfaces/schema.interface';
88
import { SchemaType, SortValue } from '@credebl/enum/enum';
99
import { ICredDefWithCount, IPlatformSchemas } from '@credebl/common/interfaces/schema.interface';
1010
import { ISchemaId } from '../schema.interface';
@@ -38,7 +38,8 @@ export class SchemaRepository {
3838
orgId: schemaResult.orgId,
3939
ledgerId: schemaResult.ledgerId,
4040
type: schemaResult.type,
41-
isSchemaArchived: false
41+
isSchemaArchived: false,
42+
alias: schemaResult.alias
4243
}
4344
});
4445
return saveResult;
@@ -119,8 +120,9 @@ export class SchemaRepository {
119120
publisherDid: true,
120121
orgId: true,
121122
issuerId: true,
123+
alias: true,
122124
organisation: {
123-
select:{
125+
select: {
124126
name: true,
125127
userOrgRoles: {
126128
select: {
@@ -299,11 +301,11 @@ export class SchemaRepository {
299301
try {
300302
const { ledgerId, schemaType, searchByText, sortField, sortBy, pageSize, pageNumber } = payload;
301303
let schemaResult;
302-
/**
303-
* This is made so because the default pageNumber is set to 1 in DTO,
304+
/**
305+
* This is made so because the default pageNumber is set to 1 in DTO,
304306
* If there is any 'searchByText' field, we ignore the pageNumbers and search
305307
* in all available records.
306-
*
308+
*
307309
* Because in that case pageNumber would be insignificant.
308310
*/
309311
if (searchByText) {
@@ -316,7 +318,8 @@ export class SchemaRepository {
316318
{ name: { contains: searchByText, mode: 'insensitive' } },
317319
{ version: { contains: searchByText, mode: 'insensitive' } },
318320
{ schemaLedgerId: { contains: searchByText, mode: 'insensitive' } },
319-
{ issuerId: { contains: searchByText, mode: 'insensitive' } }
321+
{ issuerId: { contains: searchByText, mode: 'insensitive' } },
322+
{ alias: { contains: searchByText, mode: 'insensitive' } }
320323
]
321324
},
322325
select: {
@@ -328,9 +331,10 @@ export class SchemaRepository {
328331
isSchemaArchived: true,
329332
createdBy: true,
330333
publisherDid: true,
331-
orgId: true, // This field can be null
334+
orgId: true, // This field can be null
332335
issuerId: true,
333-
type: true
336+
type: true,
337+
alias: true
334338
},
335339
orderBy: {
336340
[sortField]: SortValue.DESC === sortBy ? SortValue.DESC : SortValue.ASC
@@ -356,9 +360,10 @@ export class SchemaRepository {
356360
isSchemaArchived: true,
357361
createdBy: true,
358362
publisherDid: true,
359-
orgId: true, // This field can be null
363+
orgId: true, // This field can be null
360364
issuerId: true,
361-
type: true
365+
type: true,
366+
alias: true
362367
},
363368
orderBy: {
364369
[sortField]: SortValue.DESC === sortBy ? SortValue.DESC : SortValue.ASC
@@ -376,7 +381,7 @@ export class SchemaRepository {
376381
});
377382

378383
// Handle null orgId in the response
379-
const schemasWithDefaultOrgId = schemaResult.map(schema => ({
384+
const schemasWithDefaultOrgId = schemaResult.map((schema) => ({
380385
...schema,
381386
orgId: schema.orgId || null // Replace null orgId with 'N/A' or any default value
382387
}));
@@ -429,21 +434,23 @@ export class SchemaRepository {
429434
}
430435
}
431436

432-
async schemaExist(payload: ISchemaExist): Promise<{
433-
id: string;
434-
createDateTime: Date;
435-
createdBy: string;
436-
lastChangedDateTime: Date;
437-
lastChangedBy: string;
438-
name: string;
439-
version: string;
440-
attributes: string;
441-
schemaLedgerId: string;
442-
publisherDid: string;
443-
issuerId: string;
444-
orgId: string;
445-
ledgerId: string;
446-
}[]> {
437+
async schemaExist(payload: ISchemaExist): Promise<
438+
{
439+
id: string;
440+
createDateTime: Date;
441+
createdBy: string;
442+
lastChangedDateTime: Date;
443+
lastChangedBy: string;
444+
name: string;
445+
version: string;
446+
attributes: string;
447+
schemaLedgerId: string;
448+
publisherDid: string;
449+
issuerId: string;
450+
orgId: string;
451+
ledgerId: string;
452+
}[]
453+
> {
447454
try {
448455
return this.prisma.schema.findMany({
449456
where: {
@@ -456,4 +463,18 @@ export class SchemaRepository {
456463
throw error;
457464
}
458465
}
466+
467+
async updateSchema(schemaDetails: IUpdateSchema): Promise<UpdateSchemaResponse> {
468+
const { alias, schemaLedgerId, orgId } = schemaDetails;
469+
470+
try {
471+
return await this.prisma.schema.updateMany({
472+
where: orgId ? { schemaLedgerId, orgId } : { schemaLedgerId },
473+
data: { alias }
474+
});
475+
} catch (error) {
476+
this.logger.error(`Error in updating schema details: ${error}`);
477+
throw error;
478+
}
479+
}
459480
}

apps/ledger/src/schema/schema.controller.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
} from '@credebl/common/interfaces/schema.interface';
1818
import { IschemaPayload } from './interfaces/schema.interface';
1919
import { ISchemaId } from './schema.interface';
20+
import { UpdateSchemaDto } from 'apps/api-gateway/src/schema/dtos/update-schema-dto';
21+
2022

2123
@Controller('schema')
2224
export class SchemaController {
@@ -96,4 +98,9 @@ export class SchemaController {
9698
async getSchemaRecordBySchemaId(payload: {schemaId: string}): Promise<schema> {
9799
return this.schemaService.getSchemaBySchemaId(payload.schemaId);
98100
}
101+
102+
@MessagePattern({ cmd: 'update-schema' })
103+
updateSchema(payload:{schemaDetails:UpdateSchemaDto}): Promise<object> {
104+
return this.schemaService.updateSchema(payload.schemaDetails);
105+
}
99106
}

apps/ledger/src/schema/schema.service.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { SchemaRepository } from './repositories/schema.repository';
1212
import { Prisma, schema } from '@prisma/client';
1313
import { ISaveSchema, ISchema, ISchemaCredDeffSearchInterface, ISchemaExist, ISchemaSearchCriteria, W3CCreateSchema } from './interfaces/schema-payload.interface';
1414
import { ResponseMessages } from '@credebl/common/response-messages';
15-
import { ICreateSchema, ICreateW3CSchema, IGenericSchema, IUserRequestInterface } from './interfaces/schema.interface';
15+
import { ICreateSchema, ICreateW3CSchema, IGenericSchema, IUpdateSchema, IUserRequestInterface, UpdateSchemaResponse } from './interfaces/schema.interface';
1616
import { CreateSchemaAgentRedirection, GetSchemaAgentRedirection, ISchemaId } from './schema.interface';
1717
import { map } from 'rxjs/operators';
1818
import { JSONSchemaType, LedgerLessConstant, LedgerLessMethods, OrgAgentType, SchemaType, SchemaTypeEnum } from '@credebl/enum/enum';
@@ -48,7 +48,7 @@ export class SchemaService extends BaseService {
4848

4949
const userId = user.id;
5050
try {
51-
const {schemaPayload, type} = schemaDetails;
51+
const {schemaPayload, type, alias} = schemaDetails;
5252

5353
if (type === SchemaTypeEnum.INDY) {
5454

@@ -247,7 +247,7 @@ export class SchemaService extends BaseService {
247247
}
248248
} else if (type === SchemaTypeEnum.JSON) {
249249
const josnSchemaDetails = schemaPayload as unknown as ICreateW3CSchema;
250-
const createW3CSchema = await this.createW3CSchema(orgId, josnSchemaDetails, user.id);
250+
const createW3CSchema = await this.createW3CSchema(orgId, josnSchemaDetails, user.id, alias);
251251
return createW3CSchema;
252252
}
253253
} catch (error) {
@@ -259,7 +259,7 @@ export class SchemaService extends BaseService {
259259
}
260260
}
261261

262-
async createW3CSchema(orgId:string, schemaPayload: ICreateW3CSchema, user: string): Promise<ISchemaData> {
262+
async createW3CSchema(orgId:string, schemaPayload: ICreateW3CSchema, user: string, alias: string): Promise<ISchemaData> {
263263
try {
264264
let createSchema;
265265

@@ -325,7 +325,7 @@ export class SchemaService extends BaseService {
325325
createSchema.schemaUrl = `${process.env.SCHEMA_FILE_SERVER_URL}${createSchemaPayload.data.schemaId}`;
326326
}
327327

328-
const storeW3CSchema = await this.storeW3CSchemas(createSchema, user, orgId, attributes);
328+
const storeW3CSchema = await this.storeW3CSchemas(createSchema, user, orgId, attributes, alias);
329329

330330
if (!storeW3CSchema) {
331331
throw new BadRequestException(ResponseMessages.schema.error.storeW3CSchema, {
@@ -524,7 +524,7 @@ export class SchemaService extends BaseService {
524524
return W3CSchema;
525525
}
526526

527-
private async storeW3CSchemas(schemaDetails, user, orgId, attributes): Promise <schema> {
527+
private async storeW3CSchemas(schemaDetails, user, orgId, attributes, alias): Promise <schema> {
528528
let ledgerDetails;
529529
const schemaServerUrl = `${process.env.SCHEMA_FILE_SERVER_URL}${schemaDetails.schemaId}`;
530530
const schemaRequest = await this.commonService
@@ -563,7 +563,8 @@ export class SchemaService extends BaseService {
563563
publisherDid: schemaDetails.did,
564564
orgId,
565565
ledgerId: ledgerDetails.id,
566-
type: SchemaType.W3C_Schema
566+
type: SchemaType.W3C_Schema,
567+
alias
567568
};
568569
const saveResponse = await this.schemaRepository.saveSchema(
569570
storeSchemaDetails
@@ -928,4 +929,21 @@ export class SchemaService extends BaseService {
928929
throw new RpcException(error.response ? error.response : error);
929930
}
930931
}
932+
933+
934+
async updateSchema(schemaDetails:IUpdateSchema): Promise<UpdateSchemaResponse> {
935+
try {
936+
const schemaSearchResult = await this.schemaRepository.updateSchema(schemaDetails);
937+
938+
if (0 === schemaSearchResult?.count) {
939+
throw new NotFoundException('Records with given condition not found');
940+
}
941+
942+
return schemaSearchResult;
943+
} catch (error) {
944+
this.logger.error(`Error in updateSchema: ${error}`);
945+
throw new RpcException(error.response ? error.response : error);
946+
}
947+
}
948+
931949
}

0 commit comments

Comments
 (0)