import { HttpService } from '@nestjs/axios'; import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { lastValueFrom } from 'rxjs'; import { Action } from '../../domain/dtos/action.enum'; import { Domain } from '../../domain/dtos/domain.enum'; import { IMakeDecision } from '../../domain/interfaces/decision-maker'; import { ContextItem } from '../../domain/dtos/context-item'; import { Decision } from './decision'; import { Authorization } from '../../domain/entities/authorization'; @Injectable() export class OpaDecisionMaker extends IMakeDecision { constructor( private readonly _configService: ConfigService, private readonly _httpService: HttpService, ) { super(); } async decide( uuid: string, domain: Domain, action: Action, context: Array, ): Promise { const { data } = await lastValueFrom( this._httpService.post( this._configService.get('OPA_URL') + domain + '/' + action, { input: { uuid, ...context, }, }, ), ); return new Authorization(data.result.allow); } }