Skip to content

RCRM Import script: summarize total records created or being already existing #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions scripts/recruit-crm-job-import/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ function getPathname () {
* Process single job data. The processing consists of:
* - Validate the data.
* - Skip processing if externalId is missing.
* - Create a job if it is not already exists.
* - Create a resource booking if it is not already exists.
* - Create a job if it does not already exist.
* - Create a resource booking if it does not already exist.
* - Update the resourceBooking based on startDate and endDate.
*
* @param {Object} job the job data
Expand All @@ -96,47 +96,47 @@ async function processJob (job, info = []) {
// validate the data
const { value: data, error } = validateJob(job)
if (error) {
info.push(error.details[0].message)
info.push({ text: error.details[0].message, tag: 'validation_error' })
return { status: constants.ProcessingStatus.Failed, info }
}
if (!data.externalId) {
info.push('externalId is missing')
info.push({ text: 'externalId is missing', tag: 'external_id_missing' })
return { status: constants.ProcessingStatus.Skipped, info }
}
data.projectId = (await helper.getProjectByDirectProjectId(data.directProjectId)).id
// create a job if it is not already exists
// create a job if it does not already exist
try {
const result = await helper.getJobByExternalId(data.externalId)
info.push(`id: ${result.id} externalId: ${data.externalId} job already exists`)
info.push({ text: `id: ${result.id} externalId: ${data.externalId} job already exists`, tag: 'job_already_exists' })
data.jobId = result.id
} catch (err) {
if (!(err.message && err.message.includes('job not found'))) {
throw err
}
const result = await helper.createJob(_.pick(data, ['projectId', 'externalId', 'title', 'numPositions', 'skills']))
info.push(`id: ${result.id} job created`)
info.push({ text: `id: ${result.id} job created`, tag: 'job_created' })
data.jobId = result.id
}
data.userId = (await helper.getUserByHandle(data.userHandle)).id
logger.debug(`userHandle: ${data.userHandle} userId: ${data.userId}`)
// create a resource booking if it is not already exists
// create a resource booking if it does not already exist
try {
const result = await helper.getResourceBookingByJobIdAndUserId(data.jobId, data.userId)
info.push(`id: ${result.id} resource booking already exists`)
info.push({ text: `id: ${result.id} resource booking already exists`, tag: 'resource_booking_already_exists' })
return { status: constants.ProcessingStatus.Successful, info }
} catch (err) {
if (!(err.message && err.message.includes('resource booking not found'))) {
throw err
}
const result = await helper.createResourceBooking(_.pick(data, ['projectId', 'jobId', 'userId', 'startDate', 'endDate', 'memberRate', 'customerRate', 'rateType']))
info.push(`id: ${result.id} resource booking created`)
info.push({ text: `id: ${result.id} resource booking created`, tag: 'resource_booking_created' })
data.resourceBookingId = result.id
}
// update the resourceBooking based on startDate and endDate
const resourceBookingStatus = dateFNS.compareAsc(new Date(data.startDate), new Date(data.endDate)) === 1 ? 'closed' : 'assigned'
logger.debug(`resourceBookingId: ${data.resourceBookingId} status: ${resourceBookingStatus}`)
await helper.updateResourceBookingStatus(data.resourceBookingId, resourceBookingStatus)
info.push(`id: ${data.resourceBookingId} status: ${resourceBookingStatus} resource booking updated`)
info.push({ text: `id: ${data.resourceBookingId} status: ${resourceBookingStatus} resource booking updated`, tag: 'resource_booking_status_updated' })
return { status: constants.ProcessingStatus.Successful, info }
}

Expand All @@ -156,9 +156,9 @@ async function main () {
report.add({ lnum: job._lnum, ...result })
} catch (err) {
if (err.response) {
report.add({ lnum: job._lnum, status: constants.ProcessingStatus.Failed, info: [err.response.error.toString().split('\n')[0]] })
report.add({ lnum: job._lnum, status: constants.ProcessingStatus.Failed, info: [{ text: err.response.error.toString().split('\n')[0], tag: 'request_error' }] })
} else {
report.add({ lnum: job._lnum, status: constants.ProcessingStatus.Failed, info: [err.message] })
report.add({ lnum: job._lnum, status: constants.ProcessingStatus.Failed, info: [{ text: err.message, tag: 'internal_error' }] })
}
}
report.print()
Expand Down
17 changes: 14 additions & 3 deletions scripts/recruit-crm-job-import/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Report {
// print the last message to the console
print () {
const lastMessage = this.messages[this.messages.length - 1]
const output = `#${lastMessage.lnum} - ${lastMessage.info.join('; ')}`
const output = `#${lastMessage.lnum} - ${_.map(lastMessage.info, 'text').join('; ')}`
if (lastMessage.status === constants.ProcessingStatus.Skipped) {
logger.warn(output)
}
Expand All @@ -33,15 +33,26 @@ class Report {

// print a summary to the console
printSummary () {
// summarize total success, failure, skips
const groups = _.groupBy(this.messages, 'status')
const sucesss = groups[constants.ProcessingStatus.Successful] || []
const success = groups[constants.ProcessingStatus.Successful] || []
const failure = groups[constants.ProcessingStatus.Failed] || []
const skips = groups[constants.ProcessingStatus.Skipped] || []
// summarize records created or already existing
const groupsByTag = _.groupBy(_.flatten(_.map(this.messages, message => message.info)), 'tag')
const jobsCreated = groupsByTag.job_created || []
const resourceBookingsCreated = groupsByTag.resource_booking_created || []
const jobsAlreadyExist = groupsByTag.job_already_exists || []
const resourceBookingsAlreadyExist = groupsByTag.resource_booking_already_exists || []
logger.info('=== summary ===')
logger.info(`total: ${this.messages.length}`)
logger.info(`success: ${sucesss.length}`)
logger.info(`success: ${success.length}`)
logger.info(`failure: ${failure.length}`)
logger.info(`skips: ${skips.length}`)
logger.info(`jobs created: ${jobsCreated.length}`)
logger.info(`resource bookings created: ${resourceBookingsCreated.length}`)
logger.info(`jobs already exist: ${jobsAlreadyExist.length}`)
logger.info(`resource bookings already exist: ${resourceBookingsAlreadyExist.length}`)
logger.info('=== summary ===')
}
}
Expand Down