module.exports = function(Self) {
    Self.remoteMethodCtx('save', {
        description: 'returns the information from UserConfig model for the active user',
        accepts: [{
            arg: 'config',
            type: 'Object',
            required: true,
            description: `Code of the table you ask its configuration`,
            http: {source: 'body'}
        }],
        returns: {
            type: 'object',
            root: true
        },
        http: {
            path: `/save`,
            verb: 'post'
        }
    });

    Self.save = async(ctx, config) => {
        let userView = await Self.app.models.UserConfigView.findOne({
            where: {tableCode: config.tableCode, userFk: ctx.req.accessToken.userId}
        });

        if (userView)
            return userView.updateAttributes(config);

        config.userFk = ctx.req.accessToken.userId;

        return Self.app.models.UserConfigView.create(config);
    };
};