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

describe('sale usesMana()', () => {
    const ctx = {req: { accessToken: {userId: 18}}};

    it('should return that the worker uses mana', async() => {
        const tx = await models.Sale.beginTransaction({});

        try {
            const options = {transaction: tx};
            const teamOne = 96;
            const userId = ctx.req.accessToken.userId;

            const business = await models.Business.findOne({where: {workerFk: userId}}, options);
            await business.updateAttribute('departmentFk', teamOne, options);

            const usesMana = await models.Sale.usesMana(ctx, options);

            expect(usesMana).toBe(true);

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

    it('should return that the worker not uses mana', async() => {
        const tx = await models.Sale.beginTransaction({});

        try {
            const options = {transaction: tx};

            const usesMana = await models.Sale.usesMana(ctx, options);

            expect(usesMana).toBe(false);

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

    it('should return that the worker does not use mana because it is excluded', async() => {
        const tx = await models.Sale.beginTransaction({});
        const buyerId = 35;
        const franceDepartmentId = 133;
        const buyerCtx = {req: {accessToken: {userId: buyerId}}};

        try {
            const options = {transaction: tx}

            await models.WorkerManaExcluded.create({workerFk: buyerId}, options);
            await models.Business.updateAll({workerFk: buyerId}, {departmentFk: franceDepartmentId}, options);

            const usesMana = await models.Sale.usesMana(buyerCtx, options);

            expect(usesMana).toBe(false);

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