diff --git a/package-lock.json b/package-lock.json index a5660e0f3..bdf9b657a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34075,8 +34075,6 @@ "name": "@frameless/samenwerkende-catalogi", "version": "0.0.0-semantically-released", "devDependencies": { - "@stedi/prettier-plugin-jsonata": "1.14.0", - "prettier": "2.7.1", "xmlbuilder2": "3.0.2" } }, @@ -37195,8 +37193,6 @@ "@frameless/samenwerkende-catalogi": { "version": "file:packages/samenwerkende-catalogi", "requires": { - "@stedi/prettier-plugin-jsonata": "1.14.0", - "prettier": "2.7.1", "xmlbuilder2": "3.0.2" } }, diff --git a/packages/samenwerkende-catalogi/package.json b/packages/samenwerkende-catalogi/package.json index fd52c94cf..51c8925ad 100644 --- a/packages/samenwerkende-catalogi/package.json +++ b/packages/samenwerkende-catalogi/package.json @@ -9,12 +9,7 @@ "publishConfig": { "registry": "https://npm.pkg.github.com/" }, - "scripts": { - "build": "node src/index.js" - }, "devDependencies": { - "@stedi/prettier-plugin-jsonata": "1.14.0", - "prettier": "2.7.1", "xmlbuilder2": "3.0.2" } } diff --git a/packages/samenwerkende-catalogi/src/index.js b/packages/samenwerkende-catalogi/src/index.js index 940cc4c5d..0e2ed902c 100644 --- a/packages/samenwerkende-catalogi/src/index.js +++ b/packages/samenwerkende-catalogi/src/index.js @@ -1,160 +1,140 @@ -const { mapKeys } = require('lodash'); -const axios = require('axios'); -const { create } = require('xmlbuilder2'); -const { formatJsonata } = require("@stedi/prettier-plugin-jsonata/dist/lib"); -const fs = require('fs'); -require('dotenv').config(); -const gemeente = require('@frameless/catalogi-data'); +const { mapKeys } = require("lodash"); +const { create } = require("xmlbuilder2"); +const fs = require("fs"); +require("dotenv").config(); +const gemeente = require("@frameless/catalogi-data"); -const { createScheme, getPrefLabel } = require('./helpers') +const { createScheme, getPrefLabel } = require("./helpers"); -const dir = './dist'; +const dir = "./dist"; const prefixMap = { xsi: "http://www.w3.org/2001/XMLSchema-instance", dcterms: "http://purl.org/dc/terms/", overheid: "http://standaarden.overheid.nl/owms/terms/", overheidproduct: "http://standaarden.overheid.nl/product/terms/", -} -const xmlnsPrefixMap = mapKeys(prefixMap, (_value, key) => `xmlns:${key}`) - -const fetchProductPrice = async () => { - try { - const { data } = await axios({ - url: process.env.STRAPI_BACKEND_URL, - method: 'post', - data: { - query: `{ - products(locale: "nl"){ - data { - id - attributes { - title - excerpt - slug - locale - updatedAt - catalogiMeta { - spatial { - scheme - resourceIdentifier - } - authority { - scheme - resourceIdentifier - } - audience{ - id - type - } - onlineRequest{ - type - } - } - } - } - } - }`, - } - }) +}; +const xmlnsPrefixMap = mapKeys(prefixMap, (_value, key) => `xmlns:${key}`); +const convertJsonToXML = (data) => { + if (data && data.length > 0) { + const root = create({ version: "1.0" }) + .ele("overheidproduct:scproducten") + .att({ + ...xmlnsPrefixMap, + "xsi:schemaLocation": "https://standaarden.overheid.nl/product/terms/sc.xsd", + }); - const meta = data.data.products.data.map(({ attributes, id }) => { - const gemeenteSpatial = attributes.catalogiMeta.spatial.resourceIdentifier - const gemeenteAuthority = attributes.catalogiMeta.authority.resourceIdentifier + const meta = data.map(({ attributes, id }) => { + const gemeenteSpatial = attributes.catalogiMeta?.spatial.resourceIdentifier; + const gemeenteAuthority = attributes.catalogiMeta?.authority.resourceIdentifier; - const prefLabelSpatial = getPrefLabel(gemeente.cv.value, attributes.catalogiMeta.spatial.resourceIdentifier) - const prefLabelAuthority = getPrefLabel(gemeente.cv.value, attributes.catalogiMeta.authority.resourceIdentifier) + const prefLabelSpatial = getPrefLabel(gemeente.cv.value, attributes.catalogiMeta?.spatial.resourceIdentifier); + const prefLabelAuthority = getPrefLabel(gemeente.cv.value, attributes.catalogiMeta?.authority.resourceIdentifier); - const schemeAuthority = createScheme(attributes.catalogiMeta.authority.scheme, prefixMap) - const schemeSpatial = createScheme(attributes.catalogiMeta.spatial.scheme, prefixMap) + const schemeAuthority = createScheme(attributes.catalogiMeta?.authority?.scheme, prefixMap); + const schemeSpatial = createScheme(attributes.catalogiMeta?.spatial?.scheme, prefixMap); - const path = 'products' // can be from the CMS - const identifier = `${process.env.STRAPI_FRONTEND_URL}/${attributes.locale}/${path}/${attributes.slug}` + const path = "products"; // can be from the CMS + const identifier = `${process.env.STRAPI_FRONTEND_URL}/${attributes.locale}/${path}/${attributes.slug}`; const spatial = { schemeSpatial, resourceIdentifier: gemeenteSpatial, - label: prefLabelSpatial - } + label: prefLabelSpatial, + }; const authority = { schemeAuthority, resourceIdentifier: gemeenteAuthority, label: prefLabelAuthority, - } + }; + + const audiences = + attributes.catalogiMeta && attributes.catalogiMeta?.audience + ? attributes.catalogiMeta?.audience.map(({ id, type }) => ({ id, type, scheme: "overheid:Doelgroep" })) + : []; - const audiences = attributes.catalogiMeta.audience.map(({ id, type }) => ({ id, type, scheme: "overheid:Doelgroep" })) return { title: attributes.title, language: attributes.locale, modified: attributes.updatedAt, productId: id, abstract: attributes.excerpt, - onlineAanvragen: attributes.catalogiMeta.onlineRequest.type, + onlineAanvragen: attributes.catalogiMeta?.onlineRequest.type, identifier, spatial, authority, - audiences - } - }) - - - const root = create({ version: '1.0' }) - .ele('overheidproduct:scproducten') - .att({ - ...xmlnsPrefixMap, - 'xsi:schemaLocation': "https://standaarden.overheid.nl/product/terms/sc.xsd" - }) + audiences, + }; + }); meta.forEach((item) => { - const scproduct = root.ele('overheidproduct:scproduct').att({ 'owms-version': "4.0" }) - const meta = scproduct.ele('overheidproduct:meta') - meta.ele('overheidproduct:owmskern') - .ele('dcterms:identifier').txt(item.identifier).up() - .ele('dcterms:title').txt(item.title).up() - .ele('dcterms:language').txt(item.language).up() - .ele('dcterms:type').att({ scheme: 'overheid:Informatietype' }).txt('productbeschrijving').up() - .ele('dcterms:modified').txt(item.modified).up() - .ele('dcterms:spatial').att({ + const scproduct = root.ele("overheidproduct:scproduct").att({ "owms-version": "4.0" }); + const meta = scproduct.ele("overheidproduct:meta"); + meta + .ele("overheidproduct:owmskern") + .ele("dcterms:identifier") + .txt(item.identifier) + .up() + .ele("dcterms:title") + .txt(item.title) + .up() + .ele("dcterms:language") + .txt(item.language) + .up() + .ele("dcterms:type") + .att({ scheme: "overheid:Informatietype" }) + .txt("productbeschrijving") + .up() + .ele("dcterms:modified") + .txt(item.modified) + .up() + .ele("dcterms:spatial") + .att({ scheme: item.authority.scheme, - resourceIdentifier: item.spatial.resourceIdentifier + resourceIdentifier: item.spatial.resourceIdentifier, }) - .txt(item.spatial.label).up() - .ele('overheid:authority').att({ + .txt(item.spatial.label) + .up() + .ele("overheid:authority") + .att({ scheme: item.authority.scheme, - resourceIdentifier: item.authority.resourceIdentifier + resourceIdentifier: item.authority.resourceIdentifier, }) - .txt(item.authority.label).up() - const owmsmantel = meta.ele('overheidproduct:owmsmantel') + .txt(item.authority.label) + .up(); + const owmsmantel = meta.ele("overheidproduct:owmsmantel"); item.audiences.forEach((audience) => { - owmsmantel.ele('dcterms:audience').att({ - scheme: audience.scheme, - resourceIdentifier: `http://standaarden.overheid.nl/owms/terms/${audience?.type.toLowerCase()}` - }).txt(audience.type) - }) - owmsmantel.ele('dcterms:abstract').txt(item.abstract).up() - meta.ele('overheidproduct:scmeta') - .ele('overheidproduct:productID').txt(item.productId).up() - .ele('overheidproduct:onlineAanvragen').txt(item.onlineAanvragen).up() - scproduct.ele('overheidproduct:body') - }) + owmsmantel + .ele("dcterms:audience") + .att({ + scheme: audience.scheme, + resourceIdentifier: `http://standaarden.overheid.nl/owms/terms/${audience?.type.toLowerCase()}`, + }) + .txt(audience.type); + }); + owmsmantel.ele("dcterms:abstract").txt(item.abstract).up(); + meta + .ele("overheidproduct:scmeta") + .ele("overheidproduct:productID") + .txt(item.productId) + .up() + .ele("overheidproduct:onlineAanvragen") + .txt(item.onlineAanvragen) + .up(); + scproduct.ele("overheidproduct:body"); + }); + // convert the XML tree to string const xml = root.end({ prettyPrint: true }); - const jsonXML = root.doc().toObject() if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { - recursive: true + recursive: true, }); } - fs.writeFileSync(`${dir}/sc.xml`, xml); - fs.writeFileSync(`${dir}/sc.json`, prettierFormateJson(jsonXML)); - } catch (error) { - console.log(error); + return xml; } -} -fetchProductPrice(); +}; -function prettierFormateJson(json) { - return formatJsonata(JSON.stringify(json), { printWidth: 120, tabWidth: 2, useTabs: false }); -} \ No newline at end of file +module.exports = { convertJsonToXML } \ No newline at end of file