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

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,39 +450,56 @@
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, checkout); }), operators.map(function (coupon) { return _this.isValidCoupon(coupon, userEmail); }), operators.map(function (coupon) { return coupon; }));
453
+ })).pipe(operators.concatMap(function (coupons) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_d) {
454
+ switch (_d.label) {
455
+ case 0: return [4 /*yield*/, this.checkCouponRules(coupons, checkoutType, plan, checkout, isSubscription)];
456
+ case 1: return [2 /*return*/, _d.sent()];
457
+ }
458
+ }); }); }), operators.concatMap(function (coupon) { return _this.checkCouponUseAndLimit(coupon, userEmail); }), operators.map(function (coupon) { return _this.isValidCoupon(coupon, userEmail); }), operators.map(function (coupon) { return coupon; }));
454
459
  };
455
460
  CouponService.prototype.checkCouponRules = function (coupons, checkoutType, plan, checkout, isSubscription) {
456
- // Caso não ache nenhum cupom, retorna erro
457
- if (coupons.count < 1) {
458
- return rxjs.throwError('Cupom inválido.');
459
- }
460
- // Get Primeiro Cupom (o find do repository retorna um array)
461
- var coupon = coupons.data.shift();
462
- // Verifica se o cupom é aplicavel na loja
463
- var isInShop = coupon.shopAvailability === i2.Shops.ALL || coupon.shopAvailability === this.defaultShop;
464
- // Cupon não aplicavel a loja retorna erro
465
- if (!isInShop)
466
- return rxjs.throwError('Cupom inválido para loja.');
467
- // Verifica se o coupon é aplicado no checkout que está sendo realizado
468
- var isCheckoutType = coupon.checkoutType === i2.CheckoutTypes.ALL || coupon.checkoutType === checkoutType;
469
- // Cupon não aplicavel ao checkout retorna erro
470
- if (!isCheckoutType)
471
- return rxjs.throwError('Cupom inválido. Erro de checkout.');
472
- // Verifica se o cupom é ou pode ser aplicado para subscription
473
- if (checkoutType === i2.CheckoutTypes.ALL || checkoutType === i2.CheckoutTypes.SUBSCRIPTION) {
474
- // Se o cupom tiver um plano associado, verifica se é o mesmo plano do checkout da assinatura
475
- if (coupon.plan && coupon.plan.toUpperCase() !== plan.toUpperCase())
476
- return rxjs.throwError('Cupom inválido para sua assinatura.');
477
- }
478
- if (isSubscription)
479
- return rxjs.of(coupon);
480
- // Verifica se possui o valor minimo de compra para utilização do cupom
481
- var hasMinSubTotal = this.hasMinSubTotal(coupon, checkout);
482
- // Se não tem valor mínimo atingido, retorna erro
483
- if (!hasMinSubTotal)
484
- return rxjs.throwError('Valor mínimo não atingido');
485
- return rxjs.of(coupon);
461
+ return __awaiter(this, void 0, void 0, function () {
462
+ var coupon, isInShop, isCheckoutType, hasMinSubTotal, hasProductCategories;
463
+ return __generator(this, function (_d) {
464
+ switch (_d.label) {
465
+ case 0:
466
+ // Caso não ache nenhum cupom, retorna erro
467
+ if (coupons.count < 1) {
468
+ return [2 /*return*/, rxjs.throwError('Cupom inválido.')];
469
+ }
470
+ coupon = coupons.data.shift();
471
+ isInShop = coupon.shopAvailability === i2.Shops.ALL || coupon.shopAvailability === this.defaultShop;
472
+ // Cupon não aplicavel a loja retorna erro
473
+ if (!isInShop)
474
+ return [2 /*return*/, rxjs.throwError('Cupom inválido para loja.')];
475
+ isCheckoutType = coupon.checkoutType === i2.CheckoutTypes.ALL || coupon.checkoutType === checkoutType;
476
+ // Cupon não aplicavel ao checkout retorna erro
477
+ if (!isCheckoutType)
478
+ return [2 /*return*/, rxjs.throwError('Cupom inválido. Erro de checkout.')];
479
+ // Verifica se o cupom é ou pode ser aplicado para subscription
480
+ if (checkoutType === i2.CheckoutTypes.ALL || checkoutType === i2.CheckoutTypes.SUBSCRIPTION) {
481
+ // Se o cupom tiver um plano associado, verifica se é o mesmo plano do checkout da assinatura
482
+ if (coupon.plan && coupon.plan.toUpperCase() !== plan.toUpperCase())
483
+ return [2 /*return*/, rxjs.throwError('Cupom inválido para sua assinatura.')];
484
+ }
485
+ if (isSubscription)
486
+ return [2 /*return*/, rxjs.of(coupon)];
487
+ hasMinSubTotal = this.hasMinSubTotal(coupon, checkout);
488
+ // Se não tem valor mínimo atingido, retorna erro
489
+ if (!hasMinSubTotal)
490
+ return [2 /*return*/, rxjs.throwError('Valor mínimo não atingido')];
491
+ return [4 /*yield*/, this.hasProductCategories(coupon, checkout)];
492
+ case 1:
493
+ hasProductCategories = _d.sent();
494
+ console.log('hasProductCategories', hasProductCategories);
495
+ // Se não tem produtos elegíveis, retorna erro
496
+ if (!hasProductCategories)
497
+ return [2 /*return*/, rxjs.throwError('Seu carrinho não possui produtos elegíveis para desconto.')];
498
+ // return of(coupon)
499
+ return [2 /*return*/, coupon];
500
+ }
501
+ });
502
+ });
486
503
  };
487
504
  CouponService.prototype.isValidCoupon = function (coupon, userEmail) {
488
505
  // Verifica a data de inicio de validade do cupom
@@ -493,9 +510,9 @@
493
510
  throw new InvalidCouponError('Cupom expirado.');
494
511
  return coupon;
495
512
  };
496
- CouponService.prototype.checkCouponUseAndLimit = function (coupon, userEmail, checkout) {
513
+ CouponService.prototype.checkCouponUseAndLimit = function (coupon, userEmail) {
497
514
  return __awaiter(this, void 0, void 0, function () {
498
- var orders, ordersUserCoupon, validUser, hasProductCategories;
515
+ var orders, ordersUserCoupon, validUser;
499
516
  return __generator(this, function (_d) {
500
517
  switch (_d.label) {
501
518
  case 0: return [4 /*yield*/, this.orderRepository.find({
@@ -518,12 +535,6 @@
518
535
  validUser = _d.sent();
519
536
  if (!validUser)
520
537
  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
538
  return [2 /*return*/, coupon];
528
539
  }
529
540
  });
@@ -539,69 +550,44 @@
539
550
  return rxjs.of(discount);
540
551
  };
541
552
  CouponService.prototype.calcDiscountShopping = function (coupon, checkout) {
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
+ var discount = 0;
554
+ switch (coupon.type) {
555
+ case i2.CouponTypes.ABSOLUTE: {
556
+ discount = coupon.discount;
557
+ break;
558
+ }
559
+ case i2.CouponTypes.PERCENTAGE: {
560
+ discount = this.calcShoppingPercentageDiscount(coupon, checkout);
561
+ break;
562
+ }
563
+ }
564
+ return rxjs.of(discount);
568
565
  };
569
566
  CouponService.prototype.calcShoppingPercentageDiscount = function (coupon, checkout) {
570
567
  var _a;
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];
568
+ var discount = 0;
569
+ var shop = checkout.shop;
570
+ var lineItensDiscount = [];
571
+ if (coupon.productsCategories && coupon.productsCategories.length) {
572
+ lineItensDiscount = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
573
+ var _a;
574
+ if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
575
+ return i.categories.some(function (c) { return coupon.productsCategories.includes(c); });
602
576
  }
577
+ return true;
603
578
  });
604
- });
579
+ }
580
+ else {
581
+ lineItensDiscount = checkout.lineItems;
582
+ }
583
+ var subTotal = lineItensDiscount.reduce(function (acc, curr) {
584
+ var _a, _b, _c;
585
+ return ((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.isSubscriber) && ((_b = curr.price[shop]) === null || _b === void 0 ? void 0 : _b.subscriberPrice)
586
+ ? acc + ((_c = curr.price[shop]) === null || _c === void 0 ? void 0 : _c.subscriberPrice) * curr.quantity
587
+ : acc + curr.pricePaid * curr.quantity;
588
+ }, 0) || 0;
589
+ discount = subTotal * (coupon.discount / 100);
590
+ return discount;
605
591
  };
606
592
  CouponService.prototype.hasMinSubTotal = function (coupon, checkout) {
607
593
  var _a;
@@ -621,32 +607,38 @@
621
607
  CouponService.prototype.hasProductCategories = function (coupon, checkout) {
622
608
  var _a;
623
609
  return __awaiter(this, void 0, void 0, function () {
624
- var couponCategories, hasCategories;
610
+ var hasCategories;
611
+ var _this = this;
625
612
  return __generator(this, function (_d) {
626
- switch (_d.label) {
627
- case 0:
628
- if (!coupon.productsCategories && !coupon.productsCategories.length) {
629
- return [2 /*return*/, true];
613
+ if (coupon.productsCategories && coupon.productsCategories.length) {
614
+ hasCategories = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
615
+ var _a;
616
+ if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
617
+ // buscar categoria por id
618
+ // filtrar por id ou firestore id
619
+ return i.categories.some(function (c) { return __awaiter(_this, void 0, void 0, function () {
620
+ var categoria;
621
+ return __generator(this, function (_d) {
622
+ switch (_d.label) {
623
+ case 0: return [4 /*yield*/, this.categoryRepository.get({ id: c })];
624
+ case 1:
625
+ categoria = _d.sent();
626
+ console.log(categoria);
627
+ console.log(coupon.productsCategories.includes(categoria.id), coupon.productsCategories.includes(categoria.firestoreId), i.isGift);
628
+ console.log('validation', (coupon.productsCategories.includes(categoria.id) || coupon.productsCategories.includes(categoria.firestoreId)) && !i.isGift);
629
+ return [2 /*return*/, (coupon.productsCategories.includes(categoria.id) || coupon.productsCategories.includes(categoria.firestoreId)) && !i.isGift];
630
+ }
631
+ });
632
+ }); });
630
633
  }
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
- // })
647
- });
648
- return [2 /*return*/, hasCategories.length ? true : false];
634
+ return true;
635
+ });
636
+ return [2 /*return*/, hasCategories.length ? true : false];
649
637
  }
638
+ else {
639
+ return [2 /*return*/, true];
640
+ }
641
+ return [2 /*return*/];
650
642
  });
651
643
  });
652
644
  };
@@ -694,30 +686,6 @@
694
686
  });
695
687
  });
696
688
  };
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
- };
721
689
  return CouponService;
722
690
  }());
723
691
  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 });
@@ -776,12 +744,7 @@
776
744
  };
777
745
  CheckoutService.prototype.calcDiscount = function (coupon) {
778
746
  var _this = this;
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
- }); }); }));
747
+ return this.getCheckout().pipe(operators.concatMap(function (checkout) { return _this.couponService.calcDiscountShopping(coupon, checkout).pipe(); }));
785
748
  };
786
749
  CheckoutService.prototype.checkCoupon = function (nickname, checkoutType) {
787
750
  var _this = this;