2019-07-09 18:34:39 +00:00
|
|
|
'use strict';
|
2019-08-20 12:10:49 +00:00
|
|
|
const httpStatus = require('http-status');
|
|
|
|
|
const Sequelize = require('sequelize');
|
2019-07-09 18:34:39 +00:00
|
|
|
const generateControllers = require('../../core/controllers');
|
2019-08-20 12:10:49 +00:00
|
|
|
const { buildContext } = require('../../core/controllers');
|
2019-07-09 18:34:39 +00:00
|
|
|
const speakerService = require('./speaker.service');
|
|
|
|
|
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Module Name
|
|
|
|
|
const MODULE_NAME = '[speaker.controller]';
|
|
|
|
|
|
|
|
|
|
const controllerOptions = { MODULE_NAME };
|
|
|
|
|
const extraControllers = {
|
2019-08-20 12:10:49 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-07-09 18:34:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = generateControllers(speakerService, extraControllers, controllerOptions);
|
|
|
|
|
|