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

describe('Notification Clean()', () => {
    it('should delete old rows with error', async() => {
        const userId = 9;
        const status = 'error';
        const tx = await models.NotificationQueue.beginTransaction({});
        const options = {transaction: tx};

        const notification = await models.Notification.findOne({}, options);
        const notificationConfig = await models.NotificationConfig.findOne({});

        const cleanDate = Date.vnNew();
        cleanDate.setDate(cleanDate.getDate() - (notificationConfig.cleanDays + 1));

        let before;
        let after;

        try {
            const notificationDelete = await models.NotificationQueue.create({
                notificationFk: notification.name,
                authorFk: userId,
                status: status,
                created: cleanDate
            }, options);

            before = await models.NotificationQueue.findById(notificationDelete.id, null, options);

            await models.Notification.clean(options);

            after = await models.NotificationQueue.findById(notificationDelete.id, null, options);

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

        expect(before.notificationFk).toEqual(notification.name);
        expect(after).toBe(null);
    });
});