Skip to content

Commit

Permalink
Merge pull request #37 from hypersign-protocol/implement/cavachCredit
Browse files Browse the repository at this point in the history
Implement/cavach credit
  • Loading branch information
Vishwas1 authored Sep 15, 2024
2 parents 1eecf60 + 4ff6d42 commit 4d6082c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
19 changes: 14 additions & 5 deletions src/app-auth/services/app-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ export class AppAuthService {
],
authGrantTxnMsgAndFeeDID.fee,
);
await this.authzCreditService.createAuthzCredits({
userId,
appId,
});
}

await this.authzCreditService.createAuthzCredits({
userId,
appId,
});
// Finally stroring application in db
// const txns = {
// transactionHash: '',
Expand All @@ -228,7 +228,16 @@ export class AppAuthService {
hasDomainVerified: createAppDto.hasDomainVerified,
});
Logger.log('App created successfully', 'app-auth-service');
return this.getAppResponse(appData, apiSecretKey);
const appResponse = this.getAppResponse(appData, apiSecretKey);
if (service.id == SERVICE_TYPES.CAVACH_API) {
this.authzCreditService.grantCavachCredit(
subdomain,
appId,
createAppDto.env ? createAppDto.env : APP_ENVIRONMENT.dev,
appResponse.tenantUrl,
);
}
return appResponse;
}

private getAppResponse(
Expand Down
2 changes: 2 additions & 0 deletions src/credits/credits.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AdminPeople,
AdminPeopleSchema,
} from 'src/people/schema/people.schema';
import { JwtModule } from '@nestjs/jwt';

@Module({
imports: [
Expand All @@ -22,6 +23,7 @@ import {
MongooseModule.forFeature([
{ name: AdminPeople.name, schema: AdminPeopleSchema },
]),
JwtModule.register({}),
],
controllers: [CreditsController],
providers: [
Expand Down
40 changes: 39 additions & 1 deletion src/credits/services/credits.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { AuthZCreditsRepository } from '../repositories/authz.repository';
import { scope } from '../../credits/schemas/authz.schema';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { APP_ENVIRONMENT } from 'src/supported-service/services/iServiceList';

@Injectable()
export class AuthzCreditService {
constructor(
private readonly authzCreditsRepository: AuthZCreditsRepository,
private readonly config: ConfigService,
private readonly jwt: JwtService,
) {}

async createAuthzCredits(authz: { userId; appId }) {
Expand Down Expand Up @@ -37,4 +40,39 @@ export class AuthzCreditService {
appId,
});
}

async grantCavachCredit(
subdomain: string,
appId: string,
environmentMode: APP_ENVIRONMENT,
tenantUrl: string,
) {
Logger.log(
'grantCavachCredit() method to grant free credit to cavach service',
'AuthZCreditService',
);
const payload = {
purpose: 'CreditRecharge',
amount: 1501, // will take this as function parameter later
amountDenom: 'uHID',
validityPeriod: 6,
serviceId: appId,
validityPeriodUnit: 'MONTH',
env: environmentMode,
subdomain,
};
const token = await this.jwt.signAsync(payload, {
expiresIn: '5m',
secret: this.config.get('JWT_SECRET'),
});
fetch(`${tenantUrl}api/v1/credit`, {
method: 'POST',
headers: {
'Content-Type': 'Application/json',
authorization: `Bearer ${token}`,
},
}).catch((error) => {
Logger.error('Failed to grant credit:', error);
});
}
}
2 changes: 1 addition & 1 deletion src/people/repository/people.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class AdminPeopleRepository {
if: {
$eq: [
{
$size: '$authenticators',
$size: { $ifNull: ['$authenticators', []] },
},
0,
],
Expand Down

0 comments on commit 4d6082c

Please sign in to comment.