app2-api/helpers/composes.helper.js
2019-08-27 13:08:04 +02:00

191 lines
5.1 KiB
JavaScript

'use strict';
const cdnHelper = require('./cdn.helper');
const valuesComposer = (values) => (values) ? values.map(value => ({id: value.id, name: value.name,})): values;
const citiesComposer = (city) => {
if (city) {
return {
...city,
image: cdnHelper.getCDNCityMediaUrl(city.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 + '---';
comment.profile_picture = cdnHelper.getCDNMediaUrl(comment.user.profile_picture);
}else {
comment.user_name = comment.nameUserOld;
comment.profile_picture = cdnHelper.getCDNMediaUrl(comment.profile_pictureOld);
}
delete comment.nameUserOld;
delete comment.profile_pictureOld;
delete comment.userId;
delete comment.user;
return Object.assign({},
comment);
};
const commentsComposer = (comments) => {
if (comments) {
return comments.map(comment => ({
...commentComposer(comment)
}));
}
else return comments;
};
const multimediaComposer = (multimedias) => {
if(multimedias) {
return multimedias.map(multimedia => ({
...multimedia,
...multimedia.multimediaFile,
type: multimedia.type,
media_type: multimedia.multimediaFile.type,
multimediaFile: undefined,
createdAt: undefined,
updatedAt: undefined,
userId: undefined,
url: (multimedia.multimediaFile.provider === 'cdn') ? cdnHelper.getCDNMediaUrl(multimedia.multimediaFile.url) : multimedia.multimediaFile.url,
}));
}
else
return multimedias;
};
const socialNetworksComposer = (speaker) => {
if (speaker) {
return {
rrss: {
twitter: speaker.twitter ? speaker.twitter : null,
facebook: speaker.facebook ? speaker.facebook : null,
youtube: speaker.youtube ? speaker.youtube : null,
linkedin: speaker.linkedin ? speaker.linkedin : null,
instagram: speaker.instagram ? speaker.instagram : null,
web: speaker.web ? speaker.web : null
},
twitter: undefined,
facebook: undefined,
youtube: undefined,
linkedin: undefined,
instagram: undefined,
web: undefined
};
}
else
return speaker;
};
const speakerComposer = (speaker, context) => {
let multimedias = [];
if ((context.scopes) && (context.scopes.includes('includeMultimedias'))) {
multimedias = multimediaComposer(speaker.multimedias);
};
let comments = [];
if ((context.scopes) && (context.scopes.includes('includeComments'))) {
comments = commentsComposer(speaker.comments);
};
if (speaker.type) {
speaker.typename = speaker.type.name;
delete speaker.type;
}
speaker.values = valuesComposer(speaker.values);
const rrss = socialNetworksComposer(speaker, context);
return Object.assign({},
speaker,
rrss,
{ multimedias: multimedias },
{ comments: comments },
)
};
const eventComposer = (event, context) => {
if ((context.scopes) && (context.scopes.includes('includeVenue'))){
if (event.venue) {
delete event.venue.updatedAt;
delete event.venue.createdAt;
//event.venue.image_url = cdnHelper.getCDNCityMediaUrl(event.venue.city); <-- se hace en el modelo
}
};
let multimedias = []
if ((context.scopes) && (context.scopes.includes('includeMultimedias'))) {
multimedias = multimediaComposer(event.multimedias)
};
let speakers = []
let details = []
if ((context.scopes) && (context.scopes.includes('includeDetails'))) {
event.details.map((detail) => {
if (detail.type == 'speaker')
speakers.push({
order: detail.order,
...speakerComposer(detail.speaker, context),
});
if (detail.type == 'info')
details.push({
...detail,
speaker: undefined,
});
});
};
return Object.assign({},
event,
{ multimedias: multimedias },
{ details: details },
{ speakers: speakers }
)
};
const entityComposer = (entity, context) => {
delete entity.contact_person;
delete entity.contact_email;
return Object.assign({},
entity,
)
};
module.exports = {
entityComposer,
speakerComposer,
eventComposer,
commentComposer,
citiesComposer,
locationComposer
}