Example #1
0
export const mutateItem = function (collection, originalItem, user, operation) {
  const newItem = operateOnItem(collection, originalItem, user, operation, false);
  newItem.inactive = false;

  collection.update({_id: newItem._id}, newItem, {bypassCollection2:true});

  // --------------------- Server-Side Async Callbacks --------------------- //
  runCallbacksAsync(operation+".async", newItem, user, collection, operation);

  return newItem;
}
Example #2
0
Newsletters.send = async (isTest = false) => {

  const newsletterEmail = VulcanEmail.emails.newsletter;
  const email = await VulcanEmail.build({ emailName: 'newsletter', variables: {terms: {view: 'newsletter'}}});
  const { subject, html, data } = email;
  const text = VulcanEmail.generateTextVersion(html);

  if(newsletterEmail.isValid(data)){

    // eslint-disable-next-line no-console
    console.log('// Sending newsletter…');
    // eslint-disable-next-line no-console
    console.log('// Subject: '+subject);

    const newsletter = Newsletters[provider].send({ subject, text, html, isTest });

    // if newsletter sending is successufl and this is not a test, mark posts as sent and log newsletter
    if (newsletter && !isTest) {

      runCallbacksAsync('newsletter.send.async', email);

      const createdAt = new Date();

      // log newsletter
      await Connectors.create(Newsletters, {
        createdAt,
        subject,
        html,
        provider,
        data,
      });

      // send confirmation email
      const confirmationHtml = VulcanEmail.getTemplate('newsletterConfirmation')({
        time: createdAt.toString(),
        newsletterLink: newsletter.archive_url,
        subject: subject
      });
      VulcanEmail.send(getSetting('defaultEmail'), 'Newsletter scheduled', VulcanEmail.buildTemplate(confirmationHtml));

    }

  } else {

    // eslint-disable-next-line no-console
    console.log('No newsletter to schedule today…');
  
  }
}
// ----------------------- posts.new.async --------------------------------- //
// ----------------------- comments.new.async ------------------------------ //

/**
 * @summary Run the "upvote.async" callbacks *once* the item exists in the database
 * @param {object} item - The item being operated on
 * @param {object} user - The user doing the operation
 * @param {object} collection - The collection the item belongs to
 */
function UpvoteAsyncCallbacksAfterDocumentInsert(item, user, collection) {
  runCallbacksAsync("upvote.async", item, user, collection, 'upvote');
}
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;
}