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

72 lines
2.1 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 = {
2019-07-10 17:26:22 +00:00
fetch: async (params, context) => {
const lapse = context.lapse;
2019-07-09 16:56:22 +00:00
const findOptions = parseParamsToFindOptions(params);
2019-07-10 17:26:22 +00:00
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;
}
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 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);