@@ -7,14 +7,15 @@ import { findGeonetworkInstances } from '../lib/find-geonetwork-instances'
7
7
import { fetchViewerLayerXML } from '../lib/fetch-viewer-layer-xml'
8
8
import { formatMenusRecursive } from '../lib/format-menu'
9
9
import Mailjet from 'node-mailjet'
10
+ import fs from 'fs' ;
10
11
11
12
const mailjet = new Mailjet ( {
12
13
apiKey : process . env . MAILJET_API_TOKEN ,
13
14
apiSecret : process . env . MAILJET_API_SECRET ,
14
15
} )
15
16
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) {
18
19
menus: allMenus(first: $first, skip: $skip, locale: $locale) {
19
20
id
20
21
geonetwork {
@@ -57,46 +58,87 @@ export const handler = withServerDefaults(async (event, _) => {
57
58
}
58
59
}
59
60
60
- const layerData = JSON . parse ( event . body )
61
+ const data = JSON . parse ( event . body )
61
62
62
- const viewerLayerId = layerData . entity . id
63
+ const id = data . entity . id
63
64
64
65
const { menus } = await datocmsRequest ( {
65
- query : viewersWithLayersQuery ,
66
+ query : viewersWithViewerLayersQuery ,
67
+ preview : true
66
68
} )
67
69
const formattedMenus = formatMenusRecursive ( menus )
68
70
const menuTree = buildMenuTree ( formattedMenus )
69
71
70
72
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
+ }
72
80
}
73
81
catch ( e ) {
74
82
console . log ( 'The following error occured' , e . message )
75
83
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 )
97
86
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
+ }
98
105
} )
99
106
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
+
100
142
async function syncViewerLayers ( menuTree , eventType , viewerLayerId ) {
101
143
const geonetworkInstances = findGeonetworkInstances ( menuTree , viewerLayerId )
102
144
@@ -155,8 +197,6 @@ async function syncViewerLayers(menuTree, eventType, viewerLayerId) {
155
197
variables : { id : viewerLayerId } ,
156
198
} )
157
199
158
- console . log ( 'viewerLayer' , viewerLayer )
159
-
160
200
await addThumbnailsToRecord ( viewerLayer ?. layer ?. thumbnails , viewerLayerId , geonetwork )
161
201
}
162
202
}
0 commit comments