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

describe('claimBeginning', () => {
    const claimManagerId = 72;
    const activeCtx = {
        accessToken: {userId: claimManagerId},
    };
    const ctx = {req: activeCtx};

    describe('importToNewRefundTicket()', () => {
        it('should create a new ticket with negative sales and insert the negative sales into claimEnd', async() => {
            spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
                active: activeCtx
            });
            let claimId = 1;

            const tx = await models.Entry.beginTransaction({});
            try {
                const options = {transaction: tx};

                const ticket = await models.ClaimBeginning.importToNewRefundTicket(ctx, claimId, options);

                const refundTicketSales = await models.Sale.find({
                    where: {ticketFk: ticket.id}
                }, options);
                const salesInsertedInClaimEnd = await models.ClaimEnd.find({
                    where: {claimFk: claimId}
                }, options);

                expect(refundTicketSales.length).toEqual(1);
                expect(refundTicketSales[0].quantity).toEqual(-5);
                expect(salesInsertedInClaimEnd[0].saleFk).toEqual(refundTicketSales[0].id);

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