|
| 1 | +/** |
| 2 | + * Back up the jobs that we will update it's status |
| 3 | + */ |
| 4 | +const fs = require('fs') |
| 5 | +const path = require('path') |
| 6 | +const request = require('superagent') |
| 7 | +const { Job } = require('../../src/models') |
| 8 | +const logger = require('../../src/common/logger') |
| 9 | + |
| 10 | +const currentStep = 'Backup' |
| 11 | + |
| 12 | +async function backup () { |
| 13 | + logger.info({ component: currentStep, message: '*************************** Backup process started ***************************' }) |
| 14 | + const filePath = path.join(__dirname, '/temp/') |
| 15 | + if (fs.existsSync(filePath)) { |
| 16 | + fs.rmdirSync(filePath, { recursive: true }) |
| 17 | + } |
| 18 | + fs.mkdirSync(filePath) |
| 19 | + let { body: jobs } = await request.get('https://www.topcoder.com/api/recruit/jobs?job_status=1') |
| 20 | + jobs = jobs.map((item) => item.slug) |
| 21 | + const backupJobs = [] |
| 22 | + if (jobs && jobs.length > 0) { |
| 23 | + try { |
| 24 | + const jbsInDb = await Job.findAll({ |
| 25 | + where: { rcrmStatus: 'Open' } |
| 26 | + }) |
| 27 | + for (const j of jbsInDb) { |
| 28 | + if (j.externalId && jobs.indexOf(j.externalId) < 0) { |
| 29 | + // The open job exists in taas but not showing up on Community-App |
| 30 | + backupJobs.push(j.externalId) |
| 31 | + } |
| 32 | + } |
| 33 | + fs.writeFileSync(filePath + 'jobs-backup.json', JSON.stringify( |
| 34 | + backupJobs |
| 35 | + )) |
| 36 | + logger.info({ component: `${currentStep} Sub`, message: `There are ${backupJobs.length} jobs that need to be updated` }) |
| 37 | + } catch (err) { |
| 38 | + logger.error({ component: currentStep, message: err.message }) |
| 39 | + process.exit(1) |
| 40 | + } |
| 41 | + } |
| 42 | + logger.info({ component: `${currentStep}`, message: `Report: there are ${backupJobs.length} jobs in total` }) |
| 43 | + logger.info({ component: currentStep, message: '*************************** Backup process finished ***************************' }) |
| 44 | +} |
| 45 | + |
| 46 | +backup().then(() => { |
| 47 | + logger.info({ component: currentStep, message: 'Execution Finished!' }) |
| 48 | + process.exit() |
| 49 | +}).catch(err => { |
| 50 | + logger.error(err.message) |
| 51 | + process.exit(1) |
| 52 | +}) |
0 commit comments