-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add product feature interceptor and hook (#5327)
* feat: add product feature interceptor and hook * refactor: use product feature hook * fix: after pr comments * refactor: interceptor * test: add tests for product feature * refactor: usage of interceptor * fix: after pr comment * fix: lint error
- Loading branch information
1 parent
8d37ed2
commit 8466e06
Showing
11 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
apps/api/src/app/shared/decorators/product-feature.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Reflector } from '@nestjs/core'; | ||
import { ProductFeatureKeyEnum } from '@novu/shared'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export const ProductFeature = Reflector.createDecorator<ProductFeatureKeyEnum>(); |
67 changes: 67 additions & 0 deletions
67
apps/api/src/app/shared/interceptors/product-feature.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
HttpException, | ||
Injectable, | ||
NestInterceptor, | ||
NotFoundException, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { OrganizationRepository } from '@novu/dal'; | ||
import { | ||
ApiServiceLevelEnum, | ||
IJwtPayload, | ||
productFeatureEnabledForServiceLevel, | ||
ProductFeatureKeyEnum, | ||
} from '@novu/shared'; | ||
import { Observable } from 'rxjs'; | ||
import { ProductFeature } from '../decorators/product-feature.decorator'; | ||
|
||
@Injectable() | ||
export class ProductFeatureInterceptor implements NestInterceptor { | ||
constructor(private reflector: Reflector, private organizationRepository: OrganizationRepository) {} | ||
|
||
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> { | ||
try { | ||
const handler = context.getHandler(); | ||
const classRef = context.getClass(); | ||
const requestedFeature: ProductFeatureKeyEnum | undefined = this.reflector.getAllAndOverride(ProductFeature, [ | ||
handler, | ||
classRef, | ||
]); | ||
|
||
if (requestedFeature === undefined) { | ||
return next.handle(); | ||
} | ||
|
||
const user = this.getReqUser(context); | ||
|
||
if (!user) { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
const { organizationId } = user; | ||
|
||
const organization = await this.organizationRepository.findById(organizationId); | ||
|
||
const enabled = productFeatureEnabledForServiceLevel[requestedFeature].includes( | ||
organization?.apiServiceLevel as ApiServiceLevelEnum | ||
); | ||
|
||
if (!enabled) { | ||
throw new HttpException('Payment Required', 402); | ||
} | ||
|
||
return next.handle(); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
private getReqUser(context: ExecutionContext): IJwtPayload { | ||
const req = context.switchToHttp().getRequest(); | ||
|
||
return req.user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { OrganizationRepository } from '@novu/dal'; | ||
import { ApiServiceLevelEnum } from '@novu/shared'; | ||
import { UserSession } from '@novu/testing'; | ||
import { expect } from 'chai'; | ||
|
||
describe.only('Product feature Test', async () => { | ||
let session: UserSession; | ||
const path = '/v1/testing/product-feature'; | ||
let organizationRepository: OrganizationRepository; | ||
|
||
beforeEach(async () => { | ||
session = new UserSession(); | ||
await session.initialize(); | ||
organizationRepository = new OrganizationRepository(); | ||
}); | ||
|
||
it('should return a number as response when required api service level exists on organization for feature', async () => { | ||
await organizationRepository.update( | ||
{ _id: session.organization._id }, | ||
{ | ||
apiServiceLevel: ApiServiceLevelEnum.BUSINESS, | ||
} | ||
); | ||
const { body } = await session.testAgent.get(path).set('authorization', `ApiKey ${session.apiKey}`).expect(200); | ||
expect(typeof body.data.number === 'number').to.be.true; | ||
}); | ||
|
||
it('should return a 402 response when required api service level does not exists on organization for feature', async () => { | ||
const { body } = await session.testAgent.get(path).set('authorization', `ApiKey ${session.apiKey}`).expect(402); | ||
expect(body).to.deep.equal({ statusCode: 402, message: 'Payment Required' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ApiServiceLevelEnum, productFeatureEnabledForServiceLevel, ProductFeatureKeyEnum } from '@novu/shared'; | ||
import { useEffect, useState } from 'react'; | ||
import { useAuthController } from './useAuthController'; | ||
|
||
export const useProductFeature = (feature: ProductFeatureKeyEnum) => { | ||
const { organization } = useAuthController(); | ||
const [enabled, setEnabled] = useState(false); | ||
|
||
useEffect(() => { | ||
setEnabled( | ||
productFeatureEnabledForServiceLevel[feature].includes(organization?.apiServiceLevel as ApiServiceLevelEnum) | ||
); | ||
}, [feature, organization?.apiServiceLevel]); | ||
|
||
return enabled; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
libs/shared/src/consts/productFeatureEnabledForServiceLevel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ApiServiceLevelEnum, ProductFeatureKeyEnum } from '../types'; | ||
|
||
export const productFeatureEnabledForServiceLevel: Record<ProductFeatureKeyEnum, ApiServiceLevelEnum[]> = Object.freeze( | ||
{ | ||
[ProductFeatureKeyEnum.TRANSLATIONS]: [ApiServiceLevelEnum.BUSINESS, ApiServiceLevelEnum.ENTERPRISE], | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum ProductFeatureKeyEnum { | ||
TRANSLATIONS, | ||
} |