<commit_before>import mongoose from 'mongoose';
import register from './register';
import {
  getSession, authorizedForDomainOrSelf, requireSession,
  requireDomainAdministratorOrUser,
} from './auth';
import { unsetDomainIfNeeded } from './domains';
import { catcher, getPostData } from './utils';

mongoose.Promise = global.Promise;

// /api/payments

const deletePaymentRelated = (doc) => {
  const Form = mongoose.model('Form');
  Form.update(
    { paymentIds: doc._id },
    { $pull: { formIds: doc._id } },
  ).exec()
  .then(() => doc);
};

const canAdmin = (session, payment = {}) => (
  session.userId.administrator || (payment.domainId &&
    payment.domainId.equals(session.userId.administratorDomainId))
);

export default function (router) {
  register(router, {
    category: 'payments',
    modelName: 'Payment',
    omit: ['get', 'post', 'put', 'delete'], // special handling below
    index: {
      filterAuthorized: authorizedForDomainOrSelf,
      populate: [
        { path: 'userId', select: 'name' },
      ],
    },
  });

  router.get('/payments/:id', (req, res) => {
    getSession(req)
    .then(requireSession)
    // get payment
    .then((session) => {
      const id = req.params.id;
      const Payment = mongoose.model('Payment');
      return Payment.findOne({ _id: id })
      .populate({ path: 'userId', select: 'name' })
      .exec()
      .then(payment => ({ payment, session }));
    })
    // authorize
    .then((context) => {
      const { payment } = context;
      return requireDomainAdministratorOrUser(
        context, payment.domainId, payment.userId);
    })
    .then(context => res.status(200).json(context.payment))
    .catch(error => catcher(error, res));
  });

  router.post('/payments', (req, res) => {
    getSession(req)
    .then(requireSession)
    .then(session => getPostData(req).then(data => ({ session, data })))
    // .then((context) => {
    //   const { data } = context;
    //   const FormTemplate = mongoose.model('FormTemplate');
    //   return FormTemplate.findOne({ _id: data.formTemplateId }).exec()
    //   .then(formTemplate => ({ formTemplate, ...context }));
    // })
    .then((context) => {
      const { data, session } = context;
      if (canAdmin(session) && data.userId) {
        const User = mongoose.model('User');
        return User.findOne({ _id: data.userId }).exec()
        .then(user => ({ user, ...context }));
      }
      return context;
    })
    .then((context) => {
      const { data, session, user } = context;
      const Payment = mongoose.model('Payment');
      data.created = new Date();
      data.modified = data.created;
      if (!data.sent) {
        data.sent = data.created;
      }
      // Allow an administrator to set the userId. Otherwise, set it to
      // the current session user
      if (!data.userId || !canAdmin(session)) {
        data.userId = session.userId;
      }
      const useUser = user || session;
      data.name = useUser.name || useUser.email;
      const payment = new Payment(data);
      return payment.save().then(doc => ({ doc, ...context }));
    })
    .then((context) => {
      const { data, doc } = context;
      // update forms to record payment
      const Form = mongoose.model('Form');
      const formIds = data.formIds;
      const promises = [];
      formIds.forEach((formId) => {
        promises.push(Form.findOne({ _id: formId }).exec()
        .then((form) => {
          form.paymentIds.push(doc._id);
          return form.save();
        }));
      });
      return Promise.all(promises).then(() => doc);
    })
    .then(doc => res.status(200).send(doc))
    .catch(error => catcher(error, res));
  });

  router.put('/payments/:id', (req, res) => {
    getSession(req)
    .then(requireSession)
    // get prior payment
    .then((session) => {
      const id = req.params.id;
      const Payment = mongoose.model('Payment');
      return Payment.findOne({ _id: id }).exec()
      .then(payment => ({ payment, session }));
    })
    // authorize
    .then((context) => {
      const { payment } = context;
      return requireDomainAdministratorOrUser(
        context, payment.domainId, payment.userId);
    })
    .then((context) => {
      const { payment, session } = context;
      if (canAdmin(session, payment) && payment.userId) {
        const User = mongoose.model('User');
        return User.findOne({ _id: payment.userId }).exec()
        .then(user => ({ user, ...context }));
      }
      return context;
    })
    .then((context) => {
      const { session, payment, user } = context;
      let data = unsetDomainIfNeeded(req.body);
      // Allow an administrator to set the userId. Otherwise, set it to
      // the current session user
      if (!data.userId || !canAdmin(session, payment)) {
        data.userId = session.userId;
      }
      const useUser = user || session;
      data.name = useUser.name || useUser.email;
      data.modified = new Date();
      data = unsetDomainIfNeeded(data, session);
      return payment.update(data);
    })
    .then(doc => res.status(200).json(doc))
    .catch(error => catcher(error, res));
  });

  router.delete('/payments/:id', (req, res) => {
    getSession(req)
    .then(requireSession)
    // get prior payment
    .then((session) => {
      const id = req.params.id;
      const Payment = mongoose.model('Payment');
      return Payment.findOne({ _id: id }).exec()
      .then(payment => ({ payment, session }));
    })
    // authorize
    .then((context) => {
      const { payment } = context;<commit_msg>Fixed an issue with editing payment user.<commit_after>import mongoose from 'mongoose';
import register from './register';
import {
  getSession, authorizedForDomainOrSelf, requireSession,
  requireDomainAdministratorOrUser,
} from './auth';
import { unsetDomainIfNeeded } from './domains';
import { catcher, getPostData } from './utils';

mongoose.Promise = global.Promise;

// /api/payments

const deletePaymentRelated = (doc) => {
  const Form = mongoose.model('Form');
  Form.update(
    { paymentIds: doc._id },
    { $pull: { formIds: doc._id } },
  ).exec()
  .then(() => doc);
};

const canAdmin = (session, payment = {}) => (
  session.userId.administrator || (payment.domainId &&
    payment.domainId.equals(session.userId.administratorDomainId))
);

export default function (router) {
  register(router, {
    category: 'payments',
    modelName: 'Payment',
    omit: ['get', 'post', 'put', 'delete'], // special handling below
    index: {
      filterAuthorized: authorizedForDomainOrSelf,
      populate: [
        { path: 'userId', select: 'name' },
      ],
    },
  });

  router.get('/payments/:id', (req, res) => {
    getSession(req)
    .then(requireSession)
    // get payment
    .then((session) => {
      const id = req.params.id;
      const Payment = mongoose.model('Payment');
      return Payment.findOne({ _id: id })
      .populate({ path: 'userId', select: 'name' })
      .exec()
      .then(payment => ({ payment, session }));
    })
    // authorize
    .then((context) => {
      const { payment } = context;
      return requireDomainAdministratorOrUser(
        context, payment.domainId, payment.userId);
    })
    .then(context => res.status(200).json(context.payment))
    .catch(error => catcher(error, res));
  });

  router.post('/payments', (req, res) => {
    getSession(req)
    .then(requireSession)
    .then(session => getPostData(req).then(data => ({ session, data })))
    // .then((context) => {
    //   const { data } = context;
    //   const FormTemplate = mongoose.model('FormTemplate');
    //   return FormTemplate.findOne({ _id: data.formTemplateId }).exec()
    //   .then(formTemplate => ({ formTemplate, ...context }));
    // })
    .then((context) => {
      const { data, session } = context;
      if (canAdmin(session) && data.userId) {
        const User = mongoose.model('User');
        return User.findOne({ _id: data.userId }).exec()
        .then(user => ({ user, ...context }));
      }
      return context;
    })
    .then((context) => {
      const { data, session, user } = context;
      const Payment = mongoose.model('Payment');
      data.created = new Date();
      data.modified = data.created;
      if (!data.sent) {
        data.sent = data.created;
      }
      // Allow an administrator to set the userId. Otherwise, set it to
      // the current session user
      if (!data.userId || !canAdmin(session)) {
        data.userId = session.userId;
      }
      const useUser = user || session;
      data.name = useUser.name || useUser.email;
      const payment = new Payment(data);
      return payment.save().then(doc => ({ doc, ...context }));
    })
    .then((context) => {
      const { data, doc } = context;
      // update forms to record payment
      const Form = mongoose.model('Form');
      const formIds = data.formIds;
      const promises = [];
      formIds.forEach((formId) => {
        promises.push(Form.findOne({ _id: formId }).exec()
        .then((form) => {
          form.paymentIds.push(doc._id);
          return form.save();
        }));
      });
      return Promise.all(promises).then(() => doc);
    })
    .then(doc => res.status(200).send(doc))
    .catch(error => catcher(error, res));
  });

  router.put('/payments/:id', (req, res) => {
    getSession(req)
    .then(requireSession)
    .then(session => getPostData(req).then(data => ({ session, data })))
    // get prior payment
    .then((context) => {
      const id = req.params.id;
      const Payment = mongoose.model('Payment');
      return Payment.findOne({ _id: id }).exec()
      .then(payment => ({ payment, ...context }));
    })
    // authorize
    .then((context) => {
      const { payment } = context;
      return requireDomainAdministratorOrUser(
        context, payment.domainId, payment.userId);
    })
    .then((context) => {
      const { data, payment, session } = context;
      if (canAdmin(session, payment) && data.userId) {
        const User = mongoose.model('User');
        return User.findOne({ _id: data.userId }).exec()
        .then(user => ({ user, ...context }));
      }
      return context;
    })
    .then((context) => {
      const { data, session, payment, user } = context;
      // Allow an administrator to set the userId. Otherwise, set it to
      // the current session user
      if (!data.userId || !canAdmin(session, payment)) {
        data.userId = session.userId;
      }
      const useUser = user || session;
      data.name = useUser.name || useUser.email;
      data.modified = new Date();
      return payment.update(unsetDomainIfNeeded(data, session));
    })
    .then(doc => res.status(200).json(doc))
    .catch(error => catcher(error, res));
  });

  router.delete('/payments/:id', (req, res) => {
    getSession(req)
    .then(requireSession)
    // get prior payment
    .then((session) => {
      const id = req.params.id;
      const Payment = mongoose.model('Payment');
      return Payment.findOne({ _id: id }).exec()
      .then(payment => ({ payment, session }));
    })
    // authorize
    .then((context) => {
      const { payment } = context;
{'fraction_non_alphanumeric': 0.16894854586129754, 'fraction_numerical': 0.001610738255033557, 'mean_word_length': 2.5266645629536133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '318235', 'n_tokens_mistral': 3353, 'n_tokens_neox': 3399, 'n_words': 770}