Skip to content

Commit

Permalink
for #3 added cms support to upsert case studies to scs
Browse files Browse the repository at this point in the history
  • Loading branch information
victorkane committed Sep 7, 2018
1 parent 34cfa19 commit 35ed8e3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
metaData:
itemSlug: case-study-one
itemName: Case Study One
itemSlug: what-why
itemName: What? Why?
itemType: caseStudy
language: en-US
published: true
Expand Down
37 changes: 36 additions & 1 deletion cms/upsert/upsert.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ if (process.argv.length > 3) {
}

async function processArticles(contentItem) {
// get author _id for query population TODO authors, make sure we have one at least
const optionsGet = {
method: 'GET',
uri: apiHost + '/api/authors?itemName=' + contentItem.author + '&select=_id',
Expand All @@ -31,8 +32,9 @@ async function processArticles(contentItem) {
if (authors.length > 0) {
contentItem.author = authors[0]._id
} else {
console.log("no author specified\n")
return console.log("no author specified\n")
}
// upsert article
const optionsPut = {
method: 'PUT',
uri: apiHost + '/api/' + contentType,
Expand All @@ -45,6 +47,7 @@ async function processArticles(contentItem) {
}

async function processBooks(contentItem) {
// get author _id for query population TODO authors and/or editors, make sure we have one at least one of either
const optionsGetAuthor = {
method: 'GET',
uri: apiHost + '/api/authors?itemName=' + contentItem.author + '&select=_id',
Expand All @@ -56,6 +59,7 @@ async function processBooks(contentItem) {
} else {
console.log("no author specified\n")
}
// get publisher for query population
const optionsGetPublisher = {
method: 'GET',
uri: apiHost + '/api/publishers?itemName=' + contentItem.publisher + '&select=_id',
Expand All @@ -67,6 +71,35 @@ async function processBooks(contentItem) {
} else {
console.log("no publisher specified\n")
}
// upsert book
const optionsPut = {
method: 'PUT',
uri: apiHost + '/api/' + contentType,
body: contentItem,
json: true
}
const res = await rp(optionsPut)
console.log("res", res.message)
return res
}

async function processCaseStudies(contentItem) {
// get articles _id from provided slug for query population
let theArticles = []
for (const article of contentItem.articles) {
const optionsGetPublisher = {
method: 'GET',
uri: apiHost + '/api/articles/slug/' + article.article + '?select=_id',
json: true
}
const articleId = await rp(optionsGetPublisher)
theArticles.push({
weight: article.weight,
article: articleId._id
})
}
contentItem["articles"] = theArticles
// upsert case study
const optionsPut = {
method: 'PUT',
uri: apiHost + '/api/' + contentType,
Expand Down Expand Up @@ -103,6 +136,8 @@ fs.readFile(file, 'utf8', function (err,content) {
result = processArticles(contentItem)
} else if (contentType === 'books') {
result = processBooks(contentItem)
} else if (contentType === 'case-studies') {
result = processCaseStudies(contentItem)
} else {
result = processGenericContent(contentItem)
}
Expand Down

0 comments on commit 35ed8e3

Please sign in to comment.