Job.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const db = require('../config/db');
  2. class Job {
  3. static async findAll() {
  4. const [rows] = await db.query('SELECT * FROM jobs');
  5. return rows.map(row => ({
  6. ...row,
  7. pendingResumes: row.pending_resumes,
  8. passedResumes: row.passed_resumes,
  9. aiCriteria: JSON.parse(row.ai_criteria),
  10. createdAt: row.created_at,
  11. updatedAt: row.updated_at
  12. }));
  13. }
  14. static async findById(id) {
  15. const [rows] = await db.query('SELECT * FROM jobs WHERE id = ?', [id]);
  16. if (rows.length === 0) return null;
  17. const row = rows[0];
  18. return {
  19. ...row,
  20. pendingResumes: row.pending_resumes,
  21. passedResumes: row.passed_resumes,
  22. aiCriteria: JSON.parse(row.ai_criteria),
  23. createdAt: row.created_at,
  24. updatedAt: row.updated_at
  25. };
  26. }
  27. static async create(jobData) {
  28. const now = new Date();
  29. const [result] = await db.query('INSERT INTO jobs SET ?', {
  30. id: require('crypto').randomUUID(),
  31. title: jobData.title,
  32. department: jobData.department,
  33. location: jobData.location,
  34. description: jobData.description,
  35. status: jobData.status || 'draft',
  36. pending_resumes: jobData.pendingResumes || 0,
  37. passed_resumes: jobData.passedResumes || 0,
  38. ai_criteria: JSON.stringify(jobData.aiCriteria),
  39. created_at: now,
  40. updated_at: now
  41. });
  42. return this.findById(result.insertId);
  43. }
  44. static async update(id, updates) {
  45. const updateData = {
  46. ...updates,
  47. updated_at: new Date()
  48. };
  49. if (updates.aiCriteria) {
  50. updateData.ai_criteria = JSON.stringify(updates.aiCriteria);
  51. delete updateData.aiCriteria;
  52. }
  53. if (updates.pendingResumes !== undefined) {
  54. updateData.pending_resumes = updates.pendingResumes;
  55. delete updateData.pendingResumes;
  56. }
  57. if (updates.passedResumes !== undefined) {
  58. updateData.passed_resumes = updates.passedResumes;
  59. delete updateData.passedResumes;
  60. }
  61. const [result] = await db.query('UPDATE jobs SET ? WHERE id = ?', [updateData, id]);
  62. if (result.affectedRows === 0) {
  63. throw new Error('Job not found');
  64. }
  65. return this.findById(id);
  66. }
  67. static async triggerScreening(id) {
  68. await new Promise(resolve => setTimeout(resolve, 2000));
  69. const [result] = await db.query(
  70. `UPDATE jobs
  71. SET pending_resumes = 0,
  72. passed_resumes = passed_resumes + FLOOR(RAND() * 3) + 1,
  73. updated_at = ?
  74. WHERE id = ?`,
  75. [new Date(), id]
  76. );
  77. if (result.affectedRows === 0) {
  78. throw new Error('Job not found');
  79. }
  80. return this.findById(id);
  81. }
  82. }
  83. module.exports = Job;