import { Body, Controller, HttpException, Post } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import axios from 'axios'; import { Authenticated } from 'src/decorators/authentication.decorator'; import { ApiInternalOnlyController } from 'src/decorators/swagger.decorator'; import { User, RequestUser } from 'src/decorators/user.decorator'; import { getSecretFromSecretsManager } from 'src/utils/SecretManager'; import { INote } from './dtos'; @ApiInternalOnlyController() @ApiTags('Productboard') @Controller('productboard') @Authenticated() 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 getSecretFromSecretsManager(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; } }