<commit_before>"use strict";

const mongoose = require('mongoose');
const express = require('express');
const cel = require('connect-ensure-login');
const router = express.Router();
const email = require('../lib/email');
const log = require('../lib/logger');
const libInvoice = require('../lib/invoice');
const libFmt = require('../lib/fmt');
const libPerm = require('../lib/permissions');

const Invoice = mongoose.models.Invoice;
const InvoicingOrg = mongoose.models.InvoicingOrg;
const Team = mongoose.models.Team;

module.exports = router;

router.param('id', async function (req, res, next){
    const invoiceId = req.params.id;
    let inv;
    // if user not logged in
    if (!req.user)
        req.user = {
            isAdmin: false,
            isInvoicingManager: false,
            locales:libFmt.defaultLocales
        };

    try {
        inv = await Invoice.findById(invoiceId);
        req.invoice = inv;
        if (!inv)
            throw new Error("invoice not found");

        log.DEBUG("Invoice id="+req.invoice.id+" num="+req.invoice.number+" iorg="+req.invoice.invoicingOrg);

        try {

            const p = await libPerm.getUserInvoicePermissions(req.user.id, inv.id);
            req.user.permissions = p;

            console.log("PERM=",p);
            console.log("USER=",req.user);

        } catch (err) {
            log.WARN("Failed fetching user permissions. err="+err.message);
        }

        next();
    } catch (err) {
        res.render('message',{title:"Faktúra nenájdná",error:{}});
    }

});

router.get('/', async function (req, res, next) {
    console.log("/invoice/ - get");

    req.user.permissions = await libPerm.getUserInvoicePermissions(req.user.id, null);

    next();

});


router.get('/:id', async function (req, res, next) {
    const siteUrl = req.protocol + '://' + req.get("host");
    const cmd = req.query.cmd;
    console.log("/invoice/ID - get");

    if (cmd)
        next();
    else {
        let i = await Team.populate(req.invoice,'team');
        res.render('invoice', {inv: req.invoice, siteUrl: siteUrl, user: req.user, fmt:libFmt, PageTitle:req.invoice.number} );
    }
});

router.get('/', cel.ensureLoggedIn('/login'), async function (req, res, next) {
    const cmd = req.query.cmd;
    const teamId = req.query.teamId;
    const invType = req.query.type;
    const invOrg = req.query.invOrg;
    const invYear = req.query.year;
    const isPaid = req.query.isPaid;

    console.log("/invoice - get (with CMD)");
    console.log(req.query);

    const r = {result:"error", status:200, error:{}};

    try {

        let p = await libPerm.getUserTeamPermissions(req.user.id, teamId);
<commit_msg>added team name to invoice title<commit_after>"use strict";

const mongoose = require('mongoose');
const express = require('express');
const cel = require('connect-ensure-login');
const router = express.Router();
const email = require('../lib/email');
const log = require('../lib/logger');
const libInvoice = require('../lib/invoice');
const libFmt = require('../lib/fmt');
const libPerm = require('../lib/permissions');

const Invoice = mongoose.models.Invoice;
const InvoicingOrg = mongoose.models.InvoicingOrg;
const Team = mongoose.models.Team;

module.exports = router;

router.param('id', async function (req, res, next){
    const invoiceId = req.params.id;
    let inv;
    // if user not logged in
    if (!req.user)
        req.user = {
            isAdmin: false,
            isInvoicingManager: false,
            locales:libFmt.defaultLocales
        };

    try {
        inv = await Invoice.findById(invoiceId);
        req.invoice = inv;
        if (!inv)
            throw new Error("invoice not found");

        log.DEBUG("Invoice id="+req.invoice.id+" num="+req.invoice.number+" iorg="+req.invoice.invoicingOrg);

        try {

            const p = await libPerm.getUserInvoicePermissions(req.user.id, inv.id);
            req.user.permissions = p;

            console.log("PERM=",p);
            console.log("USER=",req.user);

        } catch (err) {
            log.WARN("Failed fetching user permissions. err="+err.message);
        }

        next();
    } catch (err) {
        res.render('message',{title:"Faktúra nenájdná",error:{}});
    }

});

router.get('/', async function (req, res, next) {
    console.log("/invoice/ - get");

    req.user.permissions = await libPerm.getUserInvoicePermissions(req.user.id, null);

    next();

});


router.get('/:id', async function (req, res, next) {
    const siteUrl = req.protocol + '://' + req.get("host");
    const cmd = req.query.cmd;
    console.log("/invoice/ID - get");

    if (cmd)
        next();
    else {
        let i = await Team.populate(req.invoice,'team');
        res.render('invoice', {inv: req.invoice, siteUrl: siteUrl, user: req.user, fmt:libFmt, PageTitle:req.invoice.number+' ('+req.invoice.team.name+')'} );
    }
});

router.get('/', cel.ensureLoggedIn('/login'), async function (req, res, next) {
    const cmd = req.query.cmd;
    const teamId = req.query.teamId;
    const invType = req.query.type;
    const invOrg = req.query.invOrg;
    const invYear = req.query.year;
    const isPaid = req.query.isPaid;

    console.log("/invoice - get (with CMD)");
    console.log(req.query);

    const r = {result:"error", status:200, error:{}};

    try {

        let p = await libPerm.getUserTeamPermissions(req.user.id, teamId);

{'fraction_non_alphanumeric': 0.16395864106351551, 'fraction_numerical': 0.0011078286558345643, 'mean_word_length': 2.7513850415512464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2056856', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1749, 'n_words': 384}