@infrab4a/connect-angular 0.14.2-beta.2 → 0.14.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.
@@ -414,16 +414,15 @@
414
414
  }(Error));
415
415
 
416
416
  var CouponService = /** @class */ (function () {
417
- function CouponService(couponRepository, defaultShop, orderRepository) {
417
+ function CouponService(couponRepository, defaultShop) {
418
418
  var _this = this;
419
419
  this.couponRepository = couponRepository;
420
420
  this.defaultShop = defaultShop;
421
- this.orderRepository = orderRepository;
422
421
  this.emailIsFromCollaborator = function (userEmail) { return !!(userEmail === null || userEmail === void 0 ? void 0 : userEmail.match(/@b4a.com.br/g)); };
423
422
  this.separateValidCoupons = function (coupons, userEmail) { return coupons
424
423
  .map(function (coupon) {
425
424
  try {
426
- if (!(coupon instanceof i2.Coupon))
425
+ if (!i2.FinancialCoupon.isFinancialCoupon(coupon))
427
426
  throw new InvalidCouponError('Coupon is not valid');
428
427
  if (_this.isValidCoupon(coupon, userEmail))
429
428
  return coupon;
@@ -432,22 +431,27 @@
432
431
  return error;
433
432
  }
434
433
  })
435
- .reduce(function (current, coupon) { return (Object.assign(Object.assign({}, current), (coupon instanceof i2.Coupon
434
+ .reduce(function (current, coupon) { return (Object.assign(Object.assign({}, current), (i2.FinancialCoupon.isFinancialCoupon(coupon)
436
435
  ? { valids: __spreadArray(__spreadArray([], __read(current.valids)), [coupon]) }
437
436
  : { invalids: __spreadArray(__spreadArray([], __read(current.invalids)), [coupon]) }))); }, {
438
437
  valids: [],
439
438
  invalids: [],
440
439
  }); };
441
440
  }
442
- CouponService.prototype.checkCoupon = function (nickname, userEmail, checkoutType, plan) {
441
+ CouponService.prototype.checkCoupon = function (nickname, userEmail, checkoutType) {
443
442
  var _this = this;
444
443
  return rxjs.from(this.couponRepository.find([
445
444
  {
446
445
  nickname: { operator: i2.Where.EQUALS, value: nickname },
447
- // shopAvailability: { operator: Where.EQUALS, value: this.defaultShop },
448
- // checkoutType: { operator: Where.EQUALS, value: checkoutType },
446
+ shopAvailability: { operator: i2.Where.EQUALS, value: this.defaultShop },
447
+ checkoutType: { operator: i2.Where.EQUALS, value: checkoutType },
449
448
  },
450
- ])).pipe(operators.concatMap(function (coupons) { return _this.checkCouponRules(coupons, checkoutType); }), operators.concatMap(function (coupons) { return _this.checkCouponUseLimit(coupons, userEmail); }), operators.map(function (coupon) { return _this.isValidCoupon(coupon, userEmail); }));
449
+ ])).pipe(operators.concatMap(function (coupons) { return coupons.count < 1 ? rxjs.throwError(function () { return new i2.NotFoundError('Coupon not found'); }) : rxjs.of(coupons.data); }), operators.map(function (coupons) { return _this.separateValidCoupons(coupons, userEmail); }), operators.map(function (_a) {
450
+ var valids = _a.valids, invalids = _a.invalids;
451
+ if (!valids.length)
452
+ throw new GroupInvalidCouponError(invalids);
453
+ return valids;
454
+ }));
451
455
  };
452
456
  CouponService.prototype.isValidCoupon = function (coupon, userEmail) {
453
457
  if (!this.emailIsFromCollaborator(userEmail) && coupon.exclusivityType === i2.Exclusivities.COLLABORATORS)
@@ -456,47 +460,11 @@
456
460
  throw new InvalidCouponError('Coupon is not valid for user');
457
461
  if ((coupon === null || coupon === void 0 ? void 0 : coupon.expiresIn) < new Date())
458
462
  throw new InvalidCouponError('Coupon is expired');
459
- return coupon;
460
- };
461
- CouponService.prototype.checkCouponRules = function (coupons, checkoutType, plan) {
462
- var _this = this;
463
- // coupons.count < 1 ? return throwError(() => new NotFoundError('Coupon not found')) : return of(coupons.data);
464
- if (coupons.count < 1)
465
- return rxjs.throwError(function () { return new i2.NotFoundError('Coupon not found'); });
466
- var isInShop = coupons.data.find(function (c) { return c.shopAvailability == i2.Shops.ALL || c.shopAvailability == _this.defaultShop; });
467
- if (!isInShop)
468
- return rxjs.throwError(function () { return new i2.NotFoundError('Coupon not found'); });
469
- var isCheckoutType = coupons.data.find(function (c) { return c.checkoutType == i2.CheckoutTypes.ALL || c.checkoutType == checkoutType; });
470
- if (!isCheckoutType)
471
- return rxjs.throwError(function () { return new i2.NotFoundError('Coupon not found'); });
472
- if (checkoutType === i2.CheckoutTypes.ALL || checkoutType === i2.CheckoutTypes.SUBSCRIPTION) {
473
- //regra
474
- }
475
- return coupons.data;
476
- };
477
- CouponService.prototype.checkCouponUseLimit = function (coupons, userEmail) {
478
- return __awaiter(this, void 0, void 0, function () {
479
- var coupon, orders, ordersUserCoupon;
480
- return __generator(this, function (_a) {
481
- switch (_a.label) {
482
- case 0:
483
- coupon = coupons.shift();
484
- return [4 /*yield*/, this.orderRepository.find([{ coupon: { id: coupon.id } }])];
485
- case 1:
486
- orders = _a.sent();
487
- ordersUserCoupon = orders.data.filter(function (o) { return o.user.email == userEmail; });
488
- if (orders.data.length >= (coupon.useLimit || Infinity))
489
- return [2 /*return*/, rxjs.throwError(function () { return new Error('Coupon já usou tudo'); })];
490
- if (ordersUserCoupon.length >= coupon.useLimitPerUser)
491
- return [2 /*return*/, rxjs.throwError(function () { return new Error('Usuário Coupon já usou tudo'); })];
492
- return [2 /*return*/, coupon];
493
- }
494
- });
495
- });
463
+ return true;
496
464
  };
497
465
  return CouponService;
498
466
  }());
499
- 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' }], target: i0__namespace.ɵɵFactoryTarget.Injectable });
467
+ CouponService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0__namespace, type: CouponService, deps: [{ token: 'CouponRepository' }, { token: DEFAULT_SHOP }], target: i0__namespace.ɵɵFactoryTarget.Injectable });
500
468
  CouponService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0__namespace, type: CouponService, providedIn: 'root' });
501
469
  i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0__namespace, type: CouponService, decorators: [{
502
470
  type: i0.Injectable,
@@ -510,9 +478,6 @@
510
478
  }] }, { type: i2__namespace.Shops, decorators: [{
511
479
  type: i0.Inject,
512
480
  args: [DEFAULT_SHOP]
513
- }] }, { type: undefined, decorators: [{
514
- type: i0.Inject,
515
- args: ['OrderRepository']
516
481
  }] }];
517
482
  } });
518
483
 
@@ -544,37 +509,30 @@
544
509
  cookie__default["default"].remove('checkoutId');
545
510
  return rxjs.of();
546
511
  };
547
- CheckoutService.prototype.applyCupon = function (coupon) { };
548
- // checkCoupon(nickname: string): Observable<FinancialCoupon> {
549
- // return this.getCheckout().pipe(
550
- // concatMap((checkout) =>
551
- // this.couponService.checkCoupon(nickname, checkout.user?.email, CheckoutTypes.ECOMMERCE).pipe(
552
- // concatMap((coupons) => of(coupons.shift())),
553
- // concatMap((coupon) =>
554
- // !!checkout.user?.email
555
- // ? from(
556
- // this.orderRepository.find([{ user: { email: checkout.user?.email }, coupon: { id: coupon.id } }]),
557
- // ).pipe(
558
- // concatMap((orders) => {
559
- // if (orders.data.length >= (coupon.useLimit || Infinity))
560
- // return throwError(() => new Error('Coupon is already applied'))
561
- // return of(coupon)
562
- // }),
563
- // )
564
- // : of(coupon),
565
- // ),
566
- // ),
567
- // ),
568
- // )
569
- // }
512
+ CheckoutService.prototype.checkCoupon = function (nickname) {
513
+ var _this = this;
514
+ return this.getCheckout().pipe(operators.concatMap(function (checkout) {
515
+ var _a;
516
+ return _this.couponService.checkCoupon(nickname, (_a = checkout.user) === null || _a === void 0 ? void 0 : _a.email, i2.CheckoutTypes.ECOMMERCE).pipe(operators.concatMap(function (coupons) { return rxjs.of(coupons.shift()); }), operators.concatMap(function (coupon) {
517
+ var _a, _b;
518
+ return !!((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.email)
519
+ ? rxjs.from(_this.orderRepository.find([{ user: { email: (_b = checkout.user) === null || _b === void 0 ? void 0 : _b.email }, coupon: { id: coupon.id } }])).pipe(operators.concatMap(function (orders) {
520
+ if (orders.data.length >= (coupon.useLimit || Infinity))
521
+ return rxjs.throwError(function () { return new Error('Coupon is already applied'); });
522
+ return rxjs.of(coupon);
523
+ }))
524
+ : rxjs.of(coupon);
525
+ }));
526
+ }));
527
+ };
570
528
  CheckoutService.prototype.createCheckout = function (checkoutData) {
571
529
  return __awaiter(this, void 0, void 0, function () {
572
530
  var checkout;
573
- return __generator(this, function (_b) {
574
- switch (_b.label) {
531
+ return __generator(this, function (_c) {
532
+ switch (_c.label) {
575
533
  case 0: return [4 /*yield*/, this.checkoutRepository.create(Object.assign(Object.assign({ createdAt: new Date() }, i2.Checkout.toInstance(i2.pick(checkoutData, ['user', 'shop'])).toPlain()), { shop: (checkoutData === null || checkoutData === void 0 ? void 0 : checkoutData.shop) || this.defaultShop }))];
576
534
  case 1:
577
- checkout = _b.sent();
535
+ checkout = _c.sent();
578
536
  cookie__default["default"].set('checkoutId', checkout.id);
579
537
  return [2 /*return*/, checkout];
580
538
  }
@@ -639,7 +597,7 @@
639
597
  if (_this.checkMaxStock(item, quantity || 0))
640
598
  throw new Error('Desculpe! Temos apenas ' + ((_c = item.stock) === null || _c === void 0 ? void 0 : _c.quantity) + ' em estoque.');
641
599
  var image = item.image || ((_d = item.images) === null || _d === void 0 ? void 0 : _d.shift());
642
- var id = item.id, name = item.name, brand = item.brand, slug = item.slug, stock = item.stock, price = item.price, weight = item.weight, categories = item.categories, sku = item.sku;
600
+ var id = item.id, name = item.name, EAN = item.EAN, brand = item.brand, slug = item.slug, stock = item.stock, price = item.price, weight = item.weight, categories = item.categories, sku = item.sku;
643
601
  var isGift = item.isGift || null;
644
602
  var pricePaid = _this.getProductPrice({
645
603
  product: item,
@@ -649,6 +607,7 @@
649
607
  return i2.LineItem.toInstance({
650
608
  id: id,
651
609
  name: name,
610
+ EAN: EAN,
652
611
  brand: brand,
653
612
  slug: slug,
654
613
  sku: sku,
@@ -703,6 +662,21 @@
703
662
  this.buildCartFromCheckout(checkout).subscribe(function (cart) { return _this.cartSubject.next(cart); });
704
663
  return this.cartSubject;
705
664
  };
665
+ /**
666
+ * @deprecated The method should not be used
667
+ */
668
+ CartService.prototype.getDiscount = function (coupon) {
669
+ switch (coupon.type) {
670
+ case i2.CouponTypes.FINANCIAL:
671
+ return this.getFinancialDiscount(coupon);
672
+ case i2.CouponTypes.PRODUCT:
673
+ return function (subTotalPrice) { return subTotalPrice; };
674
+ case i2.CouponTypes.GIFTCARD:
675
+ return this.getFinancialDiscount(coupon);
676
+ case i2.CouponTypes.VOUCHER:
677
+ return function (subTotalPrice) { return subTotalPrice; };
678
+ }
679
+ };
706
680
  /**
707
681
  * @deprecated The method should not be used
708
682
  */
@@ -736,14 +710,15 @@
736
710
  var _this = this;
737
711
  return this.checkoutService.getCheckout(checkoutData).pipe(operators.map(function (checkout) { return checkout.lineItems; }), operators.concatMap(function (lineItems) { return rxjs.of(_this.generateCartObject(lineItems)); }));
738
712
  };
739
- CartService.prototype.getSubTotalDiscount = function (coupon) {
713
+ CartService.prototype.getFinancialDiscount = function (coupon) {
740
714
  return function (subTotalPrice) {
741
- switch (coupon.type) {
742
- case i2.CouponTypes.ABSOLUTE:
743
- return subTotalPrice - coupon.discount;
744
- case i2.CouponTypes.PERCENTAGE:
745
- return subTotalPrice * (1 - coupon.discount / 100);
746
- }
715
+ if (i2.FinancialCoupon.isFinancialCoupon(coupon))
716
+ switch (coupon.subtype) {
717
+ case i2.CouponSubtypes.ABSOLUTE:
718
+ return subTotalPrice - coupon.discount * 100;
719
+ case i2.CouponSubtypes.PERCENTAGE:
720
+ return subTotalPrice * (1 - coupon.discount / 100);
721
+ }
747
722
  };
748
723
  };
749
724
  return CartService;
@@ -761,9 +736,25 @@
761
736
 
762
737
  var CheckoutSubscriptionService = /** @class */ (function () {
763
738
  function CheckoutSubscriptionService(checkoutSubscriptionRepository, subscriptionRepository, couponService) {
739
+ var _this = this;
764
740
  this.checkoutSubscriptionRepository = checkoutSubscriptionRepository;
765
741
  this.subscriptionRepository = subscriptionRepository;
766
742
  this.couponService = couponService;
743
+ this.checkCouponsWithCheckout = function (coupons, checkout) { return rxjs.forkJoin(coupons.map(function (coupon) {
744
+ var _a;
745
+ return rxjs.from(_this.subscriptionRepository.find([{ user: { email: (_a = checkout.user) === null || _a === void 0 ? void 0 : _a.email }, coupon: { id: coupon.id } }])).pipe(operators.map(function (subscriptions) { return ({ coupon: coupon, subscriptions: subscriptions }); }));
746
+ })).pipe(operators.map(function (results) { return results.map(function (_b) {
747
+ var coupon = _b.coupon, subscriptions = _b.subscriptions;
748
+ if (subscriptions.data.length >= (coupon.useLimit || Infinity))
749
+ return new InvalidCouponError('Coupon is already applied');
750
+ return coupon;
751
+ }); }), operators.map(function (validatedCoupons) { return ({
752
+ valids: validatedCoupons.filter(function (coupon) { return i2.FinancialCoupon.isFinancialCoupon(coupon); }),
753
+ invalids: validatedCoupons.filter(function (coupon) { return coupon instanceof InvalidCouponError; }),
754
+ }); }), operators.concatMap(function (_b) {
755
+ var valids = _b.valids, invalids = _b.invalids;
756
+ return valids.length ? rxjs.of(valids) : rxjs.throwError(new GroupInvalidCouponError(invalids));
757
+ })); };
767
758
  }
768
759
  CheckoutSubscriptionService.prototype.getCheckoutSubscription = function (checkoutData) {
769
760
  var checkoutId = cookie__default["default"].get('checkoutSubscriptionId');
@@ -774,11 +765,11 @@
774
765
  CheckoutSubscriptionService.prototype.createCheckoutSubscription = function (checkoutData) {
775
766
  return __awaiter(this, void 0, void 0, function () {
776
767
  var checkout;
777
- return __generator(this, function (_a) {
778
- switch (_a.label) {
768
+ return __generator(this, function (_b) {
769
+ switch (_b.label) {
779
770
  case 0: return [4 /*yield*/, this.checkoutSubscriptionRepository.create(Object.assign({ createdAt: new Date() }, i2.CheckoutSubscription.toInstance(i2.pick(checkoutData, ['user', 'shop'])).toPlain()))];
780
771
  case 1:
781
- checkout = _a.sent();
772
+ checkout = _b.sent();
782
773
  cookie__default["default"].set('checkoutSubscriptionId', checkout.id);
783
774
  return [2 /*return*/, checkout];
784
775
  }
@@ -789,6 +780,18 @@
789
780
  cookie__default["default"].remove('checkoutSubscriptionId');
790
781
  return rxjs.of();
791
782
  };
783
+ CheckoutSubscriptionService.prototype.checkCoupon = function (nickname, userEmail) {
784
+ var _this = this;
785
+ return this.getCheckoutSubscription().pipe(operators.concatMap(function (checkout) { return _this.couponService.checkCoupon(nickname, userEmail, i2.CheckoutTypes.SUBSCRIPTION).pipe(operators.concatMap(function (coupons) {
786
+ var couponsWithSamePlan = coupons.filter(function (coupon) { return checkout.subscriptionPlan.name === coupon.plan; });
787
+ var couponsWithNoPlan = coupons.filter(function (coupon) { return !coupon.plan; });
788
+ if (couponsWithSamePlan.length > 0)
789
+ return rxjs.of(couponsWithSamePlan);
790
+ if (couponsWithNoPlan.length > 0)
791
+ return rxjs.of(couponsWithNoPlan);
792
+ throw new Error("Coupon subscription plan is invalid.");
793
+ }), operators.concatMap(function (coupons) { var _a; return !!((_a = checkout.user) === null || _a === void 0 ? void 0 : _a.email) ? _this.checkCouponsWithCheckout(coupons, checkout) : rxjs.of(coupons); }), operators.map(function (validatedCoupons) { return validatedCoupons.shift(); })); }));
794
+ };
792
795
  return CheckoutSubscriptionService;
793
796
  }());
794
797
  CheckoutSubscriptionService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0__namespace, type: CheckoutSubscriptionService, deps: [{ token: 'CheckoutSubscriptionRepository' }, { token: 'SubscriptionRepository' }, { token: CouponService }], target: i0__namespace.ɵɵFactoryTarget.Injectable });
@@ -1135,10 +1138,10 @@
1135
1138
  },
1136
1139
  {
1137
1140
  provide: 'CategoryRepository',
1138
- useFactory: function (firestore, productsIndex) {
1141
+ useFactory: function (firestore) {
1139
1142
  return new i2.CategoryFirestoreRepository(firestore.firestore);
1140
1143
  },
1141
- deps: [i1$1.AngularFirestore, i2.ProductsIndex],
1144
+ deps: [i1$1.AngularFirestore],
1142
1145
  },
1143
1146
  {
1144
1147
  provide: 'CheckoutRepository',
@@ -1295,10 +1298,10 @@
1295
1298
  },
1296
1299
  {
1297
1300
  provide: 'CategoryRepository',
1298
- useFactory: function (firestore, productsIndex) {
1301
+ useFactory: function (firestore) {
1299
1302
  return new i2.CategoryFirestoreRepository(firestore.firestore);
1300
1303
  },
1301
- deps: [i1$1.AngularFirestore, i2.ProductsIndex],
1304
+ deps: [i1$1.AngularFirestore],
1302
1305
  },
1303
1306
  {
1304
1307
  provide: 'CheckoutRepository',