Created
June 2, 2020 21:01
-
-
Save wswoodruff/1d59712debabe2c1a4043a4d8eb0cf19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Schmervice = require('schmervice'); | |
const internals = {}; | |
module.exports = class DisplayService extends Schmervice.Service { | |
companyBasic(companies) { | |
const { toCompanyBasic } = DisplayService; | |
const { mapResults } = internals; | |
return mapResults(companies, toCompanyBasic); | |
} | |
// Load your model up with any extras it needs here! | |
async jobBasic(jobs, txn) { | |
const { Job } = this.server.models(); | |
const { toJobBasic } = DisplayService; | |
const { mapResults } = internals; | |
// See Objection's [fetchGraph](https://vincit.github.io/objection.js/api/model/static-methods.html#static-fetchgraph) if you're unfamiliar with this part | |
const jobsGraphed = await Job.fetchGraph(jobs, `[ | |
company, | |
views as viewCount, | |
applications as appCount | |
]`, { transaction: txn }); | |
return mapResults(jobsGraphed, toJobBasic); | |
} | |
// Load your model up with any extras it needs here! | |
async jobDetails(jobs, txn) { | |
const { Job } = this.server.models(); | |
const { toJobDetail } = DisplayService; | |
const { mapResults } = internals; | |
const jobsGraphed = await Job.fetchGraph(jobs, `[ | |
company, | |
views as viewCount, | |
applications as appCount, | |
enhancements(active) | |
]`, { transaction: txn }); | |
return mapResults(jobsGraphed, toJobDetail); | |
} | |
static toCompanyBasic(company) { | |
const { | |
id, | |
name, | |
address, | |
city, | |
state, | |
zip, | |
updatedAt, | |
createdAt | |
} = company; | |
return { | |
id, | |
name, | |
address, | |
city, | |
state, | |
zip, | |
updatedAt, | |
createdAt | |
}; | |
} | |
static toJobBasic(job) { | |
const { | |
id, | |
title, | |
isDeleted, | |
appCount, | |
viewCount, | |
createdAt, | |
updatedAt | |
} = job; | |
const { toCompanyBasic } = DisplayService; | |
const [{ count: totalViewCount = 0 } = {}] = viewCount; | |
const [{ count: totalAppCount = 0 } = {}] = appCount; | |
return { | |
id, | |
title, | |
// Notice here that 'status' and 'stats' are introduced only in this function. There are no db columns for these | |
status: ((company && company.isDeleted) || isDeleted) ? 'deleted' : job.getIsActive() ? 'active' : 'inactive', | |
stats: { | |
views: totalViewCount, | |
application: totalAppCount | |
}, | |
createdAt, | |
updatedAt, | |
company: company && toCompanyBasic(company) | |
}; | |
} | |
static toJobDetail(job) { | |
const { | |
enhancements, | |
byline, | |
description, | |
benefits, | |
requirements, | |
careerLevel, | |
education, | |
travel, | |
payType | |
} = job; | |
const { toJobBasic, toMemberBasic, toCategoriesList } = DisplayService; | |
return { | |
...toJobBasic(job), | |
enhancements, | |
byline, | |
description, | |
benefits, | |
requirements, | |
careerLevel, | |
education, | |
travel, | |
payType | |
}; | |
} | |
}; | |
internals.mapResults = (results, mapFunc) => { | |
return Array.isArray(results) ? results.map(mapFunc) : mapFunc(results); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment