import { Body, Controller, HttpException, Post } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import axios from 'axios'; import { User, RequestUser } from 'src/authentication/user.decorator'; import { getSecreteFromSecreteManager } from 'src/utils/SecretManager'; import { INote } from './dtos'; @ApiTags('Productboard') @Controller('productboard') export class ProductboardController { @Post('notes') async sendNote(@Body() note: INote, @User() user: RequestUser) { const { title, content, tags } = note; const { username, customer_name } = user; const email = username.includes('@') ? username : username + `@${customer_name}.default`; const path = process.env.PB_TOKEN_PATH; const token = await getSecreteFromSecreteManager(path); const response = await axios .post( `https://api.productboard.com/notes`, { title, content, tags, user: { email, }, }, { headers: { Authorization: 'Bearer ' + token, }, }, ) .catch(() => null); if (!response) { throw new HttpException( 'Your request could not be submitted, please try again in a few moments or contact support.', 502, ); } return response.data; } }