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

125 lines
3.5 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 16:56:22 +00:00
// Incluir
findOptions.include.push({
2019-07-14 16:44:59 +00:00
model: models.EventSchedule,
as: 'schedule',
include: {
model: models.Speaker,
as: 'speaker',
attributes: ['id', 'name', 'description']
}
2019-07-09 16:56:22 +00:00
});
findOptions.include.push({
2019-07-14 16:44:59 +00:00
model: models.EventType,
as: 'type',
attributes: ['name', 'title'],
2019-07-09 16:56:22 +00:00
});
findOptions.include.push({
model: models.Venue,
2019-07-14 16:44:59 +00:00
as: 'venue'
2019-07-09 16:56:22 +00:00
});
2019-07-11 18:05:06 +00:00
// findOptions.include.push({
// model: models.Multimedia, //where: { alias: type }, attributes: [],
// });
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-14 16:44:59 +00:00
},
fetchOne: async (params, context) => {
const findOptions = parseParamsToFindOptions(params);
// Incluir
findOptions.include.push({
model: models.EventSchedule,
as: 'schedule',
include: {
model: models.Speaker,
as: 'speaker',
attributes: ['id', 'name', 'description']
}
});
findOptions.include.push({
model: models.EventType,
as: 'type',
attributes: ['name', 'title'],
});
findOptions.include.push({
model: models.Venue,
as: 'venue'
});
findOptions.include.push({
model: models.EventInscription,
as: 'inscriptions'
});
findOptions.where = Object.assign({},
findOptions.where, {
state: 'publish'
});
return await models.Event.findOne({
where: findOptions.where,
include: findOptions.include
});
},
2019-07-09 15:37:56 +00:00
};
2019-07-09 08:51:00 +00:00
module.exports = generateService(models.Event, extraMethods);