app2-api/modules/events/events_reservations.model.js
2022-02-23 19:27:58 +01:00

160 lines
4.4 KiB
JavaScript

"use strict";
const moment = require("moment");
const Sequelize = require("sequelize");
moment.locale("es");
const getStateText = (reservation) => {
var currentDate = moment().utc(),
init_availableDate = moment.utc(reservation.init_available_date),
end_availableDate = moment.utc(reservation.end_available_date);
if (moment(currentDate).isBetween(init_availableDate, end_availableDate)) {
return reservation.sold_out == 1
? "Inscripciones abiertas a lista de espera de la reserva"
: "Inscripciones abiertas a la reserva";
} else {
return "Inscripciones a la reserva a partir del " + moment(init_availableDate).format("D [de] MMMM");
}
};
const getAssistanceType = (reservation) => (reservation.virtual ? "online" : "onsite");
const getAssistanceTypeText = (reservation) => (reservation.virtual ? "asistencia online" : "asistencia presencial");
module.exports = function (sequelize, DataTypes) {
const EventReservation = sequelize.define(
"EventReservation",
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
init_available_date: {
type: DataTypes.DATE,
},
end_available_date: {
type: DataTypes.DATE,
},
//LO DA EL VENUE DEL EVENTO, Y LO COPIAMOS PARA NO ESTAR CONSULTANDOLO
gmt: {
type: DataTypes.INTEGER,
defaultValue: 1,
},
state: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: "draft",
},
assistants: {
type: DataTypes.INTEGER,
},
confirmed: {
type: DataTypes.INTEGER,
},
sold_out: {
//Se han vendido todas y se ha abierto lista de espera si hay asignada
type: DataTypes.BOOLEAN,
defaultValue: false,
},
allow_multiple: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
multiple_limit: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
description: {
type: DataTypes.STRING,
},
reservation_code: {
type: DataTypes.STRING,
allowNull: false,
},
color: {
type: DataTypes.STRING,
allowNull: false,
},
allow_overflow: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
overflow_reservationId: {
type: DataTypes.UUID,
foreignKey: true,
},
marketing_list: {
type: DataTypes.STRING,
},
virtual: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
assistanceType: {
type: Sequelize.VIRTUAL(Sequelize.STRING, ["virtual"]),
get: function () {
return getAssistanceType(this);
},
},
assistanceTypeText: {
type: Sequelize.VIRTUAL(Sequelize.STRING, ["virtual"]),
get: function () {
return getAssistanceTypeText(this);
},
},
stateText: {
type: Sequelize.VIRTUAL(Sequelize.STRING, ["init_available_date", "end_available_date", "sold_out"]),
get: function () {
return getStateText(this);
},
},
},
{
tableName: "events_reservations",
freezeTableName: true,
timestamps: true,
}
);
EventReservation.associate = function (models) {
EventReservation.OverflowEventReservation = EventReservation.belongsTo(models.EventReservation, {
as: "EventToEvent",
foreignKey: "overflow_reservationId",
});
EventReservation.Entity = EventReservation.belongsTo(models.Entity, { foreignKey: "entityId" });
EventReservation.Event = EventReservation.belongsTo(models.Event, { foreignKey: "eventId" });
EventReservation.Inscriptions = EventReservation.hasMany(models.EventInscription, {
foreignKey: "reservationId",
as: "inscriptions",
});
EventReservation.UserCreate = EventReservation.belongsTo(models.User, { foreignKey: "userId" });
};
EventReservation.addScope("includeEvent", () => {
return {
include: [
{
model: sequelize.models.Event,
},
],
};
});
EventReservation.addScope("includeInscriptions", () => {
return {
include: [
{
model: sequelize.models.EventInscription,
as: "inscriptions",
},
],
};
});
return EventReservation;
};