app2-api/modules/events/event.service.js
2019-07-11 20:05:06 +02:00

76 lines
2.3 KiB
JavaScript

/* global Venue */
'use strict';
const _ = require('lodash');
const moment = require('moment');
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
const Sequelize = require('sequelize');
const models = require('../../core/models');
const extraMethods = {
fetch: async (params, context) => {
const lapse = context.lapse;
const findOptions = parseParamsToFindOptions(params);
switch (lapse) {
case 'next':
findOptions.where = Object.assign({},
findOptions.where, {
date: {
[Sequelize.Op.gte]: moment().add(1, 'days').startOf('day').utc()
}
});
break;
case 'current':
findOptions.where = Object.assign({},
findOptions.where, {
date: {
[Sequelize.Op.gte]: moment().startOf('day').utc(),
[Sequelize.Op.lt]: moment().add(1, 'days').startOf('day').utc(),
}
});
break;
case 'past':
findOptions.where = Object.assign ({},
findOptions.where, {
date: {
[Sequelize.Op.lt]: moment().startOf('day').utc()
}
});
break;
default:
break;
}
// Incluir
findOptions.include.push({
model: models.EventSchedule, include: {model: models.Speaker, attributes: ['id', 'name', 'description']}
});
findOptions.include.push({
model: models.EventType, attributes: ['name', 'tittle'],
});
findOptions.include.push({
model: models.Venue,
});
// findOptions.include.push({
// model: models.Multimedia, //where: { alias: type }, attributes: [],
// });
findOptions.where = Object.assign({},
findOptions.where, {
state: 'publish'
});
try {
return await models.Event.findAll(findOptions);
} catch(error) {
throw error;
}
}
};
module.exports = generateService(models.Event, extraMethods);