app2-api/modules/events/event.service.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-07-09 08:51:00 +00:00
/* global Venue */
'use strict';
const _ = require('lodash');
2019-07-09 15:59:17 +00:00
const moment = require('moment');
2019-07-09 08:51:00 +00:00
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
2019-07-09 16:56:22 +00:00
const Sequelize = require('sequelize');
2019-07-09 08:51:00 +00:00
const models = require('../../core/models');
2019-07-09 15:37:56 +00:00
const extraMethods = {
fetchNext: async (params, context) => {
2019-07-09 16:56:22 +00:00
const findOptions = parseParamsToFindOptions(params);
2019-07-09 15:37:56 +00:00
2019-07-09 18:34:39 +00:00
findOptions.where = Object.assign({},
findOptions.where, {
2019-07-09 15:37:56 +00:00
date: {
2019-07-09 18:34:39 +00:00
[Sequelize.Op.gte]: moment().add(1, 'days').startOf('day').utc()
2019-07-09 15:37:56 +00:00
}
});
2019-07-09 16:56:22 +00:00
// Incluir
findOptions.include.push({
model: models.EventSchedule,
});
findOptions.include.push({
2019-07-09 15:37:56 +00:00
model: models.EventType,
2019-07-09 16:56:22 +00:00
});
findOptions.include.push({
model: models.Venue,
});
2019-07-09 15:37:56 +00:00
2019-07-09 18:34:39 +00:00
findOptions.where = Object.assign({},
findOptions.where, {
state: 'publish'
2019-07-09 15:37:56 +00:00
});
2019-07-09 18:34:39 +00:00
2019-07-09 16:56:22 +00:00
2019-07-09 15:37:56 +00:00
try {
2019-07-09 16:56:22 +00:00
return await models.Event.findAll(findOptions);
2019-07-09 15:37:56 +00:00
} catch(error) {
throw error;
}
}
};
2019-07-09 08:51:00 +00:00
module.exports = generateService(models.Event, extraMethods);