@infrab4a/connect-angular 2.0.6-beta.0 → 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,11 +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
- // Se não tem produtos elegíveis, retorna erro
488
- if (!hasProductCategories)
489
- return rxjs.throwError('Seu carrinho não possui produtos elegíveis para desconto.');
490
485
  return rxjs.of(coupon);
491
486
  };
492
487
  CouponService.prototype.isValidCoupon = function (coupon, userEmail) {
@@ -498,9 +493,9 @@
498
493
  throw new InvalidCouponError('Cupom expirado.');
499
494
  return coupon;
500
495
  };
501
- CouponService.prototype.checkCouponUseAndLimit = function (coupon, userEmail) {
496
+ CouponService.prototype.checkCouponUseAndLimit = function (coupon, userEmail, checkout) {
502
497
  return __awaiter(this, void 0, void 0, function () {
503
- var orders, ordersUserCoupon, validUser;
498
+ var orders, ordersUserCoupon, validUser, hasProductCategories;
504
499
  return __generator(this, function (_d) {
505
500
  switch (_d.label) {
506
501
  case 0: return [4 /*yield*/, this.orderRepository.find({
@@ -523,6 +518,12 @@
523
518
  validUser = _d.sent();
524
519
  if (!validUser)
525
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.';
526
527
  return [2 /*return*/, coupon];
527
528
  }
528
529
  });
@@ -538,44 +539,69 @@
538
539
  return rxjs.of(discount);
539
540
  };
540
541
  CouponService.prototype.calcDiscountShopping = function (coupon, checkout) {
541
- var discount = 0;
542
- switch (coupon.type) {
543
- case i2.CouponTypes.ABSOLUTE: {
544
- discount = coupon.discount;
545
- break;
546
- }
547
- case i2.CouponTypes.PERCENTAGE: {
548
- discount = this.calcShoppingPercentageDiscount(coupon, checkout);
549
- break;
550
- }
551
- }
552
- 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
+ });
553
568
  };
554
569
  CouponService.prototype.calcShoppingPercentageDiscount = function (coupon, checkout) {
555
570
  var _a;
556
- var discount = 0;
557
- var shop = checkout.shop;
558
- var lineItensDiscount = [];
559
- if (coupon.productsCategories && coupon.productsCategories.length) {
560
- lineItensDiscount = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
561
- var _a;
562
- if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
563
- 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];
564
602
  }
565
- return true;
566
603
  });
567
- }
568
- else {
569
- lineItensDiscount = checkout.lineItems;
570
- }
571
- var subTotal = lineItensDiscount.reduce(function (acc, curr) {
572
- var _a, _b, _c;
573
- return ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.isSubscriber) && ((_b = curr.price[shop]) === null || _b === void 0 ? void 0 : _b.subscriberPrice)
574
- ? acc + ((_c = curr.price[shop]) === null || _c === void 0 ? void 0 : _c.subscriberPrice) * curr.quantity
575
- : acc + curr.pricePaid * curr.quantity;
576
- }, 0) || 0;
577
- discount = subTotal * (coupon.discount / 100);
578
- return discount;
604
+ });
579
605
  };
580
606
  CouponService.prototype.hasMinSubTotal = function (coupon, checkout) {
581
607
  var _a;
@@ -593,35 +619,36 @@
593
619
  return false;
594
620
  };
595
621
  CouponService.prototype.hasProductCategories = function (coupon, checkout) {
596
- var _this = this;
597
622
  var _a;
598
- if (coupon.productsCategories && coupon.productsCategories.length) {
599
- var hasCategories = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
600
- var _a;
601
- if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
602
- // buscar categoria por id
603
- // filtrar por id ou firestore id
604
- return i.categories.some(function (c) { return __awaiter(_this, void 0, void 0, function () {
605
- var categoria;
606
- return __generator(this, function (_d) {
607
- switch (_d.label) {
608
- case 0: return [4 /*yield*/, this.categoryRepository.get({ id: c })];
609
- case 1:
610
- categoria = _d.sent();
611
- console.log(categoria);
612
- console.log(coupon.productsCategories.includes(categoria.id), coupon.productsCategories.includes(categoria.firestoreId));
613
- return [2 /*return*/, (coupon.productsCategories.includes(categoria.id) || coupon.productsCategories.includes(categoria.firestoreId)) && !i.isGift];
614
- }
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
+ // })
615
647
  });
616
- }); });
648
+ return [2 /*return*/, hasCategories.length ? true : false];
617
649
  }
618
- return true;
619
650
  });
620
- return hasCategories.length ? true : false;
621
- }
622
- else {
623
- return true;
624
- }
651
+ });
625
652
  };
626
653
  CouponService.prototype.userValidationAndSubscriptionStatus = function (coupon, userEmail) {
627
654
  return __awaiter(this, void 0, void 0, function () {
@@ -667,6 +694,30 @@
667
694
  });
668
695
  });
669
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
+ };
670
721
  return CouponService;
671
722
  }());
672
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 });
@@ -725,7 +776,12 @@
725
776
  };
726
777
  CheckoutService.prototype.calcDiscount = function (coupon) {
727
778
  var _this = this;
728
- 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
+ }); }); }));
729
785
  };
730
786
  CheckoutService.prototype.checkCoupon = function (nickname, checkoutType) {
731
787
  var _this = this;