|
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 3 | +import { Repository } from 'typeorm'; |
| 4 | +import { createCipheriv, randomBytes } from 'crypto'; |
| 5 | +import { Export, ExportFormat, ExportStatus } from './entities/export.entity'; |
| 6 | +import { ExportJob, JobStatus } from './entities/export-job.entity'; |
| 7 | + |
| 8 | +@Injectable() |
| 9 | +export class ExportService { |
| 10 | + private readonly logger = new Logger(ExportService.name); |
| 11 | + |
| 12 | + constructor( |
| 13 | + @InjectRepository(Export) |
| 14 | + private readonly exportRepo: Repository<Export>, |
| 15 | + @InjectRepository(ExportJob) |
| 16 | + private readonly jobRepo: Repository<ExportJob>, |
| 17 | + ) {} |
| 18 | + |
| 19 | + async createExport(playerId: string, format: ExportFormat): Promise<Export> { |
| 20 | + const exp = this.exportRepo.create({ playerId, format }); |
| 21 | + const saved = await this.exportRepo.save(exp); |
| 22 | + await this.jobRepo.save(this.jobRepo.create({ exportId: saved.id })); |
| 23 | + return saved; |
| 24 | + } |
| 25 | + |
| 26 | + async aggregatePlayerData(playerId: string): Promise<Record<string, unknown>> { |
| 27 | + // Aggregates all player data — extend with real DB queries per domain |
| 28 | + return { |
| 29 | + playerId, |
| 30 | + exportedAt: new Date().toISOString(), |
| 31 | + profile: {}, |
| 32 | + quests: [], |
| 33 | + achievements: [], |
| 34 | + inventory: [], |
| 35 | + transactions: [], |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + toJson(data: Record<string, unknown>): string { |
| 40 | + return JSON.stringify(data, null, 2); |
| 41 | + } |
| 42 | + |
| 43 | + toCsv(data: Record<string, unknown>): string { |
| 44 | + const flat = this.flatten(data); |
| 45 | + const headers = Object.keys(flat).join(','); |
| 46 | + const values = Object.values(flat) |
| 47 | + .map((v) => `"${String(v).replace(/"/g, '""')}"`) |
| 48 | + .join(','); |
| 49 | + return `${headers}\n${values}`; |
| 50 | + } |
| 51 | + |
| 52 | + toPdf(data: Record<string, unknown>): string { |
| 53 | + // Returns a minimal text-based PDF representation |
| 54 | + const lines = Object.entries(this.flatten(data)) |
| 55 | + .map(([k, v]) => `${k}: ${v}`) |
| 56 | + .join('\n'); |
| 57 | + return `%PDF-1.4\n% Player Data Export\n${lines}`; |
| 58 | + } |
| 59 | + |
| 60 | + encrypt(plaintext: string): { encrypted: string; iv: string; key: string } { |
| 61 | + const key = randomBytes(32); |
| 62 | + const iv = randomBytes(16); |
| 63 | + const cipher = createCipheriv('aes-256-cbc', key, iv); |
| 64 | + const encrypted = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]); |
| 65 | + return { |
| 66 | + encrypted: encrypted.toString('base64'), |
| 67 | + iv: iv.toString('hex'), |
| 68 | + key: key.toString('hex'), |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + async markCompleted(exportId: string, filePath: string): Promise<void> { |
| 73 | + await this.exportRepo.update(exportId, { status: ExportStatus.COMPLETED, filePath }); |
| 74 | + await this.jobRepo.update( |
| 75 | + { exportId }, |
| 76 | + { status: JobStatus.DONE, completedAt: new Date() }, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + async markFailed(exportId: string, error: string): Promise<void> { |
| 81 | + await this.exportRepo.update(exportId, { status: ExportStatus.FAILED, errorMessage: error }); |
| 82 | + await this.jobRepo.update({ exportId }, { status: JobStatus.FAILED, errorMessage: error }); |
| 83 | + } |
| 84 | + |
| 85 | + private flatten( |
| 86 | + obj: Record<string, unknown>, |
| 87 | + prefix = '', |
| 88 | + ): Record<string, string> { |
| 89 | + return Object.entries(obj).reduce<Record<string, string>>((acc, [k, v]) => { |
| 90 | + const key = prefix ? `${prefix}.${k}` : k; |
| 91 | + if (v && typeof v === 'object' && !Array.isArray(v)) { |
| 92 | + Object.assign(acc, this.flatten(v as Record<string, unknown>, key)); |
| 93 | + } else { |
| 94 | + acc[key] = Array.isArray(v) ? JSON.stringify(v) : String(v ?? ''); |
| 95 | + } |
| 96 | + return acc; |
| 97 | + }, {}); |
| 98 | + } |
| 99 | +} |
0 commit comments