const { Router } = require('express');
const { Admin, HackerApplication } = require('js/server/models');
const { createHttpError } = require('./errors');
const reviewLogic = require('js/server/review/logic');

const adminsRouter = new Router();

adminsRouter.get('/:adminId/next-review-application', (req, res, next) => {
  Admin
    .findById(req.params.adminId)
    .then((admin) => {
      if (!admin) {
        next();
        return;
      }

      return reviewLogic
        .getNextApplicationToReviewForAdmin(admin)
        .then((application) => {
          if (!application) {
            res.json({ applicationId: null });
          } else {
            res.json({ applicationId: application.id });
          }
        });
    })
    .catch(next);
});

/**
 * Creates a review for a particluar application, by a particular admin.
 * Request body should contain a key `scores` which is an array of scored criteria:
 *
 * ```
 * {
 *   scores: [
 *     { reviewCriterionId: 1, score: 0 },
 *     { reviewCriterionId: 2, score: 1 },
 *   ],
 * }
 * ```
 */
adminsRouter.post('/:adminId/reviews/:applicationId', (req, res, next) => {
  Promise.all([
    Admin.findById(req.params.adminId),
    HackerApplication.findById(req.params.applicationId),
  ]).then(([admin, hackerApplication]) => {
    if ((!admin) || (!hackerApplication)) {
      next();
      return;
    }

    const reviewCriterionScores = req.body.scores.map(({ reviewCriterionId, score }) => ({
      reviewCriterionId,
      score,
    }));

    return reviewLogic.reviewApplication(admin, hackerApplication, reviewCriterionScores)
      .then((applicationReview) => {
        res.json({
          applicationReview,
        });
      });
  }).catch(next);
})

module.exports = adminsRouter;

@@ -239,16 +239,303 @@
 ter();%0A%0A
+adminsRouter.get('/by-email/:email', (req, res, next) =%3E %7B%0A  Admin%0A    .findOne(%7B%0A      where: %7B%0A        email: req.params.email,%0A      %7D,%0A    %7D)%0A    .then((admin) =%3E %7B%0A      if (!admin) %7B%0A        next();%0A        return;%0A      %7D%0A%0A      res.json(%7B admin %7D);%0A    %7D)%0A    .catch(next);%0A%7D);%0A%0A
 adminsRo

Add an API endpoint to find admin by email