2
2

Candidate.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const db = require('../config/db');
  2. class Candidate {
  3. static async findAll() {
  4. const [rows] = await db.query('SELECT * FROM candidates');
  5. return rows.map(row => ({
  6. ...row,
  7. jobId: row.job_id,
  8. jobTitle: row.job_title,
  9. matchScore: row.match_score,
  10. highlights: JSON.parse(row.highlights || '[]'),
  11. concerns: JSON.parse(row.concerns || '[]'),
  12. skills: JSON.parse(row.skills || '[]'),
  13. submittedAt: row.submitted_at,
  14. reviewedAt: row.reviewed_at
  15. }));
  16. }
  17. static async findById(id) {
  18. const [rows] = await db.query('SELECT * FROM candidates WHERE id = ?', [id]);
  19. if (rows.length === 0) return null;
  20. const row = rows[0];
  21. return {
  22. ...row,
  23. jobId: row.job_id,
  24. jobTitle: row.job_title,
  25. matchScore: row.match_score,
  26. highlights: JSON.parse(row.highlights || '[]'),
  27. concerns: JSON.parse(row.concerns || '[]'),
  28. skills: JSON.parse(row.skills || '[]'),
  29. submittedAt: row.submitted_at,
  30. reviewedAt: row.reviewed_at
  31. };
  32. }
  33. static async findByJobId(jobId) {
  34. const [rows] = await db.query('SELECT * FROM candidates WHERE job_id = ?', [jobId]);
  35. return rows.map(row => ({
  36. ...row,
  37. jobId: row.job_id,
  38. jobTitle: row.job_title,
  39. matchScore: row.match_score,
  40. highlights: JSON.parse(row.highlights || '[]'),
  41. concerns: JSON.parse(row.concerns || '[]'),
  42. skills: JSON.parse(row.skills || '[]'),
  43. submittedAt: row.submitted_at,
  44. reviewedAt: row.reviewed_at
  45. }));
  46. }
  47. static async updateStatus(id, status) {
  48. const [result] = await db.query(
  49. 'UPDATE candidates SET status = ?, reviewed_at = ? WHERE id = ?',
  50. [status, new Date(), id]
  51. );
  52. if (result.affectedRows === 0) {
  53. throw new Error('Candidate not found');
  54. }
  55. return this.findById(id);
  56. }
  57. static async batchUpdateStatus(candidateIds, status) {
  58. const [result] = await db.query(
  59. 'UPDATE candidates SET status = ?, reviewed_at = ? WHERE id IN (?)',
  60. [status, new Date(), candidateIds]
  61. );
  62. return result.affectedRows;
  63. }
  64. }
  65. module.exports = Candidate;