Dar ponentes similares a otro
This commit is contained in:
parent
e3976cb516
commit
2a00090c87
@ -96,8 +96,10 @@ const speakerComposer = (speaker, context) => {
|
|||||||
comments = commentsComposer(speaker.comments);
|
comments = commentsComposer(speaker.comments);
|
||||||
};
|
};
|
||||||
|
|
||||||
speaker.typename = speaker.type.name;
|
if (speaker.type) {
|
||||||
delete speaker.type;
|
speaker.typename = speaker.type.name;
|
||||||
|
delete speaker.type;
|
||||||
|
}
|
||||||
|
|
||||||
speaker.values = valuesComposer(speaker.values);
|
speaker.values = valuesComposer(speaker.values);
|
||||||
|
|
||||||
|
|||||||
@ -164,8 +164,7 @@ const generateService = (model, extraMethods = {}, options = defaultOptions) =>
|
|||||||
// Necesario para el cálculo del count
|
// Necesario para el cálculo del count
|
||||||
// https://github.com/sequelize/sequelize/issues/10557
|
// https://github.com/sequelize/sequelize/issues/10557
|
||||||
findOptions.distinct = true;
|
findOptions.distinct = true;
|
||||||
console.log('findOptions');
|
|
||||||
console.log(findOptions);
|
|
||||||
const result = await model.scope(context.scopes).findAndCountAll(findOptions);
|
const result = await model.scope(context.scopes).findAndCountAll(findOptions);
|
||||||
|
|
||||||
if (extraMethods.afterFetchAll) {
|
if (extraMethods.afterFetchAll) {
|
||||||
|
|||||||
@ -50,6 +50,7 @@ const middleware = (config) => {
|
|||||||
return c;
|
return c;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
|
|
||||||
if (Object.keys(sortObject).length === 0) {
|
if (Object.keys(sortObject).length === 0) {
|
||||||
sortObject[
|
sortObject[
|
||||||
defaultSortIsAscending
|
defaultSortIsAscending
|
||||||
@ -57,6 +58,7 @@ const middleware = (config) => {
|
|||||||
: config.default.substr(1)
|
: config.default.substr(1)
|
||||||
] = defaultSortIsAscending;
|
] = defaultSortIsAscending;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.locals.sort = sortObject;
|
res.locals.sort = sortObject;
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
const httpStatus = require('http-status');
|
||||||
|
const Sequelize = require('sequelize');
|
||||||
const generateControllers = require('../../core/controllers');
|
const generateControllers = require('../../core/controllers');
|
||||||
|
const { buildContext } = require('../../core/controllers');
|
||||||
const speakerService = require('./speaker.service');
|
const speakerService = require('./speaker.service');
|
||||||
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
||||||
|
|
||||||
@ -10,6 +12,41 @@ const MODULE_NAME = '[speaker.controller]';
|
|||||||
|
|
||||||
const controllerOptions = { MODULE_NAME };
|
const controllerOptions = { MODULE_NAME };
|
||||||
const extraControllers = {
|
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);
|
module.exports = generateControllers(speakerService, extraControllers, controllerOptions);
|
||||||
|
|||||||
@ -128,15 +128,20 @@ module.exports = function (sequelize, DataTypes) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Speaker.addScope('includeValues', () => {
|
Speaker.addScope('includeValues', (valueList = undefined) => {
|
||||||
return {
|
const config = {
|
||||||
include: [{
|
model: sequelize.models.Value,
|
||||||
model: sequelize.models.Value,
|
as: 'values',
|
||||||
as: 'values',
|
attributes: ['id', 'name'],
|
||||||
attributes: ['id', 'name'],
|
required: false,
|
||||||
required: false,
|
};
|
||||||
}]
|
if (valueList) {
|
||||||
|
config.where = { id: valueList };
|
||||||
|
config.required = true;
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
|
include: [config]
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
Speaker.addScope('includeComments', () => {
|
Speaker.addScope('includeComments', () => {
|
||||||
|
|||||||
@ -73,7 +73,7 @@ routes.get('/speakers/:id/similar',
|
|||||||
}),*/
|
}),*/
|
||||||
PaginateMiddleware.middleware(),
|
PaginateMiddleware.middleware(),
|
||||||
SortMiddleware.middleware({ default: "name" }),
|
SortMiddleware.middleware({ default: "name" }),
|
||||||
speakerController.find
|
speakerController.findSimilar()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Listar las preguntas hechas a un ponente
|
// Listar las preguntas hechas a un ponente
|
||||||
@ -84,7 +84,7 @@ routes.get('/speakers/:id/:association',
|
|||||||
}),*/
|
}),*/
|
||||||
PaginateMiddleware.middleware(),
|
PaginateMiddleware.middleware(),
|
||||||
SortMiddleware.middleware({ default: "-createdAt" }),
|
SortMiddleware.middleware({ default: "-createdAt" }),
|
||||||
speakerController.find
|
speakerController.find()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user