diff --git a/helpers/composes.helper.js b/helpers/composes.helper.js index 27d2621..4164213 100644 --- a/helpers/composes.helper.js +++ b/helpers/composes.helper.js @@ -13,6 +13,22 @@ const citiesComposer = (city) => { else return city; } +const locationComposer = (location, context) => { + if (location) { + + let multimedias = []; + if ((context.scopes) && (context.scopes.includes('includeMultimedias'))) { + multimedias = multimediaComposer(location.multimedias); + }; + + return { + ...location, + multimedias: multimedias, + }; + } + else return location; +} + const commentComposer = (comment, context) => { if (comment.user) { comment.user_name = comment.user.name + '---'; @@ -170,5 +186,6 @@ module.exports = { speakerComposer, eventComposer, commentComposer, - citiesComposer + citiesComposer, + locationComposer } \ No newline at end of file diff --git a/modules/events/event.model.js b/modules/events/event.model.js index 63d52f2..300ef72 100644 --- a/modules/events/event.model.js +++ b/modules/events/event.model.js @@ -205,7 +205,7 @@ module.exports = function (sequelize, DataTypes) { foreignKey: 'overflow_eventId', required: false }); - Event.Type = Event.belongsTo(models.EventType, { foreignKey: 'typeId', as: "type" }); + Event.Type = Event.belongsTo(models.EventType, { foreignKey: 'typeId', as: "type" }); Event.UserCreate = Event.belongsTo(models.User, { foreignKey: 'userId', as: "user" }); Event.Venue = Event.belongsTo(models.Venue, { foreignKey: 'venueId', as: "venue", required: false, @@ -224,6 +224,13 @@ module.exports = function (sequelize, DataTypes) { required: false, }); + Event.Location = Event.hasOne(models.Location, { + foreignKey: ['city', 'country'], + sourcekey: ['city', 'country'], + as: "location", + required: false, + }); + Event.Multimedias = Event.hasMany(models.Multimedia, { foreignKey: 'entityId', as: { singular: 'multimedia', plural: 'multimedias' }, @@ -252,6 +259,14 @@ module.exports = function (sequelize, DataTypes) { } }); + Event.addScope('includeLocation', () => { + return { + include: [ + { model: sequelize.models.Location, as: 'location', foreignKey: ['city', 'country'], sourcekey: ['city','country'] } + ] + } + }); + Event.addScope('includeMultimedias', () => { return { include: [{ diff --git a/modules/events/event.routes.js b/modules/events/event.routes.js index 6296870..8520783 100644 --- a/modules/events/event.routes.js +++ b/modules/events/event.routes.js @@ -72,7 +72,7 @@ routes.get('/events/past', cacheSuccesses('24 hours'), PaginateMiddleware.middleware(), SortMiddleware.middleware({ default: "-init_date" }), eventController.find({ - scopes: ['defaultScope', 'past', 'includeVenue', 'includeMultimedias', 'includeDetails'], + scopes: ['defaultScope', 'past', 'includeVenue', 'includeMultimedias', 'includeDetails'], //, 'includeLocation'], }), ); diff --git a/modules/events/event.service.js b/modules/events/event.service.js index 59d2e1d..6c20613 100644 --- a/modules/events/event.service.js +++ b/modules/events/event.service.js @@ -17,12 +17,12 @@ const extraMethods = { } let rows = result.rows.map(row => row.toJSON()); - +/* if (context.scopes.includes('CitiesOfEvents')) rows = rows.map(city => citiesComposer(city, context)) else rows = rows.map(event => eventComposer(event, context)); - +*/ return { count: result.count, rows: rows diff --git a/modules/locations/locations.controller.js b/modules/locations/locations.controller.js new file mode 100644 index 0000000..49f5881 --- /dev/null +++ b/modules/locations/locations.controller.js @@ -0,0 +1,14 @@ +'use strict'; + +const generateControllers = require('../../core/controllers'); +const locationService = require('./locations.service'); + + +// Module Name +const MODULE_NAME = '[locations.controller]'; + +const controllerOptions = { MODULE_NAME }; +const extraControllers = {}; + +module.exports = generateControllers(locationService, extraControllers, controllerOptions); + diff --git a/modules/locations/locations.model.js b/modules/locations/locations.model.js new file mode 100644 index 0000000..4872913 --- /dev/null +++ b/modules/locations/locations.model.js @@ -0,0 +1,55 @@ +const Sequelize = require('sequelize'); + +module.exports = function (sequelize, DataTypes) { + const Location = sequelize.define('Location', { + id: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + primaryKey: true, + }, + country: { + type: DataTypes.STRING(125), + allowNull: false + }, + city: { + type: DataTypes.STRING(125), + allowNull: false + }, + description: { + type: DataTypes.STRING, + }, + }, { + tableName: 'locations', + freezeTableName: true, + timestamps: true, + }); + + Location.associate = function (models) { + Location.Events = Location.hasMany(models.Event, { as: 'events', foreignKey: ['city', 'country'], sourcekey: ['city', 'country'] }); + + //OJO antes de force comentar + // OJO GENERA UN FOREIGN KEY Con eventos y habrá ID de otras entidades que no exitan en la tabla eventos, porque son post o speakers + Location.Multimedias = Location.hasMany(models.Multimedia, { + foreignKey: 'entityId', + as: { singular: 'multimedia', plural: 'multimedias' }, + }); + }; + + Location.addScope('includeMultimedias', () => { + return { + include: [{ + model: sequelize.models.Multimedia, + as: { singular: 'multimedia', plural: 'multimedias' }, + required: false, + include: [{ + model: sequelize.models.MultimediaFile, + as: "multimediaFile" + }] + }, + ] + } + }); + + + return Location; +}; \ No newline at end of file diff --git a/modules/locations/locations.routes.js b/modules/locations/locations.routes.js new file mode 100644 index 0000000..3f1c8d4 --- /dev/null +++ b/modules/locations/locations.routes.js @@ -0,0 +1,32 @@ +const routes = require('express').Router(); + +const { isAdministratorUser, isLoggedUser } = require('../../middlewares/accessValidator'); +const SchemaValidator = require('../../middlewares/schemaValidator'); + +//const PaginateMiddleware = require('../../middlewares/paginate'); +//const FieldMiddleware = require('../../middlewares/fields'); +const SortMiddleware = require('../../middlewares/sort'); +const locationController = require('./locations.controller'); + +routes.get('/locations', + isLoggedUser, +// SortMiddleware.middleware({ default: "city" }), + locationController.find({ + scopes: ['includeMultimedias'] + }) +); + +routes.get('/locations/:id', + isLoggedUser, + // SortMiddleware.middleware({ default: "city" }), + locationController.findOne({ + scopes: ['includeMultimedias'] + }) +); + +/******************************************************************************************************** + * ADMINISTRACIÓN + ********************************************************************************************************* + */ + +module.exports = routes; \ No newline at end of file diff --git a/modules/locations/locations.service.js b/modules/locations/locations.service.js new file mode 100644 index 0000000..76120b1 --- /dev/null +++ b/modules/locations/locations.service.js @@ -0,0 +1,38 @@ +/* global Venue */ +'use strict'; + +const _ = require('lodash'); +const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper'); +const { locationComposer } = require('../../helpers/composes.helper'); +const models = require('../../core/models'); + +const extraMethods = { + + afterFetchAll: (result, params, context) => { + + if (!result.count) { + return result; + } + + let rows = result.rows.map(row => row.toJSON()); + + if (context.scopes.includes('includeMultimedias')) + rows = rows.map(row => locationComposer(row, context)); + + return { + count: result.count, + rows: rows + } + }, + + afterFetchOne: (result, params, context) => { + + if (result) + result = result.toJSON(); + return locationComposer(result, context); + }, + + +}; + +module.exports = generateService(models.Location, extraMethods); \ No newline at end of file diff --git a/script-carga-evento-THDVI.sql b/script-carga-evento-THDVI.sql index 6ac6ce1..f26dc16 100644 --- a/script-carga-evento-THDVI.sql +++ b/script-carga-evento-THDVI.sql @@ -20,5 +20,38 @@ FROM lqdvi_v2.aux_table + + + +https://vimeo.com/328614321 + + + +**********Insertar multimedia asociado +insert into lqdvi_v2.`multimedia_files` (id, lqdvi_v2.`multimedia_files`.name, description, class, provider, lqdvi_v2.`multimedia_files`.code, +url, userId, CreatedAt, UpdatedAt) + +SELECT UUID() as ID, 'THDVI - 1 Edición Bilbao' , 'Making of 1ª edición', +'video','vimeo', '328614321', 'https://player.vimeo.com/video/328614321', 'ecf7dda2-258c-458d-a41f-de07a9cfb6eb', now(), now() + +SELECT UUID() as ID, 'THDVI - 2 Edición Madrid' , 'Making of 2ª edición', +'video','vimeo', '320555737', 'https://player.vimeo.com/video/320555737', 'ecf7dda2-258c-458d-a41f-de07a9cfb6eb', now(), now() + +FROM lqdvi_v2.aux_table + +insert into lqdvi_v2.multimedias (id, multimediafileId, entityId, entityName, type, createdAt, updatedat) + +select UUID() as ID, 'd7cc286d-c50a-11e9-86a6-000c295f0f58', '0998e0c5-c4ce-11e9-86a6-000c295f0f58', 'event', 'resume', now(), now() +FROM lqdvi_v2.aux_table + + + + + +insert into locations (id, country, city, description, createdat, updatedat) +select UUID() as ID, 'España', 'Madrid', 'En la ciudad de Madrid empezamos nuestra andadura', now(), now() +from aux_table + +