summaryrefslogtreecommitdiff
path: root/src/services/events/event.service.js
blob: 53914438f0baa1b41887b0f872e98cae65571241 (plain)
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
const Agenda = require('agenda');
const { getConnection } = require('../../connectDb.js');
const handleAttendClassJob = require('../../handlers');


class Events {
  setup(app) {
    this.collectionName = 'events';

    // Reuse mongoose connection
    const connection = getConnection();
    this.agenda = new Agenda();
    this.agenda.mongo(
      connection.collection(this.collectionName).conn.db,
      this.collectionName
    );

    // Define jobs
    this.agenda.define('attend class', handleAttendClassJob);


    // Logs
    this.agenda.on('start', job => console.log(`Starting ${job.attrs.name} job`));
    this.agenda.on('complete', job => console.log(`Job ${job.attrs.name} finished`));
    this.agenda.on('fail', (err, job) => console.log(`Job failed with the error ${err}`));

    return this.agenda.start();
  };

  create(data, params) {
    return this.agenda.schedule(data.date, 'attend class', data);
  };

  find(params) {
    return this.agenda.jobs();
  }
}


module.exports = app => app.use('/events', new Events());