app2-api/modules/events/event.service.js
2019-07-18 13:23:43 +02:00

151 lines
4.2 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 = {
afterFetchAll: (result, params, context) => {
return result;
/*
if (!result.count) {
return result;
}
let rows = result.rows.map(row => row.toJSON());
if (context.scopes.includes('includeSpeakers')) {
rows = rows.map(event => Object.assign({},
event,
{ details: undefined },
{ speakers: event.details.map(details => ({ ...details.speaker, order: details.order }) ) }
));
}
return {
count: rows.length,
rows: rows
}
*/
},
fetch2: 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,
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.Multimedia, //where: { alias: type }, attributes: [],
// });
findOptions.where = Object.assign({},
findOptions.where, {
state: 'publish'
});
console.log(context);
try {
return await models.Event.scope(context.scopes).findAll(findOptions);
} catch(error) {
throw error;
}
*/
},
fetchOne: async (params, context) => {
const findOptions = parseParamsToFindOptions(params);
// Incluir
findOptions.include.push({
model: models.EventDetail,
as: 'details',
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
});
},
};
module.exports = generateService(models.Event, extraMethods);