@infrab4a/connect-angular 2.0.6-beta.6 → 2.0.6-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -524,7 +524,7 @@
524
524
  console.log('hasProductCategories', hasProductCategories);
525
525
  // Se não tem produtos elegíveis, retorna erro
526
526
  if (!hasProductCategories)
527
- return [2 /*return*/, rxjs.throwError('Seu carrinho não possui produtos elegíveis para desconto.')];
527
+ throw new InvalidCouponError('Seu carrinho não possui produtos elegíveis para desconto.');
528
528
  return [2 /*return*/, coupon];
529
529
  }
530
530
  });
@@ -540,44 +540,69 @@
540
540
  return rxjs.of(discount);
541
541
  };
542
542
  CouponService.prototype.calcDiscountShopping = function (coupon, checkout) {
543
- var discount = 0;
544
- switch (coupon.type) {
545
- case i2.CouponTypes.ABSOLUTE: {
546
- discount = coupon.discount;
547
- break;
548
- }
549
- case i2.CouponTypes.PERCENTAGE: {
550
- discount = this.calcShoppingPercentageDiscount(coupon, checkout);
551
- break;
552
- }
553
- }
554
- return rxjs.of(discount);
543
+ return __awaiter(this, void 0, void 0, function () {
544
+ var discount, _d;
545
+ return __generator(this, function (_e) {
546
+ switch (_e.label) {
547
+ case 0:
548
+ discount = 0;
549
+ _d = coupon.type;
550
+ switch (_d) {
551
+ case i2.CouponTypes.ABSOLUTE: return [3 /*break*/, 1];
552
+ case i2.CouponTypes.PERCENTAGE: return [3 /*break*/, 2];
553
+ }
554
+ return [3 /*break*/, 4];
555
+ case 1:
556
+ {
557
+ discount = coupon.discount;
558
+ return [3 /*break*/, 4];
559
+ }
560
+ _e.label = 2;
561
+ case 2: return [4 /*yield*/, this.calcShoppingPercentageDiscount(coupon, checkout)];
562
+ case 3:
563
+ discount = _e.sent();
564
+ return [3 /*break*/, 4];
565
+ case 4: return [2 /*return*/, rxjs.of(discount)];
566
+ }
567
+ });
568
+ });
555
569
  };
556
570
  CouponService.prototype.calcShoppingPercentageDiscount = function (coupon, checkout) {
557
571
  var _a;
558
- var discount = 0;
559
- var shop = checkout.shop;
560
- var lineItensDiscount = [];
561
- if (coupon.productsCategories && coupon.productsCategories.length) {
562
- lineItensDiscount = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
563
- var _a;
564
- if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
565
- return i.categories.some(function (c) { return coupon.productsCategories.includes(c); });
572
+ return __awaiter(this, void 0, void 0, function () {
573
+ var discount, shop, lineItensDiscount, couponCategories, subTotal;
574
+ return __generator(this, function (_d) {
575
+ switch (_d.label) {
576
+ case 0:
577
+ discount = 0;
578
+ shop = checkout.shop;
579
+ lineItensDiscount = [];
580
+ return [4 /*yield*/, this.getCouponCategoriesId(coupon)];
581
+ case 1:
582
+ couponCategories = _d.sent();
583
+ if (coupon.productsCategories && coupon.productsCategories.length) {
584
+ lineItensDiscount = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
585
+ var _a;
586
+ if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
587
+ return i.categories.some(function (c) { return couponCategories.some(function (cat) { return (cat.id == c || cat.firestoreId == c); }); });
588
+ }
589
+ return true;
590
+ });
591
+ }
592
+ else {
593
+ lineItensDiscount = checkout.lineItems;
594
+ }
595
+ subTotal = lineItensDiscount.reduce(function (acc, curr) {
596
+ var _a, _b, _c;
597
+ return ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.isSubscriber) && ((_b = curr.price[shop]) === null || _b === void 0 ? void 0 : _b.subscriberPrice)
598
+ ? acc + ((_c = curr.price[shop]) === null || _c === void 0 ? void 0 : _c.subscriberPrice) * curr.quantity
599
+ : acc + curr.pricePaid * curr.quantity;
600
+ }, 0) || 0;
601
+ discount = subTotal * (coupon.discount / 100);
602
+ return [2 /*return*/, discount];
566
603
  }
567
- return true;
568
604
  });
569
- }
570
- else {
571
- lineItensDiscount = checkout.lineItems;
572
- }
573
- var subTotal = lineItensDiscount.reduce(function (acc, curr) {
574
- var _a, _b, _c;
575
- return ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.isSubscriber) && ((_b = curr.price[shop]) === null || _b === void 0 ? void 0 : _b.subscriberPrice)
576
- ? acc + ((_c = curr.price[shop]) === null || _c === void 0 ? void 0 : _c.subscriberPrice) * curr.quantity
577
- : acc + curr.pricePaid * curr.quantity;
578
- }, 0) || 0;
579
- discount = subTotal * (coupon.discount / 100);
580
- return discount;
605
+ });
581
606
  };
582
607
  CouponService.prototype.hasMinSubTotal = function (coupon, checkout) {
583
608
  var _a;
@@ -597,50 +622,41 @@
597
622
  CouponService.prototype.hasProductCategories = function (coupon, checkout) {
598
623
  var _a;
599
624
  return __awaiter(this, void 0, void 0, function () {
600
- var hasCategories;
601
- var _this = this;
625
+ var couponCategories, hasCategories;
602
626
  return __generator(this, function (_d) {
603
- if (!coupon.productsCategories && !coupon.productsCategories.length) {
604
- console.log('não possui categories');
605
- return [2 /*return*/, true];
606
- }
607
- hasCategories = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) { return __awaiter(_this, void 0, void 0, function () {
608
- var _b, couponCategories, index, c;
609
- return __generator(this, function (_d) {
610
- switch (_d.label) {
611
- case 0:
612
- if (!i.categories || !((_b = i.categories) === null || _b === void 0 ? void 0 : _b.length)) {
613
- console.log('produto não tem categorias');
614
- return [2 /*return*/, true];
615
- }
616
- couponCategories = [];
617
- index = 0;
618
- _d.label = 1;
619
- case 1:
620
- if (!(index < coupon.productsCategories.length)) return [3 /*break*/, 4];
621
- return [4 /*yield*/, this.categoryRepository.get({ id: coupon.productsCategories[index] })];
622
- case 2:
623
- c = _d.sent();
624
- couponCategories.push({ id: c.id, firestoreId: c.firestoreId });
625
- _d.label = 3;
626
- case 3:
627
- index++;
628
- return [3 /*break*/, 1];
629
- case 4:
630
- console.log('couponCategories', couponCategories.length, couponCategories);
631
- return [2 /*return*/, i.categories.some(function (c) {
632
- console.log('some 1 ', c);
633
- return couponCategories.some(function (cat) {
634
- console.log('some 2', cat, cat.id == c, cat.firestoreId);
635
- if (cat.id == c || cat.firestoreId)
636
- return true;
637
- });
638
- })];
627
+ switch (_d.label) {
628
+ case 0:
629
+ if (!coupon.productsCategories && !coupon.productsCategories.length) {
630
+ console.log('não possui categories');
631
+ return [2 /*return*/, true];
639
632
  }
640
- });
641
- }); });
642
- console.log('final return', hasCategories, hasCategories.length);
643
- return [2 /*return*/, hasCategories.length ? true : false];
633
+ return [4 /*yield*/, this.getCouponCategoriesId(coupon)];
634
+ case 1:
635
+ couponCategories = _d.sent();
636
+ console.log('couponCategories', couponCategories.length, couponCategories);
637
+ hasCategories = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
638
+ var _a;
639
+ console.log('entrou no filter');
640
+ if (!i.categories || !((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length)) {
641
+ console.log('produto não tem categorias');
642
+ return true;
643
+ }
644
+ return i.categories.some(function (c) {
645
+ console.log('entrou no some 1', c);
646
+ return couponCategories.some(function (cat) { return (cat.id == c || cat.firestoreId == c); });
647
+ });
648
+ // return i.categories.some(async (c) => {
649
+ // console.log('some category c', c)
650
+ // const categoria: Category & { firestoreId?: string } = await this.categoryRepository.get({id: c})
651
+ // console.log(categoria)
652
+ // console.log(coupon.productsCategories.includes(categoria.id), coupon.productsCategories.includes(categoria.firestoreId), i.isGift)
653
+ // console.log('validation', (coupon.productsCategories.includes(categoria.id) || coupon.productsCategories.includes(categoria.firestoreId)) && !i.isGift)
654
+ // return (coupon.productsCategories.includes(categoria.id) || coupon.productsCategories.includes(categoria.firestoreId)) && !i.isGift
655
+ // })
656
+ });
657
+ console.log('final return', hasCategories, hasCategories.length);
658
+ return [2 /*return*/, hasCategories.length ? true : false];
659
+ }
644
660
  });
645
661
  });
646
662
  };
@@ -688,6 +704,30 @@
688
704
  });
689
705
  });
690
706
  };
707
+ CouponService.prototype.getCouponCategoriesId = function (coupon) {
708
+ return __awaiter(this, void 0, void 0, function () {
709
+ var couponCategories, index, c;
710
+ return __generator(this, function (_d) {
711
+ switch (_d.label) {
712
+ case 0:
713
+ couponCategories = [];
714
+ index = 0;
715
+ _d.label = 1;
716
+ case 1:
717
+ if (!(index < coupon.productsCategories.length)) return [3 /*break*/, 4];
718
+ return [4 /*yield*/, this.categoryRepository.get({ id: coupon.productsCategories[index] })];
719
+ case 2:
720
+ c = _d.sent();
721
+ couponCategories.push({ id: c.id, firestoreId: c.firestoreId });
722
+ _d.label = 3;
723
+ case 3:
724
+ index++;
725
+ return [3 /*break*/, 1];
726
+ case 4: return [2 /*return*/, couponCategories];
727
+ }
728
+ });
729
+ });
730
+ };
691
731
  return CouponService;
692
732
  }());
693
733
  CouponService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0__namespace, type: CouponService, deps: [{ token: 'CouponRepository' }, { token: DEFAULT_SHOP }, { token: 'OrderRepository' }, { token: 'SubscriptionRepository' }, { token: 'CategoryRepository' }], target: i0__namespace.ɵɵFactoryTarget.Injectable });
@@ -746,7 +786,7 @@
746
786
  };
747
787
  CheckoutService.prototype.calcDiscount = function (coupon) {
748
788
  var _this = this;
749
- return this.getCheckout().pipe(operators.concatMap(function (checkout) { return _this.couponService.calcDiscountShopping(coupon, checkout).pipe(); }));
789
+ return this.getCheckout().pipe(operators.concatMap(function (checkout) { return _this.couponService.calcDiscountShopping(coupon, checkout); }));
750
790
  };
751
791
  CheckoutService.prototype.checkCoupon = function (nickname, checkoutType) {
752
792
  var _this = this;