From 948bd01edce322bfc48d71eef313c166ab0e376c Mon Sep 17 00:00:00 2001 From: david Date: Fri, 26 Jul 2019 19:06:19 +0200 Subject: [PATCH] a --- modules/events/event.model.js | 54 +++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/modules/events/event.model.js b/modules/events/event.model.js index f1baeb2..c8349d0 100644 --- a/modules/events/event.model.js +++ b/modules/events/event.model.js @@ -3,6 +3,39 @@ const moment = require('moment'); const Sequelize = require('sequelize'); moment.locale('es'); +const getStateCode = (event) => { + var currentDate = moment().utc(), + initDate = moment.utc(event.ini_date), + endDate = moment.utc(event.end_date), + init_availableDate = moment.utc(event.init_available_date), + end_availableDate = moment.utc(event.end_available_date); + + if (moment(currentDate).isBetween(initDate, endDate)) { + return 'current_event'; + } else { + if (moment(endDate).isBefore(currentDate)) { + return 'closed_event'; + } else { + if (moment(currentDate).isBefore(init_availableDate)){ + return 'future_registrations' + } else { + if (moment(currentDate).isBetween(init_availableDate, end_availableDate)) { + return (event.sold_out == 1) ? 'waitinglist_open' : 'registrations_open'; + } + else { + if (moment(currentDate).isAfter(end_availableDate)) + return 'closed_registrations' + else + return 'N/A'; + } + + } + } + }; +}; + + + const getStateText = (event) => { var currentDate = moment().utc(), initDate = moment.utc(event.ini_date), @@ -16,10 +49,19 @@ const getStateText = (event) => { if (moment(endDate).isBefore(currentDate)) { return 'Congreso finalizado'; } else { - if (moment(currentDate).isBetween(init_availableDate, end_availableDate)) { - return (event.sold_out == 1) ? 'Inscripciones abiertas a lista de espera' : 'Inscripciones abiertas'; - } else { + if (moment(currentDate).isBefore(init_availableDate)) { return 'Inscripciones a partir del ' + moment(init_availableDate).format('D [de] MMMM'); + } else { + if (moment(currentDate).isBetween(init_availableDate, end_availableDate)) { + return (event.sold_out == 1) ? 'Inscripciones abiertas a lista de espera' : 'Inscripciones abiertas'; + } + else { + if (moment(currentDate).isAfter(end_availableDate)) + return 'Inscripciones cerradas' + else + return 'N/A'; + } + } } }; @@ -95,6 +137,12 @@ module.exports = function (sequelize, DataTypes) { allowNull: false, defaultValue: 'draft' }, + stateCode: { + type: Sequelize.VIRTUAL(Sequelize.STRING, ['init_date', 'end_date', 'init_available_date', 'end_available_date', 'sold_out']), + get: function () { + return getStateCode(this); + }, + }, stateText: { type: Sequelize.VIRTUAL(Sequelize.STRING, ['init_date', 'end_date', 'init_available_date', 'end_available_date', 'sold_out']), get: function () {