def deletePref(self, consumerId, cardId, categoryKey, periodKey): pref = None millis = SSUtil.getMillis() cacheService = CacheService() txnService = TxnService() try: pref = ConsumerPrefs.objects.get(consumerId=consumerId, cardId=cardId, periodKey=periodKey, categoryKey=categoryKey) pref.delete() except ConsumerPrefs.DoesNotExist: pass try: agg = ConsumerAgg.objects.get(consumerId=consumerId, cardId=cardId, periodKey=periodKey, categoryKey=categoryKey) agg.delete() except ConsumerAgg.DoesNotExist: pass cacheService.setConsumerPrefs(consumerId) txnService.recalculateAgg(consumerId, None, None) cacheService.setConsumerAggs(consumerId) return True
def savePref(self, consumerId, cardId, categoryKey, periodKey, action, userLimit): pref = None millis = SSUtil.getMillis() cacheService = CacheService() txnService = TxnService() try: if categoryKey is None: pref = ConsumerPrefs.objects.get(consumerId=consumerId, cardId=cardId, periodKey=periodKey, categoryKey='Any') else: pref = ConsumerPrefs.objects.get(consumerId=consumerId, cardId=cardId, periodKey=periodKey, categoryKey=categoryKey) except ConsumerPrefs.DoesNotExist: pref = ConsumerPrefs() pref.consumerId = consumerId pref.cardId = cardId pref.periodKey = periodKey pref.categoryKey = categoryKey pref.created = millis pref.txType = "Any" pref.merchantId = -1 pref.ssApproval = action pref.limit = userLimit pref.updated = millis pref.save() cacheService.setConsumerPrefs(consumerId) txnService.recalculateAgg(consumerId, None, None) cacheService.setConsumerAggs(consumerId) return pref
def createDefaultPrefsAndAggs(self, card): agg = None pref = None defPeriodKeys = {"Daily","Monthly"} defAction = "Approve" millis = SSUtil.getMillis() cacheService = CacheService() for periodKey in defPeriodKeys: try: pref = ConsumerPrefs.objects.get(consumerId=card.consumerId, cardId = card.id, periodKey=periodKey, categoryKey='Any') except: pref = ConsumerPrefs() pref.consumerId = card.consumerId pref.cardId = card.id pref.periodKey = periodKey pref.categoryKey = 'Any' pref.created = millis pref.txType = "Any" pref.merchantId = -1 pref.ssApproval = defAction pref.limit = card.limit pref.updated = millis pref.save() try: agg = ConsumerAgg.objects.get(consumerId=card.consumerId, cardId = card.id, periodKey=periodKey, categoryKey='Any') except: agg = ConsumerAgg() agg.consumerId = card.consumerId agg.cardId = card.id agg.periodKey = periodKey agg.categoryKey = 'Any' agg.created = millis agg.txType = "Any" agg.merchantId = -1 agg.ssApproval = defAction agg.amtSpentSS = 0.00 agg.updated = millis agg.save() cacheService.setConsumerPrefs(card.consumerId) cacheService.setConsumerAggs(card.consumerId)
def recalculateAgg(self, consumerId, recentAmount, recentTxn): currTimeMillis = SSUtil.getMillis() ssConst = SSConst() cacheService = CacheService() prefs = cacheService.getConsumerPrefs(consumerId) name_map = {'total': 'total', 'pk': 'id'} periodKeysJson = ssConst.getJson(ssConst.PERIOD_KEYS) approvalTypeJson = ssConst.getJson(ssConst.APPROVAL_TYPES) if prefs: for pref in prefs: consumerAgg = ConsumerAgg() querySt = "select sum(amtSpentSS) as total, 1 as id from ss_consumer_txn where consumerId =" + str( consumerId) querySt += " and ssApproval='" + ssConst.APPROVAL_TYPES[0][ 0] + "'" if pref.periodKey != 'Any': print "period key is " + pref.periodKey print " value is " + periodKeysJson[pref.periodKey] querySt += " and FROM_UNIXTIME(created/1000) >= DATE_SUB(FROM_UNIXTIME(" + str( currTimeMillis / 1000 ) + "), INTERVAL " + periodKeysJson[pref.periodKey] + ")" if pref.categoryKey != 'Any': category = TxnCategory.objects.get(name=pref.categoryKey) querySt += " and category_id = " + str(category.id) if pref.txType != 'Any': querySt += " and txType = '" + pref.txType + "'" if pref.cardId != -1: querySt += " and cardId = " + str(pref.cardId) if pref.merchantId != -1: querySt += " and merchant_id = " + str(pref.merchantId) print querySt res = ConsumerTxn.objects.raw(querySt, translations=name_map) total = 0 for x in res: total = x.total if total == None: total = 0 print total # check if aggregate exists for this pref print str(consumerId) + str( pref.cardId ) + pref.periodKey + " " + pref.categoryKey + " " + pref.txType + " " + str( pref.merchantId) try: consumerAgg = ConsumerAgg.objects.get( consumerId=consumerId, cardId=pref.cardId, periodKey=pref.periodKey, categoryKey=pref.categoryKey, txType=pref.txType, merchantId=pref.merchantId) print "Got consumer agg " + str(total) except ConsumerAgg.DoesNotExist: consumerAgg.periodKey = pref.periodKey consumerAgg.categoryKey = pref.categoryKey consumerAgg.txType = pref.txType consumerAgg.cardId = pref.cardId consumerAgg.merchantId = pref.merchantId consumerAgg.consumerId = pref.consumerId consumerAgg.created = currTimeMillis consumerAgg.amtSpentSS = total consumerAgg.updated = currTimeMillis consumerAgg.save() cacheService.setConsumerAggs(consumerId) #fix balance now if not recentTxn is None: if recentTxn.ssApproval == ssConst.APPROVAL_TYPES[0][0]: card = ConsumerCard.objects.get(id=recentTxn.cardId) card.amtSpentSS = card.amtSpentSS + recentAmount card.currOS = card.currOS + recentAmount card.avaialbleLimit = card.limit - card.currOS card.updated = currTimeMillis card.save() cacheService.setCard(card.id)