import { Body, Controller, Delete, Get, Param, Patch, Post, Res, StreamableFile, UploadedFile, UseGuards, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { memoryStorage } from 'multer'; import type { Response } from 'express'; import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { CurrentUser } from '../auth/current-user.decorator'; import type { Actor } from '../common/record-scope'; import { OrgModuleGuard } from '../billing/org-module.guard'; import { RequireOrgModule } from '../billing/org-module.decorator'; import { CreateDealDto, ImportDealRowsDto, MoveDealDto, UpdateDealDto } from './dto'; import { DealsService } from './deals.service'; import { DealsExcelService } from './deals-excel.service'; import { brand } from '@mento/shared'; @Controller('deals') @UseGuards(JwtAuthGuard, OrgModuleGuard) @RequireOrgModule('pipeline') export class DealsController { constructor( private readonly dealsService: DealsService, private readonly excelService: DealsExcelService, ) {} @Get('export') async export( @CurrentUser() user: Actor, @Res({ passthrough: true }) res: Response, ) { const buffer = await this.excelService.export(user); res.set({ 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'Content-Disposition': `attachment; filename="${brand.filePrefix}-deals.xlsx"`, }); return new StreamableFile(buffer); } @Post('import/preview') @UseInterceptors( FileInterceptor('file', { storage: memoryStorage(), limits: { fileSize: 15_000_000 }, }), ) importPreview( @UploadedFile() file: Express.Multer.File, ) { return this.excelService.preview(file); } @Post('import') @UseInterceptors( FileInterceptor('file', { storage: memoryStorage(), limits: { fileSize: 15_000_000 }, }), ) import( @CurrentUser() user: Actor, @UploadedFile() file: Express.Multer.File | undefined, @Body('mapping') mapping?: string, @Body('sessionId') sessionId?: string, @Body('offset') offset?: string, @Body('limit') limit?: string, ) { return this.excelService.import(user, file, mapping, { sessionId, offset, limit, }); } @Post('import/rows') importRows(@CurrentUser() user: Actor, @Body() dto: ImportDealRowsDto) { return this.excelService.importRows(user, dto); } @Get(':id') get(@CurrentUser() user: Actor, @Param('id') id: string) { return this.dealsService.get(user.organizationId, id, user); } @Post(':id/promote-from-prior') promoteFromPrior(@CurrentUser() user: Actor, @Param('id') id: string) { return this.dealsService.promoteFromPrior(user, id); } @Post() create(@CurrentUser() user: Actor, @Body() dto: CreateDealDto) { return this.dealsService.create(user.organizationId, user, dto); } @Patch(':id') update( @CurrentUser() user: Actor, @Param('id') id: string, @Body() dto: UpdateDealDto, ) { return this.dealsService.update(user.organizationId, id, user, dto); } @Patch(':id/move') move( @CurrentUser() user: Actor, @Param('id') id: string, @Body() dto: MoveDealDto, ) { return this.dealsService.move(user.organizationId, id, user, dto); } @Delete(':id') async remove(@CurrentUser() user: Actor, @Param('id') id: string) { await this.dealsService.remove(user.organizationId, id, user); return { ok: true }; } }