forked from perfectmak/events-tracking-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
51 lines (45 loc) · 1.21 KB
/
events.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
const aargh = require('aargh');
const { eventFromModelToObject, eventFromObjectToModel } = require('./mapper');
const { DuplicateEventID } = require('../services/eventServiceErrors');
module.exports = ({ eventsService }) => {
const getAllEvents = _input => {
return eventsService
.getAll()
.then(events => ({ data: events.map(eventFromModelToObject) }));
};
/**
* Store new event.
* If new event id exists return 400 error
*
*/
const addEvent = input => {
return eventsService
.save(eventFromObjectToModel(input.data))
.then(() => ({ code: 201 }))
.catch(err =>
aargh(err)
.type(DuplicateEventID, () => ({
code: 400
}))
.throw()
);
};
const getByActor = input => {
return eventsService.getEventsByActor(input.params.actorId).then(events => {
if (events.length === 0) {
return { code: 404 };
} else {
return { data: events.map(eventFromModelToObject) };
}
});
};
const eraseEvents = () => {
return eventsService.eraseAll();
};
return {
getAllEvents: getAllEvents,
addEvent: addEvent,
getByActor: getByActor,
eraseEvents: eraseEvents
};
};