-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolvers.js
195 lines (177 loc) · 6.05 KB
/
resolvers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const { response } = require('express')
const fetch = require('node-fetch')
const db = require('./db')
const User = require('./models/User')
const Book = require('./models/Book')
const Channel = require('./models/Channel')
const Message = require('./models/Message')
const { PubSub } = require('graphql-subscriptions')
const { withFilter } = require('apollo-server-express')
const MESSAGE_ADDED = 'MESSAGE_ADDED'
const pubSub = new PubSub()
function requireAuth(userId) {
if (!userId) {
throw new Error('Unauthorized');
}
}
const Query = {
messages: async(_root, args, {userId}) => {
//requireAuth(userId);
const { messageList } = await Channel.findById(args.channelId)
.populate({path: 'messageList', Model: Message,
populate: [{
path: 'messageFrom',
Model: User
},{
path: 'messageChannel',
Model: Channel
}]
})
return messageList
},
getUserChannels: async(root, args, {userId}) => {
console.log("USERID" , userId)
const { channels } = await User.findById(userId)
.populate({path: 'channels', Model: Channel,
populate: [{
path: 'channelMembers',
Model: Channel
}, {
path: 'messageList',
Model: Channel,
populate: [{path: 'messageFrom', Model: Message}]
}]
})
console.log("CHANNELS", channels)
return channels
},
getUsers: async (_root, _args, context) => {
console.log(context)
requireAuth(context.userId)
const users = await User.find({}).exec()
return users
},
getBooks: async (root, args, {userId}) => {
const { books_selected, books_rejected, books_snoozed } = await User.findById(userId)
const books_snoozed_ids = books_snoozed.map(book => book.book_id)
const ignore_books = books_selected.concat(books_rejected).concat(books_snoozed_ids)
bookList = await Book
.find({ "_id": { "$nin": ignore_books } })
.sort([['bestsellers_date', -1]])
.limit(10)
console.log(bookList)
return bookList
},
getSelectedBooks: async (_root, {userId}) => {
const { books_selected } = await User.findById(userId).populate('books_selected')
return books_selected
},
getUserFriends: async (_root, {userId}) => {
const { friends } = await User.findById(userId).populate('friends')
return friends
}
}
const Mutation = {
addMessage: async(_root, {input}, {userId}) => {
//requireAuth(userId);
console.log(input)
const newMessage = await Message.create({messageFrom: input.messageFrom, messageText: input.messageText, messageChannel: input.messageChannel})
const newMessagePop= await Message.findById(newMessage._id).populate('messageFrom').populate('messageChannel')
const newMessageList = await Channel.updateOne({_id: input.messageChannel}, {$push: {messageList: newMessage._id}})
console.log("newMessage", newMessage)
console.log("newMessageList", newMessageList)
pubSub.publish(MESSAGE_ADDED, {messageChannel: input.messageChannel, messageAdded: newMessagePop})
return newMessagePop
},
addUser: async (_root, args) => {
try {
let response = await User.create(args)
return response
} catch(err) {
return err.message
}
},
selectBook: async (_root, args) => {
try {
let books_selected_update = await User.findOneAndUpdate(
{_id: userId },
{ $push: { books_selected: args.new_book_selected } },
)
return books_selected_update
} catch(err) {
return err.message
}
},
rejectBook: async (_root, args) => {
try {
let books_rejected_update = await User.findOneAndUpdate(
{_id: userId },
{ $push: { books_rejected: args.new_book_rejected } },
)
return books_rejected_update
} catch(err) {
return err.message
}
},
snoozeBook: async (_root, args) => {
try {
let books_snoozed_update = await User.findOneAndUpdate(
{_id: userId },
{ $push: { books_snoozed: {book_id: args.new_book_snoozed, date_created: new Date().toISOString()} } },
)
return books_snoozed_update
} catch(err) {
return err.message
}
},
addBook: async(_root, {input}, context) => {
try {
const response = await fetch('https://api.nytimes.com/svc/books/v3/lists/2010-09-03/hardcover-fiction.json?api-key=rFRBnSgrDocNqeQ8na4HO1oCw6OyAYaX')
const books = await response.json()
//console.log(books.results.bestsellers_date)
const bookList = await books.results.books.map(book => ({
title: book.title,
author: book.author,
book_image: book.book_image,
description: book.description.replace(/\n/g, ''),
primary_isbn13: book.primary_isbn13,
bestsellers_date: books.results.bestsellers_date
}))
const saveBook = async(book) => {
const bookExists = await Book.findOne({primary_isbn13: book.primary_isbn13}).exec()
if (bookExists) {
console.log({...bookExists._doc, status: "Already in database."})
return {...bookExists._doc, status: "Already in database."}
} else {
const savedBook = await Book.create(book)
//console.log(savedBook)
return {...savedBook._doc, status: "Added to database."}
}
}
const savedBookList = async() => {
console.log(Promise.all(bookList.map(book => saveBook(book))))
return Promise.all(bookList.map(book => saveBook(book)))
}
const finalBookList = await savedBookList().then(data => {
//console.log(data)
return data
})
//console.log("final: ", finalBookList)
return finalBookList
} catch(err) {
return err.message
}
}
}
const Subscription = {
messageAdded: {
subscribe: withFilter(
() => pubSub.asyncIterator(MESSAGE_ADDED),
(payload, variables) => {
console.log("PAYLOAD", payload)
return (payload.messageChannel === variables.messageChannel)
}
)
}
}
module.exports = { Query, Mutation, Subscription };