This commit is contained in:
David Arranz 2019-07-19 12:17:52 +02:00
parent 8ae7aac71b
commit 49822f6737
9 changed files with 150 additions and 70 deletions

View File

@ -25,7 +25,9 @@ const generateControllers = (service, extraControllers = {}, options = {}) => {
};
const defaultControllers = {
find: (config) => {
return async (req, res, next) => {
config = config || {
scopes: [],
@ -44,8 +46,6 @@ const generateControllers = (service, extraControllers = {}, options = {}) => {
findOne: (config) => {
return async (req, res, next) => {
const params = extractParamsFromRequest(req, res, _options.params.findOne);
console.log('------------------------------------PARAMS');
console.log(params);
try {
const result = await service.fetchOne(params, buildContext(req, config));
return handleResultResponse(result, null, params, res, (result === null) ? httpStatus.NOT_FOUND : httpStatus.OK);

View File

@ -9,31 +9,6 @@ const MODULE_NAME = '[entity.controller]';
const controllerOptions = { MODULE_NAME };
const extraControllers = {
findColleges: async (req, res, next) => {
const params = extractParamsFromRequest(req, res, {});
console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-params');
console.log(params);
try {
const result = await entityService.fetch(params, { user: req.user, type: 'college' });
return handleResultResponse(result, result.count, params, res);
} catch (error) {
handleErrorResponse(MODULE_NAME, 'findNext', error, res);
}
},
findPartners: async (req, res, next) => {
const params = extractParamsFromRequest(req, res, {});
try {
const result = await entityService.fetch(params, { user: req.user, type: 'partner' });
return handleResultResponse(result, result.count, params, res);
} catch (error) {
handleErrorResponse(MODULE_NAME, 'findNext', error, res);
}
},
};
module.exports = generateControllers(entityService, extraControllers, controllerOptions);

View File

@ -1,3 +1,5 @@
const Sequelize = require('sequelize');
module.exports = function (sequelize, DataTypes) {
const Entity = sequelize.define('Entity', {
id: {
@ -13,11 +15,23 @@ module.exports = function (sequelize, DataTypes) {
type: DataTypes.STRING(45),
allowNull: false,
default: 'draft',
},
contact_person: {
type: DataTypes.STRING,
},
contact_email: {
type: DataTypes.STRING,
}
}, {
tableName: 'entities',
freezeTableName: true,
timestamps: true,
defaultScope: {
where: {
state: 'publish'
},
},
});
Entity.associate = function (models) {
@ -29,5 +43,29 @@ module.exports = function (sequelize, DataTypes) {
Entity.User = Entity.hasMany(models.User, { foreignKey: 'entityId' });
Entity.EventsReservations = Entity.hasMany(models.EventReservation, { foreignKey: 'entityId', as: 'reservations' });
};
Entity.addScope('onlyColleges', () => {
return {
include: [{ model: sequelize.models.EntityType,
as: 'types',
where: {alias: 'college'},
attributes: [],
}]
}
});
Entity.addScope('onlyPartners', () => {
return {
include: [{
model: sequelize.models.EntityType,
as: 'types',
where: { alias: 'partner' },
attributes: [],
}]
}
});
return Entity;
};

View File

@ -10,27 +10,45 @@ const SortMiddleware = require('../../middlewares/sort');
//const entityValidation = require('./entity.validations');
const entityController = require('./entity.controller');
routes.get ('/entities',
isLoggedUser,
PaginateMiddleware.middleware(),
entityController.find);
const generalInvalidFields = [
'state',, 'createdAt', 'updatedAt'
];
routes.get('/entities',
//isLoggedUser,
FieldMiddleware.middleware({
invalidFields: generalInvalidFields
}),
PaginateMiddleware.middleware(),
SortMiddleware.middleware({ default: "name" }),
entityController.find({
scopes: ['defaultScope'],
}),
);
routes.get ('/entities/colleges',
// PaginateMiddleware.middleware(),
FieldMiddleware.middleware({
invalidFields: ['state', 'createdAt', 'updatedAt']
invalidFields: generalInvalidFields
}),
SortMiddleware.middleware({ default: "name" }),
entityController.findColleges
entityController.find({
scopes: ['defaultScope', 'onlyColleges'],
}),
);
routes.get ('/entities/partners',
// PaginateMiddleware.middleware(),
FieldMiddleware.middleware({
invalidFields: ['state', 'createdAt', 'updatedAt']
invalidFields: generalInvalidFields
}),
SortMiddleware.middleware({ default: "name" }),
entityController.find({
scopes: ['defaultScope', 'onlyPartners'],
}),
SortMiddleware.middleware({ default: "name" }),
entityController.findPartners
);

View File

@ -5,28 +5,33 @@ const _ = require('lodash');
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
const models = require('../../core/models');
const entityComposer = (entity, context) => {
delete entity.contact_person;
delete entity.contact_email;
return Object.assign({},
entity,
)
};
const extraMethods = {
fetch: async (params, context) => {
const type = context.type;
const findOptions = parseParamsToFindOptions(params);
afterFetchAll: (result, params, context) => {
findOptions.include.push({
model: models.EntityType,
as: 'types',
where: { alias: type },
attributes: [],
});
try {
return await models.Entity.findAll(findOptions);
} catch (error) {
throw error;
if (!result.count) {
return result;
}
}
let rows = result.rows.map(row => row.toJSON());
rows = rows.map(entity => entityComposer(entity, context));
return {
count: rows.length,
rows: rows
}
},
};

View File

@ -1,6 +1,5 @@
'use strict';
const httpStatus = require('http-status');
const generateControllers = require('../../core/controllers');
const eventService = require('./event.service');
const { extractParamsFromRequest, handleErrorResponse, handleResultResponse } = require('../../helpers/controller.helper');

View File

@ -13,10 +13,11 @@ module.exports = function (sequelize, DataTypes) {
end_available_date: {
type: DataTypes.DATE,
},
//LO DA EL VENUE DEL EVENTO, Y LO COPIAMOS PARA NO ESTAR CONSULTANDOLO
gmt: {
type: DataTypes.INTEGER,
defaultValue: 1,
},
},
state: {
type: DataTypes.STRING,
allowNull: false,
@ -35,6 +36,7 @@ module.exports = function (sequelize, DataTypes) {
},
multiple_limit: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
description: {
type: DataTypes.STRING,

View File

@ -68,20 +68,6 @@ SELECT UUID() as ID, speakerID as eventID, 'speaker', videoId as multimediafilei
FROM lqdvi.`video-speaker`
where videoId in (select id from lqdvi_v2.multimedia_files);
//METEMOS LOS DETALLES DE LOS CONGRESOS AGENDAS DE TODOS LOS CONGRESOS
Cargar
script-carga-bd-events_details.sql
update lqdvi_v2.events_details
set type = 'speaker'
where description like'%conference-speaker%';
update lqdvi_v2.events_details
set type = 'info'
where type is null;
//Modificamos la tabla para crear otro campo autoinc que nos rellene el order
/*
insert into lqdvi_v2.events_schedules (id, eventId, speakerId, description, createdAt, updatedAt)
@ -98,6 +84,63 @@ insert into lqdvi_v2.events_schedules (id, eventId, speakerId, order, descriptio
select UUID() as ID, conferenceId, speakerId, 0, 'relacion conference-speaker modelo anterior', now(), now() FROM lqdvi.`conference-speaker`;
*/
//METEMOS LOS DETALLES DE LOS CONGRESOS AGENDAS DE TODOS LOS CONGRESOS
Cargar
script-carga-bd-events_details.sql
update lqdvi_v2.events_details
set type = 'speaker'
where description like'%conference-speaker%';
update lqdvi_v2.events_details
set type = 'info'
where type is null;
//RESERVAS-INVITACIONES
Crear campo entityname (varchar(255)) en tabla events_reservations
insert into lqdvi_v2.events_reservations
(id, init_available_date, end_available_date, gmt, state,
assistants, confirmed, allow_multiple, multiple_limit,
description, lqdvi_v2.events_reservations.code, color,
allow_overflow, overflow_event_reservationID, marketing_list,
createdAt, updatedAt, userID,
entityId, eventid, entityname)
SELECT b.id, init_date, end_date, gmt, status,
assistants, confirmed, 0, 0,
b.name as name_invitation, invitation_code, color,
0, null, marketinglist, now(), now(), '0939bb2a-d33d-4290-ac81-fc9faa1c015e',
null, null, a.name
FROM lqdvi.partner a
left join lqdvi.level b on (b.partnerId = a.id);
//INSERTAMOS LAS ENTIDADES
insert into lqdvi_v2.entities
(id, name, state, createdAt, updatedAt, contact_person, contact_email )
SELECT UUID() as ID, name, 'ff', now(), now(), contact_name, contact_email
FROM lqdvi.partner;
//ASIGNAMOS QUE SON PARTNERS
insert into lqdvi_v2.entities_entities_types
(entityid, typeid, createdAt, updatedAt)
select id, '32d6306a-aa02-11e9-a553-000c295f0f58', now(), now()
from lqdvi_v2.entities
where state = 'ff';
update lqdvi_v2.entities
set state = 'publish'
where state = 'ff';
//ENLAZAMOS CON LAS RESERVAS
//Metemos las preguntas de los congresos
insert into lqdvi_v2.events_questions (id, eventId, speakerId, anonimous, answered, discared, answer, userId, createdAt, updatedAt)
SELECT UUID() as ID, conferenceId, speakerId, anonymous, answered, discared, content, '0939bb2a-d33d-4290-ac81-fc9faa1c015e', created, now() FROM lqdvi.question;

View File

@ -21,7 +21,7 @@
LOCK TABLES `entities_types` WRITE;
/*!40000 ALTER TABLE `entities_types` DISABLE KEYS */;
INSERT INTO `entities_types` VALUES ('2617e44a-a303-11e9-94b5-000c295f0f58','ENTIDADES PATROCINADORAS Y COLABORADORAS','2019-07-02 12:00:00','2019-07-02 12:00:00','partner'),('76b17163-a167-11e9-a57c-000c295f0f58','ENTIDADES EDUCATIVAS COLABORADORAS','2019-07-08 12:02:15','2019-07-08 12:02:15','college');
INSERT INTO `entities_types` VALUES ('32d6306a-aa02-11e9-a553-000c295f0f58','ENTIDADES PATROCINADORAS COLABORADORAS','partner','2019-07-08 12:02:15','2019-07-08 12:02:15'),('76b17163-a167-11e9-a57c-000c295f0f58','ENTIDADES EDUCATIVAS COLABORADORAS','college','2019-07-08 12:02:15','2019-07-08 12:02:15');
/*!40000 ALTER TABLE `entities_types` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
@ -34,4 +34,4 @@ UNLOCK TABLES;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2019-07-10 13:10:42
-- Dump completed on 2019-07-19 10:52:18