123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- const ObjectID = require("mongodb").ObjectID;
- const jwt = require('jsonwebtoken')
- const jwtSecret = 'CbymrfGfnB'
- const expressJwt = require('express-jwt');
- const anonResolvers = ['login', 'createUser'];
- ;(async () => {
- const {Savable, slice, getModels} = await require('./models.js')('cb')
- class User extends Savable {
- static get relations(){
- return {
- events: "users"
- }
- }
- }
- Savable.addClass(User)
- const express = require('express');
- const express_graphql = require('express-graphql');
- const { buildSchema } = require('graphql');
- const schema = buildSchema(`
- type Query {
- login(login: String!, password: String!): String
- events: [Event]
- event(eventId: String!): Event
- eventMoneysByEvent(eventId: String!): [EventMoney]
- eventMoneys: [EventMoney]
- }
- type Mutation {
- createUser(login: String!, password: String!): User
- changePassword(password: String!): User
- createEvent(name: String!, total: Float!):Event
- changeEvent(eventId:String!, name: String!, total: Float!):Event
- createEventMoney(eventId: String!, amount: Float):EventMoney
- }
- type User {
- _id: String
- login: String
- nick : String
- moneyEvents: [EventMoney]
- }
- type Event {
- _id: String
- name: String
- moneyEvents: [EventMoney]
- owner: String,
- total: Float,
- usersSum: Float,
- moneyDiff: Float,
- avg: Float
- }
- type EventMoney {
- _id: String
- user: User
- event: Event
- amount: Float
- owner: String
- avgDiff: Float
- }
- `);
- var app = express();
- app.use(express.static('public'));
- const rootResolvers = {
- createUser:async function ({login, password}){
- let user = await Savable.m.User.findOne({login, password})
- if (user)
- return null;
- user = await (new User({login, password})).save()
- user.___owner = user._id.toString()
- user.___permissions = {
- read: ["owner", "user"]
- }
- return await user.save()
- },
- login: async function({login, password}){
- const user = await Savable.m.User.findOne({login, password})
- if (!user)
- return null;
- const token = jwt.sign({ sub: {id: user._id, login}}, jwtSecret); //подписывам токен нашим ключем
- return token
- },
- changePassword:async function ({password}, {jwt: {id}, models: {SlicedSavable, User}} ){
- id = new ObjectID(id)
- const user = await SlicedSavable.m.User.findOne({_id: id})
- if (!user)
- return null;
- user.password = password;
- return await user.save()
- },
- async createEvent({name, total}, {jwt: {id}, models: {Event}}){
- return await (new Event({name, total})).save()
- },
- async changeEvent({eventId, name, total}, {jwt: {id}, models: {SlicedSavable}}){
- const event = await SlicedSavable.m.Event.findOne({_id: ObjectID(eventId)})
- if (!event)
- return null;
- event.name = name
- event.total = total
- return await event.save()
- },
- async events({}, {jwt: {id}, models: {SlicedSavable}}){
- let events = []
- for (let event of SlicedSavable.m.Event.find({})){
- event = await event
- //const {_id, name, owner, moneyEvents, usersSum, moneyDiff, total} = event
- //event = {_id, name, owner, moneyEvents, usersSum: await event.usersSum, total}
- //console.log(event)
- events.push(event)
- }
- return events;
- },
- async event({eventId}, {jwt: {id}, models: {SlicedSavable}}){
- return await SlicedSavable.m.Event.findOne({_id: ObjectID(eventId)});
- },
- async createEventMoney({eventId, amount}, {jwt: {id}, models: {SlicedSavable, EventMoney}}){
- let eventMoney = await SlicedSavable.m.EventMoney.findOne({"user._id": ObjectID(id), "event._id": ObjectID(eventId)})
- if (eventMoney){
- eventMoney.amount = amount;
- return await eventMoney.save()
- }
- const event = await SlicedSavable.m.Event.findOne({_id: ObjectID(eventId)})
- if (!event)
- return null;
- const me = await SlicedSavable.m.User.findOne({_id: ObjectID(id)})
- if (!me)
- return null;
- return await (new EventMoney({user: me, event, amount})).save()
- },
- async eventMoneysByEvent({eventId}, {jwt: {id}, models: {SlicedSavable}}){
- const event = await SlicedSavable.m.Event.findOne({_id: ObjectID(eventId)});
- if (!event)
- return null
- const moneys = [];
- for (let money of event.moneyEvents){
- try {
- await money
- console.log(money)
- }
- catch (e){
- console.log('skip no access user' ,e)
- }
- moneys.push(money)
- console.log('in loop')
- }
- console.log('after loop')
- return moneys;
- },
- async eventMoneys({}, {jwt: {id}, models: {SlicedSavable}}){
- const me = await SlicedSavable.m.User.findOne({_id: ObjectID(id)});
- if (!me)
- return null
- const moneys = [];
- for (let money of me.moneyEvents){
- moneys.push(await money)
- }
- return moneys;
- },
- }
- app.use('/graphql', express_graphql(async (req, res, gql) => {
- if (gql.operationName === null || anonResolvers.includes(gql.operationName))
- return {
- schema: schema,
- rootValue: rootResolvers,
- graphiql: true,
- }
-
- const authorization = req.headers.authorization
- if (authorization && authorization.startsWith('Bearer ')){
- console.log('token provided')
- const token = authorization.substr("Bearer ".length)
- const decoded = jwt.verify(token, jwtSecret)
- if (decoded){
- console.log('token verified', decoded)
- let slicedModels = getModels(decoded.sub.id)
- return {
- schema: schema,
- rootValue: rootResolvers,
- graphiql: true,
- context: {jwt: decoded.sub,
- models: slicedModels}
- }
- }
- }
- }))
- app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
- })()
|