const models = require('vn-loopback/server/server').models;

describe('AgencyTerm createInvoiceIn()', () => {
    const ctx = beforeAll.getCtx();
    const rows = [
        {
            routeFk: 2,
            supplierFk: 1,
            created: '2022-03-02T23:00:00.000Z',
            totalPrice: 165
        }
    ];
    const dms = [
        {
            id: 6
        }
    ];

    it('should make an invoiceIn', async() => {
        const tx = await models.AgencyTerm.beginTransaction({});
        const options = {transaction: tx};

        try {
            const invoiceInId = 10;
            const invoiceInDueDayId = 11;
            const invoiceInTaxId = 12;

            const oldInvoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options);
            const oldInvoiceInDueDay = await models.InvoiceInDueDay.findById(invoiceInDueDayId, null, options);
            const oldInvoiceInTax = await models.InvoiceInTax.findById(invoiceInTaxId, null, options);

            await models.AgencyTerm.createInvoiceIn(ctx, rows, dms, options);

            const [newInvoiceIn] = await models.InvoiceIn.rawSql('SELECT MAX(id) id FROM invoiceIn', null, options);

            const dueDayQuery = 'SELECT MAX(id) id FROM invoiceInDueDay';
            const [newInvoiceInDueDay] = await models.InvoiceInDueDay.rawSql(dueDayQuery, null, options);

            const taxQuery = 'SELECT MAX(id) id FROM invoiceInTax';
            const [newInvoiceInTax] = await models.InvoiceInTax.rawSql(taxQuery, null, options);

            expect(newInvoiceIn.id).toBeGreaterThan(oldInvoiceIn.id);
            expect(newInvoiceInDueDay.id).toBeGreaterThan(oldInvoiceInDueDay.id);
            expect(newInvoiceInTax.id).toBeGreaterThan(oldInvoiceInTax.id);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            throw e;
        }
    });
});