Skip to content

Commit 3cfc367

Browse files
Update sync logic and error handling for viewer layers
1 parent f84cacc commit 3cfc367

4 files changed

+73
-35
lines changed

src/api/sync-viewer-layer-background.js

+69-29
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import { findGeonetworkInstances } from '../lib/find-geonetwork-instances'
77
import { fetchViewerLayerXML } from '../lib/fetch-viewer-layer-xml'
88
import { formatMenusRecursive } from '../lib/format-menu'
99
import Mailjet from 'node-mailjet'
10+
import fs from 'fs';
1011

1112
const mailjet = new Mailjet({
1213
apiKey: process.env.MAILJET_API_TOKEN,
1314
apiSecret: process.env.MAILJET_API_SECRET,
1415
})
1516

16-
const viewersWithLayersQuery = /* graphql */ `
17-
query viewersWithLayers ($first: IntType, $skip: IntType = 0, $locale: SiteLocale = nl) {
17+
const viewersWithViewerLayersQuery = /* graphql */ `
18+
query viewersWithViewerLayersQuery ($first: IntType, $skip: IntType = 0, $locale: SiteLocale = nl) {
1819
menus: allMenus(first: $first, skip: $skip, locale: $locale) {
1920
id
2021
geonetwork {
@@ -57,46 +58,87 @@ export const handler = withServerDefaults(async (event, _) => {
5758
}
5859
}
5960

60-
const layerData = JSON.parse(event.body)
61+
const data = JSON.parse(event.body)
6162

62-
const viewerLayerId = layerData.entity.id
63+
const id = data.entity.id
6364

6465
const { menus } = await datocmsRequest({
65-
query: viewersWithLayersQuery,
66+
query: viewersWithViewerLayersQuery,
67+
preview: true
6668
})
6769
const formattedMenus = formatMenusRecursive(menus)
6870
const menuTree = buildMenuTree(formattedMenus)
6971

7072
try {
71-
await syncViewerLayers(menuTree, layerData.event_type, viewerLayerId)
73+
const type = data.related_entities.find(entity => entity.type === 'item_type').attributes.api_key
74+
75+
if (type === 'viewer_layer') {
76+
await syncViewerLayers(menuTree, data.event_type, id)
77+
} else if (type === 'menu') {
78+
await syncViewer(menuTree, data.event_type, id)
79+
}
7280
}
7381
catch (e) {
7482
console.log('The following error occured', e.message)
7583

76-
// for (let email of findEmailContactsForLayerId(menuTree, layerId)) {
77-
// console.log('Sending email to', email)
78-
79-
// await mailjet.post('send', { version: 'v3.1' }).request({
80-
// Messages: [
81-
// {
82-
// From: {
83-
// Email: process.env.MAILJET_FROM_EMAIL,
84-
// },
85-
// To: [
86-
// {
87-
// Email: email,
88-
// },
89-
// ],
90-
// Subject: `Fout bij opslaan metadata voor laag ${layerId}`,
91-
// HTMLPart: e.message,
92-
// },
93-
// ],
94-
// })
95-
// }
96-
}
84+
for (let email of findEmailContactsForLayerId(menuTree, layerId)) {
85+
console.log('Sending email to', email)
9786

87+
await mailjet.post('send', { version: 'v3.1' }).request({
88+
Messages: [
89+
{
90+
From: {
91+
Email: process.env.MAILJET_FROM_EMAIL,
92+
},
93+
To: [
94+
{
95+
Email: email,
96+
},
97+
],
98+
Subject: `Fout bij opslaan metadata voor laag ${layerId}`,
99+
HTMLPart: e.message,
100+
},
101+
],
102+
})
103+
}
104+
}
98105
})
99106

107+
async function syncViewer(menuTree, eventType, viewerId) {
108+
const viewerLayers = new Set()
109+
110+
const findChilrenInMenu = (menu, viewerId) => {
111+
const { children } = menu
112+
113+
if (children) {
114+
children.forEach((child) => {
115+
if (child.id === viewerId && child.children) {
116+
child.children.forEach((viewerLayer) => {
117+
viewerLayers.add(viewerLayer.id)
118+
})
119+
}
120+
121+
findChilrenInMenu(child, viewerId)
122+
})
123+
}
124+
}
125+
126+
menuTree.forEach((viewer) => {
127+
findChilrenInMenu(viewer, viewerId)
128+
})
129+
130+
const viewerLayersArray = Array.from(viewerLayers)
131+
132+
133+
const requestsPromises = viewerLayersArray.map(
134+
async (viewerLayerId) => {
135+
await syncViewerLayers(menuTree, eventType, viewerLayerId)
136+
}
137+
)
138+
139+
const results = await Promise.allSettled(requestsPromises)
140+
}
141+
100142
async function syncViewerLayers(menuTree, eventType, viewerLayerId) {
101143
const geonetworkInstances = findGeonetworkInstances(menuTree, viewerLayerId)
102144

@@ -155,8 +197,6 @@ async function syncViewerLayers(menuTree, eventType, viewerLayerId) {
155197
variables: { id: viewerLayerId },
156198
})
157199

158-
console.log('viewerLayer', viewerLayer)
159-
160200
await addThumbnailsToRecord(viewerLayer?.layer?.thumbnails, viewerLayerId, geonetwork)
161201
}
162202
}

src/lib/fetch-layer-xml.js

-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ export async function fetchLayerXML({ id }) {
130130
...viewerLayer
131131
} } = await datocmsRequest({ query, variables: { id } })
132132

133-
console.log(layer)
134-
135133
const data = {
136134
layer: {
137135
...layer,

src/lib/fetch-viewer-layer-xml.js

-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ export async function fetchViewerLayerXML({ id }) {
131131
...viewerLayer
132132
} } = await datocmsRequest({ query, variables: { id } })
133133

134-
console.log(layer)
135-
136134
const data = {
137135
layer: {
138136
...layer,

src/lib/find-geonetwork-instances.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
export function findGeonetworkInstances(menuTree, layerId) {
1+
export function findGeonetworkInstances(menuTree, id) {
22
const geonetworkInstances = new Map()
33

4+
console.log('searching for geonetwork instances with id', id)
5+
46
menuTree.forEach((viewer) => {
57
const findInMenu = (menu) => {
68
const { children } = menu
79

810
if (children) {
911
children.forEach((child) => {
10-
if (child.id === layerId) {
12+
if (child.id === id) {
1113
const { geonetwork } = viewer
1214

1315
if (geonetwork) {

0 commit comments

Comments
 (0)