Files
CRM_Montzar/apps/api/src/lead-forms/lead-forms.controller.ts
T

123 lines
3.4 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 { Roles } from '../auth/roles.decorator';
import { RolesGuard } from '../auth/roles.guard';
import { CreateLeadFormDto, UpdateLeadFormDto } from './dto';
import { LeadFormsService } from './lead-forms.service';
import { LocalFileStorage } from '../storage/local-file.storage';
@Controller('lead-forms')
@UseGuards(JwtAuthGuard, RolesGuard)
export class LeadFormsController {
constructor(
private readonly leadFormsService: LeadFormsService,
private readonly files: LocalFileStorage,
) {}
@Get()
@Roles('owner', 'admin')
list(@CurrentUser() user: { organizationId: string }) {
return this.leadFormsService.list(user.organizationId);
}
@Get(':id')
@Roles('owner', 'admin')
get(
@CurrentUser() user: { organizationId: string },
@Param('id') id: string,
) {
return this.leadFormsService.get(user.organizationId, id);
}
@Post()
@Roles('owner', 'admin')
create(
@CurrentUser() user: { organizationId: string },
@Body() dto: CreateLeadFormDto,
) {
return this.leadFormsService.create(user.organizationId, dto);
}
@Patch(':id')
@Roles('owner', 'admin')
update(
@CurrentUser() user: { organizationId: string },
@Param('id') id: string,
@Body() dto: UpdateLeadFormDto,
) {
return this.leadFormsService.update(user.organizationId, id, dto);
}
@Get(':id/submissions')
@Roles('owner', 'admin')
submissions(
@CurrentUser() user: { organizationId: string },
@Param('id') id: string,
) {
return this.leadFormsService.listSubmissions(user.organizationId, id);
}
@Post(':id/cover')
@Roles('owner', 'admin')
@UseInterceptors(
FileInterceptor('file', { storage: memoryStorage(), limits: { fileSize: 4_000_000 } }),
)
uploadCover(
@CurrentUser() user: { organizationId: string },
@Param('id') id: string,
@UploadedFile() file: Express.Multer.File,
) {
return this.leadFormsService.saveCover(user.organizationId, id, file);
}
@Delete(':id/cover')
@Roles('owner', 'admin')
removeCover(
@CurrentUser() user: { organizationId: string },
@Param('id') id: string,
) {
return this.leadFormsService.removeCover(user.organizationId, id);
}
@Get(':id/cover')
@Roles('owner', 'admin')
async cover(
@CurrentUser() user: { organizationId: string },
@Param('id') id: string,
@Res({ passthrough: true }) res: Response,
) {
const form = await this.leadFormsService.get(user.organizationId, id);
const row = await this.leadFormsService.getByToken(form.publicToken);
if (!row?.coverStorageKey) return { ok: false };
res.set({ 'Content-Type': this.leadFormsService.coverContentType(row.coverStorageKey) });
return new StreamableFile(this.files.openReadStream(row.coverStorageKey));
}
@Delete(':id')
@Roles('owner', 'admin')
async remove(
@CurrentUser() user: { organizationId: string },
@Param('id') id: string,
) {
await this.leadFormsService.remove(user.organizationId, id);
return { ok: true };
}
}