refactoring some code and fixing typos

This commit is contained in:
Elias Papanikitas
2024-03-05 18:04:52 +01:00
parent 4827f1bb9b
commit 8fa52c7c55
4 changed files with 255 additions and 185 deletions

View File

@@ -1,17 +1,17 @@
export const APIERRORCODES = {
STATUS200:{
export const API_STATUS_CODES = {
STATUS_200:{
STATUS: "OK",
CODE: 200
},
STATUS402:{
STATUS_402:{
STATUS: "no data has been found, check if kbo numer is correct or organisation has OVO number. Organisation without OVO number are not in wegwijs",
CODE: 402
},
STATUS403:{
STATUS_403:{
STATUS: "no kbo number found in OP",
CODE: 403
},
STATUS500:{
STATUS_500:{
STATUS: '',
CODE: 500
}

136
app.js
View File

@@ -17,48 +17,41 @@ import {
WEGWIJSAPI,
WEGWIJSAPIFIELDS,
} from "./config";
import { APIERRORCODES } from "./apiErrorHandler";
import { API_STATUS_CODES } from "./apiErrorHandler";
import { WEGWIJS_DATA_OBJECT_IDS } from "./wegwijObjectDataIds";
app.post("/sync-kbo-data/:kboStructuredIdUuid", async function (req, res) {
try {
const kboStructuredIdUuid = req.params.kboStructuredIdUuid;
const identifiers = await getIdentifiers(kboStructuredIdUuid);
console.log("create kbo? " + createKbo);
if (!identifiers?.kbo) {
return throwServerError(
APIERRORCODES.STATUS403,
res
);
return throwServerError(API_STATUS_CODES.STATUS_403, res);
}
const wegwijsUrl = `${WEGWIJSAPI}?q=kboNumber:${identifiers.kbo}&fields=${WEGWIJSAPIFIELDS}`;
console.log("url: " + wegwijsUrl);
const response = await fetch(wegwijsUrl);
const data = await response.json();
let wegwijsOvo = null;
let kboObject = null;
if (!data.length) {
return throwServerError(APIERRORCODES.STATUS402, res);
return throwServerError(API_STATUS_CODES.STATUS_402, res);
}
// We got a match on the KBO, getting the associated OVO back
const wegwijsInfo = data[0]; // Wegwijs should only have only one entry per KBO
kboObject = getKboFields(wegwijsInfo);
if (kboObject.ovoNumber) {
wegwijsOvo = kboObject.ovoNumber;
const kboIdentifiers = await getKboIdentifiers(identifiers.adminUnit);
if (!kboIdentifiers && kboObject) {
await createKbo(kboObject, identifiers.kboId, identifiers.adminUnit);
}
if (kboObject) {
const kboIdentifiers = await getKboIdentifiers(identifiers.adminUnit);
if (!kboIdentifiers) {
await createKbo(kboObject, identifiers.kboId, identifiers.adminUnit);
}
if (isUpdate(kboObject, kboIdentifiers)) {
updateKboOrg(kboObject, kboIdentifiers);
}
if (isUpdate(kboObject, kboIdentifiers)) {
updateKboOrg(kboObject, kboIdentifiers);
}
let wegwijsOvo = kboObject.ovoNumber ?? null;
//Update Ovo Number
if (wegwijsOvo && wegwijsOvo != identifiers.ovo) {
console.log(identifiers.ovo);
@@ -73,9 +66,9 @@ app.post("/sync-kbo-data/:kboStructuredIdUuid", async function (req, res) {
await updateOvoNumberAndUri(ovoStructuredIdUri, wegwijsOvo);
}
return throwServerError(APIERRORCODES.STATUS200, res); // since we await, it should be 200
return throwServerError(API_STATUS_CODES.STATUS_200, res); // since we await, it should be 200
} catch (e) {
return throwServerError(APIERRORCODES.STATUS500, res, e);
return throwServerError(API_STATUS_CODES.STATUS_500, res, e);
}
});
@@ -92,68 +85,44 @@ function getKboFields(data) {
locations = [],
} = data;
//no lables = undefined
//formal naam according to KBO
//no labels = undefined
//formal name according to KBO
let formalName = extractObjectData(labels, WEGWIJS_DATA_OBJECT_IDS.LABELS)?.value;
let email = extractObjectData(contacts, WEGWIJS_DATA_OBJECT_IDS.CONTACTS.EMAIL)?.value;
let phone = extractObjectData(contacts, WEGWIJS_DATA_OBJECT_IDS.CONTACTS.PHONE)?.value;
let website = extractObjectData(contacts, WEGWIJS_DATA_OBJECT_IDS.CONTACTS.WEBSITE)?.value;
let formalName = labels?.findLast((fields) => {
return fields.labelTypeId === "83c1c22a-6776-0ad6-68d2-819e1c6eec66";
})?.value;
//main location according to KBO
let formattedAddress = extractObjectData(locations, WEGWIJS_DATA_OBJECT_IDS.ADDRESS)?.formattedAddress;
let adressComponent = extractObjectData(locations, WEGWIJS_DATA_OBJECT_IDS.ADDRESS)?.components;
let rechtsvorm = organisationClassifications?.findLast((fields) => {
return (
fields[WEGWIJS_DATA_OBJECT_IDS.RECHTSVORM.NAME] ===
WEGWIJS_DATA_OBJECT_IDS.RECHTSVORM.ID_1 ||
fields.organisationClassificationTypeId ===
WEGWIJS_DATA_OBJECT_IDS.RECHTSVORM.ID_2
);
})?.organisationClassificationName;
let startDate = labels?.findLast((fields) => {
return fields.labelTypeId === "83c1c22a-6776-0ad6-68d2-819e1c6eec66";
})?.validity?.start;
return (
fields[WEGWIJS_DATA_OBJECT_IDS.LABELS.NAME] ===
WEGWIJS_DATA_OBJECT_IDS.LABELS.ID
);
})?.validity?.start;
let activeState = labels?.findLast((fields) => {
return (
fields.labelTypeId === "83c1c22a-6776-0ad6-68d2-819e1c6eec66" &&
fields[WEGWIJS_DATA_OBJECT_IDS.LABELS.NAME] ===
WEGWIJS_DATA_OBJECT_IDS.LABELS.ID &&
!fields.validity.hasOwnProperty("end")
);
}) ? ORGANIZATION_STATUS.ACTIVE : ORGANIZATION_STATUS.INACTIVE;
let email = data.contacts?.findLast((fields) => {
return (
fields.contactTypeId === "f611e91d-a811-4519-afa6-08f0e9dcdd9b" &&
!fields.validity.hasOwnProperty("end")
);
})?.value;
let rechtsvorm = organisationClassifications?.findLast((fields) => {
return (
fields.organisationClassificationTypeId ===
"9bae7539-64fd-5759-743c-ad9dfa4143d4" ||
fields.organisationClassificationTypeId ===
"1131205e-9212-435d-b4cd-b0d955d08bcf"
);
})?.organisationClassificationName;
let phone = contacts?.findLast((fields) => {
return (
fields.contactTypeId === "d792d4ff-8c34-4336-8434-56ecb40f2fa4" &&
!fields.validity.hasOwnProperty("end")
);
})?.value;
let website = contacts?.findLast((fields) => {
return (
fields.contactTypeId === "6763f372-02c5-478c-b7d7-802dc60a64b9" &&
!fields.validity.hasOwnProperty("end")
);
})?.value;
let formattedAddress = locations?.findLast((fields) => {
return (
fields.locationTypeId === "537c0b5b-8ab8-fc3d-0b37-f8249cbdd3ba" &&
!fields.validity?.hasOwnProperty("end")
);
})?.formattedAddress;
//main location according to KBO
let adressComponent = locations?.findLast((fields) => {
return (
fields.locationTypeId === "537c0b5b-8ab8-fc3d-0b37-f8249cbdd3ba" &&
!fields.validity?.hasOwnProperty("end")
);
})?.components;
console.log(formattedAddress);
console.log(startDate);
console.log(activeState);
return {
changeTime: changeTime ?? "",
@@ -177,12 +146,12 @@ new CronJob(
CRON_PATTERN,
async function () {
const now = new Date().toISOString();
console.log(`OVO numbers healing triggered by cron job at ${now}`);
console.log(`Wegwijs data healing triggered by cron job at ${now}`);
try {
await healAbbWithWegWijsData();
} catch (err) {
console.log(
`An error occurred during OVO numbers healing at ${now}: ${err}`
`An error occurred during wegwijs data healing at ${now}: ${err}`
);
}
},
@@ -214,7 +183,6 @@ async function healAbbWithWegWijsData() {
await updateOvoNumberAndUri(ovoStructuredIdUri, wegwijsOvo);
}
console.log(kboIdentifierOP);
if (!kboIdentifierOP?.kboOrg) {
console.log("creating new kboUnit");
await createKbo(
@@ -225,7 +193,7 @@ async function healAbbWithWegWijsData() {
}
if (isUpdate(wegwijsKboOrg, kboIdentifierOP)) {
const kboIdentifiers = await getKboIdentifiers(identifiers.adminUnit);
const kboIdentifiers = await getKboIdentifiers(kboIdentifierOP.abbOrg);
updateKboOrg(wegwijsKboOrg, kboIdentifiers);
}
}
@@ -276,10 +244,20 @@ async function createKbo(wegwijsKboOrg, kboId, abbOrg) {
}
function throwServerError(statusCode, res, message) {
if(statusCode.CODE === 500){
if (statusCode.CODE === 500) {
console.log("Something went wrong while calling /sync-from-kbo", message);
}
return res.status(statusCode.CODE).send(statusCode.STATUS);
}
function extractObjectData(object, field) {
let test = object?.findLast((fields) => {
return fields[field.NAME] === field.ID;
});
console.log(test);
return test;
}
app.use(errorHandler);

View File

@@ -240,9 +240,9 @@ export async function getAllAbbKboOrganizations() {
<http://schema.org/telephone> ?phone ;
<http://schema.org/contactType> ?contactType ;
<http://xmlns.com/foaf/0.1/page> ?website ;
<http://www.w3.org/ns/locn#address> ?adress .
<http://www.w3.org/ns/locn#address> ?address .
?adress <http://www.w3.org/ns/locn#fullAddress> ?fullAddress.
?address <http://www.w3.org/ns/locn#fullAddress> ?fullAddress.
}
OPTIONAL{
@@ -367,7 +367,7 @@ export async function createNewKboOrg(kboOrg, kboId) {
kboOrg.changeTime
)}`
}
if(kboOrg.kboId){
if(kboId){
updateStr += `; <http://www.w3.org/ns/adms#identifier> ${sparqlEscapeUri(
kboId
)}`
@@ -393,60 +393,81 @@ export async function createNewKboOrg(kboOrg, kboId) {
}
export async function updateKboOrg(newKboInfo, identifiers) {
//update Adress
//update address
let updateStr = `
DELETE{
GRAPH ?g{
?adressUri a <http://www.w3.org/ns/locn#Address> ;
DELETE{
GRAPH ?g{
?adressUri a <http://www.w3.org/ns/locn#Address> ;
<http://www.w3.org/ns/locn#fullAddress> ?fulladdress .
}
INSERT{
graph ?g{
?adressUri a <http://www.w3.org/ns/locn#Address>
`;
if(newKboInfo.formattedAddress){
updateStr += `
;
<http://www.w3.org/ns/locn#fullAddress> ${sparqlEscapeString(
newKboInfo.formattedAddress
)}
`;
}
updateStr += `
.
}
}
`;
updateStr += `
WHERE{
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
}
GRAPH ?g {
?adressUri a <http://www.w3.org/ns/locn#Address> ;
<http://www.w3.org/ns/locn#fullAddress> ?fulladdress .
}
}
INSERT{
graph ?g{
?adressUri a <http://www.w3.org/ns/locn#Address> ;
<http://www.w3.org/ns/locn#fullAddress> ${sparqlEscapeString(
newKboInfo.formattedAddress
)} .
}
}
WHERE{
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
}
GRAPH ?g {
?adressUri a <http://www.w3.org/ns/locn#Address> ;
<http://www.w3.org/ns/locn#fullAddress> ?fulladdress .
FILTER(?adressUri = ${sparqlEscapeUri(identifiers.addressUri)})
}
}
FILTER(?adressUri = ${sparqlEscapeUri(identifiers.addressUri)})
}
}
`;
await update(updateStr);
//update ContactPoint
updateStr = `
DELETE{
GRAPH ?g{
?contact a <http://schema.org/ContactPoint> ;
<http://schema.org/email> ?email ;
<http://schema.org/telephone> ?phone ;
<http://xmlns.com/foaf/0.1/page> ?website .
}
DELETE{
GRAPH ?g{
?contact a <http://schema.org/ContactPoint> ;
<http://schema.org/email> ?email ;
<http://schema.org/telephone> ?phone ;
<http://xmlns.com/foaf/0.1/page> ?website .
}
}
INSERT{
graph ?g{
?contact a <http://schema.org/ContactPoint>
`;
if(newKboInfo.email){
updateStr += `; <http://schema.org/email> ${sparqlEscapeString(
newKboInfo.email
)}`
}
INSERT{
graph ?g{
?contact a <http://schema.org/ContactPoint> ;
<http://schema.org/email> ${sparqlEscapeString(
newKboInfo.email
)} ;
<http://schema.org/telephone> ${sparqlEscapeString(
newKboInfo.phone
)} ;
<http://xmlns.com/foaf/0.1/page> ${sparqlEscapeString(
newKboInfo.website
)} .
if(newKboInfo.phone){
updateStr += `; <http://schema.org/telephone> ${sparqlEscapeString(
newKboInfo.phone
)}`
}
if(newKboInfo.phone){
updateStr += `; <http://xmlns.com/foaf/0.1/page> ${sparqlEscapeString(
newKboInfo.website
)}`
}
updateStr += `
.
}
}
`;
updateStr += `
WHERE{
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
@@ -465,61 +486,103 @@ export async function updateKboOrg(newKboInfo, identifiers) {
//update kboUnit
updateStr = `
DELETE{
GRAPH ?g{
?kboUnit a <http://mu.semte.ch/vocabularies/ext/KboEenheid> ;
<http://mu.semte.ch/vocabularies/ext/rechtsvorm> ?rechtsVorm ;
<http://mu.semte.ch/vocabularies/ext/shortName> ?shortName ;
<http://mu.semte.ch/vocabularies/ext/startDate> ?startDate ;
<http://www.w3.org/2004/02/skos/core#prefLabel> ?orgName ;
<http://www.w3.org/2004/02/skos/core#altLabel> ?commercialName ;
<https://data.lblod.info/vocabularies/generiek/geplandeEindDatum> ?changeTime ;
<http://www.w3.org/ns/regorg#orgStatus> ?activeState .
}
}INSERT{
graph ?g{
?kboUnit a <http://mu.semte.ch/vocabularies/ext/KboEenheid> ;
<http://mu.semte.ch/vocabularies/ext/rechtsvorm> ${sparqlEscapeString(
newKboInfo.rechtsvorm
)} ;
<http://mu.semte.ch/vocabularies/ext/shortName> ${sparqlEscapeString(
newKboInfo.shortName
)} ;
<http://mu.semte.ch/vocabularies/ext/startDate> ${sparqlEscapeString(
newKboInfo.startDate
)} ;
<http://www.w3.org/2004/02/skos/core#prefLabel> ${sparqlEscapeString(
newKboInfo.organisationName
)} ;
<http://www.w3.org/2004/02/skos/core#altLabel> ${sparqlEscapeString(
newKboInfo.formalName
)} ;
<https://data.lblod.info/vocabularies/generiek/geplandeEindDatum> ${sparqlEscapeString(
newKboInfo.changeTime
)} ;
<http://www.w3.org/ns/regorg#orgStatus> ${sparqlEscapeUri(
newKboInfo.activeState
)} .
}
}
WHERE{
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
}
GRAPH ?g {
?kboUnit a <http://mu.semte.ch/vocabularies/ext/KboEenheid> ;
<http://mu.semte.ch/vocabularies/ext/rechtsvorm> ?rechtsVorm ;
<http://mu.semte.ch/vocabularies/ext/shortName> ?shortName ;
<http://mu.semte.ch/vocabularies/ext/startDate> ?startDate ;
<http://www.w3.org/2004/02/skos/core#prefLabel> ?orgName ;
<http://www.w3.org/2004/02/skos/core#altLabel> ?commercialName ;
<https://data.lblod.info/vocabularies/generiek/geplandeEindDatum> ?changeTime ;
<http://www.w3.org/ns/regorg#orgStatus> ?activeState .
FILTER(?kboUnit = ${sparqlEscapeUri(identifiers.kboUnit)})
}
}
DELETE{
GRAPH ?g{
?kboUnit a <http://mu.semte.ch/vocabularies/ext/KboEenheid> ;
<http://mu.semte.ch/vocabularies/ext/rechtsvorm> ?rechtsVorm ;
<http://mu.semte.ch/vocabularies/ext/shortName> ?shortName ;
<http://mu.semte.ch/vocabularies/ext/startDate> ?startDate ;
<http://www.w3.org/2004/02/skos/core#prefLabel> ?orgName ;
<http://www.w3.org/2004/02/skos/core#altLabel> ?commercialName ;
<https://data.lblod.info/vocabularies/generiek/geplandeEindDatum> ?changeTime ;
<http://www.w3.org/ns/regorg#orgStatus> ?activeState .
}
}
INSERT{
graph ?g{
?kboUnit a <http://mu.semte.ch/vocabularies/ext/KboEenheid>
`;
if(newKboInfo.rechtsvorm){
updateStr += `
;
<http://mu.semte.ch/vocabularies/ext/rechtsvorm> ${sparqlEscapeString(
newKboInfo.rechtsvorm
)}
`;
}
if(newKboInfo.shortName){
updateStr += `
;
<http://mu.semte.ch/vocabularies/ext/shortName> ${sparqlEscapeString(
newKboInfo.shortName
)}
`;
}
if(newKboInfo.startDate){
updateStr += `
;
<http://mu.semte.ch/vocabularies/ext/startDate> ${sparqlEscapeString(
newKboInfo.startDate
)}
`;
}
if(newKboInfo.organisationName){
updateStr += `
;
<http://www.w3.org/2004/02/skos/core#prefLabel> ${sparqlEscapeString(
newKboInfo.organisationName
)}
`;
}
if(newKboInfo.formalName){
updateStr += `
;
<http://www.w3.org/2004/02/skos/core#altLabel> ${sparqlEscapeString(
newKboInfo.formalName
)}
`;
}
if(newKboInfo.changeTime){
updateStr += `
;
<https://data.lblod.info/vocabularies/generiek/geplandeEindDatum> ${sparqlEscapeString(
newKboInfo.changeTime
)}
`;
}
if(newKboInfo.activeState){
updateStr += `
;
<http://www.w3.org/ns/regorg#orgStatus> ${sparqlEscapeUri(
newKboInfo.activeState
)}
`;
}
updateStr += `
.
}
}
`;
updateStr += `
WHERE{
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
}
GRAPH ?g {
?kboUnit a <http://mu.semte.ch/vocabularies/ext/KboEenheid> ;
<http://mu.semte.ch/vocabularies/ext/rechtsvorm> ?rechtsVorm ;
<http://mu.semte.ch/vocabularies/ext/shortName> ?shortName ;
<http://mu.semte.ch/vocabularies/ext/startDate> ?startDate ;
<http://www.w3.org/2004/02/skos/core#prefLabel> ?orgName ;
<http://www.w3.org/2004/02/skos/core#altLabel> ?commercialName ;
<https://data.lblod.info/vocabularies/generiek/geplandeEindDatum> ?changeTime ;
<http://www.w3.org/ns/regorg#orgStatus> ?activeState .
FILTER(?kboUnit = ${sparqlEscapeUri(identifiers.kboUnit)})
}
}
`;
await update(updateStr);
}

29
wegwijObjectDataIds.js Normal file
View File

@@ -0,0 +1,29 @@
export const WEGWIJS_DATA_OBJECT_IDS = {
LABELS: {
ID: "83c1c22a-6776-0ad6-68d2-819e1c6eec66",
NAME: "labelTypeId",
},
ADDRESS: {
ID: "537c0b5b-8ab8-fc3d-0b37-f8249cbdd3ba",
NAME: "locationTypeId",
},
RECHTSVORM: {
ID_1: "9bae7539-64fd-5759-743c-ad9dfa4143d4",
ID_2: "1131205e-9212-435d-b4cd-b0d955d08bcf",
NAME: "organisationClassificationTypeId",
},
CONTACTS: {
EMAIL: {
ID: "6763f372-02c5-478c-b7d7-802dc60a64b9",
NAME: "contactTypeId",
},
PHONE: {
ID: "d792d4ff-8c34-4336-8434-56ecb40f2fa4",
NAME: "contactTypeId",
},
WEBSITE: {
ID: "f611e91d-a811-4519-afa6-08f0e9dcdd9b",
NAME: "contactTypeId",
},
},
};