module.exports = Self => {
    Self.remoteMethod('sumAmount', {
        description: 'Returns the sum of greuge for a client',
        accessType: 'READ',
        accepts: [{
            arg: 'id',
            type: 'number',
            required: true,
            description: 'clientFk',
            http: {source: 'path'}
        }],
        returns: {
            arg: 'sumAmount'
        },
        http: {
            path: `/:id/sumAmount`,
            verb: 'get'
        }
    });

    Self.sumAmount = async(clientFk, options) => {
        const myOptions = {};

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

        const query = `SELECT SUM(amount) AS sumAmount FROM vn.greuge WHERE clientFk = ?`;
        try {
            const [response] = await Self.rawSql(query, [clientFk], myOptions);

            return response ? response.sumAmount : 0;
        } catch (e) {
            throw new Error(e);
        }
    };
};