VAT Checker Apps Script - rest

I'm trying to make VAT checker in Google Apps script, to use in a Google Sheet. My code looks like this:
function VATChecker(input1, input2) {
var country = input1
var VATid = input2
// Builds the VIES URL
const VATUrl = `https://ec.europa.eu/taxation_customs/vies/rest-api/ms/${country}/vat/${VATid}`;
const encodedVATUrl = encodeURI(VATUrl);
// Fetches & parses the URL
const fetchRequest = UrlFetchApp.fetch(encodedVATUrl);
const results = JSON.parse(fetchRequest.getContentText());
// Returns the Data
var data = [];
results.data.forEach(function(pieceOfData){
data.push(String(pieceOfData.isValid));
});
if (data = true)
return "VALID";
else
return "NOT VALID";
For now it's giving me the following error in the cell with the function:
TypeError: Cannot read property 'forEach' of undefined (line 20).
When manually creating the URL, this is the data that is returned
{
"isValid" : true,
"requestDate" : "2022-09-29T08:42:50.353Z",
"userError" : "VALID",
"name" : "MVI E-COM B.V.",
"address" : "\nTRASMOLENLAAN 00012\n3447GZ WOERDEN\n",
"requestIdentifier" : "",
"vatNumber" : "861876295B01",
"viesApproximate" : {
"name" : "---",
"street" : "---",
"postalCode" : "---",
"city" : "---",
"companyType" : "---",
"matchName" : 3,
"matchStreet" : 3,
"matchPostalCode" : 3,
"matchCity" : 3,
"matchCompanyType" : 3
}
}
Any suggestions?

There is no array returned and also object property data. So you can get the isValid directly from results:
/**
* Returns valid or invalid check on VAT number
*
* #param {string} vat The VAT id.
* #param {string} country the ISO2 country id. Default is NL
* #return {string} valid / not valid
* #customfunction
*/
function VATChecker(vat, country = "NL") {
// Builds the VIES URL
const url = `https://ec.europa.eu/taxation_customs/vies/rest-api/ms/${country}/vat/${vat}`;
// Fetches & parses the URL
const fetchRequest = UrlFetchApp.fetch(url);
const result = JSON.parse(fetchRequest.getContentText());
return (result.isValid) ? "VALID" : "NOT VALID"
}
function testVatInCodeEditor(){
console.log(VATChecker("003177397B01"))
}
Succes met het implementeren!

Related

Flutter : can't get all response body from get request

I have get request in Flutter app, when I test the request in postman I get all data, something like this :
{
"result":{
"name" : "somename",
"images":[
"test.jpg",
"test2.jpg"
],
"sizes":[
{
"id": 1,
"value" : 5
},
{
"id": 2,
"value" : 15
}
]
}
}
I call data and print them like this without using models:
var data = json.decode(response.body);
print(data['result']['name']);
print(data['result']['images']);
print(data['result']['sizes']);
it is print all things expect last one.
where must be the mistake?
Solved, by adding "?sizesView = true" to the link
final response = await http.get(path +'?sizesView = true'):
you should get the index of the last one because it is in a dictionary not a list do this:
print(data['result']['sizes'][0]['id']) // it will get the first index of the sizes list and then get the id key
or you can creat a model of list to get the indexes of your list

Flurry - custom events

Hi I am wondering why the custom events I have set up don't seem to be showing on flurry.com portal.
I am going to guess it has something to do with how I have set it up - but according to the flurry documentation I have done it correctly.
This is the result when I click a button that fires logEvent
msg = <FlurryStreamEvent: 0x28350e000, type = 134, json = { "fl.event.type" : "CUSTOM_EVENT", "fl.event.id" : 2, "fl.timed.event.duration" : 0, "fl.event.timed" : false, "fl.event.uptime" : 2284321185, "fl.timed.event.starting" : false, "fl.event.user.parameters" : { "RXBpc29kZV90aXRsZQ==" : "WW91ciBTbyBTdHVwaWQ=", "cG9kY2FzdA==" : "Mm5lcmRzIEluIEEgUm9vbQ==" }, "fl.event.name" : "UG9kY2FzdF9QbGF5", "fl.frame.version" : 1, "fl.event.flurry.parameters" : { }, "fl.event.timestamp" : 1603026151045 }>
My concern is "fl.event.flurry.parameters" : { }, it's empty - I have no idea if it is meant to be empty..
This is how I am calling it:
let data = ["podcast": post.title, "Episode_title":podcast.title]
Flurry.logEvent("Podcast_Play", withParameters: data)
fl.event.user.parameters is the one that contains the parameters you set, so in your example, they are reporting in. Not seeing them in the portal could be due to the expected time it takes for data to propagate. If it takes more than several hours, email us at support#flurry.com with the details.

How to add the new field to Object in Purescript

I am first about the Purescript.
I am going to add the new field to Object and send it as a function param.
But I can not find a good solution for this.
For example.
oldFiled = {
title : "title",
description : "d"
}
newField = {
time : "time"
}
//result after added new field
oldFiled = {
title : "title",
description : "d",
time : "time"
}
How can I do it?
If it's just about adding a single field you can use https://pursuit.purescript.org/packages/purescript-record/2.0.1/docs/Record#v:insert like so:
import Data.Record as Record
import Data.Symbol (SProxy(..))
oldFiled = {
title : "title",
description : "d"
}
newFiled = Record.insert (SProxy :: _ "time") "time" oldFiled
If you're merging records look at the merge union and disjointUnion functions in the Data.Record module

how to make async call using request-promise in google home action

I am trying to call a API in onSync() and return payload so that I will get number of devices. Even the api is returning me the proper data I am unable to show the devices. Following is the code snippet.
app.onSync((body) => {
// TODO: Implement SYNC response
console.log('************** inside sync **************');
var payload = {
agentUserId:'123',
devices:[]
};
//Calling Discove Devices API
requestAPI('------ calling API ------')
.then(function(data){
let result = JSON.parse(data);
console.log('********** RESULT ********** '+util.inspect(result,{depth:null}));
var count = 0;
count = Object.keys(result.Devices).length;
console.log('********** DEVICE COUNT ********** '+count);
//forming payload json of devices
for(var i in result.Devices){
var item = result.Devices[i];
payload.devices.push({
"id" : item.ieee_address,
"type" : 'action.devices.types.OUTLET',
"traits" : ['action.devices.traits.OnOff'],
name : {
"defaultNames" : [item.mapped_load],
"name" : item.mapped_load,
"nicknames" : [item.mapped_load],
},
"willReportState" : false,
"deviceInfo" : {
"manufacturer" : 'some manufacturer',
"model" : 'Smart-Plug',
"hwVersion" : '1.0',
"swVersion" : '1.0.1',
},
});
}
}).catch(function(err){
console.log(err);
});
console.log('PAYLOAD %J ',payload); <----- it is always empty
return {
requestId: body.requestId,
payload: payload,
};
});
API is returning me correct value but the payload is always empty.
Please help. I am new to node.js and I dont know how to make async call.
You're using an asynchronous call to get devices, and you'll need to make sure you don't return data before your request is complete. You return the Promise to the function, so it will wait:
app.onSync((body) => {
return requestApi('...')
.then(data => {
return {
requestId: body.requestId,
payload: payload
}
})
})

Translate postgres query to sequelize

I am having trouble connecting relationships in sequelize.
SELECT * from actors
JOIN "actorStatuses"
on "actorStatuses".actor_id = actors.id
JOIN movies
on movies.id = "actorStatuses".actor_id
WHERE movies.date = '7/8/2017';
Here you go :
model.Actor.findAll({ // change model name as per yours
include : [
{
model : model.ActorStatuses // change model name as per yours
required : true ,
},{
model : model.Movies // change model name as per yours
required : true ,
where : { date : 'your_date' }
}
]
});
This will create exact same query / result as you required.