Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. const moment = require('moment')
  2.  
  3. const Album = require("../../mongoose/collections/album")
  4. const Feed = require("../../mongoose/collections/feed")
  5. const Photosession = require("../../mongoose/collections/photosession")
  6. const Specialization = require("../../mongoose/collections/specialization")
  7. const Story = require("../../mongoose/collections/stories")
  8. const User = require("../../mongoose/collections/user")
  9.  
  10. const apiModels = {
  11. album: require('../../api-models/mobile/album'),
  12. city: require('../../api-models/mobile/city'),
  13. feed_section: require('../../api-models/mobile/feed-section'),
  14. offer: require('../../api-models/mobile/offer'),
  15. photosession: require('../../api-models/mobile/photosession'),
  16. short_photosession: require('../../api-models/mobile/short-photosession'),
  17. short_story: require('../../api-models/mobile/short-story'),
  18. short_user: require('../../api-models/mobile/short-user'),
  19. story: require('../../api-models/mobile/story'),
  20. specialization: require('../../api-models/mobile/specialization'),
  21. }
  22.  
  23. const Validators = require('../../modules/validators')
  24. const mongoose = require('../../mongoose/modules');
  25. const config = require('../../config/app.json');
  26.  
  27. exports.GetFeed = async (ctx) => {
  28. const specialization = ctx.request.query.specialization;
  29.  
  30. let query = {}
  31.  
  32. if(Validators.object_id(specialization)){
  33. const specExists = await Specialization.findById(specialization).exec()
  34.  
  35. if(!specExists){
  36. ctx.body = {
  37. error: "Invalid specialization"
  38. }
  39. return ctx.status = 400
  40. }
  41.  
  42. query.specialization = specialization
  43. }
  44.  
  45. let feedResponse = await Feed.find({
  46. status: 'ACTIVE'
  47. }).exec()
  48.  
  49. for(let feed of feedResponse){
  50. if(query.specialization){
  51. let index = Array.isArray(feed.by_specialization) ? feed.by_specialization.findIndex(doc => {
  52. return String(doc.specialization) === String(specialization)
  53. }) : -1
  54. feed.content = index !== -1 ? feed.by_specialization[i].items : []
  55. }
  56. else{
  57. feed.content = feed.items
  58. }
  59.  
  60. if(feed.type === "USERS"){
  61. feed.content = await User.find({
  62. _id: {$in: feed.content}
  63. })
  64. .populate(['city', 'avatar'])
  65. .exec()
  66.  
  67. for(let contentObj of feed.content){
  68. contentObj.photosessions = await contentObj.getSuccessfullPhotosessions()
  69. }
  70. }
  71.  
  72. if(feed.type === "ALBUMS"){
  73. feed.content = await Album.find({
  74. _id: {$in: feed.content}
  75. })
  76. .populate(['images', 'specialization'])
  77. .exec()
  78. }
  79. }
  80.  
  81. feedResponse = feedResponse.filter(doc => {
  82. return doc.content.length > 0
  83. }).sort((a,b) => {
  84. if(!a.position)
  85. return 1
  86. if(!b.position)
  87. return 1
  88. return a.position - b.position
  89. })
  90.  
  91. return ctx.body = feedResponse.map(doc => apiModels.feed_section(ctx, doc))
  92. }
  93.  
  94. exports.GetStories = async (ctx) => {
  95.  
  96. let stories = await Story.find({})
  97. .populate('cover')
  98. .exec()
  99.  
  100. return ctx.body = stories.map(doc => apiModels.short_story(ctx,doc))
  101. }
  102.  
  103. exports.GetSingleStory = async (ctx) => {
  104.  
  105. let id = cxt.params.id;
  106.  
  107. if(!Validators.object_id(id)){
  108. ctx.body = {
  109. error: "Invalid id"
  110. }
  111. return ctx.status = 400
  112. }
  113.  
  114. let story = await Story.findById(id)
  115. .populate(['cover', 'slides'])
  116. .exec()
  117.  
  118. if(!story){
  119. return ctx.status = 404
  120. }
  121.  
  122. return ctx.body = apiModels.story(ctx, story)
  123. }
  124.  
  125.  
  126. exports.GetSingleAlbum = async (ctx) => {
  127. let id = ctx.params.id;
  128.  
  129. if(!Validators.object_id(id)){
  130. ctx.body = {
  131. error: "Invalid id"
  132. }
  133. return ctx.status = 400
  134. }
  135.  
  136. let album = await Album.findOne({
  137. _id: id,
  138. visibility: "PUBLIC"
  139. })
  140. .populate('images')
  141. .populate('specialization')
  142. .populate('participants')
  143. .populate([
  144. {
  145. path: 'participants', populate: { path: 'avatar' }
  146. },
  147. {
  148. path: 'participants', populate: { path: 'city' }
  149. }
  150. ])
  151. .exec()
  152.  
  153. if (!album) {
  154. ctx.body = {
  155. error: "Album not found"
  156. }
  157. return ctx.status = 404
  158. }
  159.  
  160. let sessionExists = await Photosession.findOne({
  161. album: album._id
  162. }).select({
  163. participants_types: 1
  164. })
  165. .exec()
  166.  
  167. if(sessionExists){
  168. album.participants_types = sessionExists.participants_types
  169. }
  170.  
  171. return ctx.body = apiModels.album(ctx, album)
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement