So, i wanted to check if an ad was displayed on an allowed website using javascript. I used a methodology explained in that post Determine if string is in list in JavaScript.
Here is my code :
async function ImpCertification(tx) {
Impression = await getAssetRegistry('org.example.basic.Impression')
const source = Impression.PrintedSite
whitelist = ["Youtube", "Google", "Facebook", "Twitter"]
if (whitelist.indexOf(source) < 0) {
// Checks if source in whitelist
throw new Error ('This impression does not respect branding')
}
// Checks every necessary conditions to validate impression
if (whitelist.indexOf(source) >=0) {
// Save the old value of the asset.
const oldValue = Impression.Valid;
// Update the asset with the new value.
Impression.Valid = true;
// Get the asset registry for the asset.
const assetRegistry = await getAssetRegistry('org.example.basic.transaction.ValidateImpression');
// Update the asset in the asset registry.
await assetRegistry.update(Impression);
// Emit an event for the modified asset.
let event = getFactory().newEvent('org.example.basic', 'Validation');
event.asset = tx.asset;
event.oldValue = oldValue;
event.newValue = true;
emit(event);
}
await null
}
I'm developing an hyperledger business network so some parts maybe seem exotic but I must bring your attention to the whitelist/indexOf stuff. It just doesn't work although I get the logic of it.
I tried to enter every element of the whitelist in Impression.PrintedSite but it keeps throwing the "this impression does not respect branding" error everytime, wether it is right or not.
Before you ask, I checked for caps in Impression.PrintedSite, it is a string, I tried the other method suggested using booleans and "includes" but it just won't work. Halp !