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

155 lines
4.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-17 12:07:35 +00:00
afterFetchAll: (result, params, context) => {
2019-07-18 16:13:27 +00:00
2019-07-17 12:07:35 +00:00
if (!result.count) {
return result;
}
let rows = result.rows.map(row => row.toJSON());
if (context.scopes.includes('includeSpeakers')) {
2019-07-17 12:12:12 +00:00
rows = rows.map(event => Object.assign({},
event,
2019-07-17 14:47:27 +00:00
{ details: undefined },
2019-07-18 16:13:27 +00:00
{ speakers: event.details.map((details) => {
const multimedias = details.speaker.multimedias.map((multimedias) => {
return {...multimedias, ...multimedias.MultimediaFile, MultimediaFile: undefined };
});
const data = { ...details.speaker, order: details.order, multimedias: multimedias };
return data;
} ) }
2019-07-17 12:12:12 +00:00
));
2019-07-17 12:07:35 +00:00
}
return {
count: rows.length,
rows: rows
}
},
fetch2: 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
2019-07-17 12:07:35 +00:00
/*switch (lapse) {
2019-07-10 17:26:22 +00:00
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-17 12:07:35 +00:00
}*/
2019-07-10 17:26:22 +00:00
2019-07-09 16:56:22 +00:00
// Incluir
2019-07-17 14:47:27 +00:00
/* 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-17 12:07:35 +00:00
console.log(context);
2019-07-09 15:37:56 +00:00
try {
2019-07-17 12:07:35 +00:00
return await models.Event.scope(context.scopes).findAll(findOptions);
2019-07-09 15:37:56 +00:00
} catch(error) {
throw error;
}
2019-07-17 14:47:27 +00:00
*/
2019-07-14 16:44:59 +00:00
},
fetchOne: async (params, context) => {
const findOptions = parseParamsToFindOptions(params);
// Incluir
findOptions.include.push({
2019-07-17 14:47:47 +00:00
model: models.EventDetail,
as: 'details',
2019-07-14 16:44:59 +00:00
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);