'use strict';

const service = require('./sync.service');
const queue = require('./sync.queue');

async function status(req, res) {
  res.json({ ok: true, status: await service.getStatus() });
}

async function events(req, res) {
  const status = req.query.status;
  res.json({ ok: true, events: await queue.listEvents(status ? { status } : {}) });
}

async function enqueue(req, res) {
  const event = await service.enqueueEvent(req.body);
  res.status(201).json({ ok: true, event });
}

async function retry(req, res) {
  const result = await service.flushOnce();
  res.json({ ok: true, result, status: await service.getStatus() });
}

module.exports = { status, events, enqueue, retry };
