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 ?organization WHERE { VALUES ?g { } GRAPH ?g { ?kboStructuredId a ; ${sparqlEscapeString( kboStructuredIdUuid )} ; ?kbo . ?kboId ?kboStructuredId ; ?kboNotation . ?organization ?kboId . FILTER (?kboNotation IN ("KBO nummer"@nl, "KBO nummer")) OPTIONAL { ?organization ?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.organization.value, }; } return null; } export async function getKboOrganizationInfo(administrativeUnit) { const queryStr = ` SELECT DISTINCT ?kboOrg ?abbUnit ?changeTime ?contactPointUri ?addressUri WHERE { VALUES ?g { } GRAPH ?g { ?kboOrg a ; ?uuid ; ?contactPointUri ; ?changeTime . ?contactPointUri ?addressUri . ?kboOrg ?abbUnit FILTER(?abbUnit = ${sparqlEscapeUri(administrativeUnit)}) } } `; const result = await query(queryStr); if (result.results.bindings.length) { const binding = result.results.bindings[0]; return { kboOrg: binding.kboOrg?.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 { ?organization ${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 . ?organization ?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, "kboOrganisaties","i")) OPTIONAL{ ?kboOrg owl:sameAs ?abbOrg . ?kboOrg ?changeTime ; ?rechtsVrom ; ?shortName ; ?startDate ; ?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, kboStructuredId) { const newKboOrgIdUuId = uuid(); const newKboIdUri = `http://data.lblod.info/id/kboOrganisaties/${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.startDate) { updateStr += `; ${sparqlEscapeString( kboOrg.startDate )}`; } if (kboOrg.formalName) { updateStr += `; ${sparqlEscapeString( kboOrg.formalName )}`; } if (kboOrg.shortName) { updateStr += `; ${sparqlEscapeString( kboOrg.shortName )}`; } 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 { } GRAPH ?g { ?organization ${sparqlEscapeUri( kboId )} } } `; await update(updateStr); return newKboIdUri; } export async function updateKboOrg(newKboInfo, identifiers) { //update address await updateKboAddress(newKboInfo, identifiers); //update ContactPoint await updateKboContact(newKboInfo, identifiers); //update kboOrg await updateKboOrganization(newKboInfo, identifiers); } 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); } } 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 updateKboOrganization(newKboInfo, identifiers) { let updateStr = ` DELETE{ GRAPH ?g{ ?kboOrg a ; ?rechtsVorm ; ?shortName ; ?startDate ; ?orgName ; ?changeTime ; ?activeState . } } INSERT{ graph ?g{ ?kboOrg a `; if (newKboInfo.rechtsvorm) { updateStr += ` ; ${sparqlEscapeString( newKboInfo.rechtsvorm )} `; } if (newKboInfo.startDate) { updateStr += ` ; ${sparqlEscapeString( newKboInfo.startDate )} `; } if (newKboInfo.formalName) { updateStr += `; ${sparqlEscapeString( newKboInfo.formalName )}`; } if (newKboInfo.shortName) { updateStr += `; ${sparqlEscapeString( newKboInfo.shortName )}`; } if (newKboInfo.changeTime) { updateStr += ` ; ${sparqlEscapeString( newKboInfo.changeTime )} `; } if (newKboInfo.activeState) { updateStr += ` ; ${sparqlEscapeUri( newKboInfo.activeState )} `; } updateStr += ` . } } `; updateStr += ` WHERE{ VALUES ?g { } GRAPH ?g { ?kboOrg a ; ?rechtsVorm ; ?shortName ; ?startDate ; ?orgName ; ?changeTime ; ?activeState . FILTER(?kboOrg = ${sparqlEscapeUri(identifiers.kboOrg)}) } } `; await update(updateStr); } // Update queries: via mu-auth if it comes from the frontend ?