123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- const db = require('../config/db');
- class Job {
- static async findAll() {
- const [rows] = await db.query('SELECT * FROM jobs');
- return rows.map(row => ({
- ...row,
- pendingResumes: row.pending_resumes,
- passedResumes: row.passed_resumes,
- aiCriteria: JSON.parse(row.ai_criteria),
- createdAt: row.created_at,
- updatedAt: row.updated_at
- }));
- }
- static async findById(id) {
- const [rows] = await db.query('SELECT * FROM jobs WHERE id = ?', [id]);
- if (rows.length === 0) return null;
- const row = rows[0];
- return {
- ...row,
- pendingResumes: row.pending_resumes,
- passedResumes: row.passed_resumes,
- aiCriteria: JSON.parse(row.ai_criteria),
- createdAt: row.created_at,
- updatedAt: row.updated_at
- };
- }
- static async create(jobData) {
- const now = new Date();
- const [result] = await db.query('INSERT INTO jobs SET ?', {
- id: require('crypto').randomUUID(),
- title: jobData.title,
- department: jobData.department,
- location: jobData.location,
- description: jobData.description,
- status: jobData.status || 'draft',
- pending_resumes: jobData.pendingResumes || 0,
- passed_resumes: jobData.passedResumes || 0,
- ai_criteria: JSON.stringify(jobData.aiCriteria),
- created_at: now,
- updated_at: now
- });
- return this.findById(result.insertId);
- }
- static async update(id, updates) {
- const updateData = {
- ...updates,
- updated_at: new Date()
- };
-
- if (updates.aiCriteria) {
- updateData.ai_criteria = JSON.stringify(updates.aiCriteria);
- delete updateData.aiCriteria;
- }
-
- if (updates.pendingResumes !== undefined) {
- updateData.pending_resumes = updates.pendingResumes;
- delete updateData.pendingResumes;
- }
-
- if (updates.passedResumes !== undefined) {
- updateData.passed_resumes = updates.passedResumes;
- delete updateData.passedResumes;
- }
-
- const [result] = await db.query('UPDATE jobs SET ? WHERE id = ?', [updateData, id]);
- if (result.affectedRows === 0) {
- throw new Error('Job not found');
- }
- return this.findById(id);
- }
- static async triggerScreening(id) {
- await new Promise(resolve => setTimeout(resolve, 2000));
-
- const [result] = await db.query(
- `UPDATE jobs
- SET pending_resumes = 0,
- passed_resumes = passed_resumes + FLOOR(RAND() * 3) + 1,
- updated_at = ?
- WHERE id = ?`,
- [new Date(), id]
- );
-
- if (result.affectedRows === 0) {
- throw new Error('Job not found');
- }
-
- return this.findById(id);
- }
- }
- module.exports = Job;
|