import suiviService from '../services/suivi.service.js'; import typesError from '../error/types.error.js'; import { DateTime } from 'luxon' const getAllRoster = async (request, reply) => { try { const rosters = await suiviService.getAllRoster() const ret = [] for (let roster of rosters) { ret.push(roster.toJson()) } return reply.code(200).send({ rosters: ret }) } catch (e) { return reply.code(500).send({ message: e.message }) } } const getAllHistory = async (request, reply) => { try { const histories = await suiviService.getAllHistory() const ret = [] for (let history of histories) { ret.push(history.toJson()) } return reply.code(200).send({ histories: ret }) } catch (e) { return reply.code(500).send({ message: e.message }) } } const addRoster = async (request, reply) => { try { await suiviService.addRoster(request.body) return reply.code(200).send({ success: true, message: `Roster member ${request.body.name} added` }) } catch (e) { return reply.code(500).send({ message: typesError.TECHNICAL_UNKNOWN }) } } const addHistories = async (request, reply) => { const histories = request.body.history const input = JSON.parse(histories) const proms = [] if (Array.isArray(input)) { for (const hist of input) { const newHist = { name: hist.player, fullDate: formatDate(hist), itemID: hist.itemID, itemName: hist.itemName, class: hist.class, response: hist.response, votes: hist.votes, instance: hist.instance, boss: hist.boss, equipLoc: hist.equipLoc, note: hist.note } proms.push(suiviService.addHistory(newHist).catch((e) => { console.log(e) })) } } if (proms.length) { try { return Promise.all(proms).then(() => { return reply.code(200).send({ success: true, message: 'Histories added' }) }) } catch (e) { return reply.code(500).send({ message: typesError.TECHNICAL_UNKNOWN }) } } } const deleteRoster = async (request, reply) => { try { await suiviService.removeRoster(request.params.name) return reply.code(200).send({ success: true }) } catch (e) { return reply.code(500).send({ message: e.message }) } } const deleteHistories = async (request, reply) => { const histories = request.body.histories const proms = [] if (Array.isArray(histories)) { for (const id of histories) { proms.push(suiviService.removeHistory(id).catch((e) => { console.log(e) })) } } if (proms.length) { try { return Promise.all(proms).then(() => { return reply.code(200).send({ success: true }) }) } catch (e) { return reply.code(500).send({ message: typesError.TECHNICAL_UNKNOWN }) } } } const getBisList = async (request, reply) => { try { const bisList = await suiviService.getBisList() return reply.code(200).send({ bis: bisList }) } catch (e) { return reply.code(500).send({ message: e.message }) } } const formatDate = (item) => { const dArray = item.date.split('/') const date = `${dArray[0].length === 1 ? `0${dArray[0]}` : dArray[0]}/${dArray[1].length === 1 ? `0${dArray[1]}` : dArray[1]}/${dArray[2]} ${item.time}` return DateTime.fromFormat(date, 'dd/MM/yy HH:mm:ss').toMillis() } export default { getAllRoster, getAllHistory, addRoster, addHistories, deleteRoster, deleteHistories, getBisList }