module.exports = Self => {
    Self.remoteMethod('hasActiveRecovery', {
        description: 'Returns a boolean that is true when a client has an active recovery',
        accessType: 'READ',
        accepts: [
            {
                arg: 'clientFk',
                type: 'number',
                required: true,
                description: 'The id of a client',
                http: {source: 'path'}
            }
        ],
        returns: {
            type: 'boolean',
            root: true
        },
        http: {
            path: `/:clientFk/hasActiveRecovery`,
            verb: 'GET'
        }
    });

    Self.hasActiveRecovery = async(id, options) => {
        const myOptions = {};

        if (typeof options == 'object')
            Object.assign(myOptions, options);

        const date = new Date();
        date.setHours(0, 0, 0, 0);
        const query = `
            SELECT count(*) AS hasActiveRecovery
            FROM vn.recovery
            WHERE clientFk = ?
                AND IFNULL(finished, ?) >= ?;
        `;
        const [result] = await Self.rawSql(query, [id, date, date], myOptions);

        return result.hasActiveRecovery != 0;
    };
};