Spaces:
Runtime error
Runtime error
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Body, | |
| Param, | |
| UseGuards, | |
| UsePipes, | |
| ValidationPipe, | |
| Res, | |
| } from '@nestjs/common'; | |
| import { Response } from 'express'; | |
| import { IsArray, IsBoolean, IsOptional, IsString } from 'class-validator'; | |
| import { ExportService } from './export.service'; | |
| import { StorageService } from './storage.service'; | |
| import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; | |
| import { LicenseGuard } from '../../common/guards/license.guard'; | |
| import { CurrentUser, AuthenticatedUser } from '../../common/decorators/current-user.decorator'; | |
| class ExportDto { | |
| () () ({ each: true }) formats?: string[]; | |
| () bundle_as_zip: boolean; | |
| } | |
| () | |
| (JwtAuthGuard, LicenseGuard) | |
| export class ExportController { | |
| constructor( | |
| private readonly exports: ExportService, | |
| private readonly storage: StorageService, | |
| ) {} | |
| ('campaigns/:id/export') | |
| (new ValidationPipe({ whitelist: true, transform: true })) | |
| async createExport( | |
| () user: AuthenticatedUser, | |
| ('id') id: string, | |
| () dto: ExportDto, | |
| ) { | |
| const formats = dto.formats && dto.formats.length ? dto.formats : ['review', 'bonus', 'emails', 'social', 'cta']; | |
| return this.exports.createExport(user.id, id, { formats, bundle_as_zip: dto.bundle_as_zip }); | |
| } | |
| ('campaigns/:id/exports') | |
| async listExports(() user: AuthenticatedUser, ('id') id: string) { | |
| return this.exports.listExports(user.id, id); | |
| } | |
| ('exports/:id/download') | |
| async download(() user: AuthenticatedUser, ('id') id: string, () res: Response) { | |
| const exportRec = await this.exports.getDownload(user.id, id); | |
| const buffer = await this.storage.readBuffer(exportRec.storage_path); | |
| res.setHeader('Content-Type', 'application/zip'); | |
| res.setHeader('Content-Length', String(buffer.length)); | |
| res.setHeader('Content-Disposition', `attachment; filename="campaign-${exportRec.campaign_id}.zip"`); | |
| res.send(buffer); | |
| } | |
| } | |