Skip to content

Commit ba330fc

Browse files
authored
Merge pull request #169 from hypersign-protocol/bug/fix
Bug/fix
2 parents e9a8656 + 413c1e7 commit ba330fc

File tree

6 files changed

+180
-22
lines changed

6 files changed

+180
-22
lines changed

src/credit-manager/controllers/credit-manager.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export class CreditManagerController {
9797
@ApiOkResponse({
9898
description: 'Fetched all credit detail',
9999
type: createCreditResponse,
100+
isArray: true,
100101
})
101102
@ApiBadRequestResponse({
102103
description: 'Unable to fetch credit detail',
@@ -118,7 +119,7 @@ export class CreditManagerController {
118119
@UseGuards(AuthGuard('jwt'))
119120
@ApiOkResponse({
120121
description: 'The details of the credit have been successfully fetched.',
121-
type: ActivateCredtiResponse,
122+
type: createCreditResponse,
122123
})
123124
@ApiBadRequestResponse({
124125
description: 'Unable to fetch particular credit detail',

src/credit-manager/dto/create-credit-manager.dto.ts

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { IsNumber, IsString, Min } from 'class-validator';
2+
import { IsNumber, IsString, Min, ValidateNested } from 'class-validator';
33
import { Status } from '../schema/credit-manager.schema';
4+
import { Type } from 'class-transformer';
45
export enum ValidityPeriodUnit {
56
DAYS = 'DAYS',
67
WEEK = 'WEEK',
@@ -22,6 +23,29 @@ export class CreateCreditManagerDto {
2223
creditDenom: string;
2324
}
2425

26+
export class Credit {
27+
@ApiProperty({
28+
name: 'amount',
29+
description: 'Total available hid',
30+
example: '5000000',
31+
})
32+
@IsNumber()
33+
amount: number;
34+
@ApiProperty({
35+
name: 'denom',
36+
description: 'Token denom',
37+
example: 'uhid',
38+
})
39+
@IsNumber()
40+
denom: number;
41+
@ApiProperty({
42+
name: 'used',
43+
description: 'Total used credit',
44+
example: 0,
45+
})
46+
@IsNumber()
47+
used: number;
48+
}
2549
export class createCreditResponse {
2650
@ApiProperty({
2751
name: 'totalCredits',
@@ -30,6 +54,13 @@ export class createCreditResponse {
3054
})
3155
@IsNumber()
3256
totalCredits: number;
57+
@ApiProperty({
58+
name: 'creditDenom',
59+
description: 'Token denom',
60+
example: 'uHID',
61+
})
62+
@IsNumber()
63+
creditDenom: string;
3364
@ApiProperty({
3465
name: 'used',
3566
description: 'Total number of credit used till now',
@@ -41,10 +72,17 @@ export class createCreditResponse {
4172
name: 'validityDuration',
4273
description:
4374
'The number of days the credit is valid from the date of activation',
44-
example: 60,
75+
example: 42,
4576
})
4677
@IsNumber()
4778
validityDuration: number;
79+
// @ApiProperty({
80+
// name: 'expiresAt',
81+
// description: 'Time at which document is added',
82+
// example: '2025-04-22T12:50:03.984Z',
83+
// required: false
84+
// })
85+
// expiresAt: Date;
4886
@ApiProperty({
4987
name: 'status',
5088
description:
@@ -54,6 +92,34 @@ export class createCreditResponse {
5492
})
5593
@IsString()
5694
status: string;
95+
@ApiProperty({
96+
name: 'serviceId',
97+
description: 'Id of the service',
98+
example: 'fc0392830696e097b1d7e0607968e9dd3400',
99+
})
100+
@IsString()
101+
serviceId: string;
102+
@ApiProperty({
103+
name: 'credit',
104+
type: Credit,
105+
})
106+
@Type(() => Credit)
107+
@ValidateNested()
108+
credit: Credit;
109+
@ApiProperty({
110+
name: 'creditScope',
111+
description: 'Scope that one will get',
112+
example: [
113+
'MsgRegisterDID',
114+
'MsgDeactivateDID',
115+
'MsgRegisterCredentialSchema',
116+
'MsgUpdateDID',
117+
'MsgUpdateCredentialStatus',
118+
'MsgRegisterCredentialStatus',
119+
],
120+
})
121+
@IsString()
122+
creditScope: Array<string>;
57123
@ApiProperty({
58124
name: '_id',
59125
description: 'Unique identifier of credit detail',
@@ -64,14 +130,14 @@ export class createCreditResponse {
64130
@ApiProperty({
65131
name: 'createdAt',
66132
description: 'Time at which document is added',
67-
example: '2024-09-10T12:50:03.984Z',
133+
example: '2025-03-10T12:50:03.984Z',
68134
})
69135
@IsString()
70136
createdAt: string;
71137
@ApiProperty({
72138
name: 'updatedAt',
73139
description: 'Time at which document last updated',
74-
example: '2024-09-10T12:50:03.984Z',
140+
example: '2025-03-10T12:50:03.984Z',
75141
})
76142
@IsString()
77143
updatedAt: string;
@@ -91,8 +157,8 @@ export class ActivateCredtiResponse extends createCreditResponse {
91157
name: 'expiresAt',
92158
description:
93159
'The date and time when the credit expires. After this timestamp, the credit is no longer valid.',
94-
example: '2024-11-10T12:50:03.984Z',
160+
example: '2025-04-22T12:50:03.984Z',
95161
})
96162
@IsString()
97-
expiresAt: string;
163+
expiresAt: Date;
98164
}

src/credit-manager/gaurd/reduce-credit.gaurd.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class ReduceCreditGuard implements CanActivate {
2323
//Check if the user has a valid plan with enough balance
2424
const creditDetails = await this.creditManagerService.hasValidCredit(req);
2525
const activeCredit = await this.creditService.getActiveCredit(
26-
String(creditDetails.attestationCost),
26+
String(creditDetails.attestationCost.hidCost),
2727
);
2828
if (!creditDetails['hasSufficientFund']) {
2929
Logger.error(
@@ -53,7 +53,7 @@ export class ReduceCreditGuard implements CanActivate {
5353
used: activeCredit.used + creditDetails.creditAmountRequired,
5454
[`credit.used`]:
5555
Number(activeCredit?.credit?.used || 0) +
56-
Number(creditDetails.attestationCost),
56+
Number(creditDetails.attestationCost.hidCost),
5757
},
5858
);
5959
Logger.log('Credits deducted successfully', 'ReduceCreditGuard');

src/credit-manager/managers/credit-manager.service.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ export class CreditManagerService {
2727
: 0;
2828
const attestationCost = attestationType
2929
? await this.attestationCreditService.calculateCost(attestationType)
30-
: '0';
31-
const creditAmountRequired = apiCost + storageCost;
30+
: { hidCost: 0, creditCost: 0 };
31+
const { hidCost, creditCost } = attestationCost;
32+
const creditAmountRequired = apiCost + storageCost + creditCost;
3233

3334
// Fetch user's active plan
3435
let activeCredit = await this.creditService.getActiveCredit(
35-
String(attestationCost),
36+
String(hidCost),
3637
);
3738

3839
if (
@@ -69,8 +70,9 @@ export class CreditManagerService {
6970
: 0;
7071
const attestationCost = attestationType
7172
? await this.attestationCreditService.calculateCost(attestationType)
72-
: '0';
73-
const creditAmountRequired = apiCost + storageCost;
74-
return { attestationCost, creditAmountRequired };
73+
: { hidCost: 0, creditCost: 0 };
74+
const { hidCost, creditCost } = attestationCost;
75+
const creditAmountRequired = apiCost + storageCost + creditCost;
76+
return { hidCost, creditAmountRequired };
7577
}
7678
}

src/credit-manager/services/attestation-credit.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export class AttestationCreditService {
88
'Inside calculateCost to calculate credit',
99
'AttestationCreditService',
1010
);
11-
return `${CREDIT_COSTS.ATTESTATION[type]}` || '50';
11+
const hidCost = CREDIT_COSTS.ATTESTATION[type] || 50;
12+
const creditCost = hidCost / 10;
13+
return { hidCost, creditCost };
1214
}
1315
}

src/log/services/log.service.ts

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class LogService {
88
constructor(
99
private readonly logRepo: LogRepository,
1010
private readonly creditManagerService: CreditManagerService,
11-
) {}
11+
) { }
1212
async createLog(log: any) {
1313
Logger.log(
1414
`Storing log to db: ${log.method} ${log.path} ${log.statusCode} ${log.contentLenght} ${log.userAgent} ${log.appId}`,
@@ -68,10 +68,49 @@ export class LogService {
6868
],
6969
},
7070
},
71+
{
72+
$project: {
73+
method: 1,
74+
normalizedPath: {
75+
$switch: {
76+
branches: [
77+
{
78+
case: {
79+
$regexMatch: {
80+
input: '$path',
81+
regex: '^/api/v1/did/resolve/',
82+
},
83+
},
84+
then: '/api/v1/did/resolve',
85+
},
86+
{
87+
case: {
88+
$regexMatch: {
89+
input: '$path',
90+
regex: '^/api/v1/schema/sch:',
91+
},
92+
},
93+
then: '/api/v1/schema/resolve',
94+
},
95+
{
96+
case: {
97+
$regexMatch: {
98+
input: '$path',
99+
regex: '^/api/v1/credential/vc:',
100+
},
101+
},
102+
then: '/api/v1/credential/resolve',
103+
},
104+
],
105+
default: '$path', // If no match, keep the original path
106+
},
107+
},
108+
},
109+
},
71110
{
72111
$group: {
73112
_id: {
74-
path: '$path',
113+
path: '$normalizedPath',
75114
method: '$method',
76115
},
77116
count: { $sum: 1 },
@@ -86,9 +125,9 @@ export class LogService {
86125
},
87126
},
88127
];
128+
89129
const serviceDetails =
90130
await this.logRepo.findDataBasedOnAgggregationPipeline(pipeline);
91-
92131
const updatedServiceDetails = await Promise.all(
93132
serviceDetails.map(async (x) => {
94133
x['apiPath'] = x['apiPath'];
@@ -98,7 +137,7 @@ export class LogService {
98137
x['method'],
99138
x['apiPath'],
100139
);
101-
(x['onchain_unit_cost'] = x['unit_cost']['attestationCost']),
140+
(x['onchain_unit_cost'] = x['unit_cost']['hidCost']),
102141
(x['offchain_unit_cost'] = x['unit_cost']['creditAmountRequired']);
103142
x['onchainAmount'] = Number(
104143
(x['onchain_unit_cost'] * x['quantity']).toFixed(2),
@@ -129,19 +168,67 @@ export class LogService {
129168
{
130169
$match: {
131170
createdAt: { $gte: startDate, $lte: endDate },
171+
path: { $exists: true, $ne: null },
132172
$nor: [
133173
{ path: { $regex: 'usage' } },
134174
{ path: { $regex: 'credit' } },
135175
{ path: { $regex: 'presentation' } },
136176
],
137177
},
138178
},
179+
{
180+
$project: {
181+
method: 1,
182+
createdAt: 1,
183+
path: { $ifNull: ['$path', 'unknown'] },
184+
normalizedPath: {
185+
$switch: {
186+
branches: [
187+
{
188+
case: {
189+
$regexMatch: {
190+
input: '$path',
191+
regex: '^/api/v1/did/resolve/',
192+
},
193+
},
194+
then: '/api/v1/did/resolve',
195+
},
196+
{
197+
case: {
198+
$regexMatch: {
199+
input: '$path',
200+
regex: '^/api/v1/schema/sch:',
201+
},
202+
},
203+
then: '/api/v1/schema/resolve',
204+
},
205+
{
206+
case: {
207+
$regexMatch: {
208+
input: '$path',
209+
regex: '^/api/v1/credential(/|$)',
210+
},
211+
},
212+
then: '/api/v1/credential/resolve',
213+
},
214+
],
215+
default: '$path', // Keep original path if no match
216+
},
217+
},
218+
},
219+
},
139220
{
140221
$group: {
141222
_id: {
142-
path: '$path',
143-
date: { $dateToString: { format: '%Y-%m-%d', date: '$createdAt' } },
223+
path: '$normalizedPath',
224+
date: {
225+
$ifNull: [
226+
{ $dateToString: { format: '%Y-%m-%d', date: '$createdAt' } },
227+
'unknown-date',
228+
],
229+
},
144230
},
231+
145232
count: { $sum: 1 },
146233
},
147234
},

0 commit comments

Comments
 (0)