import { uuid, sparqlEscapeUri, sparqlEscapeString } from "mu"; import { querySudo as query, updateSudo as update } from "@lblod/mu-auth-sudo"; export async function getAbbOrganizationInfo(kboStructuredIdUuid) { const queryStr = ` SELECT DISTINCT ?kbo ?kboId ?ovo ?kboStructuredId ?ovoStructuredId ?bestuurseenheid WHERE { VALUES ?g { } GRAPH ?g { ?kboStructuredId a ; ${sparqlEscapeString( kboStructuredIdUuid )} ; ?kbo . ?kboId ?kboStructuredId ; ?kboNotation . ?bestuurseenheid ?kboId . FILTER (?kboNotation IN ("KBO nummer"@nl, "KBO nummer")) OPTIONAL { ?bestuurseeheid ?ovoId . ?ovoId ?ovoStructuredId ; ?ovoNotation . OPTIONAL { ?ovoStructuredId ?ovo . } FILTER (?ovoNotation IN ("OVO-nummer"@nl, "OVO-nummer")) } } } `; const result = await query(queryStr); if (result.results.bindings.length) { const binding = result.results.bindings[0]; // We should only have one KBO/OVO couple return { kbo: binding.kbo.value, kboStructuredId: binding.kboStructuredId.value, ovo: binding.ovo?.value, ovoStructuredId: binding.ovoStructuredId?.value, kboId: binding.kboId?.value, adminUnit: binding.bestuurseenheid.value, }; } return null; } export async function getKboOrgnizationInfo(administrativeUnit) { const queryStr = ` SELECT DISTINCT ?kboUnit ?abbUnit ?changeTime ?contactPointUri ?addressUri WHERE { VALUES ?g { } GRAPH ?g { ?kboUnit a ; ?uuid ; ?contactPointUri ; ?changeTime . ?contactPointUri ?addressUri . ?kboUnit ?abbUnit FILTER(?abbUnit = ${sparqlEscapeUri(administrativeUnit)}) } } `; const result = await query(queryStr); if (result.results.bindings.length) { const binding = result.results.bindings[0]; return { kboUnit: binding.kboUnit?.value, abbUnit: binding.abbUnit?.value, changeTime: binding.changeTime?.value, contactPointUri: binding.contactPointUri?.value, addressUri: binding.addressUri?.value, }; } return null; } export async function constructOvoStructure(kboStructuredId) { const idUuid = uuid(); const idUri = `http://data.lblod.info/id/identificatoren/${idUuid}`; const structuredIdUuid = uuid(); const structuredIdUri = `http://data.lblod.info/id/gestructureerdeIdentificatoren/${structuredIdUuid}`; const updateStr = ` INSERT { GRAPH ?g { ?bestuurseenheid ${sparqlEscapeUri( idUri )} . ${sparqlEscapeUri(idUri)} a ; ${sparqlEscapeString( idUuid )} ; "OVO-nummer" ; ${sparqlEscapeUri( structuredIdUri )} . ${sparqlEscapeUri( structuredIdUri )} a ; ${sparqlEscapeString( structuredIdUuid )} . } } WHERE { VALUES ?g { } GRAPH ?g { ${sparqlEscapeUri( kboStructuredId )} a ; ?kbo . ?kboId ${sparqlEscapeUri( kboStructuredId )} ; ?notation . ?bestuurseenheid ?kboId . FILTER (?notation IN ("KBO nummer"@nl, "KBO nummer")) } } `; await update(updateStr); return structuredIdUri; } export async function updateOvoNumberAndUri(ovoStructuredIdUri, wegwijsOvo) { let updateStr = ` DELETE { GRAPH ?g { ?bestuureseenheid ?ovoUri . ${sparqlEscapeUri( ovoStructuredIdUri )} ?ovo . } } `; if (wegwijsOvo) { updateStr += ` INSERT { GRAPH ?g { ?bestuureseenheid ${sparqlEscapeUri( `http://data.vlaanderen.be/id/organisatie/${wegwijsOvo}` )} . ${sparqlEscapeUri( ovoStructuredIdUri )} ${sparqlEscapeString( wegwijsOvo )} . } } `; } updateStr += ` WHERE { VALUES ?g { } GRAPH ?g { ${sparqlEscapeUri( ovoStructuredIdUri )} a . OPTIONAL { ?bestuureseenheid / ${sparqlEscapeUri( ovoStructuredIdUri )} ; ?ovoUri . } OPTIONAL { ${sparqlEscapeUri( ovoStructuredIdUri )} ?ovo . } } } `; await update(updateStr); } export async function getAllAbbKboOrganizations() { let queryStr = ` SELECT ?kbo ?ovo ?kboStructuredId ?ovoStructuredId ?abbOrg ?kboOrg ?kboId ?changeTime where { VALUES ?g { } GRAPH ?g { ?kboId ?kboStructuredId ; ?kboNotation . ?kboStructuredId a ; ?kbo . ?abbOrg ?kboId . FILTER (?kboNotation IN ("KBO nummer"@nl, "KBO nummer")) FILTER (!regex(?abbOrg, "kboBestuurseenheden","i")) OPTIONAL{ ?kboOrg owl:sameAs ?abbOrg . ?kboOrg ?changeTime ; ?rechtsVrom ; ?shortName ; ?startDate ; ?commercialName ; ?legalName ; ?activeState ; ?contact . ?contact ?email ; ?phone ; ?contactType ; ?website ; ?address . ?address ?fullAddress. } OPTIONAL{ ?abbOrg ?ovoId . ?ovoId ?ovoStructuredId ; ?ovoNotation . OPTIONAL { ?ovoStructuredId ?ovo . } FILTER (?ovoNotation IN ("OVO-nummer"@nl, "OVO-nummer")) } } } `; const result = await query(queryStr); if (result.results.bindings.length) { const bindings = result.results.bindings; return bindings.map((binding) => { return { kbo: binding.kbo?.value, kboStructuredId: binding.kboStructuredId.value, ovo: binding.ovo?.value, ovoStructuredId: binding.ovoStructuredId?.value, kboOrg: binding.kboOrg?.value, abbOrg: binding.abbOrg?.value, kboId: binding.kboId?.value, changeTime: binding.changeTime?.value, }; }); } return null; } export async function createNewKboOrg(kboOrg, kboId) { const newKboOrgIdUuId = uuid(); const newKboIdUri = `http://data.lblod.info/id/kboBestuurseenheden/${newKboOrgIdUuId}`; const contactPointUuidId = uuid(); const contactPointUri = `http://data.lblod.info/id/contact-punten/${contactPointUuidId}`; const adressUuidId = uuid(); const adressUri = `http://data.lblod.info/id/adressen/${adressUuidId}`; let updateStr = ` INSERT { GRAPH ?g { ${sparqlEscapeUri(adressUri)} a ; ${sparqlEscapeString( adressUuidId )} ` if(kboOrg.formattedAddress){ updateStr += `; ${sparqlEscapeString( kboOrg.formattedAddress )}` } updateStr += " ." updateStr += ` ${sparqlEscapeUri(contactPointUri)} a ; ${sparqlEscapeString( contactPointUuidId)} ` if(kboOrg.email){ updateStr += `; ${sparqlEscapeString( kboOrg.email )}` } if(kboOrg.phone){ updateStr += `; ${sparqlEscapeString( kboOrg.phone )}` } if(kboOrg.website){ updateStr += `; ${sparqlEscapeString( kboOrg.website )}` } updateStr += ` ; ${sparqlEscapeString("Kbocontact")} ; ${sparqlEscapeUri(adressUri)}.` updateStr +=` ${sparqlEscapeUri( newKboIdUri )} a ; ${sparqlEscapeString( newKboOrgIdUuId )}` if(kboOrg.rechtsvorm){ updateStr += `; ${sparqlEscapeString( kboOrg.rechtsvorm )}` } if(kboOrg.shortName){ updateStr += `; ${sparqlEscapeString( kboOrg.shortName )}` } if(kboOrg.startDate){ updateStr += `; ${sparqlEscapeString( kboOrg.startDate )}` } if(kboOrg.formalName){ updateStr += `; ${sparqlEscapeString( kboOrg.formalName )}` } if(kboOrg.organisationName){ updateStr += `; ${sparqlEscapeString( kboOrg.organisationName )}` } if(kboOrg.changeTime){ updateStr += `; ${sparqlEscapeString( kboOrg.changeTime )}` } if(kboId){ updateStr += `; ${sparqlEscapeUri( kboId )}` } if(kboOrg.activeState){ updateStr += `; ${sparqlEscapeUri( kboOrg.activeState )}` } updateStr += `; ${sparqlEscapeUri( contactPointUri )} . } }WHERE{ VALUES ?g { } } `; await update(updateStr); return newKboIdUri; } 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); //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); //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); } export async function linkAbbOrgToKboOrg(abbOrgUri, kboOrgUri) { if (abbOrgUri && kboOrgUri) { let updateStr = ` INSERT { GRAPH ?g { ${sparqlEscapeUri( kboOrgUri )} ${sparqlEscapeUri(abbOrgUri)} . } }WHERE { VALUES ?g { } ${sparqlEscapeUri( kboOrgUri )} a . } `; await update(updateStr); } } // Update queries: via mu-auth if it comes from the frontend ?