-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteEvent.js
More file actions
76 lines (64 loc) · 1.85 KB
/
deleteEvent.js
File metadata and controls
76 lines (64 loc) · 1.85 KB
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
'use strict';
const VError = require('@openagenda/verror');
const _ = require('lodash');
const upStats = require('./lib/upStats');
const potentialOaError = require('./utils/potentialOaError');
const getAgenda = require('./lib/getAgenda');
module.exports = async function deleteEvent(context, {
list,
index,
}) {
const {
methods,
oa,
syncDb,
publicKey,
simulate,
log,
stats,
agendaMap,
catchError,
} = context;
const { startSyncDate } = stats;
const eventToRemove = list[index];
const { agendaUid } = eventToRemove.data;
const agendaStats = stats.agendas[agendaUid];
try {
if (!agendaMap[agendaUid]) {
agendaMap[agendaUid] = {
agenda: await getAgenda(agendaUid, publicKey)
.catch(err => {
throw new VError(err, `Can\'t get agenda ${agendaUid}`);
}),
};
}
const shouldRemove = await methods.event.shouldRemove(eventToRemove);
if (!shouldRemove) {
log('info', 'SKIP REMOVE', { oaEventUid: eventToRemove.data.uid });
return;
}
if (!simulate) {
await potentialOaError(oa.events.delete(agendaUid, eventToRemove.data.uid)
.catch(e => {
// already removed on OA
if (e?.response?.status !== 404) {
throw e;
}
}));
await syncDb.events.remove({ _id: eventToRemove._id }, {});
}
log('info', 'REMOVE EVENT', { oaEventUid: eventToRemove.data.uid });
upStats(agendaStats, 'removedEvents');
} catch (e) {
const error = new VError({
cause: e,
info: {
agendaUid: eventToRemove.data.agendaUid,
oaEventUid: eventToRemove.data.uid,
eventToRemove
}
}, 'Error on event remove');
upStats(agendaStats, 'eventRemoveErrors', error);
catchError(error, `${startSyncDate.toISOString()}:${eventToRemove.data.uid}.json`);
}
};