<commit_before>/*
Passport-Live is a modern web app for schools that helps them manage passes.
    Copyright (C) 2017  <NAME>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

email: <EMAIL>
*/
/**
 * A set of Apis for importing large amounts of data into passport
 * @module api/import
 */
var express = require("express");
var config = require("config");
var cors = require("cors");
var router = express.Router();
const importJS = require("../../modules/passport-api/import.js");
var ssarv = require("ssarv");
var passport = require("passport");
var db = require("../../modules/db/index.js");
let utils = require("../../modules/passport-utils/index.js");

router.use(cors());
router.options("*", cors());

/**
 * Searches the bulk log database 
 * @link module:api/import
 * @function searchBulkImport
 * @api GET /api/import/log
 * @apiquery {(String|undefined)} name - Bulk Log Name
 * @apiquery {(String|undefined)} type - importType. Current values: "account", "schedule" 
 * @apiquery {(String|undefined)} from - ISO Strng Low end.  inclusive
 * @apiquery {(String|undefined)} to - ISO Strng High end. inclusive
 * @apiresponse {Object[]}
 */
router.get("/log", passport.authenticate("jwt", {
    session: false
}), utils.middlewarePermission(["administrator"]), function searchBulkImport(req, res, next) {
    importJS.searchBulkLogs({
        name: req.query.name,
        type: req.query.type,
        date: {
            from: req.query.from,
            to: req.query.to
        }
    }).then((logs) => {
        res.json(logs);
    }).catch((err) => {
        return next(err);
    });

});




/** 
 * Takes in an array of account json objects and imports them 
 * NOTE: Email domains are still must follow userGroup settings.
 * If the json object lacks the nessessary values to create an account, the row is skipped 
 * @link module:api/import
 * @function importJsonAccounts
 * @api POST /api/import/accounts
 * @param {Object} req.body - Include these in the body
 * @param {accountImport[]} req.body.accounts - The accountImport objects 
 * @param {String} req.body.importName - a (non unique) name for this import job
 * @apibody {Object} req.body
 * @apiresponse {Object[]} - Array of objects with key "account" containing the user imported, and key "error" with an error that occured during import for that user 
 */

router.post("/accounts", passport.authenticate("jwt", {
    session: false
}), utils.middlewarePermission(["administrator"]), function importJsonAccounts(req, res, next) {
    importJS.accounts.json(req.body.accounts, req.body.importName).then((trans) => {
        return res.json(trans);
    }).catch((err) => {
        return next(err);
    });
});

/** 
 * Sends activation emails to all unverified accounts
 * @link module:api/import
 * @function activate
 * @api PATCH /api/import/accounts/:bulkID/activate
 * @apiparam {String} bulkID - The id of the bulk import log.
 * @apiresponse {Object} - A 202 accepted will be returned 
 */

router.post("/accounts/:bulkID/activate", passport.authenticate("jwt", {
    session: false
}), utils.middlewarePermission(["administrator"]), function activate(req, res, next) {
    if (typeof req.params.bulkID !== "string") {
        let err = new Error("Not Found");
        err.status = 404;
        return next(err);
    }
    importJS.accounts.sendActivation(req.params.bulkID).then((trans) => {
        return res.json(trans);
    }).catch((err) => {
        return next(err);
    });
});



/** 
 * Undos a bulk import for accounts.  This will delete records and cannot be undone.
 * @link module:api/import
 * @function rollback
 * @api delete /api/import/accounts/:bulkID/rollback
 * @apiquery {Boolean} [ignoreVerified=true] - Will leave accounts that have been verified untouched
 * @apiparam {String} bulkID - The id of the bulk import log.
 * @apiresponse {Object} - The transaction statement
 */

router.delete("/accounts/:bulkID/rollback", passport.authenticate("jwt", {
    session: false
}), utils.middlewarePermission(["administrator"]), function rollback(req, res, next) {
    if (typeof req.params.bulkID !== "string") {
        let err = new Error("Not Found");
        err.status = 404;
        return next(err);
    }
    if (req.query.ignoreVerified === "false") {
        req.query.ignoreVerified = false;
    } else {
        req.query.ignoreVerified = true;
    }
    importJS.accounts.rollback(req.params.bulkID, req.query.ignoreVerified).then((trans) => {
        return res.json(trans);<commit_msg>Allcaps DELETE<commit_after>/*
Passport-Live is a modern web app for schools that helps them manage passes.
    Copyright (C) 2017  <NAME>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

email: hi<EMAIL>
*/
/**
 * A set of Apis for importing large amounts of data into passport
 * @module api/import
 */
var express = require("express");
var config = require("config");
var cors = require("cors");
var router = express.Router();
const importJS = require("../../modules/passport-api/import.js");
var ssarv = require("ssarv");
var passport = require("passport");
var db = require("../../modules/db/index.js");
let utils = require("../../modules/passport-utils/index.js");

router.use(cors());
router.options("*", cors());

/**
 * Searches the bulk log database 
 * @link module:api/import
 * @function searchBulkImport
 * @api GET /api/import/log
 * @apiquery {(String|undefined)} name - Bulk Log Name
 * @apiquery {(String|undefined)} type - importType. Current values: "account", "schedule" 
 * @apiquery {(String|undefined)} from - ISO Strng Low end.  inclusive
 * @apiquery {(String|undefined)} to - ISO Strng High end. inclusive
 * @apiresponse {Object[]}
 */
router.get("/log", passport.authenticate("jwt", {
    session: false
}), utils.middlewarePermission(["administrator"]), function searchBulkImport(req, res, next) {
    importJS.searchBulkLogs({
        name: req.query.name,
        type: req.query.type,
        date: {
            from: req.query.from,
            to: req.query.to
        }
    }).then((logs) => {
        res.json(logs);
    }).catch((err) => {
        return next(err);
    });

});




/** 
 * Takes in an array of account json objects and imports them 
 * NOTE: Email domains are still must follow userGroup settings.
 * If the json object lacks the nessessary values to create an account, the row is skipped 
 * @link module:api/import
 * @function importJsonAccounts
 * @api POST /api/import/accounts
 * @param {Object} req.body - Include these in the body
 * @param {accountImport[]} req.body.accounts - The accountImport objects 
 * @param {String} req.body.importName - a (non unique) name for this import job
 * @apibody {Object} req.body
 * @apiresponse {Object[]} - Array of objects with key "account" containing the user imported, and key "error" with an error that occured during import for that user 
 */

router.post("/accounts", passport.authenticate("jwt", {
    session: false
}), utils.middlewarePermission(["administrator"]), function importJsonAccounts(req, res, next) {
    importJS.accounts.json(req.body.accounts, req.body.importName).then((trans) => {
        return res.json(trans);
    }).catch((err) => {
        return next(err);
    });
});

/** 
 * Sends activation emails to all unverified accounts
 * @link module:api/import
 * @function activate
 * @api PATCH /api/import/accounts/:bulkID/activate
 * @apiparam {String} bulkID - The id of the bulk import log.
 * @apiresponse {Object} - A 202 accepted will be returned 
 */

router.post("/accounts/:bulkID/activate", passport.authenticate("jwt", {
    session: false
}), utils.middlewarePermission(["administrator"]), function activate(req, res, next) {
    if (typeof req.params.bulkID !== "string") {
        let err = new Error("Not Found");
        err.status = 404;
        return next(err);
    }
    importJS.accounts.sendActivation(req.params.bulkID).then((trans) => {
        return res.json(trans);
    }).catch((err) => {
        return next(err);
    });
});



/** 
 * Undos a bulk import for accounts.  This will delete records and cannot be undone.
 * @link module:api/import
 * @function rollback
 * @api DELETE /api/import/accounts/:bulkID/rollback
 * @apiquery {Boolean} [ignoreVerified=true] - Will leave accounts that have been verified untouched
 * @apiparam {String} bulkID - The id of the bulk import log.
 * @apiresponse {Object} - The transaction statement
 */

router.delete("/accounts/:bulkID/rollback", passport.authenticate("jwt", {
    session: false
}), utils.middlewarePermission(["administrator"]), function rollback(req, res, next) {
    if (typeof req.params.bulkID !== "string") {
        let err = new Error("Not Found");
        err.status = 404;
        return next(err);
    }
    if (req.query.ignoreVerified === "false") {
        req.query.ignoreVerified = false;
    } else {
        req.query.ignoreVerified = true;
    }
    importJS.accounts.rollback(req.params.bulkID, req.query.ignoreVerified).then((trans) => {
        return res.json(trans);
{'fraction_non_alphanumeric': 0.13224267211997273, 'fraction_numerical': 0.0027266530334014998, 'mean_word_length': 3.4229112833763997, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3265793', 'n_tokens_mistral': 3072, 'n_tokens_neox': 2983, 'n_words': 976}