Skip to content

Commit e09428d

Browse files
Merge pull request #670 from topcoder-platform/rcrm-migration-2
feat: rcrm migration v2
2 parents 363f608 + bc35917 commit e09428d

File tree

6 files changed

+157
-2
lines changed

6 files changed

+157
-2
lines changed

data/demo-data.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"createdBy": "57646ff9-1cd3-4d3c-88ba-eb09a395366c",
3232
"updatedBy": "00000000-0000-0000-0000-000000000000",
3333
"createdAt": "2021-05-09T21:21:10.394Z",
34-
"updatedAt": "2021-05-09T21:21:14.010Z"
34+
"updatedAt": "2021-05-09T21:21:14.010Z",
35+
"rcrmStatus": "Open"
3536
},
3637
{
3738
"id": "728ff056-63f6-4730-8a9f-3074acad8479",

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
"migrate:restore-withdrawn": "node scripts/withdrawn-migration/restore.js",
3737
"migrate:backup-rcrm-status": "node scripts/job-rcrm-status-migration/backup.js",
3838
"migrate:migration-rcrm-status": "node scripts/job-rcrm-status-migration/migration.js",
39-
"migrate:restore-rcrm-status": "node scripts/job-rcrm-status-migration/restore.js"
39+
"migrate:restore-rcrm-status": "node scripts/job-rcrm-status-migration/restore.js",
40+
"migrate:backup-rcrm-status-v2": "node scripts/job-rcrm-status-migration-v2/backup.js",
41+
"migrate:migration-rcrm-status-v2": "node scripts/job-rcrm-status-migration-v2/migration.js",
42+
"migrate:restore-rcrm-status-v2": "node scripts/job-rcrm-status-migration-v2/restore.js"
4043
},
4144
"keywords": [],
4245
"author": "",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Migration the job rcrm status into Open status
3+
*/
4+
const fs = require('fs')
5+
const path = require('path')
6+
const { Job } = require('../../src/models')
7+
const logger = require('../../src/common/logger')
8+
9+
const currentStep = 'Migration'
10+
11+
async function migration () {
12+
logger.info({ component: currentStep, message: '*************************** Migration process started ***************************' })
13+
const filePath = path.join(__dirname, '/temp/')
14+
const files = []
15+
fs.readdirSync(filePath).forEach(async (file) => {
16+
files.push(`${filePath}${file}`)
17+
})
18+
let totalSum = 0
19+
for (let j = 0; j < files.length; j++) {
20+
const data = fs.readFileSync(files[j], 'utf-8')
21+
const rcrmIds = JSON.parse(data)
22+
let summary = 0
23+
for (let i = 0; i < rcrmIds.length; i++) {
24+
const jbs = await Job.findAll({
25+
where: { externalId: rcrmIds[i] }
26+
})
27+
for (let j = 0; j < jbs.length; j++) {
28+
if (jbs[j]) {
29+
const oldStatus = jbs[j].rcrmStatus
30+
const updated = await jbs[j].update({ rcrmStatus: null })
31+
summary++
32+
totalSum++
33+
logger.info({ component: currentStep, message: `job with rcrmId ${rcrmIds[i]} status changed from ${oldStatus} to ${updated.rcrmStatus}` })
34+
}
35+
}
36+
};
37+
logger.info({ component: `${currentStep} Sub`, message: `Updated ${summary} jobs from ${files[j]}` })
38+
}
39+
logger.info({ component: currentStep, message: `Report: Totally Updated ${totalSum} jobs` })
40+
logger.info({ component: currentStep, message: '*************************** Migration process finished ***************************' })
41+
}
42+
43+
migration().then(() => {
44+
logger.info({ component: currentStep, message: 'Execution Finished!' })
45+
process.exit()
46+
}).catch(err => {
47+
logger.error(err.message)
48+
process.exit(1)
49+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Restore the job rcrm status into Open status
3+
*/
4+
const fs = require('fs')
5+
const path = require('path')
6+
const { Job } = require('../../src/models')
7+
const logger = require('../../src/common/logger')
8+
9+
const currentStep = 'Restore'
10+
11+
async function restore () {
12+
logger.info({ component: currentStep, message: '*************************** Restore process started ***************************' })
13+
const filePath = path.join(__dirname, '/temp/')
14+
const files = []
15+
fs.readdirSync(filePath).forEach(async (file) => {
16+
files.push(`${filePath}${file}`)
17+
})
18+
let totalSum = 0
19+
for (let j = 0; j < files.length; j++) {
20+
const data = fs.readFileSync(files[j], 'utf-8')
21+
const rcrmIds = JSON.parse(data)
22+
let summary = 0
23+
for (let i = 0; i < rcrmIds.length; i++) {
24+
const jbs = await Job.findAll({
25+
where: { externalId: rcrmIds[i] }
26+
})
27+
for (let j = 0; j < jbs.length; j++) {
28+
if (jbs[j]) {
29+
const oldStatus = jbs[j].rcrmStatus
30+
const updated = await jbs[j].update({ rcrmStatus: 'Open' })
31+
summary++
32+
totalSum++
33+
logger.info({ component: currentStep, message: `job with rcrmId ${rcrmIds[i]} status changed from ${oldStatus} to ${updated.rcrmStatus}` })
34+
}
35+
}
36+
};
37+
logger.info({ component: `${currentStep} Sub`, message: `Updated ${summary} jobs from ${files[j]}` })
38+
}
39+
logger.info({ component: currentStep, message: `Report: Totally Restored ${totalSum} jobs` })
40+
logger.info({ component: currentStep, message: '*************************** Restore process finished ***************************' })
41+
}
42+
43+
restore().then(() => {
44+
logger.info({ component: currentStep, message: 'Execution Finished!' })
45+
process.exit()
46+
}).catch(err => {
47+
logger.error(err.message)
48+
process.exit(1)
49+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["88774632"]

0 commit comments

Comments
 (0)