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

describe('ticket setDelivered()', () => {
    const userId = 49;
    const activeCtx = {
        accessToken: {userId: userId},
        __: value => value
    };
    const ctx = {req: activeCtx};

    beforeAll(async() => {
        spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
            active: activeCtx
        });
    });

    it('should return the state which has been applied to the given tickets', async() => {
        const tx = await models.TicketTracking.beginTransaction({});

        try {
            const options = {transaction: tx};

            const originalTicketOne = await models.Ticket.findById(8, null, options);
            const originalTicketTwo = await models.Ticket.findById(10, null, options);

            originalTicketOne.id = null;
            originalTicketTwo.id = null;

            const ticketOne = await models.Ticket.create(originalTicketOne, options);
            const ticketTwo = await models.Ticket.create(originalTicketTwo, options);

            const delivered = await models.State.findOne({where: {code: 'delivered'}, fields: ['id']}, options);

            const params = [ticketOne.id, ticketTwo.id];
            const state = await models.TicketTracking.setDelivered(ctx, params, options);

            expect(state.id).toEqual(delivered.id);

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