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

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,56 +450,39 @@
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 __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; }));
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; }));
459
454
  };
460
455
  CouponService.prototype.checkCouponRules = function (coupons, checkoutType, plan, checkout, isSubscription) {
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
- });
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);
503
486
  };
504
487
  CouponService.prototype.isValidCoupon = function (coupon, userEmail) {
505
488
  // Verifica a data de inicio de validade do cupom
@@ -510,9 +493,9 @@
510
493
  throw new InvalidCouponError('Cupom expirado.');
511
494
  return coupon;
512
495
  };
513
- CouponService.prototype.checkCouponUseAndLimit = function (coupon, userEmail) {
496
+ CouponService.prototype.checkCouponUseAndLimit = function (coupon, userEmail, checkout) {
514
497
  return __awaiter(this, void 0, void 0, function () {
515
- var orders, ordersUserCoupon, validUser;
498
+ var orders, ordersUserCoupon, validUser, hasProductCategories;
516
499
  return __generator(this, function (_d) {
517
500
  switch (_d.label) {
518
501
  case 0: return [4 /*yield*/, this.orderRepository.find({
@@ -535,6 +518,13 @@
535
518
  validUser = _d.sent();
536
519
  if (!validUser)
537
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
+ console.log('hasProductCategories', hasProductCategories);
525
+ // Se não tem produtos elegíveis, retorna erro
526
+ if (!hasProductCategories)
527
+ return [2 /*return*/, rxjs.throwError('Seu carrinho não possui produtos elegíveis para desconto.')];
538
528
  return [2 /*return*/, coupon];
539
529
  }
540
530
  });
@@ -614,8 +604,6 @@
614
604
  hasCategories = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter(function (i) {
615
605
  var _a;
616
606
  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
607
  return i.categories.some(function (c) { return __awaiter(_this, void 0, void 0, function () {
620
608
  var categoria;
621
609
  return __generator(this, function (_d) {