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

44 lines
1.2 KiB
TypeScript

import {
Body,
Controller,
Get,
NotFoundException,
Param,
Post,
Res,
StreamableFile,
} from '@nestjs/common';
import type { Response } from 'express';
import { SubmitLeadFormDto } from './dto';
import { LeadFormsService } from './lead-forms.service';
import { LocalFileStorage } from '../storage/local-file.storage';
@Controller('public/forms')
export class PublicLeadFormsController {
constructor(
private readonly leadFormsService: LeadFormsService,
private readonly files: LocalFileStorage,
) {}
@Get(':token')
get(@Param('token') token: string) {
return this.leadFormsService.getPublic(token);
}
@Get(':token/cover')
async cover(
@Param('token') token: string,
@Res({ passthrough: true }) res: Response,
) {
const row = await this.leadFormsService.getByToken(token);
if (!row?.coverStorageKey) throw new NotFoundException('کاور پیدا نشد');
res.set({ 'Content-Type': 'image/jpeg', 'Cache-Control': 'public, max-age=3600' });
return new StreamableFile(this.files.openReadStream(row.coverStorageKey));
}
@Post(':token/submit')
submit(@Param('token') token: string, @Body() dto: SubmitLeadFormDto) {
return this.leadFormsService.submit(token, dto.values ?? {});
}
}