Skip to content

Instantly share code, notes, and snippets.

@Janekk
Last active September 25, 2017 11:17
Show Gist options
  • Save Janekk/c8cd18f683370afbc820864c455582ec to your computer and use it in GitHub Desktop.
Save Janekk/c8cd18f683370afbc820864c455582ec to your computer and use it in GitHub Desktop.
node_example_handling_async
export async function getCardPage(req, res, next) {
try {
const query = parseCardQuery(req.query);
const cardId = req.params.id;
const [card, campaign] = await Promise.all([
CardApi.getCard(cardId),
getCardCampaign(cardId, query.campaignId)
]);
const record = CardUtils.mergeCardWithProgram(card, card.program);
let state = getCardState(record, campaign, true, 'standalone', query);
let redirectViewParams = null;
if (campaign) {
redirectViewParams = await getCardRedirectParams(card, campaign, req);
cookieVisitedCampaign(req.cookies, res, campaign.id);
state = {
...state,
isRedirectMode: true,
redirectView: redirectViewParams
};
renderVuCardPage(res, 'vucard', record, null, state);
}
else {
const recordHtml = ReactDOMServer.renderToString(<VuCard {...state}/>);
renderVuCardPage(res, 'vucard', record, recordHtml, state);
}
}
catch (e) {
console.trace(e);
next(e);
}
}
async function getCardRedirectParams(card, campaign, req) {
try {
if (CampaignUtils.isRedirectEnabled(campaign)) {
let redirectCookie = getRedirectCookie(req.cookies);
const isFirstTimeVisit = !RedirectCookieUtils.hasVisitedCampaign(redirectCookie, campaign.id);
if (isFirstTimeVisit) {
switch (campaign.firstTimeRedirectAction) {
default:
case RedirectConst.firstTimeRedirectActions.default: {
const deviceTypeRedirectParams = getDeviceTypeRedirectParams(card, campaign, req);
if (deviceTypeRedirectParams) {
return deviceTypeRedirectParams;
}
else {
return await getDefaultCardRedirectParams(card, campaign, req);
}
}
case RedirectConst.firstTimeRedirectActions.networkRedirect: {
const network = getRedirectNetwork(card);
if (network) {
return {
redirectType: RedirectConst.redirectTypes.firstTime,
cardId: card.id,
campaignId: campaign.id,
network: null,
destination: network
}
}
break;
}
case RedirectConst.firstTimeRedirectActions.noRedirect:
return null;
}
}
else {
return await getDefaultCardRedirectParams(card, campaign, req);
}
}
}
catch (e) {
const message = 'Proceed with showing the default card';
console.trace(e);
console.log(message);
if (isSentryActive()) {
raven.captureException(e, {
req,
extra: {
message
}
})
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment