UNPKG

ondc-campaign-sdk

Version:

[![npm version](https://img.shields.io/npm/v/ondc-campaign-sdk.svg)](https://www.npmjs.com/package/ondc-campaign-sdk) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![Made with ❤️](https://img.shields.io/badge/Made%20with-%

163 lines (140 loc) 4.59 kB
export async function fetchLiveCampaignProducts( withTransaction: boolean = false, campignOrder: number = 1 ): Promise<any> { try { // Get or generate user ID from localStorage let userId = ""; if (typeof window !== "undefined") { // Check if userId exists in localStorage const storedUserId = localStorage.getItem("ondc_user_id"); // If userId doesn't exist, generate a UUID and store it if (!storedUserId) { userId = crypto.randomUUID(); localStorage.setItem("ondc_user_id", userId); } else { userId = storedUserId; } } // First fetch the active campaign data const response = await fetch( "https://ondc-sdk.samhita.org/api/campaigns/active?order=" + campignOrder ); if (!response.ok) { throw new Error("Failed to fetch campaign data"); } const campaignData = await response.json(); if (!campaignData._id) { throw new Error("No campaign id found"); } // If transaction is not required or not in browser, return campaign data only if (!withTransaction || typeof window === "undefined") { return campaignData; } // Get actual current URL for transaction const currentUrl = window.location.href; interface TransactionPayload { company_url: string; user_id: string; campaign_id: string; } // Create transaction with campaign_id const transactionPayload: TransactionPayload = { company_url: currentUrl, user_id: userId, campaign_id: campaignData._id }; const transactionResponse = await fetch( "https://ondc-sdk.samhita.org/api/transactions/create", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(transactionPayload), } ); if (!transactionResponse.ok) { throw new Error(transactionResponse.statusText); } // Get transaction data const transactionData = await transactionResponse.json(); return { campaign: campaignData, transaction: transactionData, user_id: userId, }; } catch (error) { console.error("Error fetching campaign products:", error); return { message: "No active campaign found", error: error }; } } export async function fetchLiveCampaignBasedOnCount( count: string, withTransaction: boolean = false ) { try { // Get or generate user ID from localStorage let userId = ""; if (typeof window !== "undefined") { // Check if userId exists in localStorage const storedUserId = localStorage.getItem("ondc_user_id"); // If userId doesn't exist, generate a UUID and store it if (!storedUserId) { userId = crypto.randomUUID(); localStorage.setItem("ondc_user_id", userId); } else { userId = storedUserId; } } // First fetch the campaigns data const response = await fetch( "https://ondc-sdk.samhita.org/api/campaigns/active-based-on-count?count=" + count ); if (!response.ok) { throw new Error("Failed to fetch campaigns data"); } const campaignsData = await response.json(); // If transaction is not required or not in browser, return campaigns data only if (!withTransaction || typeof window === "undefined") { return campaignsData; } // Get actual current URL for transaction const currentUrl = window.location.href; interface TransactionPayload { company_url: string; user_id: string; campaign_id?: string; } // Create transaction without campaign_id since this is for multiple campaigns const transactionPayload: TransactionPayload = { company_url: currentUrl, user_id: userId, campaign_id : campaignsData.map((campaign: any) => campaign._id) }; const transactionResponse = await fetch( "https://ondc-sdk.samhita.org/api/transactions/create", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(transactionPayload), } ); if (!transactionResponse.ok) { throw new Error(transactionResponse.statusText); } // Get transaction data const transactionData = await transactionResponse.json(); return { campaigns: campaignsData, transaction: transactionData, user_id: userId, }; } catch (error) { console.error("Error fetching campaigns:", error); return { message: "No active campaigns found", error: error }; } }