...
Code Block |
---|
{ "data": { "loyaltyRewardsV2": [ { "id": "3b5bdddc-c7c9-42d8-9332-b0c3f36c8e1c", "name": "Value Powerade Zero", "errors": null }, { "id": "728aaedd-3234-4eae-89af-35cd5672adcb", "name": "Large Dr. Pepper", "errors": null }, { "id": "480122d7-86ff-4db0-965c-7dbb72bb5179", "name": "Value Soft Drinks", "errors": null } ] } } |
Mapping loyalty offers between Sanity and the Loyalty Engine
After retrieving all the available offers from both sources, you need to map this data to get a complete list of offers available to the user. Below there is a simple example in JavaScript demonstrating how to perform this mapping:
Note |
---|
This example focuses on the basic data mapping process. It does not include logic related to offer rules, authentication requirements, or error handling. Each implementation should incorporate the necessary logic based on its specific needs. |
Code Block |
---|
const { liveConfigOffers, sortedSystemwideOffers } = sanityOffers;
const { loyaltyOffersV2 } = loyaltyEngineOffers;
const allAvailableUserOffers = loyaltyOffersV2
.map(loyaltyOffer => {
let sanityOffer;
if (loyaltyOffer.type === 'GLOBAL') {
sanityOffer = sortedSystemwideOffers.find(
offer => offer.loyaltyEngineId === loyaltyOffer.id
);
} else if (loyaltyOffer.type === 'PERSONALIZED') {
sanityOffer = liveConfigOffers.find(
offer => offer.loyaltyEngineId === loyaltyOffer.id
);
}
if (sanityOffer) {
return sanityOffer;
}
})
.filter(offer => offer !== undefined); |
In this example 💡:
sanityOffers contains the offers retrieved from Sanity, destructured into liveConfigOffers (personalized offer templates) and sortedSystemwideOffers (system-wide offers).
loyaltyEngineOffers contains the offers retrieved from the Loyalty Engine, specifically the loyaltyOffersV2 array.
allAvailableUserOffers contains all the available offers for the user.