Skip to content

Instantly share code, notes, and snippets.

@owenallenaz
Created August 26, 2019 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owenallenaz/2efa2a102a2d6e80ad35ad981bd56ef0 to your computer and use it in GitHub Desktop.
Save owenallenaz/2efa2a102a2d6e80ad35ad981bd56ef0 to your computer and use it in GitHub Desktop.
Norway script example
const mongolayer = require("mongolayer");
async function findDraft(civid) {
}
async function findNav(civid) {
const contentResult = await findContent(civid);
const result = await site.plugins.nav.apis.navItemVersions.promises.aggregate([
{
$match : {
_id : { $in : contentResult.drafts.map(val => val._id) }
}
},
{
$lookup : {
from : "plugins_nav_navItems",
localField : "nav_id",
foreignField : "_id",
as : "navItem"
}
},
{
$project : {
_id : 1,
active : 1,
folderHref : "$navItem.folderHref"
}
}
]);
return result;
}
async function findContent(civid) {
const [navItemVersions, contentVersions] = await Promise.all([
site.plugins.nav.apis.navItemVersions.promises.find({ civid, active : true }),
site.plugins.nav.apis.contentVersions.promises.find({ "content.civid" : civid })
]);
if (contentVersions.length > 0) {
const drafts = [];
for(var [key, version] of Object.entries(contentVersions)) {
const instance = await site.plugins.nav.apis.contentInstances.promises.findById(version.instance_id);
if (instance === null) {
throw new Error("Orphan version");
}
const myDrafts = await findContent(instance.id);
if (myDrafts.success === true) {
drafts.push(...myDrafts.drafts);
}
}
return {
success : true,
drafts
}
}
if (navItemVersions.length > 0) {
return {
success : true,
drafts : navItemVersions
}
}
return {
success : false,
drafts : [],
message : `Oprhan civid: ${civid}.`
}
}
return findNav("5581ec5e7a11d91c38f88476");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment