{"size": 1828, "ext": "js", "max_stars_repo_path": "web/campaigns/utils/getActive.js", "max_stars_repo_name": "Universal-Health-Services/campaign-pages", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "web/campaigns/utils/getActive.js", "max_issues_repo_name": "Universal-Health-Services/campaign-pages", "max_issues_repo_issues_event_min_datetime": "2020-06-22T15:28:52.000Z", "max_issues_repo_issues_event_max_datetime": "2020-10-22T19:53:14.000Z", "max_forks_repo_path": "web/campaigns/utils/getActive.js", "max_forks_repo_name": "Universal-Health-Services/campaign-pages", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 25.7464788732, "max_line_length": 85, "alphanum_fraction": 0.6066739606}
const BlocksToMarkdown=require('@sanity/block-content-to-markdown')
const groq=require('groq')
const client=require('./sanityClient.js')
const serializers=require('./serializers')

function generate(ea) {
  /* Convert to Markdown, for Each with Teaser's Body */
  ea.teasers.forEach(function (tease) {
    tease.body=BlocksToMarkdown(tease.body, { serializers, ...client.config() })
  });
  /* Convert to Markdown, for Each with Main's and Thank You's Body */
  return {
    ...ea,
    main: BlocksToMarkdown(ea.main.body, { serializers, ...client.config() }),
    thankyou: BlocksToMarkdown(ea.thankyou.body, { serializers, ...client.config() })
  }
}

async function getActive() {
  const filter=groq`*[_type == "campaign" && schedule.startDate < now()]`
  const projection=groq`{
    _id,
    description,
    "oSlug": owner->code.current,
    "sSlug": serviceline->code.current,
    tel,
    "hero": hero {
      "image": heroImage {
        ...,
        "alt": alt,
        "src": asset->url
      },
      "title": title,
      "subtitle": subtitle
    },
    main,
    analytics,
    "teasers": teasers[]{
  		"image": image {
  			...,
  			"alt": alt,
  			"src": asset->url
			},
      "title": title,
			"body": body,
			"terms": terms
    },
    awards,
    map,
    locations[]->,
    "owner": owner-> {
      ...,
      "logo": logo {
        ...,
        asset->
      }
    },
    "serviceline": serviceline->name,
    "serviceTagline": serviceline->tagline,
    thankyou
  }`
  const order=`| order(schedual.startDate asc)`
  const query=[filter, projection, order].join(' ')
  const docs=await client.fetch(query).catch(err => console.error(err))
  const activePages=docs.map(generate)
  //console.log(`Page: `+JSON.stringify(activePages))
  //console.log(docs)
  return activePages
}

module.exports=getActive