From 6b559889d7ace6f1ae870e02943fafcf726e764b Mon Sep 17 00:00:00 2001 From: Elias Papanikitas Date: Wed, 13 Mar 2024 11:21:08 +0100 Subject: [PATCH] fixed some pr comments --- apiErrorHandler.js => api-error-handler.js | 0 app.js | 49 +- config.js | 4 +- lib/queries.js | 491 +++++++++--------- ...ctDataIds.js => wegwijs-object-data-ids.js | 0 5 files changed, 280 insertions(+), 264 deletions(-) rename apiErrorHandler.js => api-error-handler.js (100%) rename wegwijObjectDataIds.js => wegwijs-object-data-ids.js (100%) diff --git a/apiErrorHandler.js b/api-error-handler.js similarity index 100% rename from apiErrorHandler.js rename to api-error-handler.js diff --git a/app.js b/app.js index 3de45e0..92de0da 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,4 @@ -import { app, errorHandler, uuid } from "mu"; +import { app, errorHandler } from "mu"; import fetch from "node-fetch"; import { CronJob } from "cron"; import { @@ -14,53 +14,54 @@ import { import { CRON_PATTERN, ORGANIZATION_STATUS, - WEGWIJSAPI, - WEGWIJSAPIFIELDS, + WEGWIJS_API, + WEGWIJS_API_FIELDS, } from "./config"; -import { API_STATUS_CODES } from "./apiErrorHandler"; -import { WEGWIJS_DATA_OBJECT_IDS } from "./wegwijObjectDataIds"; +import { API_STATUS_CODES } from "./api-error-handler"; +import { WEGWIJS_DATA_OBJECT_IDS } from "./wegwijs-object-data-ids"; app.post("/sync-kbo-data/:kboStructuredIdUuid", async function (req, res) { try { const kboStructuredIdUuid = req.params.kboStructuredIdUuid; - const identifiers = await getAbbOrganizationInfo(kboStructuredIdUuid); + const AbbOrganizationInfo = await getAbbOrganizationInfo(kboStructuredIdUuid); - if (!identifiers?.kbo) { + if (!AbbOrganizationInfo?.kbo) { return throwServerError(API_STATUS_CODES.STATUS_403, res); } - const wegwijsUrl = `${WEGWIJSAPI}?q=kboNumber:${identifiers.kbo}&fields=${WEGWIJSAPIFIELDS}`; + const wegwijsUrl = `${WEGWIJS_API}?q=kboNumber:${AbbOrganizationInfo.kbo}&fields=${WEGWIJS_API_FIELDS}`; console.log("url: " + wegwijsUrl); const response = await fetch(wegwijsUrl); const data = await response.json(); - let kboObject = null; if (!data.length) { 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); - const kboIdentifiers = await getKboOrgnizationInfo(identifiers.adminUnit); + const kboObject = getKboFields(wegwijsInfo); + const kboIdentifiers = await getKboOrgnizationInfo(AbbOrganizationInfo.adminUnit); if (!kboIdentifiers && kboObject) { - await createKbo(kboObject, identifiers.kboId, identifiers.adminUnit); + await createKbo(kboObject, AbbOrganizationInfo.kboId, AbbOrganizationInfo.adminUnit); } if (isUpdate(kboObject, kboIdentifiers)) { updateKboOrg(kboObject, kboIdentifiers); } - let wegwijsOvo = kboObject.ovoNumber ?? null; - //Update Ovo Number - if (wegwijsOvo && wegwijsOvo != identifiers.ovo) { - console.log(identifiers.ovo); + await healAbbWithWegWijsData(); - let ovoStructuredIdUri = identifiers.ovoStructuredId; + let wegwijsOvo = kboObject.ovoNumber ?? null; + + //Update Ovo Number + if (wegwijsOvo && wegwijsOvo != AbbOrganizationInfo.ovo) { + + let ovoStructuredIdUri = AbbOrganizationInfo.ovoStructuredId; if (!ovoStructuredIdUri) { ovoStructuredIdUri = await constructOvoStructure( - identifiers.kboStructuredId + AbbOrganizationInfo.kboStructuredId ); } await updateOvoNumberAndUri(ovoStructuredIdUri, wegwijsOvo); @@ -167,9 +168,13 @@ async function healAbbWithWegWijsData() { const wegwijsOvo = wegwijsKboOrg.ovoNumber; // If a KBO can't be found in wegwijs but we already have an OVO for it in OP, we keep that OVO. // It happens especially a lot for worship services that sometimes lack data in Wegwijs - if (wegwijsOvo && kboIdentifiersOP.ovo != wegwijsOvo) { + + if (wegwijsOvo && kboIdentifierOP.ovo != wegwijsOvo) { // We have a mismatch, let's update the OVO number - let ovoStructuredIdUri = kboIdentifiersOP.ovoStructuredId; + let ovoStructuredIdUri = kboIdentifierOP.ovoStructuredId; + + console.log(ovoStructuredIdUri); + if (!ovoStructuredIdUri) { ovoStructuredIdUri = await constructOvoStructure( kboIdentifierOP.kboStructuredId @@ -203,7 +208,7 @@ async function getAllOvoAndKboCouplesWegwijs() { let couples = {}; const response = await fetch( - `${WEGWIJSAPI}?q=kboNumber:/.*[0-9].*/&fields=${WEGWIJSAPIFIELDS},parents&scroll=true` + `${WEGWIJS_API}?q=kboNumber:/.*[0-9].*/&fields=${WEGWIJS_API_FIELDS},parents&scroll=true` ); const scrollId = JSON.parse( response.headers.get("x-search-metadata") @@ -216,7 +221,7 @@ async function getAllOvoAndKboCouplesWegwijs() { couples[wegwijsUnit.kboNumber] = wegwijsUnit; }); - const response = await fetch(`${WEGWIJSAPI}/scroll?id=${scrollId}`); + const response = await fetch(`${WEGWIJS_API}/scroll?id=${scrollId}`); data = await response.json(); } while (data.length); diff --git a/config.js b/config.js index 437e802..a52c0c5 100644 --- a/config.js +++ b/config.js @@ -5,6 +5,6 @@ export const ORGANIZATION_STATUS = { INACTIVE: "http://lblod.data.gift/concepts/d02c4e12bf88d2fdf5123b07f29c9311" } -export const WEGWIJSAPI = "https://api.wegwijs.vlaanderen.be/v1/search/organisations"; -export const WEGWIJSAPIFIELDS = "changeTime,name,shortName,ovoNumber,kboNumber,labels,contacts,organisationClassifications,locations" +export const WEGWIJS_API = "https://api.wegwijs.vlaanderen.be/v1/search/organisations"; +export const WEGWIJS_API_FIELDS = "changeTime,name,shortName,ovoNumber,kboNumber,labels,contacts,organisationClassifications,locations" diff --git a/lib/queries.js b/lib/queries.js index abd4064..f900478 100644 --- a/lib/queries.js +++ b/lib/queries.js @@ -23,13 +23,12 @@ export async function getAbbOrganizationInfo(kboStructuredIdUuid) { FILTER (?kboNotation IN ("KBO nummer"@nl, "KBO nummer")) OPTIONAL { - ?bestuurseeheid ?ovoId . + ?bestuurseenheid ?ovoId . ?ovoId ?ovoStructuredId ; ?ovoNotation . - OPTIONAL { ?ovoStructuredId ?ovo . - } + OPTIONAL { ?ovoStructuredId ?ovo . } FILTER (?ovoNotation IN ("OVO-nummer"@nl, "OVO-nummer")) } @@ -294,96 +293,97 @@ export async function createNewKboOrg(kboOrg, kboId) { ${sparqlEscapeUri(adressUri)} a ; ${sparqlEscapeString( adressUuidId - )} ` - if(kboOrg.formattedAddress){ + )} `; + if (kboOrg.formattedAddress) { updateStr += `; ${sparqlEscapeString( - kboOrg.formattedAddress - )}` + kboOrg.formattedAddress + )}`; } - updateStr += " ." - + updateStr += " ."; updateStr += ` ${sparqlEscapeUri(contactPointUri)} a ; ${sparqlEscapeString( - contactPointUuidId)} ` - if(kboOrg.email){ + contactPointUuidId + )} `; + if (kboOrg.email) { updateStr += `; ${sparqlEscapeString( kboOrg.email - )}` + )}`; } - if(kboOrg.phone){ + if (kboOrg.phone) { updateStr += `; ${sparqlEscapeString( kboOrg.phone - )}` + )}`; } - if(kboOrg.website){ + if (kboOrg.website) { updateStr += `; ${sparqlEscapeString( kboOrg.website - )}` - } + )}`; + } updateStr += ` ; ${sparqlEscapeString("Kbocontact")} ; - ${sparqlEscapeUri(adressUri)}.` + ${sparqlEscapeUri(adressUri)}.`; - - - updateStr +=` ${sparqlEscapeUri( - newKboIdUri - )} a ; + updateStr += ` ${sparqlEscapeUri( + newKboIdUri + )} a ; ${sparqlEscapeString( newKboOrgIdUuId - )}` + )}`; - if(kboOrg.rechtsvorm){ + if (kboOrg.rechtsvorm) { updateStr += `; ${sparqlEscapeString( kboOrg.rechtsvorm - )}` - } - if(kboOrg.shortName){ - updateStr += `; ${sparqlEscapeString( + )}`; + } + if (kboOrg.shortName) { + updateStr += `; ${sparqlEscapeString( kboOrg.shortName - )}` - } - if(kboOrg.startDate){ + )}`; + } + if (kboOrg.startDate) { updateStr += `; ${sparqlEscapeString( kboOrg.startDate - )}` - } - if(kboOrg.formalName){ - updateStr += `; ${sparqlEscapeString( + )}`; + } + if (kboOrg.formalName) { + updateStr += `; ${sparqlEscapeString( kboOrg.formalName - )}` - } - if(kboOrg.organisationName){ - updateStr += `; ${sparqlEscapeString( - kboOrg.organisationName - )}` - } - if(kboOrg.changeTime){ + )}`; + } + // if (kboOrg.organisationName) { + // updateStr += `; ${sparqlEscapeString( + // kboOrg.organisationName + // )}`; + //} + if (kboOrg.changeTime) { updateStr += `; ${sparqlEscapeString( kboOrg.changeTime - )}` - } - if(kboId){ + )}`; + } + if (kboId) { updateStr += `; ${sparqlEscapeUri( kboId - )}` - } - if(kboOrg.activeState){ + )}`; + } + if (kboOrg.activeState) { updateStr += `; ${sparqlEscapeUri( kboOrg.activeState - )}` - } + )}`; + } updateStr += `; ${sparqlEscapeUri( - contactPointUri - )} . + contactPointUri + )} . } }WHERE{ VALUES ?g { + } + + } `; @@ -392,197 +392,14 @@ export async function createNewKboOrg(kboOrg, kboId) { } export async function updateKboOrg(newKboInfo, identifiers) { - //update address - let updateStr = ` - DELETE{ - GRAPH ?g{ - ?adressUri a ; - ?fulladdress . - } - INSERT{ - graph ?g{ - ?adressUri a - `; - if(newKboInfo.formattedAddress){ - updateStr += ` - ; - ${sparqlEscapeString( - newKboInfo.formattedAddress - )} - `; - } - updateStr += ` - . - } - } - `; - updateStr += ` - WHERE{ - VALUES ?g { - - } - GRAPH ?g { - ?adressUri a ; - ?fulladdress . - - FILTER(?adressUri = ${sparqlEscapeUri(identifiers.addressUri)}) - } - } - `; - await update(updateStr); + updateKboAddress(newKboInfo, identifiers); //update ContactPoint - updateStr = ` - DELETE{ - GRAPH ?g{ - ?contact a ; - ?email ; - ?phone ; - ?website . - } - } - INSERT{ - graph ?g{ - ?contact a - `; - if(newKboInfo.email){ - updateStr += `; ${sparqlEscapeString( - newKboInfo.email - )}` - } - if(newKboInfo.phone){ - updateStr += `; ${sparqlEscapeString( - newKboInfo.phone - )}` - } - if(newKboInfo.phone){ - updateStr += `; ${sparqlEscapeString( - newKboInfo.website - )}` - } - updateStr += ` - . - } - } - `; - updateStr += ` - WHERE{ - VALUES ?g { - - } - GRAPH ?g { - ?contact a ; - ?email ; - ?phone ; - ?website . - - FILTER(?contact = ${sparqlEscapeUri(identifiers.contactPointUri)}) - } - } - `; - await update(updateStr); + updateKboContact(newKboInfo, identifiers); //update kboUnit - updateStr = ` - DELETE{ - GRAPH ?g{ - ?kboUnit a ; - ?rechtsVorm ; - ?shortName ; - ?startDate ; - ?orgName ; - ?commercialName ; - ?changeTime ; - ?activeState . - } - } - INSERT{ - graph ?g{ - ?kboUnit a - `; - if(newKboInfo.rechtsvorm){ - updateStr += ` - ; - ${sparqlEscapeString( - newKboInfo.rechtsvorm - )} - `; - } - if(newKboInfo.shortName){ - updateStr += ` - ; - ${sparqlEscapeString( - newKboInfo.shortName - )} - `; - } - if(newKboInfo.startDate){ - updateStr += ` - ; - ${sparqlEscapeString( - newKboInfo.startDate - )} - `; - } - if(newKboInfo.organisationName){ - updateStr += ` - ; - ${sparqlEscapeString( - newKboInfo.organisationName - )} - `; - } - if(newKboInfo.formalName){ - updateStr += ` - ; - ${sparqlEscapeString( - newKboInfo.formalName - )} - `; - } - if(newKboInfo.changeTime){ - updateStr += ` - ; - ${sparqlEscapeString( - newKboInfo.changeTime - )} - `; - } - if(newKboInfo.activeState){ - updateStr += ` - ; - ${sparqlEscapeUri( - newKboInfo.activeState - )} - `; - } - updateStr += ` - . - } - } - `; - updateStr += ` - WHERE{ - VALUES ?g { - - } - GRAPH ?g { - ?kboUnit a ; - ?rechtsVorm ; - ?shortName ; - ?startDate ; - ?orgName ; - ?commercialName ; - ?changeTime ; - ?activeState . - - FILTER(?kboUnit = ${sparqlEscapeUri(identifiers.kboUnit)}) - } - } - `; - - await update(updateStr); + updateKboAdministrativeUnit(newKboInfo, identifiers); } export async function linkAbbOrgToKboOrg(abbOrgUri, kboOrgUri) { @@ -597,6 +414,7 @@ export async function linkAbbOrgToKboOrg(abbOrgUri, kboOrgUri) { }WHERE { VALUES ?g { + } ${sparqlEscapeUri( @@ -610,4 +428,197 @@ export async function linkAbbOrgToKboOrg(abbOrgUri, kboOrgUri) { } } +async function updateKboAddress(newKboInfo, identifiers) { + let updateStr = ` + DELETE{ + GRAPH ?g{ + ?adressUri a ; + ?fulladdress . + } + INSERT{ + graph ?g{ + ?adressUri a +`; + if (newKboInfo.formattedAddress) { + updateStr += ` + ; + ${sparqlEscapeString( + newKboInfo.formattedAddress + )} + `; + } + updateStr += ` + . + } + } +`; + updateStr += ` + WHERE{ + VALUES ?g { + + } + GRAPH ?g { + ?adressUri a ; + ?fulladdress . + + FILTER(?adressUri = ${sparqlEscapeUri(identifiers.addressUri)}) + } + } +`; + await update(updateStr); +} + +async function updateKboContact(newKboInfo, identifiers) { + let updateStr = ` + DELETE{ + GRAPH ?g{ + ?contact a ; + ?email ; + ?phone ; + ?website . + } + } + INSERT{ + graph ?g{ + ?contact a +`; + if (newKboInfo.email) { + updateStr += `; ${sparqlEscapeString( + newKboInfo.email + )}`; + } + if (newKboInfo.phone) { + updateStr += `; ${sparqlEscapeString( + newKboInfo.phone + )}`; + } + if (newKboInfo.phone) { + updateStr += `; ${sparqlEscapeString( + newKboInfo.website + )}`; + } + updateStr += ` + . + } +} +`; + updateStr += ` +WHERE{ + VALUES ?g { + + } + GRAPH ?g { + ?contact a ; + ?email ; + ?phone ; + ?website . + + FILTER(?contact = ${sparqlEscapeUri(identifiers.contactPointUri)}) + } +} +`; + await update(updateStr); +} +async function updateKboAdministrativeUnit(newKboInfo, identifiers) { + let updateStr = ` + DELETE{ + GRAPH ?g{ + ?kboUnit a ; + ?rechtsVorm ; + ?shortName ; + ?startDate ; + ?orgName ; + ?commercialName ; + ?changeTime ; + ?activeState . + } + } + INSERT{ + graph ?g{ + ?kboUnit a +`; + if (newKboInfo.rechtsvorm) { + updateStr += ` + ; + ${sparqlEscapeString( + newKboInfo.rechtsvorm + )} + `; + } + if (newKboInfo.shortName) { + updateStr += ` + ; + ${sparqlEscapeString( + newKboInfo.shortName + )} + `; + } + if (newKboInfo.startDate) { + updateStr += ` + ; + ${sparqlEscapeString( + newKboInfo.startDate + )} + `; + } + if (newKboInfo.organisationName) { + updateStr += ` + ; + ${sparqlEscapeString( + newKboInfo.organisationName + )} + `; + } + if (newKboInfo.formalName) { + updateStr += ` + ; + ${sparqlEscapeString( + newKboInfo.formalName + )} + `; + } + if (newKboInfo.changeTime) { + updateStr += ` + ; + ${sparqlEscapeString( + newKboInfo.changeTime + )} + `; + } + if (newKboInfo.activeState) { + updateStr += ` + ; + ${sparqlEscapeUri( + newKboInfo.activeState + )} + `; + } + updateStr += ` + . + } + } +`; + updateStr += ` + WHERE{ + VALUES ?g { + + } + GRAPH ?g { + ?kboUnit a ; + ?rechtsVorm ; + ?shortName ; + ?startDate ; + ?orgName ; + ?commercialName ; + ?changeTime ; + ?activeState . + + FILTER(?kboUnit = ${sparqlEscapeUri(identifiers.kboUnit)}) + } + } +`; + + await update(updateStr); +} + // Update queries: via mu-auth if it comes from the frontend ? diff --git a/wegwijObjectDataIds.js b/wegwijs-object-data-ids.js similarity index 100% rename from wegwijObjectDataIds.js rename to wegwijs-object-data-ids.js