72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
NotFoundException,
|
|
Param,
|
|
Post,
|
|
Req,
|
|
Res,
|
|
StreamableFile,
|
|
} from '@nestjs/common';
|
|
import type { Request, Response } from 'express';
|
|
import { LeadFormPublicEventDto, 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': this.leadFormsService.coverContentType(row.coverStorageKey),
|
|
'Cache-Control': 'public, max-age=3600',
|
|
});
|
|
return new StreamableFile(this.files.openReadStream(row.coverStorageKey));
|
|
}
|
|
|
|
@Post(':token/event')
|
|
event(@Param('token') token: string, @Body() dto: LeadFormPublicEventDto) {
|
|
return this.leadFormsService.recordPublicEvent(token, dto.type);
|
|
}
|
|
|
|
@Post(':token/submit')
|
|
submit(
|
|
@Param('token') token: string,
|
|
@Body() dto: SubmitLeadFormDto,
|
|
@Req() req: Request,
|
|
) {
|
|
const forwarded = req.headers['x-forwarded-for'];
|
|
const ip =
|
|
(typeof forwarded === 'string' ? forwarded.split(',')[0]?.trim() : null) ||
|
|
req.ip ||
|
|
null;
|
|
return this.leadFormsService.submit(
|
|
token,
|
|
{
|
|
values: dto.values ?? {},
|
|
utm: dto.utm,
|
|
pageUrl: dto.pageUrl,
|
|
referrer: dto.referrer,
|
|
website: dto.website,
|
|
},
|
|
ip,
|
|
);
|
|
}
|
|
}
|