From 510974162fff09f16218d523c77b821c7532c812 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Tue, 2 Jan 2018 10:29:54 -0300 Subject: [PATCH] [v4] Updates MeLi integration --- .../buy-mercado-libre/buy-mercado-libre.ts | 25 +++++++++-------- .../mercado-libre-cards.ts | 28 ++++++++++++++++--- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/pages/integrations/mercado-libre/buy-mercado-libre/buy-mercado-libre.ts b/src/pages/integrations/mercado-libre/buy-mercado-libre/buy-mercado-libre.ts index be78d7718..b71a2be29 100644 --- a/src/pages/integrations/mercado-libre/buy-mercado-libre/buy-mercado-libre.ts +++ b/src/pages/integrations/mercado-libre/buy-mercado-libre/buy-mercado-libre.ts @@ -276,21 +276,15 @@ export class BuyMercadoLibrePage { if (err) { this.sendStatus = ''; this.onGoingProcessProvider.set('Comprando Vale-Presente', false, this.statusChangeHandler); - giftCard = {}; - giftCard.status = 'FAILURE'; + giftCard = giftCard || {}; + giftCard['status'] = 'FAILURE'; } if (giftCard && giftCard.cardStatus && (giftCard.cardStatus != 'active' && giftCard.cardStatus != 'inactive' && giftCard.cardStatus != 'expired')) { this.sendStatus = ''; this.onGoingProcessProvider.set('Comprando Vale-Presente', false, this.statusChangeHandler); - giftCard = {}; - giftCard.status = 'FAILURE'; - } - - if (giftCard.status == 'PENDING' && count < 3) { - this.logger.debug("Waiting for payment confirmation"); - this.checkTransaction(count + 1, dataSrc); - return; + giftCard = giftCard || {}; + giftCard['status'] = 'FAILURE'; } var now = moment().unix() * 1000; @@ -304,9 +298,18 @@ export class BuyMercadoLibrePage { newData.date = dataSrc.invoiceTime || now; newData.uuid = dataSrc.uuid; + if (giftCard.status == 'PENDING' && count < 3) { + this.logger.debug("Waiting for payment confirmation"); + this.mercadoLibreProvider.savePendingGiftCard(newData, null, (err: any) => { + this.logger.debug("Saving new gift card with status: " + newData.status); + }); + this.checkTransaction(count + 1, dataSrc); + return; + } + this.mercadoLibreProvider.savePendingGiftCard(newData, null, (err: any) => { this.onGoingProcessProvider.set('Comprando Vale-Presente', false, this.statusChangeHandler); - this.logger.debug("Saving new gift card with status: " + newData.status); + this.logger.debug("Saved new gift card with status: " + newData.status); this.mlGiftCard = newData; }); }); diff --git a/src/pages/integrations/mercado-libre/mercado-libre-cards/mercado-libre-cards.ts b/src/pages/integrations/mercado-libre/mercado-libre-cards/mercado-libre-cards.ts index 7ce3ecc62..59f1595c1 100644 --- a/src/pages/integrations/mercado-libre/mercado-libre-cards/mercado-libre-cards.ts +++ b/src/pages/integrations/mercado-libre/mercado-libre-cards/mercado-libre-cards.ts @@ -10,6 +10,7 @@ import { MercadoLibreCardDetailsPage } from '../mercado-libre-card-details/merca import { MercadoLibreProvider } from '../../../../providers/mercado-libre/mercado-libre'; import { ExternalLinkProvider } from '../../../../providers/external-link/external-link'; import { PopupProvider } from '../../../../providers/popup/popup'; +import { TimeProvider } from '../../../../providers/time/time'; @Component({ selector: 'page-mercado-libre-cards', @@ -17,11 +18,13 @@ import { PopupProvider } from '../../../../providers/popup/popup'; }) export class MercadoLibreCardsPage { + private updateGiftCard: boolean; public giftCards: any; public card: any; public invoiceId; constructor( + private timeProvider: TimeProvider, private mercadoLibreProvider: MercadoLibreProvider, private externalLinkProvider: ExternalLinkProvider, private logger: Logger, @@ -48,6 +51,7 @@ export class MercadoLibreCardsPage { return; } this.openCardModal(card); + this.updateGiftCard = this.checkIfCardNeedsUpdate(card); } }).catch((err: any) => { this.logger.warn(err); @@ -58,6 +62,19 @@ export class MercadoLibreCardsPage { this.externalLinkProvider.open(url); } + private checkIfCardNeedsUpdate(card: any) { + // Continues normal flow (update card) + if (card.status == 'PENDING') { + return true; + } + // Check if card status FAILURE for 24 hours + if (card.status == 'FAILURE' && this.timeProvider.withinPastDay(card.date)) { + return true; + } + // Success: do not update + return false; + }; + private updateGiftCards(): Promise { return new Promise((resolve, reject) => { this.mercadoLibreProvider.getPendingGiftCards((err: any, gcds: any) => { @@ -75,15 +92,18 @@ export class MercadoLibreCardsPage { this.updateGiftCards().then(() => { let gcds = this.giftCards; _.forEach(gcds, (dataFromStorage: any) => { - if (dataFromStorage.status == 'PENDING' || dataFromStorage.status == 'invalid') { + + this.updateGiftCard = this.checkIfCardNeedsUpdate(dataFromStorage); + + if (this.updateGiftCard) { this.logger.debug("Creating / Updating gift card"); this.mercadoLibreProvider.createGiftCard(dataFromStorage, (err: any, giftCard: any) => { if (err) { this.logger.error('Error creating gift card:', err); - giftCard = {}; - giftCard.status = 'FAILURE'; + giftCard = giftCard || {}; + giftCard['status'] = 'FAILURE'; } if (giftCard.status != 'PENDING') { @@ -98,7 +118,7 @@ export class MercadoLibreCardsPage { _.merge(newData, dataFromStorage, giftCard); this.mercadoLibreProvider.savePendingGiftCard(newData, null, (err: any) => { - this.logger.debug("Saving new gift card"); + this.logger.debug("Mercado Libre gift card updated"); this.updateGiftCards(); }); }