2019-07-09 18:34:39 +00:00
|
|
|
/* global Venue */
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
|
|
|
|
const Sequelize = require('sequelize');
|
|
|
|
|
const models = require('../../core/models');
|
2019-07-25 17:06:12 +00:00
|
|
|
const { speakerComposer } = require('../../helpers/composes.helper');
|
2019-07-09 18:34:39 +00:00
|
|
|
|
2019-07-19 15:47:06 +00:00
|
|
|
const extraMethods = {
|
|
|
|
|
afterFetchAll: (result, params, context) => {
|
|
|
|
|
|
|
|
|
|
if (!result.count) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let rows = result.rows.map(row => row.toJSON());
|
|
|
|
|
rows = rows.map(speaker => speakerComposer(speaker, context));
|
|
|
|
|
|
|
|
|
|
return {
|
2019-07-31 10:40:36 +00:00
|
|
|
count: result.count,
|
2019-07-19 15:47:06 +00:00
|
|
|
rows: rows
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
afterFetchOne: (result, params, context) => {
|
|
|
|
|
return speakerComposer(result.toJSON(), context);
|
|
|
|
|
},
|
2019-07-21 09:20:16 +00:00
|
|
|
|
2019-08-06 17:23:56 +00:00
|
|
|
beforeUpdate: (values, findOptions, context) => {
|
|
|
|
|
delete values.createdAt;
|
|
|
|
|
delete values.updatedAt;
|
|
|
|
|
delete values.userId;
|
|
|
|
|
|
|
|
|
|
// Descomponemos RRSS
|
|
|
|
|
values = Object.assign(values, values.rrss, { rrss: undefined });
|
|
|
|
|
|
|
|
|
|
return values;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
update: async (params, values, context) => {
|
|
|
|
|
const findOptions = parseParamsToFindOptions(params);
|
|
|
|
|
if (extraMethods.beforeUpdate) {
|
|
|
|
|
values = extraMethods.beforeUpdate(values, findOptions, context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let speaker = await models.Speaker.findOne(findOptions);
|
|
|
|
|
await speaker.update(values);
|
|
|
|
|
speaker.setValues(values.values.map(value => value.id));
|
|
|
|
|
|
|
|
|
|
await speaker.save();
|
|
|
|
|
return [1];
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-19 15:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-09 18:34:39 +00:00
|
|
|
module.exports = generateService(models.Speaker, extraMethods);
|