-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstructures2contentful.js
109 lines (90 loc) · 3.47 KB
/
structures2contentful.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint-disable @typescript-eslint/no-unused-vars,no-console,no-await-in-loop,import/no-unresolved */
const { path,
map,
filter,
sortBy,
} = require('ramda');
const { writeFile } = require('node:fs/promises');
const getContentfulEnv = require('./getContentfulEnvironment');
const data = require('./data.json');
const createEntries = async () => {
console.log(`${data.length} items\n`);
const contentful = await getContentfulEnv();
for (const structure of data) {
await contentful.createEntryWithId('structure', structure.id, {
fields: {
nom: { fr: structure.nom },
organisation: { fr: structure.organisation },
type: { fr: structure.type },
// description: { fr: structure.description },
// specialite: { fr: structure.specialite },
latLon: {
fr: {
lat: parseFloat(structure.lat),
lon: parseFloat(structure.lon),
},
},
adresse: { fr: structure.adresse },
siteWeb: { fr: structure.siteWeb },
tel: { fr: structure.tel },
email: { fr: structure.email },
},
});
console.log(structure.id);
}
};
// createEntries().catch(console.error);
const publishEntries = async () => {
const contentful = await getContentfulEnv();
const { items } = await contentful.getEntries({ content_type: 'structure', 'sys.archivedAt[exists]': false, 'sys.publishedAt[exists]': false, /* 'fields.type': 'CADA', */ select: 'sys.id,sys.version', limit: 200 });
const action = await contentful.createPublishBulkAction({ entities: { items: items.map(({ sys }) => ({
sys: { ...sys, linkType: 'Entry', type: 'Link' },
})) } });
return action.waitProcessing();
};
const unPublishEntries = async () => {
const contentful = await getContentfulEnv();
const { items } = await contentful.getEntries({ content_type: 'structure', 'sys.archivedAt[exists]': false, 'sys.publishedAt[exists]': true, select: 'sys.id', limit: 200 });
const action = await contentful.createUnpublishBulkAction({ entities: { items: items.map(({ sys }) => ({
sys: { ...sys, linkType: 'Entry', type: 'Link' },
})) } });
await action.waitProcessing();
};
const allEntries = async () => {
const contentful = await getContentfulEnv();
const res = await contentful.getEntries({
limit: 1000,
content_type: 'structure',
});
// writeFileSync('./entries.json', JSON.stringify(res, null, 2));
return res.items;
};
const deleteAllEntries = async () => {
const contentful = await getContentfulEnv();
const entries = await allEntries();
for (const entry of entries) {
await contentful.deleteEntry(entry.sys.id);
}
};
// Export all entries to JSON
allEntries()
.then(filter(({ sys }) => !sys.archivedAt && sys.publishedAt))
.then(map(({ sys, fields }) => ({
id: Number(sys.id),
updatedAt: sys.updatedAt,
title: fields.nom?.fr,
organization: fields.organisation?.fr,
type: fields.type?.fr,
lat: fields.latLon?.fr.lat,
lon: fields.latLon?.fr.lon,
address: fields.adresse?.fr,
url: fields.siteWeb?.fr,
phone: fields.tel?.fr,
email: fields.email?.fr,
})))
.then(sortBy(path(['id'])))
.then(arr => writeFile('./export-structures.json', JSON.stringify(arr, null, 2)).then(() => arr))
// https://jsononline.net/fr/json-to-csv pour le CSV (tout en browser, pas d'appel API)
.then(console.log)
.catch(console.error);
// publishEntries().catch(e => console.error(e, e.action.error.details.errors.map(path(['error', 'details', 'errors']))));