Skip to content

Commit

Permalink
allow specifying a library field to use
Browse files Browse the repository at this point in the history
  • Loading branch information
karel kremer committed Jan 7, 2025
1 parent 5c7f750 commit acb71c6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
FROM local-js-template
#FROM semtech/mu-javascript-template:1.8.0
FROM semtech/mu-javascript-template:1.8.0
LABEL maintainer="[email protected]"
2 changes: 1 addition & 1 deletion docker-compose.debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
- ./:/app
# ignore app/dist because this is where we map the built files to, otherwise we get an infinite loop of creating files (on mac)
- /app/dist
- ./dist:/build/
- ./dist:/usr/src/dist
networks:
- debug
networks:
Expand Down
73 changes: 69 additions & 4 deletions services/custom-forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ type FieldDescription =
| {
name: string;
displayType: string;
libraryEntryId?: never;
libraryEntryUri?: never;
order?: number;
path?: string;
}
| {
name: string;
displayType?: never;
libraryEntryId: string;
libraryEntryUri: string;
order?: number;
path?: string;
};
Expand Down Expand Up @@ -51,8 +51,8 @@ function verifyFieldDescription(description: FieldDescription) {
const noDisplayType =
!description.displayType || description.displayType.trim().length === 0;
const noLibraryEntry =
!description.libraryEntryId ||
description.libraryEntryId.trim().length === 0;
!description.libraryEntryUri ||
description.libraryEntryUri.trim().length === 0;
if (noDisplayType && noLibraryEntry) {
throw new HttpError(
'Field description must have a display type or a library entry id',
Expand Down Expand Up @@ -88,6 +88,10 @@ async function addFieldToFormExtension(
formTtl: string,
fieldDescription: FieldDescription,
) {
if (fieldDescription.libraryEntryUri) {
return addLibraryFieldToFormExtension(formUri, formTtl, fieldDescription);
}

const id = uuidv4();
const uri = `http://data.lblod.info/id/lmb/form-fields/${id}`;
const name = fieldDescription.name;
Expand Down Expand Up @@ -115,6 +119,67 @@ async function addFieldToFormExtension(
return { id, uri };
}

async function addLibraryFieldToFormExtension(
formUri: string,
formTtl: string,
fieldDescription: FieldDescription,
) {
const id = uuidv4();
const uri = `http://data.lblod.info/id/lmb/form-fields/${id}`;
const fieldGroupUri = await fetchGroupFromFormTtl(formTtl);

const libraryEntryUri = await verifyLibraryEntryUri(
fieldDescription.libraryEntryUri,
);
if (!libraryEntryUri) {
throw new HttpError('Library entry not found', 404);
}

await update(`
PREFIX form: <http://lblod.data.gift/vocabularies/forms/>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX prov: <http://www.w3.org/ns/prov#>
INSERT {
${sparqlEscapeUri(uri)} a form:Field;
ext:extendsGroup ${sparqlEscapeUri(fieldGroupUri)} ;
sh:name ${sparqlEscapeString(fieldDescription.name)} ;
prov:wasDerivedFrom ${sparqlEscapeUri(libraryEntryUri)} ;
form:displayType ?displayType ;
sh:order ${sparqlEscapeInt(99999)} ;
sh:path ?path ;
mu:uuid ${sparqlEscapeString(id)} .
${sparqlEscapeUri(formUri)} form:includes ${sparqlEscapeUri(uri)} .
} WHERE {
${sparqlEscapeUri(libraryEntryUri)} a ext:FormLibraryEntry ;
sh:path ?path ;
form:displayType ?displayType .
}
`);
return { id, uri };
}

async function verifyLibraryEntryUri(libraryEntryUri: string) {
const result = await query(`
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX form: <http://lblod.data.gift/vocabularies/forms/>
SELECT ?libraryEntry
WHERE {
VALUES ?libraryEntry {
${sparqlEscapeUri(libraryEntryUri)}
}
?libraryEntry a ext:FormLibraryEntry ;
form:displayType ?type ;
mu:uuid ?uuid ;
sh:path ?path .
}`);
return result.results.bindings[0]?.libraryEntry?.value;
}

async function fetchGroupFromFormTtl(formTtl: string) {
const store = await ttlToStore(formTtl);
const engine = new QueryEngine();
Expand Down

0 comments on commit acb71c6

Please sign in to comment.