mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 01:51:40 -05:00
Use connectors and await/async
This commit is contained in:
parent
a899532c4f
commit
270c6b4215
7 changed files with 50 additions and 41 deletions
|
@ -4,9 +4,11 @@ Default mutations
|
|||
|
||||
*/
|
||||
|
||||
import { registerCallback, newMutator, editMutator, removeMutator, Utils } from 'meteor/vulcan:lib';
|
||||
import { registerCallback, newMutator, editMutator, removeMutator, Utils, Connectors, getSetting } from 'meteor/vulcan:lib';
|
||||
import Users from 'meteor/vulcan:users';
|
||||
|
||||
const database = getSetting('database', 'mongo');
|
||||
|
||||
export const getDefaultMutations = (collectionName, options = {}) => {
|
||||
|
||||
// register callbacks for documentation purposes
|
||||
|
@ -78,7 +80,7 @@ export const getDefaultMutations = (collectionName, options = {}) => {
|
|||
const collection = context[collectionName];
|
||||
|
||||
// get entire unmodified document from database
|
||||
const document = collection.findOne(documentId);
|
||||
const document = await Connectors[database].get(collection, documentId);
|
||||
|
||||
// check if user can perform operation; if not throw error
|
||||
Utils.performCheck(this.check, context.currentUser, document);
|
||||
|
@ -107,7 +109,7 @@ export const getDefaultMutations = (collectionName, options = {}) => {
|
|||
const collection = context[collectionName];
|
||||
|
||||
// check if document exists already
|
||||
const existingDocument = collection.findOne(search, { fields: { _id: 1 } });
|
||||
const existingDocument = await Connectors[database].get(collection, search, { fields: { _id: 1 } });
|
||||
|
||||
if (existingDocument) {
|
||||
const editArgs = {
|
||||
|
@ -143,7 +145,7 @@ export const getDefaultMutations = (collectionName, options = {}) => {
|
|||
|
||||
const collection = context[collectionName];
|
||||
|
||||
const document = collection.findOne(documentId);
|
||||
const document = await Connectors[database].get(collection, documentId);
|
||||
Utils.performCheck(this.check, context.currentUser, document, context);
|
||||
|
||||
return await removeMutator({
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import Newsletters from "../modules/collection.js";
|
||||
import Users from 'meteor/vulcan:users';
|
||||
import { addGraphQLMutation, addGraphQLResolvers, Utils } from 'meteor/vulcan:core';
|
||||
import { addGraphQLMutation, addGraphQLResolvers, Utils, getSetting, Connectors } from 'meteor/vulcan:core';
|
||||
|
||||
const database = getSetting('database', 'mongo');
|
||||
|
||||
addGraphQLMutation('sendNewsletter : JSON');
|
||||
addGraphQLMutation('testNewsletter : JSON');
|
||||
|
@ -10,38 +12,38 @@ addGraphQLMutation('removeUserNewsletter(userId: String) : JSON');
|
|||
|
||||
const resolver = {
|
||||
Mutation: {
|
||||
sendNewsletter(root, args, context) {
|
||||
async sendNewsletter(root, args, context) {
|
||||
if(context.currentUser && Users.isAdminById(context.currentUser._id)) {
|
||||
return Newsletters.send();
|
||||
return await Newsletters.send();
|
||||
} else {
|
||||
throw new Error(Utils.encodeIntlError({id: "app.noPermission"}));
|
||||
}
|
||||
},
|
||||
testNewsletter(root, args, context) {
|
||||
async testNewsletter(root, args, context) {
|
||||
if(context.currentUser && Users.isAdminById(context.currentUser._id))
|
||||
return Newsletters.send(true);
|
||||
return await Newsletters.send(true);
|
||||
},
|
||||
addUserNewsletter(root, {userId}, context) {
|
||||
async addUserNewsletter(root, {userId}, context) {
|
||||
|
||||
const currentUser = context.currentUser;
|
||||
const user = Users.findOne({_id: userId});
|
||||
const user = await Connectors[database].get(Users, userId);
|
||||
if (!user || !Users.options.mutations.edit.check(currentUser, user)) {
|
||||
throw new Error(Utils.encodeIntlError({id: "app.noPermission"}));
|
||||
}
|
||||
return Newsletters.subscribeUser(user, false);
|
||||
return await Newsletters.subscribeUser(user, false);
|
||||
},
|
||||
addEmailNewsletter(root, {email}, context) {
|
||||
return Newsletters.subscribeEmail(email, true);
|
||||
async addEmailNewsletter(root, {email}, context) {
|
||||
return await Newsletters.subscribeEmail(email, true);
|
||||
},
|
||||
removeUserNewsletter(root, { userId }, context) {
|
||||
async removeUserNewsletter(root, { userId }, context) {
|
||||
const currentUser = context.currentUser;
|
||||
const user = Users.findOne({_id: userId});
|
||||
const user = await Connectors[database].get(Users, userId);
|
||||
if (!user || !Users.options.mutations.edit.check(currentUser, user)) {
|
||||
throw new Error(Utils.encodeIntlError({id: "app.noPermission"}));
|
||||
}
|
||||
|
||||
try {
|
||||
return Newsletters.unsubscribeUser(user);
|
||||
return await Newsletters.unsubscribeUser(user);
|
||||
} catch (error) {
|
||||
const errorMessage = error.message.includes('subscription-failed') ? Utils.encodeIntlError({id: "newsletter.subscription_failed"}) : error.message
|
||||
throw new Error(errorMessage);
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import { debug, debugGroup, debugGroupEnd, getSetting, registerSetting, newMutation, editMutation, Collections, registerCallback, runCallbacks, runCallbacksAsync } from 'meteor/vulcan:core';
|
||||
import { webAppConnectHandlersUse, debug, debugGroup, debugGroupEnd, getSetting, registerSetting, newMutation, editMutation, Collections, registerCallback, runCallbacks, runCallbacksAsync, Connectors } from 'meteor/vulcan:core';
|
||||
import express from 'express';
|
||||
import Stripe from 'stripe';
|
||||
import Charges from '../../modules/charges/collection.js';
|
||||
import Users from 'meteor/vulcan:users';
|
||||
import { Products } from '../../modules/products.js';
|
||||
import { webAppConnectHandlersUse } from 'meteor/vulcan:core';
|
||||
import { Promise } from 'meteor/promise';
|
||||
|
||||
const database = getSetting('database', 'mongo');
|
||||
|
||||
registerSetting('stripe', null, 'Stripe settings');
|
||||
registerSetting('stripe.publishableKey', null, 'Publishable key', true);
|
||||
registerSetting('stripe.publishableKeyTest', null, 'Publishable key (test)', true);
|
||||
|
@ -48,7 +49,7 @@ export const performAction = async (args) => {
|
|||
// get the associated collection and document
|
||||
if (associatedCollection && associatedId) {
|
||||
collection = _.findWhere(Collections, {_name: associatedCollection});
|
||||
document = collection.findOne(associatedId);
|
||||
document = await Connectors[database].get(collection, associatedId);
|
||||
}
|
||||
|
||||
// get the product from Products (either object or function applied to doc)
|
||||
|
@ -57,7 +58,7 @@ export const performAction = async (args) => {
|
|||
const product = typeof definedProduct === 'function' ? definedProduct(document) : definedProduct || sampleProduct;
|
||||
|
||||
// get the user performing the transaction
|
||||
const user = Users.findOne(userId);
|
||||
const user = await Connectors[database].get(Users, userId);
|
||||
|
||||
// create metadata object
|
||||
let metadata = {
|
||||
|
@ -177,7 +178,7 @@ export const processCharge = async ({collection, document, charge, args, user})
|
|||
// make sure charge hasn't already been processed
|
||||
// (could happen with multiple endpoints listening)
|
||||
|
||||
const existingCharge = Charges.findOne({ 'data.id': charge.id });
|
||||
const existingCharge = await Connectors[database].get(Charges, { 'data.id': charge.id });
|
||||
|
||||
if (existingCharge) {
|
||||
// eslint-disable-next-line no-console
|
||||
|
@ -438,7 +439,7 @@ app.post('/stripe', async function(req, res) {
|
|||
|
||||
if (associatedCollection && associatedId) {
|
||||
const collection = _.findWhere(Collections, {_name: associatedCollection});
|
||||
const document = collection.findOne(associatedId);
|
||||
const document = await Connectors[database].get(collection, associatedId);
|
||||
|
||||
// make sure document actually exists
|
||||
if (!document) {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import { newMutation, editMutation, removeMutation, Utils } from 'meteor/vulcan:lib';
|
||||
import { newMutation, editMutation, removeMutation, Utils, Connectors, getSetting } from 'meteor/vulcan:lib';
|
||||
import Users from './collection'; // circular dependency?
|
||||
|
||||
const database = getSetting('database', 'mongo');
|
||||
|
||||
const performCheck = (mutation, user, document) => {
|
||||
if (!mutation.check(user, document)) throw new Error(Utils.encodeIntlError({id: `app.mutation_not_allowed`, value: `"${mutation.name}" on _id "${document._id}"`}));
|
||||
};
|
||||
|
@ -40,9 +42,9 @@ const mutations = {
|
|||
return Users.owns(user, document) ? Users.canDo(user, 'users.edit.own') : Users.canDo(user, `users.edit.all`);
|
||||
},
|
||||
|
||||
mutation(root, {documentId, set, unset}, context) {
|
||||
async mutation(root, {documentId, set, unset}, context) {
|
||||
|
||||
const document = context.Users.findOne(documentId);
|
||||
const document = await Connectors[database].get(context.Users, documentId);
|
||||
performCheck(this, context.currentUser, document);
|
||||
|
||||
return editMutation({
|
||||
|
@ -67,9 +69,9 @@ const mutations = {
|
|||
return Users.owns(user, document) ? Users.canDo(user, 'users.remove.own') : Users.canDo(user, `users.remove.all`);
|
||||
},
|
||||
|
||||
mutation(root, {documentId}, context) {
|
||||
async mutation(root, {documentId}, context) {
|
||||
|
||||
const document = context.Users.findOne(documentId);
|
||||
const document = await Connectors[database].get(context.Users, documentId);
|
||||
performCheck(this, context.currentUser, document);
|
||||
|
||||
return removeMutation({
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
import { addGraphQLResolvers } from 'meteor/vulcan:lib';
|
||||
import { addGraphQLResolvers, Connectors, getSetting } from 'meteor/vulcan:lib';
|
||||
|
||||
const database = getSetting('database');
|
||||
|
||||
const specificResolvers = {
|
||||
Query: {
|
||||
currentUser(root, args, context) {
|
||||
async currentUser(root, args, context) {
|
||||
let user = null;
|
||||
if (context && context.userId) {
|
||||
user = context.Users.findOne(context.userId);
|
||||
user = await Connectors[database].get(context.Users, context.userId);
|
||||
|
||||
if (user.services) {
|
||||
Object.keys(user.services).forEach((key) => {
|
||||
|
@ -26,13 +28,13 @@ const resolvers = {
|
|||
|
||||
name: 'UsersList',
|
||||
|
||||
async resolver(root, {terms}, {currentUser, Users}, info) {
|
||||
async resolver(root, { terms = {} }, {currentUser, Users}, info) {
|
||||
|
||||
// get selector and options from terms and perform Mongo query
|
||||
let {selector, options} = await Users.getParameters(terms);
|
||||
options.limit = (terms.limit < 1 || terms.limit > 100) ? 100 : terms.limit;
|
||||
options.skip = terms.offset;
|
||||
const users = Users.find(selector, options).fetch();
|
||||
const users = await Connectors[database].find(Users, selector, options);
|
||||
|
||||
// restrict documents fields
|
||||
const restrictedUsers = Users.restrictViewableFields(currentUser, Users, users);
|
||||
|
@ -49,9 +51,9 @@ const resolvers = {
|
|||
|
||||
name: 'UsersSingle',
|
||||
|
||||
async resolver(root, {documentId, slug}, {currentUser, Users}) {
|
||||
async resolver(root, { documentId, slug }, {currentUser, Users}) {
|
||||
// don't use Dataloader if user is selected by slug
|
||||
const user = documentId ? await Users.loader.load(documentId) : (slug ? Users.findOne({slug}): Users.findOne());
|
||||
const user = documentId ? await Users.loader.load(documentId) : (slug ? await Connectors[database].get(Users, {slug}): await Connectors[database].get(Users));
|
||||
return Users.restrictViewableFields(currentUser, Users, user);
|
||||
},
|
||||
|
||||
|
@ -61,9 +63,9 @@ const resolvers = {
|
|||
|
||||
name: 'UsersTotal',
|
||||
|
||||
async resolver(root, {terms}, context) {
|
||||
const {selector} = await context.Users.getParameters(terms);
|
||||
return context.Users.find(selector).count();
|
||||
async resolver(root, { terms = {} }, { Users }) {
|
||||
const {selector} = await Users.getParameters(terms);
|
||||
return await Connectors[database].count(Users, selector);
|
||||
},
|
||||
|
||||
}
|
||||
|
|
|
@ -272,8 +272,8 @@ const schema = {
|
|||
order: 60,
|
||||
resolveAs: {
|
||||
type: 'String',
|
||||
resolver: (user, args, { Users }) => {
|
||||
return Users.getTwitterName(Users.findOne(user._id));
|
||||
resolver: async (user, args, { Users }) => {
|
||||
return Users.getTwitterName(await Connectors[database].get(Users, user._id));
|
||||
},
|
||||
},
|
||||
onInsert: user => {
|
||||
|
|
|
@ -280,7 +280,7 @@ return an updated document without performing any database operations on it.
|
|||
export const performVoteServer = async ({ documentId, document, voteType = 'upvote', collection, voteId, user, updateDocument = true }) => {
|
||||
|
||||
const collectionName = collection.options.collectionName;
|
||||
document = document || collection.findOne(documentId);
|
||||
document = document || await Connectors[database].get(collection, documentId);
|
||||
|
||||
debug('');
|
||||
debugGroup(`--------------- start \x1b[35mperformVoteServer\x1b[0m ---------------`);
|
||||
|
|
Loading…
Add table
Reference in a new issue