chore: add business logic
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
Logger,
|
||||
NestInterceptor,
|
||||
} from "@nestjs/common";
|
||||
import { FastifyRequest } from "fastify";
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
import { BusinessMessage } from "../../common/enums/message.enum";
|
||||
import { Business } from "../../modules/businesses/entities/business.entity";
|
||||
import { BusinessService } from "../../modules/businesses/services/businesses.service";
|
||||
|
||||
declare module "fastify" {
|
||||
interface FastifyRequest {
|
||||
business?: Business;
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class BusinessInterceptor implements NestInterceptor {
|
||||
private readonly headerName = "x-business-id";
|
||||
private readonly logger = new Logger(BusinessInterceptor.name);
|
||||
|
||||
constructor(private readonly businessService: BusinessService) {}
|
||||
|
||||
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
|
||||
const request = context.switchToHttp().getRequest<FastifyRequest>();
|
||||
|
||||
const businessId = request.headers[this.headerName.toLocaleLowerCase()] as string;
|
||||
|
||||
if (businessId) {
|
||||
try {
|
||||
const business = await this.businessService.getBusinessByDanakSubscriptionId(businessId);
|
||||
|
||||
request.business = business;
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
throw new BadRequestException(BusinessMessage.NOT_FOUND);
|
||||
}
|
||||
} else {
|
||||
throw new ForbiddenException(BusinessMessage.BUSINESS_ID_REQUIRED);
|
||||
}
|
||||
|
||||
return next.handle();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user