From 43e9aa1eb3a2f227d0c13b53200c43a5e53997ae Mon Sep 17 00:00:00 2001 From: Adhidarma Hadiwinoto <me@adhisimon.org> Date: Tue, 14 Jul 2020 13:57:06 +0700 Subject: [PATCH] dispose and putBack --- index.js | 3 ++- lib/redis-util.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index fce2c66..2a970ce 100644 --- a/index.js +++ b/index.js @@ -9,4 +9,5 @@ const redisUtil = require('./lib/redis-util'); require('./lib/control-panel'); -exports.decrement = redisUtil.decrement; +exports.dispose = redisUtil.dispose; +exports.putBack = redisUtil.putBack; diff --git a/lib/redis-util.js b/lib/redis-util.js index b206f28..2745d47 100644 --- a/lib/redis-util.js +++ b/lib/redis-util.js @@ -148,6 +148,25 @@ const decrement = (product, xid) => new Promise((resolve) => { }); exports.decrement = decrement; +const increment = (product, xid) => new Promise((resolve) => { + const keyword = composeKeyword(product); + + redisClient.incr(keyword, (err, reply) => { + if (err) { + logger.warn(`8DA99C28 ${MODULE_NAME}: Error on incrementing`, { + xid, product, eCode: err.code, eMessage: err.message, + }); + + redisClient.expire(keyword, DEFAULT_TTL); + + resolve(null); + return; + } + + resolve(reply); + }); +}); + const incrementBy = (product, val, xid) => new Promise((resolve) => { const keyword = composeKeyword(product); @@ -187,6 +206,15 @@ const dispose = async (product, xid) => { }; exports.dispose = dispose; +const putBack = async (product, xid) => { + if (!await isLimited(product, xid)) { + return; + } + + await increment(product, xid); +}; +exports.putBack = putBack; + const limitedList = (xid) => new Promise((resolve, reject) => { redisClient.smembers(LIMITED_SET_NAME, (err, reply) => { if (err) { -- 1.9.0