UNPKG

@scayle/storefront-core

Version:

Collection of essential utilities to work with the Storefront API

43 lines (42 loc) 1.15 kB
import { campaignHasNotEnded, sortCampaignsByDateAscending, isCampaignActive } from "../../utils/campaign.mjs"; import { defineRpcHandler } from "../../utils/index.mjs"; const getCampaigns = async (context) => { const { cached, sapiClient } = context; const { campaigns } = await cached( async () => { const { entities } = await sapiClient.campaigns.get(); return { campaigns: entities.filter((campaign) => { return campaignHasNotEnded(campaign); }).sort(sortCampaignsByDateAscending) }; }, { cacheKeyPrefix: "get-campaigns", ttl: 5 * 60 } )(); return campaigns; }; export const getCampaign = defineRpcHandler( async (context) => { const campaigns = await getCampaigns(context); return campaigns.find(isCampaignActive); }, { method: "GET" } ); export const getCampaignKey = defineRpcHandler( async (context) => { if (context.campaignKey) { return context.campaignKey; } const campaigns = await getCampaigns(context); const campaign = campaigns.find(isCampaignActive); return campaign?.key; }, { method: "GET" } );