Files
app-organization-portal/lib/queries.js
2024-02-23 08:30:56 +01:00

543 lines
19 KiB
JavaScript

import { uuid, sparqlEscapeUri, sparqlEscapeString } from "mu";
import { querySudo as query, updateSudo as update } from "@lblod/mu-auth-sudo";
export async function getIdentifiers(kboStructuredIdUuid) {
const queryStr = `
SELECT DISTINCT ?kbo ?kboId ?ovo ?kboStructuredId ?ovoStructuredId ?bestuurseenheid WHERE {
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
<http://mu.semte.ch/graphs/worship-service>
}
GRAPH ?g {
?kboStructuredId a <https://data.vlaanderen.be/ns/generiek#GestructureerdeIdentificator> ;
<http://mu.semte.ch/vocabularies/core/uuid> ${sparqlEscapeString(
kboStructuredIdUuid
)} ;
<https://data.vlaanderen.be/ns/generiek#lokaleIdentificator> ?kbo .
?kboId <https://data.vlaanderen.be/ns/generiek#gestructureerdeIdentificator> ?kboStructuredId ;
<http://www.w3.org/2004/02/skos/core#notation> ?kboNotation .
?bestuurseenheid <http://www.w3.org/ns/adms#identifier> ?kboId .
FILTER (?kboNotation IN ("KBO nummer"@nl, "KBO nummer"))
OPTIONAL {
?bestuurseeheid <http://www.w3.org/ns/adms#identifier> ?ovoId .
?ovoId <https://data.vlaanderen.be/ns/generiek#gestructureerdeIdentificator> ?ovoStructuredId ;
<http://www.w3.org/2004/02/skos/core#notation> ?ovoNotation .
OPTIONAL { ?ovoStructuredId <https://data.vlaanderen.be/ns/generiek#lokaleIdentificator> ?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,
kboUnit: binding.kboOrganisation?.value,
};
}
return null;
}
export async function getKboIdentifiers(administrativeUnit) {
const queryStr = `
SELECT DISTINCT ?kboUnit ?abbUnit ?changeTime ?contactPointUri ?addressUri WHERE {
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
<http://mu.semte.ch/graphs/worship-service>
}
GRAPH ?g {
?kboUnit a <http://mu.semte.ch/vocabularies/ext/KboEenheid> ;
<http://mu.semte.ch/vocabularies/core/uuid> ?uuid ;
<http://schema.org/contactPoint> ?contactPointUri ;
<https://data.lblod.info/vocabularies/generiek/geplandeEindDatum> ?changeTime .
?contactPointUri <http://www.w3.org/ns/locn#address> ?addressUri .
?kboUnit <http://www.w3.org/2002/07/owl#sameAs> ?abbUnit
FILTER(?abbUnit = ${sparqlEscapeUri(administrativeUnit)})
}
}
`;
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 {
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 <http://www.w3.org/ns/adms#identifier> ${sparqlEscapeUri(
idUri
)} .
${sparqlEscapeUri(idUri)} a <http://www.w3.org/ns/adms#Identifier> ;
<http://mu.semte.ch/vocabularies/core/uuid> ${sparqlEscapeString(
idUuid
)} ;
<http://www.w3.org/2004/02/skos/core#notation> "OVO-nummer" ;
<https://data.vlaanderen.be/ns/generiek#gestructureerdeIdentificator> ${sparqlEscapeUri(
structuredIdUri
)} .
${sparqlEscapeUri(
structuredIdUri
)} a <https://data.vlaanderen.be/ns/generiek#GestructureerdeIdentificator> ;
<http://mu.semte.ch/vocabularies/core/uuid> ${sparqlEscapeString(
structuredIdUuid
)} .
}
} WHERE {
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
<http://mu.semte.ch/graphs/worship-service>
}
GRAPH ?g {
${sparqlEscapeUri(
kboStructuredId
)} a <https://data.vlaanderen.be/ns/generiek#GestructureerdeIdentificator> ;
<https://data.vlaanderen.be/ns/generiek#lokaleIdentificator> ?kbo .
?kboId <https://data.vlaanderen.be/ns/generiek#gestructureerdeIdentificator> ${sparqlEscapeUri(
kboStructuredId
)} ;
<http://www.w3.org/2004/02/skos/core#notation> ?notation .
?bestuurseenheid <http://www.w3.org/ns/adms#identifier> ?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 <http://www.w3.org/2002/07/owl#sameAs> ?ovoUri .
${sparqlEscapeUri(
ovoStructuredIdUri
)} <https://data.vlaanderen.be/ns/generiek#lokaleIdentificator> ?ovo .
}
}
`;
if (wegwijsOvo) {
updateStr += `
INSERT {
GRAPH ?g {
?bestuureseenheid <http://www.w3.org/2002/07/owl#sameAs> ${sparqlEscapeUri(
`http://data.vlaanderen.be/id/organisatie/${wegwijsOvo}`
)} .
${sparqlEscapeUri(
ovoStructuredIdUri
)} <https://data.vlaanderen.be/ns/generiek#lokaleIdentificator> ${sparqlEscapeString(
wegwijsOvo
)} .
}
}
`;
}
updateStr += `
WHERE {
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
<http://mu.semte.ch/graphs/worship-service>
}
GRAPH ?g {
${sparqlEscapeUri(
ovoStructuredIdUri
)} a <https://data.vlaanderen.be/ns/generiek#GestructureerdeIdentificator> .
OPTIONAL {
?bestuureseenheid
<http://www.w3.org/ns/adms#identifier>/<https://data.vlaanderen.be/ns/generiek#gestructureerdeIdentificator> ${sparqlEscapeUri(
ovoStructuredIdUri
)} ;
<http://www.w3.org/2002/07/owl#sameAs> ?ovoUri .
}
OPTIONAL { ${sparqlEscapeUri(
ovoStructuredIdUri
)} <https://data.vlaanderen.be/ns/generiek#lokaleIdentificator> ?ovo . }
}
}
`;
await update(updateStr);
}
export async function getAllOvoAndKboCouples() {
const queryStr = `
SELECT DISTINCT ?kbo ?ovo ?kboStructuredId ?ovoStructuredId WHERE {
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
<http://mu.semte.ch/graphs/worship-service>
}
GRAPH ?g {
?kboId <https://data.vlaanderen.be/ns/generiek#gestructureerdeIdentificator> ?kboStructuredId ;
<http://www.w3.org/2004/02/skos/core#notation> ?kboNotation .
?kboStructuredId a <https://data.vlaanderen.be/ns/generiek#GestructureerdeIdentificator> ;
<https://data.vlaanderen.be/ns/generiek#lokaleIdentificator> ?kbo .
?bestuurseeheid <http://www.w3.org/ns/adms#identifier> ?kboId .
FILTER (?kboNotation IN ("KBO nummer"@nl, "KBO nummer"))
OPTIONAL {
?bestuurseeheid <http://www.w3.org/ns/adms#identifier> ?ovoId .
?ovoId <https://data.vlaanderen.be/ns/generiek#gestructureerdeIdentificator> ?ovoStructuredId ;
<http://www.w3.org/2004/02/skos/core#notation> ?ovoNotation .
OPTIONAL { ?ovoStructuredId <https://data.vlaanderen.be/ns/generiek#lokaleIdentificator> ?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,
};
});
}
return null;
}
export async function getAllAbbKboOrganizations(){
let queryStr = `
SELECT ?kbo ?abbOrg ?kboOrg ?changeTime ?kboId where {
GRAPH ?g {
?kboId <https://data.vlaanderen.be/ns/generiek#gestructureerdeIdentificator> ?kboStructuredId ;
<http://www.w3.org/2004/02/skos/core#notation> ?kboNotation .
?kboStructuredId a <https://data.vlaanderen.be/ns/generiek#GestructureerdeIdentificator> ;
<https://data.vlaanderen.be/ns/generiek#lokaleIdentificator> ?kbo .
?abbOrg <http://www.w3.org/ns/adms#identifier> ?kboId .
FILTER (?kboNotation IN ("KBO nummer"@nl, "KBO nummer"))
OPTIONAL{
?kboOrg owl:sameAs ?abbOrg .
?kboOrg <https://data.lblod.info/vocabularies/generiek/geplandeEindDatum> ?changeTime .
}
}
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
}
}
`
const result = await query(queryStr);
if (result.results.bindings.length) {
const bindings = result.results.bindings;
return bindings.map((binding) => {
return {
kbo: binding.kbo.value,
abbOrg: binding.abbOrg.value,
kboOrg: binding.kboOrg?.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}`;
console.log("creating:" + kboOrg);
console.log("with:" + kboId);
let updateStr = `
INSERT {
GRAPH ?g {
${sparqlEscapeUri(adressUri)} a <http://www.w3.org/ns/locn#Address> ;
<http://mu.semte.ch/vocabularies/core/uuid> ${sparqlEscapeString(
adressUuidId
)} ;
<http://www.w3.org/ns/locn#fullAddress> ${sparqlEscapeString(
kboOrg.formattedAddress
)} .
${sparqlEscapeUri(contactPointUri)} a <http://schema.org/ContactPoint> ;
<http://mu.semte.ch/vocabularies/core/uuid> ${sparqlEscapeString(
contactPointUuidId
)} ;
<http://schema.org/email> ${sparqlEscapeString(
kboOrg.email
)} ;
<http://schema.org/telephone> ${sparqlEscapeString(
kboOrg.phone
)} ;
<http://schema.org/contactType> ${sparqlEscapeString(
"Kbocontact"
)} ;
<http://xmlns.com/foaf/0.1/page> ${sparqlEscapeString(
kboOrg.website
)} ;
<http://www.w3.org/ns/locn#address> ${sparqlEscapeUri(
adressUri
)}.
${sparqlEscapeUri(
newKboIdUri
)} a <http://mu.semte.ch/vocabularies/ext/KboEenheid> ;
<http://mu.semte.ch/vocabularies/core/uuid> ${sparqlEscapeString(
newKboOrgIdUuId
)} ;
<http://mu.semte.ch/vocabularies/ext/rechtsvorm> ${sparqlEscapeString(
kboOrg.rechtsvorm
)} ;
<http://mu.semte.ch/vocabularies/ext/shortName> ${sparqlEscapeString(
kboOrg.shortName
)} ;
<http://mu.semte.ch/vocabularies/ext/startDate> ${sparqlEscapeString(
kboOrg.startDate
)} ;
<http://www.w3.org/2004/02/skos/core#prefLabel> ${sparqlEscapeString(
kboOrg.organisationName
)} ;
<http://www.w3.org/2004/02/skos/core#altLabel> ${sparqlEscapeString(
kboOrg.formalName
)} ;
<https://data.lblod.info/vocabularies/generiek/geplandeEindDatum> ${sparqlEscapeString(
kboOrg.changeTime
)} ;
<http://www.w3.org/ns/adms#identifier> ${sparqlEscapeUri(
kboId
)} ;
<http://www.w3.org/ns/regorg#orgStatus> ${sparqlEscapeUri(
kboOrg.activeState
)} ;
<http://schema.org/contactPoint> ${sparqlEscapeUri(
contactPointUri
)} .
}
}WHERE{
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
}
}
`;
await update(updateStr);
return newKboIdUri;
}
export async function updateKboOrg(newKboInfo, identifiers) {
//update Adress
let updateStr = `
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> ;
<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)})
}
}
`;
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 .
}
}
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
)} .
}
}
WHERE{
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
}
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 .
FILTER(?contact = ${sparqlEscapeUri(identifiers.contactPointUri)})
}
}
`;
await update(updateStr);
//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)})
}
}
`;
await update(updateStr);
}
export async function linkAbbOrgToKboOrg(abbOrgUri, kboOrgUri) {
let updateStr = `
INSERT {
GRAPH ?g {
${sparqlEscapeUri(
kboOrgUri
)} <http://www.w3.org/2002/07/owl#sameAs> ${sparqlEscapeUri(abbOrgUri)} .
}
}WHERE {
VALUES ?g {
<http://mu.semte.ch/graphs/administrative-unit>
}
${sparqlEscapeUri(
kboOrgUri
)} a <http://mu.semte.ch/vocabularies/ext/KboEenheid> .
}
`;
await update(updateStr);
}
// Update queries: via mu-auth if it comes from the frontend ?