Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const moment = require('moment')
- const Album = require("../../mongoose/collections/album")
- const Feed = require("../../mongoose/collections/feed")
- const Photosession = require("../../mongoose/collections/photosession")
- const Specialization = require("../../mongoose/collections/specialization")
- const Story = require("../../mongoose/collections/stories")
- const User = require("../../mongoose/collections/user")
- const apiModels = {
- album: require('../../api-models/mobile/album'),
- city: require('../../api-models/mobile/city'),
- feed_section: require('../../api-models/mobile/feed-section'),
- offer: require('../../api-models/mobile/offer'),
- photosession: require('../../api-models/mobile/photosession'),
- short_photosession: require('../../api-models/mobile/short-photosession'),
- short_story: require('../../api-models/mobile/short-story'),
- short_user: require('../../api-models/mobile/short-user'),
- story: require('../../api-models/mobile/story'),
- specialization: require('../../api-models/mobile/specialization'),
- }
- const Validators = require('../../modules/validators')
- const mongoose = require('../../mongoose/modules');
- const config = require('../../config/app.json');
- exports.GetFeed = async (ctx) => {
- const specialization = ctx.request.query.specialization;
- let query = {}
- if(Validators.object_id(specialization)){
- const specExists = await Specialization.findById(specialization).exec()
- if(!specExists){
- ctx.body = {
- error: "Invalid specialization"
- }
- return ctx.status = 400
- }
- query.specialization = specialization
- }
- let feedResponse = await Feed.find({
- status: 'ACTIVE'
- }).exec()
- for(let feed of feedResponse){
- if(query.specialization){
- let index = Array.isArray(feed.by_specialization) ? feed.by_specialization.findIndex(doc => {
- return String(doc.specialization) === String(specialization)
- }) : -1
- feed.content = index !== -1 ? feed.by_specialization[i].items : []
- }
- else{
- feed.content = feed.items
- }
- if(feed.type === "USERS"){
- feed.content = await User.find({
- _id: {$in: feed.content}
- })
- .populate(['city', 'avatar'])
- .exec()
- for(let contentObj of feed.content){
- contentObj.photosessions = await contentObj.getSuccessfullPhotosessions()
- }
- }
- if(feed.type === "ALBUMS"){
- feed.content = await Album.find({
- _id: {$in: feed.content}
- })
- .populate(['images', 'specialization'])
- .exec()
- }
- }
- feedResponse = feedResponse.filter(doc => {
- return doc.content.length > 0
- }).sort((a,b) => {
- if(!a.position)
- return 1
- if(!b.position)
- return 1
- return a.position - b.position
- })
- return ctx.body = feedResponse.map(doc => apiModels.feed_section(ctx, doc))
- }
- exports.GetStories = async (ctx) => {
- let stories = await Story.find({})
- .populate('cover')
- .exec()
- return ctx.body = stories.map(doc => apiModels.short_story(ctx,doc))
- }
- exports.GetSingleStory = async (ctx) => {
- let id = cxt.params.id;
- if(!Validators.object_id(id)){
- ctx.body = {
- error: "Invalid id"
- }
- return ctx.status = 400
- }
- let story = await Story.findById(id)
- .populate(['cover', 'slides'])
- .exec()
- if(!story){
- return ctx.status = 404
- }
- return ctx.body = apiModels.story(ctx, story)
- }
- exports.GetSingleAlbum = async (ctx) => {
- let id = ctx.params.id;
- if(!Validators.object_id(id)){
- ctx.body = {
- error: "Invalid id"
- }
- return ctx.status = 400
- }
- let album = await Album.findOne({
- _id: id,
- visibility: "PUBLIC"
- })
- .populate('images')
- .populate('specialization')
- .populate('participants')
- .populate([
- {
- path: 'participants', populate: { path: 'avatar' }
- },
- {
- path: 'participants', populate: { path: 'city' }
- }
- ])
- .exec()
- if (!album) {
- ctx.body = {
- error: "Album not found"
- }
- return ctx.status = 404
- }
- let sessionExists = await Photosession.findOne({
- album: album._id
- }).select({
- participants_types: 1
- })
- .exec()
- if(sessionExists){
- album.participants_types = sessionExists.participants_types
- }
- return ctx.body = apiModels.album(ctx, album)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement