Skip to content

Commit 5cd8cca

Browse files
Merge pull request #446 from topcoder-platform/migrate-jobcandidates-scripts
Add migrate jobcandidates scripts and commands
2 parents 1057409 + 5d05945 commit 5cd8cca

File tree

6 files changed

+152
-8
lines changed

6 files changed

+152
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ build/Release
4040
# Dependency directories
4141
node_modules/
4242
jspm_packages/
43+
scripts/withdrawn-migration/temp/
4344

4445
# Snowpack dependency directory (https://snowpack.dev/)
4546
web_modules/

data/demo-data.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"isApplicationPageActive": false,
5555
"minSalary": 100,
5656
"maxSalary": 200,
57-
"hoursPerWeek": 20,
57+
"hoursPerWeek": 80,
5858
"jobLocation": "Any location",
5959
"jobTimezone": "GMT",
6060
"currency": "USD",
@@ -765,7 +765,7 @@
765765
"id": "b0fc417b-3f41-4c06-9f2b-8e680c3a03c6",
766766
"jobId": "728ff056-63f6-4730-8a9f-3074acad8479",
767767
"userId": "a55fe1bc-1754-45fa-9adc-cf3d6d7c377a",
768-
"status": "open",
768+
"status": "placed",
769769
"externalId": "300234321",
770770
"resume": "http://example.com",
771771
"remark": "excellent",
@@ -793,7 +793,7 @@
793793
"id": "02a622f4-7894-4ac0-a823-a952ffa1b3f3",
794794
"jobId": "728ff056-63f6-4730-8a9f-3074acad8479",
795795
"userId": "a55fe1bc-1754-45fa-9adc-cf3d6d7c377a",
796-
"status": "open",
796+
"status": "selected",
797797
"externalId": "300234321",
798798
"resume": "http://example.com",
799799
"remark": "excellent",
@@ -807,7 +807,7 @@
807807
"id": "b32b4819-7bfa-49a8-851e-69cdddff8149",
808808
"jobId": "728ff056-63f6-4730-8a9f-3074acad8479",
809809
"userId": "a55fe1bc-1754-45fa-9adc-cf3d6d7c377a",
810-
"status": "open",
810+
"status": "skills-test",
811811
"externalId": "300234321",
812812
"resume": "http://example.com",
813813
"remark": "excellent",
@@ -1359,11 +1359,11 @@
13591359
{
13601360
"id": "d6103727-6615-4168-8169-0485577bfb3f",
13611361
"projectId": 111,
1362-
"userId": "bef43122-426b-4b2b-acdd-9b5b3bd1c0bf",
1363-
"jobId": "a8adb1f8-a6ee-48b1-8661-33bd851b726e",
1362+
"userId": "a55fe1bc-1754-45fa-9adc-cf3d6d7c377a",
1363+
"jobId": "728ff056-63f6-4730-8a9f-3074acad8479",
13641364
"status": "placed",
13651365
"startDate": "2021-03-27",
1366-
"endDate": "2021-04-27",
1366+
"endDate": "2021-08-23",
13671367
"memberRate": 13.23,
13681368
"customerRate": 13,
13691369
"rateType": "hourly",

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
"local:reset": "npm run delete-index -- --force || true && npm run create-index -- --force && npm run init-db force",
3131
"cov": "nyc --reporter=html --reporter=text npm run test",
3232
"demo-payment-scheduler": "node scripts/demo-payment-scheduler/index.js && npm run index:all -- --force",
33-
"demo-payment": "node scripts/demo-payment"
33+
"demo-payment": "node scripts/demo-payment",
34+
"migrate:backup-withdrawn": "node scripts/withdrawn-migration/backup.js",
35+
"migrate:migration-withdrawn": "node scripts/withdrawn-migration/migration.js",
36+
"migrate:restore-withdrawn": "node scripts/withdrawn-migration/restore.js"
3437
},
3538
"keywords": [],
3639
"author": "",

scripts/withdrawn-migration/backup.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Back up the jobCandidates that we will update it's status
3+
*/
4+
const config = require('config')
5+
const Sequelize = require('sequelize')
6+
const fs = require('fs')
7+
const path = require('path')
8+
const { JobCandidate, ResourceBooking, Job } = require('../../src/models')
9+
const logger = require('../../src/common/logger')
10+
11+
const currentStep = 'Backup'
12+
13+
async function backup () {
14+
logger.info({ component: currentStep, message: '*************************** Backup process started ***************************' })
15+
const filePath = path.join(__dirname, '/temp/')
16+
const Op = Sequelize.Op
17+
const jobCandidates = await JobCandidate.findAll({
18+
where: {
19+
status: 'placed'
20+
}
21+
})
22+
23+
for (let i = 0; i < jobCandidates.length; i++) {
24+
const jc = jobCandidates[i]
25+
const job = await Job.findById(jc.jobId)
26+
const rb = await ResourceBooking.findOne({
27+
where: {
28+
userId: jc.userId,
29+
jobId: jc.jobId
30+
}
31+
})
32+
let completed = false
33+
if (rb && rb.endDate) {
34+
completed = new Date(rb.endDate) < new Date() && new Date(rb.endDate).toDateString() !== new Date().toDateString()
35+
}
36+
if (job.hoursPerWeek > config.JOBS_HOUR_PER_WEEK && !completed) {
37+
const statuses = ['applied', 'skills-test', 'phone-screen', 'open', 'interview', 'selected', 'offered']
38+
const filter = { [Op.and]: [] }
39+
filter[Op.and].push({ status: statuses })
40+
filter[Op.and].push({ userId: jc.userId })
41+
const candidates = await JobCandidate.findAll({
42+
where: filter
43+
})
44+
if (candidates && candidates.length > 0) {
45+
fs.writeFile(filePath + `jobcandidate-backup.json`, JSON.stringify(
46+
candidates
47+
), (err) => {
48+
if (!err) {
49+
logger.info({ component: `${currentStep} Summary`, message: `Backup up finished. There are ${candidates.length} jobCandidates that need to be updated` })
50+
logger.info({ component: currentStep, message: '*************************** Backup process finished ***************************' })
51+
return
52+
}
53+
logger.error({ component: currentStep, message: err.message })
54+
process.exit(1)
55+
})
56+
}
57+
}
58+
}
59+
}
60+
61+
backup().then(() => {
62+
logger.info({ component: currentStep, message: 'Execution Finished!' })
63+
process.exit()
64+
}).catch(err => {
65+
logger.error(err.message)
66+
process.exit(1)
67+
})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Migration the jobCandidate status into expected status
3+
*/
4+
const config = require('config')
5+
const fs = require('fs')
6+
const path = require('path')
7+
const { JobCandidate } = require('../../src/models')
8+
const logger = require('../../src/common/logger')
9+
10+
const currentStep = 'Migration'
11+
12+
async function migration () {
13+
logger.info({ component: currentStep, message: '*************************** Migration process started ***************************' })
14+
const filePath = path.join(__dirname, '/temp/')
15+
const data = fs.readFileSync(filePath + 'jobCandidate-backup.json', 'utf-8')
16+
const jobCandidates = JSON.parse(data)
17+
let summary = 0
18+
for (var i = 0; i < jobCandidates.length; i++) {
19+
const jc = await JobCandidate.findById(jobCandidates[i].id)
20+
if (jc) {
21+
const oldStatus = jc.status
22+
const updated = await jc.update({ status: config.WITHDRAWN_STATUS_CHANGE_MAPPING[jobCandidates[i].status] })
23+
summary++
24+
logger.info({ component: currentStep, message: `jobCandidate with ${jc.id} status changed from ${oldStatus} to ${updated.status}` })
25+
}
26+
};
27+
logger.info({ component: currentStep, message: `Totally updated ${summary} jobCandidates` })
28+
logger.info({ component: currentStep, message: '*************************** Migration process finished ***************************' })
29+
}
30+
31+
migration().then(() => {
32+
logger.info({ component: currentStep, message: 'Execution Finished!' })
33+
process.exit()
34+
}).catch(err => {
35+
logger.error(err.message)
36+
process.exit(1)
37+
})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Resotre the changed jobCandidates into its original state.
3+
*/
4+
const fs = require('fs')
5+
const path = require('path')
6+
const { JobCandidate } = 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 data = fs.readFileSync(filePath + 'jobCandidate-backup.json', 'utf-8')
15+
const jobCandidates = JSON.parse(data)
16+
let summary = 0
17+
for (var i = 0; i < jobCandidates.length; i++) {
18+
const jc = await JobCandidate.findById(jobCandidates[i].id)
19+
if (jc) {
20+
const oldStatus = jc.status
21+
const updated = await jc.update({ status: jobCandidates[i].status })
22+
summary++
23+
logger.info({ component: currentStep, message: `jobCandidate with ${jc.id} status restored from ${oldStatus} to ${updated.status}` })
24+
}
25+
};
26+
logger.info({ component: currentStep, message: `Totally restored ${summary} jobCandidates` })
27+
logger.info({ component: currentStep, message: '*************************** Restore process finished ***************************' })
28+
}
29+
30+
restore().then(() => {
31+
logger.info({ component: currentStep, message: 'Execution Finished!' })
32+
process.exit()
33+
}).catch(err => {
34+
logger.error(err.message)
35+
process.exit(1)
36+
})

0 commit comments

Comments
 (0)