File size: 5,948 Bytes
d8ea863
9ffaa46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8ea863
 
 
 
 
 
 
9ffaa46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f666f7
9ffaa46
2f666f7
9ffaa46
2f666f7
9ffaa46
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Injectable, Logger, BadRequestException, NotFoundException, ForbiddenException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { GroqService } from '../ai/groq.service';
import {
  GeneratedDocument,
  DocumentType,
} from './entities/generated-document.entity';
import { GenerateDocumentDto } from './dto/generate-document.dto';
import { UserProfile } from '../users/entities/user-profile.entity';
import { User } from '../users/user.entity';
import {
  COVER_LETTER_PROMPT,
  PROFESSIONAL_EMAIL_PROMPT,
  UNIVERSITY_STATEMENT_PROMPT,
} from './prompts/document.prompts';

@Injectable()
export class DocumentGenerationService {
  private readonly logger = new Logger(DocumentGenerationService.name);

  constructor(
    @InjectRepository(GeneratedDocument)
    private readonly docRepo: Repository<GeneratedDocument>,
    @InjectRepository(UserProfile)
    private readonly profileRepo: Repository<UserProfile>,
    @InjectRepository(User)
    private readonly userRepo: Repository<User>,
    private readonly groq: GroqService,
  ) {}

  async generate(
    userId: string,
    dto: GenerateDocumentDto,
  ): Promise<{ documentId: string; content: string; title: string }> {
    this.validateFormData(dto.documentType, dto.formData);

    const [user, profile] = await Promise.all([
      this.userRepo.findOne({ where: { id: userId } }),
      this.profileRepo.findOne({ where: { userId } }),
    ]);

    const profileBlock = this.buildProfileBlock(user, profile);
    const systemPrompt = this.getSystemPrompt(dto.documentType);
    const userMessage = this.buildUserMessage(dto, profileBlock);

    const content = await this.groq.analyzeWithSystemPrompt(
      systemPrompt,
      userMessage,
      { temperature: 0.7, maxTokens: 1500 },
    );

    const cleaned = content.trim();
    const title = this.buildTitle(dto.documentType, dto.formData);

    const saved = await this.docRepo.save(
      this.docRepo.create({
        userId,
        documentType: dto.documentType,
        formData: dto.formData,
        content: cleaned,
        title,
      }),
    );

    return { documentId: saved.id, content: cleaned, title };
  }

  async getHistory(userId: string) {
    const docs = await this.docRepo.find({
      where: { userId },
      order: { createdAt: 'DESC' },
    });
    return docs.map((d) => ({
      id: d.id,
      documentType: d.documentType,
      title: d.title,
      preview: d.content.slice(0, 200),
      createdAt: d.createdAt,
    }));
  }

  async getDocumentDetail(userId: string, docId: string) {
    const doc = await this.docRepo.findOne({ where: { id: docId } });
    if (!doc) throw new NotFoundException('Document not found.');
    if (doc.userId !== userId) throw new ForbiddenException('Access denied.');
    return doc;
  }

  // ─── helpers ──────────────────────────────────────────────────────────
  private validateFormData(type: DocumentType, data: Record<string, any>) {
    const requiredByType: Record<DocumentType, string[]> = {
      [DocumentType.COVER_LETTER]: ['companyName', 'position'],
      [DocumentType.PROFESSIONAL_EMAIL]: ['recipient', 'purpose'],
      [DocumentType.UNIVERSITY_STATEMENT]: ['universityName', 'program', 'motivation'],
    };
    const required = requiredByType[type];
    const missing = required.filter((f) => !data[f] || String(data[f]).trim().length === 0);
    if (missing.length > 0) {
      throw new BadRequestException(`Missing required fields: ${missing.join(', ')}`);
    }
  }

  private getSystemPrompt(type: DocumentType): string {
    switch (type) {
      case DocumentType.COVER_LETTER:
        return COVER_LETTER_PROMPT;
      case DocumentType.PROFESSIONAL_EMAIL:
        return PROFESSIONAL_EMAIL_PROMPT;
      case DocumentType.UNIVERSITY_STATEMENT:
        return UNIVERSITY_STATEMENT_PROMPT;
    }
  }

  private buildProfileBlock(user: User | null, profile: UserProfile | null): string {
    const lines: string[] = [];
    if (user?.name) lines.push(`Name: ${user.name}`);
    if (profile) {
      if (profile.currentRole) lines.push(`Current Role: ${profile.currentRole}`);
      if (profile.targetRole) lines.push(`Target Role: ${profile.targetRole}`);
      if (profile.experienceLevel) lines.push(`Experience Level: ${profile.experienceLevel}`);
      if (profile.yearsOfExperience != null) lines.push(`Years of Experience: ${profile.yearsOfExperience}`);
      if (profile.educationLevel) lines.push(`Education: ${profile.educationLevel}`);
      if (profile.skills?.length) lines.push(`Skills: ${profile.skills.join(', ')}`);
      if (profile.interests?.length) lines.push(`Interests: ${profile.interests.join(', ')}`);
      if (profile.location) lines.push(`Location: ${profile.location}`);
    }
    return lines.length > 0
      ? `Applicant profile:\n${lines.join('\n')}`
      : 'Applicant profile: (not yet completed)';
  }

  private buildUserMessage(dto: GenerateDocumentDto, profileBlock: string): string {
    const { documentType, formData } = dto;
    const formBlock = `Form details:\n${Object.entries(formData)
      .filter(([_, v]) => v != null && String(v).trim().length > 0)
      .map(([k, v]) => `${k}: ${v}`)
      .join('\n')}`;
    return `${profileBlock}\n\n${formBlock}\n\nGenerate the ${documentType.replace('_', ' ')} now.`;
  }

  private buildTitle(type: DocumentType, data: Record<string, any>): string {
    switch (type) {
      case DocumentType.COVER_LETTER:
        return `Cover Letter: ${data.companyName ?? 'Untitled'}`;
      case DocumentType.PROFESSIONAL_EMAIL:
        return `Email: ${data.subject || data.purpose?.slice(0, 40) || 'Untitled'}`;
      case DocumentType.UNIVERSITY_STATEMENT:
        return `Personal Statement: ${data.universityName ?? 'Untitled'}`;
    }
  }
}