@infrab4a/connect-angular 2.0.6-beta.1 → 2.0.6-beta.10

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.
@@ -450,7 +450,7 @@
450
450
  nickname: { operator: i2.Where.EQUALS, value: nickname },
451
451
  active: { operator: i2.Where.EQUALS, value: true },
452
452
  },
453
- })).pipe(operators.concatMap(function (coupons) { return _this.checkCouponRules(coupons, checkoutType, plan, checkout, isSubscription); }), operators.concatMap(function (coupon) { return _this.checkCouponUseAndLimit(coupon, userEmail); }), operators.map(function (coupon) { return _this.isValidCoupon(coupon, userEmail); }), operators.map(function (coupon) { return coupon; }));
453
+ })).pipe(operators.concatMap(function (coupons) { return _this.checkCouponRules(coupons, checkoutType, plan, checkout, isSubscription); }), operators.concatMap(function (coupon) { return _this.checkCouponUseAndLimit(coupon, userEmail, checkout); }), operators.map(function (coupon) { return _this.isValidCoupon(coupon, userEmail); }), operators.map(function (coupon) { return coupon; }));
454
454
  };
455
455
  CouponService.prototype.checkCouponRules = function (coupons, checkoutType, plan, checkout, isSubscription) {
456
456
  // Caso não ache nenhum cupom, retorna erro
@@ -482,12 +482,6 @@
482
482
  // Se não tem valor mínimo atingido, retorna erro
483
483
  if (!hasMinSubTotal)
484
484
  return rxjs.throwError('Valor mínimo não atingido');
485
- // Verifica se a compra possui produtos elegíveis para desconto
486
- var hasProductCategories = this.hasProductCategories(coupon, checkout);
487
- console.log('hasProductCategories', hasProductCategories);
488
- // Se não tem produtos elegíveis, retorna erro
489
- if (!hasProductCategories)
490
- return rxjs.throwError('Seu carrinho não possui produtos elegíveis para desconto.');
491
485
  return rxjs.of(coupon);
492
486
  };
493
487
  CouponService.prototype.isValidCoupon = function (coupon, userEmail) {
@@ -499,9 +493,9 @@
499
493
  throw new InvalidCouponError('Cupom expirado.');
500
494
  return coupon;
501
495
  };
502
- CouponService.prototype.checkCouponUseAndLimit = function (coupon, userEmail) {
496
+ CouponService.prototype.checkCouponUseAndLimit = function (coupon, userEmail, checkout) {
503
497
  return __awaiter(this, void 0, void 0, function () {
504
- var orders, ordersUserCoupon, validUser;
498
+ var orders, ordersUserCoupon, validUser, hasProductCategories;
505
499
  return __generator(this, function (_d) {
506
500
  switch (_d.label) {
507
501
  case 0: return [4 /*yield*/, this.orderRepository.find({
@@ -524,6 +518,12 @@
524
518
  validUser = _d.sent();
525
519
  if (!validUser)
526
520
  throw new InvalidCouponError('Usuário não elegível.');
521
+ return [4 /*yield*/, this.hasProductCategories(coupon, checkout)];
522
+ case 3:
523
+ hasProductCategories = _d.sent();
524
+ // Se não tem produtos elegíveis, retorna erro
525
+ if (!hasProductCategories)
526
+ throw 'Seu carrinho não possui produtos elegíveis para desconto.';
527
527
  return [2 /*return*/, coupon];
528
528
  }
529
529
  });
@@ -539,44 +539,69 @@
539
539
  return rxjs.of(discount);
540
540
  };
541
541
  CouponService.prototype.calcDiscountShopping = function (coupon, checkout) {
542
- var discount = 0;
543
- switch (coupon.type) {
544
- case i2.CouponTypes.ABSOLUTE: {
545
- discount = coupon.discount;
546
- break;
547
- }
548
- case i2.CouponTypes.PERCENTAGE: {
549
- discount = this.calcShoppingPercentageDiscount(coupon, checkout);
550
- break;
551
- }
552
- }
553
- return rxjs.of(discount);
542
+ return __awaiter(this, void 0, void 0, function () {
543
+ var discount, _d;
544
+ return __generator(this, function (_e) {
545
+ switch (_e.label) {
546
+ case 0:
547
+ discount = 0;
548
+ _d = coupon.type;
549
+ switch (_d) {
550
+ case i2.CouponTypes.ABSOLUTE: return [3 /*break*/, 1];
551
+ case i2.CouponTypes.PERCENTAGE: return [3 /*break*/, 2];
552
+ }
553
+ return [3 /*break*/, 4];
554
+ case 1:
555
+ {
556
+ discount = coupon.discount;
557
+ return [3 /*break*/, 4];
558
+ }
559
+ _e.label = 2;
560
+ case 2: return [4 /*yield*/, this.calcShoppingPercentageDiscount(coupon, checkout)];
561
+ case 3:
562
+ discount = _e.sent();
563
+ return [3 /*break*/, 4];
564
+ case 4: return [2 /*return*/, discount];
565
+ }
566
+ });
567
+ });
554
568
  };
555
569
  CouponService.prototype.calcShoppingPercentageDiscount = function (coupon, checkout) {
556
570
  var _a;
557
- var discount = 0;
558
- var shop = checkout.shop;
559
- var lineItensDiscount = [];
560
- if (coupon.productsCategories && coupon.productsCategories.length) {
561
- lineItensDiscount = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
562
- var _a;
563
- if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
564
- return i.categories.some(function (c) { return coupon.productsCategories.includes(c); });
571
+ return __awaiter(this, void 0, void 0, function () {
572
+ var discount, shop, lineItensDiscount, couponCategories, subTotal;
573
+ return __generator(this, function (_d) {
574
+ switch (_d.label) {
575
+ case 0:
576
+ discount = 0;
577
+ shop = checkout.shop;
578
+ lineItensDiscount = [];
579
+ return [4 /*yield*/, this.getCouponCategoriesId(coupon)];
580
+ case 1:
581
+ couponCategories = _d.sent();
582
+ if (coupon.productsCategories && coupon.productsCategories.length) {
583
+ lineItensDiscount = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
584
+ var _a;
585
+ if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
586
+ return i.categories.some(function (c) { return couponCategories.some(function (cat) { return (cat.id == c || cat.firestoreId == c); }); });
587
+ }
588
+ return true;
589
+ });
590
+ }
591
+ else {
592
+ lineItensDiscount = checkout.lineItems;
593
+ }
594
+ subTotal = lineItensDiscount.reduce(function (acc, curr) {
595
+ var _a, _b, _c;
596
+ return ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.isSubscriber) && ((_b = curr.price[shop]) === null || _b === void 0 ? void 0 : _b.subscriberPrice)
597
+ ? acc + ((_c = curr.price[shop]) === null || _c === void 0 ? void 0 : _c.subscriberPrice) * curr.quantity
598
+ : acc + curr.pricePaid * curr.quantity;
599
+ }, 0) || 0;
600
+ discount = subTotal * (coupon.discount / 100);
601
+ return [2 /*return*/, discount];
565
602
  }
566
- return true;
567
603
  });
568
- }
569
- else {
570
- lineItensDiscount = checkout.lineItems;
571
- }
572
- var subTotal = lineItensDiscount.reduce(function (acc, curr) {
573
- var _a, _b, _c;
574
- return ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.isSubscriber) && ((_b = curr.price[shop]) === null || _b === void 0 ? void 0 : _b.subscriberPrice)
575
- ? acc + ((_c = curr.price[shop]) === null || _c === void 0 ? void 0 : _c.subscriberPrice) * curr.quantity
576
- : acc + curr.pricePaid * curr.quantity;
577
- }, 0) || 0;
578
- discount = subTotal * (coupon.discount / 100);
579
- return discount;
604
+ });
580
605
  };
581
606
  CouponService.prototype.hasMinSubTotal = function (coupon, checkout) {
582
607
  var _a;
@@ -594,36 +619,36 @@
594
619
  return false;
595
620
  };
596
621
  CouponService.prototype.hasProductCategories = function (coupon, checkout) {
597
- var _this = this;
598
622
  var _a;
599
- if (coupon.productsCategories && coupon.productsCategories.length) {
600
- var hasCategories = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
601
- var _a;
602
- if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
603
- // buscar categoria por id
604
- // filtrar por id ou firestore id
605
- return i.categories.some(function (c) { return __awaiter(_this, void 0, void 0, function () {
606
- var categoria;
607
- return __generator(this, function (_d) {
608
- switch (_d.label) {
609
- case 0: return [4 /*yield*/, this.categoryRepository.get({ id: c })];
610
- case 1:
611
- categoria = _d.sent();
612
- console.log(categoria);
613
- console.log(coupon.productsCategories.includes(categoria.id), coupon.productsCategories.includes(categoria.firestoreId), i.isGift);
614
- console.log('validation', (coupon.productsCategories.includes(categoria.id) || coupon.productsCategories.includes(categoria.firestoreId)) && !i.isGift);
615
- return [2 /*return*/, (coupon.productsCategories.includes(categoria.id) || coupon.productsCategories.includes(categoria.firestoreId)) && !i.isGift];
616
- }
623
+ return __awaiter(this, void 0, void 0, function () {
624
+ var couponCategories, hasCategories;
625
+ return __generator(this, function (_d) {
626
+ switch (_d.label) {
627
+ case 0:
628
+ if (!coupon.productsCategories && !coupon.productsCategories.length) {
629
+ return [2 /*return*/, true];
630
+ }
631
+ return [4 /*yield*/, this.getCouponCategoriesId(coupon)];
632
+ case 1:
633
+ couponCategories = _d.sent();
634
+ hasCategories = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
635
+ var _a;
636
+ if (!i.categories || !((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length))
637
+ return true;
638
+ return i.categories.some(function (c) { return couponCategories.some(function (cat) { return (cat.id == c || cat.firestoreId == c); }); });
639
+ // return i.categories.some(async (c) => {
640
+ // console.log('some category c', c)
641
+ // const categoria: Category & { firestoreId?: string } = await this.categoryRepository.get({id: c})
642
+ // console.log(categoria)
643
+ // console.log(coupon.productsCategories.includes(categoria.id), coupon.productsCategories.includes(categoria.firestoreId), i.isGift)
644
+ // console.log('validation', (coupon.productsCategories.includes(categoria.id) || coupon.productsCategories.includes(categoria.firestoreId)) && !i.isGift)
645
+ // return (coupon.productsCategories.includes(categoria.id) || coupon.productsCategories.includes(categoria.firestoreId)) && !i.isGift
646
+ // })
617
647
  });
618
- }); });
648
+ return [2 /*return*/, hasCategories.length ? true : false];
619
649
  }
620
- return true;
621
650
  });
622
- return hasCategories.length ? true : false;
623
- }
624
- else {
625
- return true;
626
- }
651
+ });
627
652
  };
628
653
  CouponService.prototype.userValidationAndSubscriptionStatus = function (coupon, userEmail) {
629
654
  return __awaiter(this, void 0, void 0, function () {
@@ -669,6 +694,30 @@
669
694
  });
670
695
  });
671
696
  };
697
+ CouponService.prototype.getCouponCategoriesId = function (coupon) {
698
+ return __awaiter(this, void 0, void 0, function () {
699
+ var couponCategories, index, c;
700
+ return __generator(this, function (_d) {
701
+ switch (_d.label) {
702
+ case 0:
703
+ couponCategories = [];
704
+ index = 0;
705
+ _d.label = 1;
706
+ case 1:
707
+ if (!(index < coupon.productsCategories.length)) return [3 /*break*/, 4];
708
+ return [4 /*yield*/, this.categoryRepository.get({ id: coupon.productsCategories[index] })];
709
+ case 2:
710
+ c = _d.sent();
711
+ couponCategories.push({ id: c.id, firestoreId: c.firestoreId });
712
+ _d.label = 3;
713
+ case 3:
714
+ index++;
715
+ return [3 /*break*/, 1];
716
+ case 4: return [2 /*return*/, couponCategories];
717
+ }
718
+ });
719
+ });
720
+ };
672
721
  return CouponService;
673
722
  }());
674
723
  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 });
@@ -727,7 +776,12 @@
727
776
  };
728
777
  CheckoutService.prototype.calcDiscount = function (coupon) {
729
778
  var _this = this;
730
- return this.getCheckout().pipe(operators.concatMap(function (checkout) { return _this.couponService.calcDiscountShopping(coupon, checkout).pipe(); }));
779
+ return this.getCheckout().pipe(operators.concatMap(function (checkout) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_b) {
780
+ switch (_b.label) {
781
+ case 0: return [4 /*yield*/, this.couponService.calcDiscountShopping(coupon, checkout)];
782
+ case 1: return [2 /*return*/, _b.sent()];
783
+ }
784
+ }); }); }));
731
785
  };
732
786
  CheckoutService.prototype.checkCoupon = function (nickname, checkoutType) {
733
787
  var _this = this;