import Recommendation from '~/Contracts/Recommendation'; import Response from '~/Contracts/ClientResponse'; import Guard from '~/Guards/Guard'; class InvalidateProductsGuard extends Guard { /** * * * @param {object} response * @return {boolean} */ public check(response: Response): boolean { return this.validate(response.data); } /** * * * @param {Recommendation} recommendation * @return {boolean} */ private validate(recommendation: Recommendation) { return recommendation.products.every(({ code }) => !! this.products[code]); } /** * * * @param {Recommendation} recommendation * @return {Trending | null} */ public contingency(recommendation: Recommendation) { const trending = this.storage.get(); const badProducts = recommendation.products.filter(({ code }) => ! this.products[code]); console.error('TrendingService - Invalidate product(s) found in payload.', badProducts, recommendation); if (! trending) { console.warn('TrendingService - No previous trending data available in local-storage.'); } return trending; } /** * * * @return {boolean} */ public retry() { return false; } } export default InvalidateProductsGuard;