<commit_before>let express = require("express");
let form = require("express-form"), field = form.field;

let user = require("./user");
let models = require("../models"),
    OsuUser = models.OsuUser,
    User = models.User;
let utils = require("../utils");

let router = express.Router();

router.get("/all", (req, res, next) => {
    res.locals.teams = [];
    User.find().then((users) => {
        let teamPromises = [];
        users.forEach((user) => {
            let members = [];
            let memberPromises = [];
            user.team.forEach((member) => {
                let promise = OsuUser.findOne({ usernameLower: member.username.toLowerCase() }).then((ou) => {
                    if (!ou) {
                        ou = { rank: "None", id: -1 };
                    }
                    members.push({
                        username: member.username,
                        rank: ou.rank,
                        id: ou.id
                    });
                    return Promise.resolve();
                });
                memberPromises.push(promise);
            });
            teamPromises.push(Promise.all(memberPromises).then(() => {
                res.locals.teams.push({
                    approved: user.approved,
                    approvalDate: user.approvalDate,
                    captain: user.username,
                    lastUpdated: user.lastUpdated,
                    members: members,
                    teamname: user.teamname
                });
            }));
        });
        return Promise.all(teamPromises);
    }).then(() => {
        res.render("team/all");
    });
});

router.get("/manage", user.requireLogin, (req, res, next) => {
    res.locals.page.csrfToken = req.csrfToken();
    // res.locals.user.checkTeamEligibility().then((checklist) => {
    //     res.locals.user.registrationChecklist = checklist;
    //     res.locals.user.allGood = checklist.filter(x => !x.good).length == 0;
    res.render("team/manage");
});

router.post("/rename", user.requireLogin, form(field("teamname").trim().maxLength(24).required()), (req, res, next) => {
    if (!req.form.isValid) {
        req.flash("danger", "Couldn't update teamname: please enter a teamname.");
        return res.redirect("/team/manage");
    }
    let newTeamname = req.form.teamname;
    User.findOne({ _id: res.locals.user.id }).then((user) => {
        user.teamname = newTeamname;
        user.save().then(() => {<commit_msg>Order teams on /team/all page.<commit_after>let express = require("express");
let form = require("express-form"), field = form.field;

let user = require("./user");
let models = require("../models"),
    OsuUser = models.OsuUser,
    User = models.User;
let utils = require("../utils");

let router = express.Router();

router.get("/all", (req, res, next) => {
    res.locals.teams = [];
    User.find().then((users) => {
        let teamPromises = [];
        users.forEach((user) => {
            let members = [];
            let memberPromises = [];
            user.team.forEach((member) => {
                let promise = OsuUser.findOne({ usernameLower: member.username.toLowerCase() }).then((ou) => {
                    if (!ou) {
                        ou = { rank: "None", id: -1 };
                    }
                    members.push({
                        username: member.username,
                        rank: ou.rank,
                        id: ou.id
                    });
                    return Promise.resolve();
                });
                memberPromises.push(promise);
            });
            teamPromises.push(Promise.all(memberPromises).then(() => {
                members.sort(function (a, b) {
                    if (a.username == b.username) return 0;
                    return a.username > b.username ? 1 : -1;
                });
                res.locals.teams.push({
                    approved: user.approved,
                    approvalDate: user.approvalDate,
                    captain: user.username,
                    lastUpdated: user.lastUpdated,
                    members: members,
                    teamname: user.teamname
                });
            }));
        });
        return Promise.all(teamPromises);
    }).then(() => {
        res.locals.teams.sort(function (a, b) {
            let m = a.approved ? 1 : 0;
            let n = b.approved ? 1 : 0;
            return n - m;
        });
        res.render("team/all");
    });
});

router.get("/manage", user.requireLogin, (req, res, next) => {
    res.locals.page.csrfToken = req.csrfToken();
    // res.locals.user.checkTeamEligibility().then((checklist) => {
    //     res.locals.user.registrationChecklist = checklist;
    //     res.locals.user.allGood = checklist.filter(x => !x.good).length == 0;
    res.render("team/manage");
});

router.post("/rename", user.requireLogin, form(field("teamname").trim().maxLength(24).required()), (req, res, next) => {
    if (!req.form.isValid) {
        req.flash("danger", "Couldn't update teamname: please enter a teamname.");
        return res.redirect("/team/manage");
    }
    let newTeamname = req.form.teamname;
    User.findOne({ _id: res.locals.user.id }).then((user) => {
        user.teamname = newTeamname;
        user.save().then(() => {
{'fraction_non_alphanumeric': 0.16142399698624976, 'fraction_numerical': 0.00282539084573366, 'mean_word_length': 1.6683417085427135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, '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': '1403178', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1473, 'n_words': 277}