import suiviService from '../services/suivi.service.js'; import configService from '../services/config.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 config = await configService.getConfig() const config_ = config.toJson() const raidDate = config_.startingDateRaid const bisList = await suiviService.getBisList(raidDate) 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() } const assignLegendary = async (request, reply) => { const { legendary, member, classe } = request.body const proms = [] if (legendary === 'tarecgosa') { const newHist = { name: member, fullDate: DateTime.now().toMillis(), itemID: 71086, itemName: 'Courroux du dragon, le Repos de Tarecgosa', class: classe, response: 'Bis', votes: 0, instance: 'Terres de Feu-25 joueurs', boss: 'Inconnu', equipLoc: 'Deux mains', note: '' } proms.push(suiviService.addHistory(newHist).catch((e) => { console.log(e) })) } else if (legendary === 'crocsPere') { const newHist1 = { name: member, fullDate: DateTime.now().toMillis(), itemID: 77949, itemName: 'Golad, le Crépuscule des Aspects', class: classe, response: 'Bis', votes: 0, instance: 'L\'âme des dragons-25 joueurs', boss: 'Inconnu', equipLoc: 'Main droite', note: '' } const newHist2 = { name: member, fullDate: DateTime.now().toMillis(), itemID: 77950, itemName: 'Tiriosh, le Cauchemar des âges', class: classe, response: 'Bis', votes: 0, instance: 'L\'âme des dragons-25 joueurs', boss: 'Inconnu', equipLoc: 'Main gauche', note: '' } proms.push(suiviService.addHistory(newHist1).catch((e) => { console.log(e) })) proms.push(suiviService.addHistory(newHist2).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 }) } } } export default { getAllRoster, getAllHistory, addRoster, addHistories, deleteRoster, deleteHistories, getBisList, assignLegendary }