Merge branch 'master' of wopr.rodax-software.com:lqdvi/app2-api
This commit is contained in:
commit
aca1bf193b
@ -96,8 +96,10 @@ const speakerComposer = (speaker, context) => {
|
||||
comments = commentsComposer(speaker.comments);
|
||||
};
|
||||
|
||||
speaker.typename = speaker.type.name;
|
||||
delete speaker.type;
|
||||
if (speaker.type) {
|
||||
speaker.typename = speaker.type.name;
|
||||
delete speaker.type;
|
||||
}
|
||||
|
||||
speaker.values = valuesComposer(speaker.values);
|
||||
|
||||
|
||||
@ -164,8 +164,7 @@ const generateService = (model, extraMethods = {}, options = defaultOptions) =>
|
||||
// Necesario para el cálculo del count
|
||||
// https://github.com/sequelize/sequelize/issues/10557
|
||||
findOptions.distinct = true;
|
||||
console.log('findOptions');
|
||||
console.log(findOptions);
|
||||
|
||||
const result = await model.scope(context.scopes).findAndCountAll(findOptions);
|
||||
|
||||
if (extraMethods.afterFetchAll) {
|
||||
|
||||
@ -50,6 +50,7 @@ const middleware = (config) => {
|
||||
return c;
|
||||
}, {});
|
||||
|
||||
|
||||
if (Object.keys(sortObject).length === 0) {
|
||||
sortObject[
|
||||
defaultSortIsAscending
|
||||
@ -57,6 +58,7 @@ const middleware = (config) => {
|
||||
: config.default.substr(1)
|
||||
] = defaultSortIsAscending;
|
||||
}
|
||||
|
||||
res.locals.sort = sortObject;
|
||||
next();
|
||||
};
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const httpStatus = require('http-status');
|
||||
const Sequelize = require('sequelize');
|
||||
const generateControllers = require('../../core/controllers');
|
||||
const { buildContext } = require('../../core/controllers');
|
||||
const speakerService = require('./speaker.service');
|
||||
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
||||
|
||||
@ -10,6 +12,41 @@ const MODULE_NAME = '[speaker.controller]';
|
||||
|
||||
const controllerOptions = { MODULE_NAME };
|
||||
const extraControllers = {
|
||||
findSimilar: () => {
|
||||
return async (req, res, next) => {
|
||||
let params = extractParamsFromRequest(req, res, { includeAll: false, paginate: { limit: 1, page: 1 } });
|
||||
|
||||
try {
|
||||
const speaker = await speakerService.fetchOne(params, buildContext(req, { scopes: ['includeValues'] }));
|
||||
const values = speaker.values.map(function(value) { return value.id });
|
||||
|
||||
const newParams = Object.assign({}, params, {
|
||||
params: undefined,
|
||||
query: {
|
||||
id: {
|
||||
[Sequelize.Op.ne]: params.params.id
|
||||
}
|
||||
},
|
||||
includeAll: false,
|
||||
paginate: { limit: 10, page: 1 }
|
||||
});
|
||||
|
||||
const similar = await speakerService.fetchAll(newParams, buildContext(req, {
|
||||
scopes: [
|
||||
'defaultScope', 'onlyPublished', 'includeMultimedias',
|
||||
{ method: ['includeValues', values]}
|
||||
]
|
||||
}));
|
||||
|
||||
similar.rows.sort(function(item1, item2) { return Math.random() - Math.random() });
|
||||
return handleResultResponse(similar.rows, similar.count, params, res);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return handleErrorResponse(_options.MODULE_NAME, 'findSimilar', error, res)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
module.exports = generateControllers(speakerService, extraControllers, controllerOptions);
|
||||
|
||||
@ -128,15 +128,20 @@ module.exports = function (sequelize, DataTypes) {
|
||||
}
|
||||
});
|
||||
|
||||
Speaker.addScope('includeValues', () => {
|
||||
return {
|
||||
include: [{
|
||||
model: sequelize.models.Value,
|
||||
as: 'values',
|
||||
attributes: ['id', 'name'],
|
||||
required: false,
|
||||
}]
|
||||
Speaker.addScope('includeValues', (valueList = undefined) => {
|
||||
const config = {
|
||||
model: sequelize.models.Value,
|
||||
as: 'values',
|
||||
attributes: ['id', 'name'],
|
||||
required: false,
|
||||
};
|
||||
if (valueList) {
|
||||
config.where = { id: valueList };
|
||||
config.required = true;
|
||||
}
|
||||
return {
|
||||
include: [config]
|
||||
};
|
||||
});
|
||||
|
||||
Speaker.addScope('includeComments', () => {
|
||||
|
||||
@ -73,7 +73,7 @@ routes.get('/speakers/:id/similar',
|
||||
}),*/
|
||||
PaginateMiddleware.middleware(),
|
||||
SortMiddleware.middleware({ default: "name" }),
|
||||
speakerController.find
|
||||
speakerController.findSimilar()
|
||||
);
|
||||
|
||||
// Listar las preguntas hechas a un ponente
|
||||
@ -84,7 +84,7 @@ routes.get('/speakers/:id/:association',
|
||||
}),*/
|
||||
PaginateMiddleware.middleware(),
|
||||
SortMiddleware.middleware({ default: "-createdAt" }),
|
||||
speakerController.find
|
||||
speakerController.find()
|
||||
);
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user