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

describe('collection getSales()', () => {
    const collectionOrTicketFk = 999999;
    const print = true;
    const source = 'CHECKER';
    const ctx = beforeAll.getCtx();

    it('should return a collection with tickets, placements and barcodes settled correctly', async() => {
        const tx = await models.Collection.beginTransaction({});
        const options = {transaction: tx};
        try {
            const collection = await models.Collection.getSales(ctx,
                collectionOrTicketFk, print, source, options);

            const [firstTicket] = collection.tickets;
            const [firstSale] = firstTicket.sales;
            const [firstPlacement] = firstSale.placements;

            expect(collection.tickets.length).toBeTruthy();
            expect(collection.collectionFk).toEqual(firstTicket.ticketFk);

            expect(firstSale.ticketFk).toEqual(firstTicket.ticketFk);
            expect(firstSale.placements.length).toBeTruthy();
            expect(firstSale.barcodes.length).toBeTruthy();

            expect(firstSale.saleFk).toEqual(firstPlacement.saleFk);
            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            throw e;
        }
    });

    it('should print a sticker', async() => {
        const tx = await models.Collection.beginTransaction({});
        const options = {transaction: tx};
        const query = 'SELECT * FROM printQueue pq JOIN printQueueArgs pqa ON pqa.printQueueFk = pq.id';
        try {
            const printQueueBefore = await models.Collection.rawSql(
                query, [], options);
            await models.Collection.getSales(ctx,
                collectionOrTicketFk, true, source, options);
            const printQueueAfter = await models.Collection.rawSql(
                query, [], options);

            expect(printQueueAfter.length).toEqual(printQueueBefore.length + 1);
            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            throw e;
        }
    });
});