Example #1
0
 seedData.forEach(document => {
   newMutation({
     action: 'movies.new',
     collection: Movies,
     document: document, 
     currentUser: currentUser,
     validate: false
   });
 });
Example #2
0
const createUser = function (username, email) {
  const user = {
    username,
    email,
    isDummy: true
  };
  newMutation({
    collection: Users, 
    document: user,
    validate: false
  });
}
Example #3
0
    
    name: 'postsNew',
    
    check(user, document) {
      if (!user) return false;
      return Users.canDo(user, 'posts.new');
    },
    
    mutation(root, {document}, context) {
      
      performCheck(this, context.currentUser, document);

      return newMutation({
        collection: context.Posts,
        document: document, 
        currentUser: context.currentUser,
        validate: true,
        context,
      });
    },

  },

  edit: {
    
    name: 'postsEdit',
    
    check(user, document) {
      if (!user || !document) return false;
      return Users.owns(user, document) ? Users.canDo(user, 'posts.edit.own') : Users.canDo(user, `posts.edit.all`);
    },
Example #4
0
export const createCharge = async (args) => {
  
  let collection, document, returnDocument = {};

  const {token, userId, productKey, associatedCollection, associatedId, properties } = args;

  if (!stripeSettings) {
    throw new Error('Please fill in your Stripe settings');
  }
  
  // initialize Stripe
  const keySecret = stripeSettings.secretKey;
  const stripe = new Stripe(keySecret);

  // if an associated collection name and document id have been provided, 
  // get the associated collection and document
  if (associatedCollection && associatedId) {
    collection = _.findWhere(Collections, {_name: associatedCollection});
    document = collection.findOne(associatedId);
  }

  // get the product from Products (either object or function applied to doc)
  // or default to sample product
  const definedProduct = Products[productKey];
  const product = typeof definedProduct === 'function' ? definedProduct(document) : definedProduct || sampleProduct;

  // get the user performing the transaction
  const user = Users.findOne(userId);

  // create Stripe customer
  const customer = await stripe.customers.create({
    email: token.email,
    source: token.id
  });

  // create Stripe charge
  const charge = await stripe.charges.create({
    amount: product.amount,
    description: product.description,
    currency: product.currency,
    customer: customer.id,
    metadata: {
      userId: userId,
      userName: Users.getDisplayName(user),
      userProfile: Users.getProfileUrl(user, true),
      ...properties
    }
  });

  // create charge document for storing in our own Charges collection
  const chargeDoc = {
    createdAt: new Date(),
    userId,
    tokenId: token.id, 
    type: 'stripe',
    test: !token.livemode,
    data: charge,
    ip: token.client_ip,
    properties,
    productKey,
  }

  // insert
  const chargeSaved = newMutation({
    collection: Charges,
    document: chargeDoc, 
    validate: false,
  });

  // if an associated collection and id have been provided, 
  // update the associated document
  if (collection && document) {
    
    const chargeIds = document.chargeIds ? [...document.chargeIds, chargeSaved._id] : [chargeSaved._id];

    let modifier = {
      $set: {chargeIds},
      $unset: {}
    }
    // run collection.charge.sync callbacks
    modifier = runCallbacks(`${collection._name}.charge.sync`, modifier, document, chargeDoc);

    returnDocument = editMutation({
      collection,
      documentId: associatedId,
      set: modifier.$set,
      unset: modifier.$unset,
      validate: false
    });

    returnDocument.__typename = collection.typeName;

  }

  runCallbacksAsync(`${collection._name}.charge.async`, returnDocument, chargeDoc);

  return returnDocument;
}