Files
CRM_Montzar/apps/api/src/pipelines/deals.controller.ts
T

113 lines
2.9 KiB
TypeScript

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, 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,
@Body('mapping') mapping?: string,
) {
return this.excelService.import(user, file, mapping);
}
@Get(':id')
get(@CurrentUser() user: Actor, @Param('id') id: string) {
return this.dealsService.get(user.organizationId, id, user);
}
@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 };
}
}