@infrab4a/connect-angular 0.14.2-beta.3 → 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.
- package/bundles/infrab4a-connect-angular.umd.js +90 -69
- package/bundles/infrab4a-connect-angular.umd.js.map +1 -1
- package/esm2015/angular-firestore.module.js +6 -6
- package/esm2015/services/cart.service.js +27 -10
- package/esm2015/services/checkout-subscription.service.js +27 -3
- package/esm2015/services/checkout.service.js +14 -3
- package/esm2015/services/coupon.service.js +17 -45
- package/fesm2015/infrab4a-connect-angular.js +78 -55
- package/fesm2015/infrab4a-connect-angular.js.map +1 -1
- package/package.json +2 -2
- package/services/cart.service.d.ts +8 -2
- package/services/checkout-subscription.service.d.ts +3 -1
- package/services/checkout.service.d.ts +2 -3
- package/services/coupon.service.d.ts +3 -6
|
@@ -414,16 +414,15 @@
|
|
|
414
414
|
}(Error));
|
|
415
415
|
|
|
416
416
|
var CouponService = /** @class */ (function () {
|
|
417
|
-
function CouponService(couponRepository, defaultShop
|
|
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
|
|
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), (
|
|
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
|
|
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
|
-
|
|
448
|
-
|
|
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
|
|
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
|
|
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 }
|
|
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,19 +509,30 @@
|
|
|
544
509
|
cookie__default["default"].remove('checkoutId');
|
|
545
510
|
return rxjs.of();
|
|
546
511
|
};
|
|
547
|
-
CheckoutService.prototype.applyCupon = function (coupon) { };
|
|
548
512
|
CheckoutService.prototype.checkCoupon = function (nickname) {
|
|
549
513
|
var _this = this;
|
|
550
|
-
return this.getCheckout().pipe(operators.concatMap(function (checkout) {
|
|
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
|
+
}));
|
|
551
527
|
};
|
|
552
528
|
CheckoutService.prototype.createCheckout = function (checkoutData) {
|
|
553
529
|
return __awaiter(this, void 0, void 0, function () {
|
|
554
530
|
var checkout;
|
|
555
|
-
return __generator(this, function (
|
|
556
|
-
switch (
|
|
531
|
+
return __generator(this, function (_c) {
|
|
532
|
+
switch (_c.label) {
|
|
557
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 }))];
|
|
558
534
|
case 1:
|
|
559
|
-
checkout =
|
|
535
|
+
checkout = _c.sent();
|
|
560
536
|
cookie__default["default"].set('checkoutId', checkout.id);
|
|
561
537
|
return [2 /*return*/, checkout];
|
|
562
538
|
}
|
|
@@ -621,7 +597,7 @@
|
|
|
621
597
|
if (_this.checkMaxStock(item, quantity || 0))
|
|
622
598
|
throw new Error('Desculpe! Temos apenas ' + ((_c = item.stock) === null || _c === void 0 ? void 0 : _c.quantity) + ' em estoque.');
|
|
623
599
|
var image = item.image || ((_d = item.images) === null || _d === void 0 ? void 0 : _d.shift());
|
|
624
|
-
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;
|
|
625
601
|
var isGift = item.isGift || null;
|
|
626
602
|
var pricePaid = _this.getProductPrice({
|
|
627
603
|
product: item,
|
|
@@ -631,6 +607,7 @@
|
|
|
631
607
|
return i2.LineItem.toInstance({
|
|
632
608
|
id: id,
|
|
633
609
|
name: name,
|
|
610
|
+
EAN: EAN,
|
|
634
611
|
brand: brand,
|
|
635
612
|
slug: slug,
|
|
636
613
|
sku: sku,
|
|
@@ -685,6 +662,21 @@
|
|
|
685
662
|
this.buildCartFromCheckout(checkout).subscribe(function (cart) { return _this.cartSubject.next(cart); });
|
|
686
663
|
return this.cartSubject;
|
|
687
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
|
+
};
|
|
688
680
|
/**
|
|
689
681
|
* @deprecated The method should not be used
|
|
690
682
|
*/
|
|
@@ -718,14 +710,15 @@
|
|
|
718
710
|
var _this = this;
|
|
719
711
|
return this.checkoutService.getCheckout(checkoutData).pipe(operators.map(function (checkout) { return checkout.lineItems; }), operators.concatMap(function (lineItems) { return rxjs.of(_this.generateCartObject(lineItems)); }));
|
|
720
712
|
};
|
|
721
|
-
CartService.prototype.
|
|
713
|
+
CartService.prototype.getFinancialDiscount = function (coupon) {
|
|
722
714
|
return function (subTotalPrice) {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
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
|
+
}
|
|
729
722
|
};
|
|
730
723
|
};
|
|
731
724
|
return CartService;
|
|
@@ -743,9 +736,25 @@
|
|
|
743
736
|
|
|
744
737
|
var CheckoutSubscriptionService = /** @class */ (function () {
|
|
745
738
|
function CheckoutSubscriptionService(checkoutSubscriptionRepository, subscriptionRepository, couponService) {
|
|
739
|
+
var _this = this;
|
|
746
740
|
this.checkoutSubscriptionRepository = checkoutSubscriptionRepository;
|
|
747
741
|
this.subscriptionRepository = subscriptionRepository;
|
|
748
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
|
+
})); };
|
|
749
758
|
}
|
|
750
759
|
CheckoutSubscriptionService.prototype.getCheckoutSubscription = function (checkoutData) {
|
|
751
760
|
var checkoutId = cookie__default["default"].get('checkoutSubscriptionId');
|
|
@@ -756,11 +765,11 @@
|
|
|
756
765
|
CheckoutSubscriptionService.prototype.createCheckoutSubscription = function (checkoutData) {
|
|
757
766
|
return __awaiter(this, void 0, void 0, function () {
|
|
758
767
|
var checkout;
|
|
759
|
-
return __generator(this, function (
|
|
760
|
-
switch (
|
|
768
|
+
return __generator(this, function (_b) {
|
|
769
|
+
switch (_b.label) {
|
|
761
770
|
case 0: return [4 /*yield*/, this.checkoutSubscriptionRepository.create(Object.assign({ createdAt: new Date() }, i2.CheckoutSubscription.toInstance(i2.pick(checkoutData, ['user', 'shop'])).toPlain()))];
|
|
762
771
|
case 1:
|
|
763
|
-
checkout =
|
|
772
|
+
checkout = _b.sent();
|
|
764
773
|
cookie__default["default"].set('checkoutSubscriptionId', checkout.id);
|
|
765
774
|
return [2 /*return*/, checkout];
|
|
766
775
|
}
|
|
@@ -771,6 +780,18 @@
|
|
|
771
780
|
cookie__default["default"].remove('checkoutSubscriptionId');
|
|
772
781
|
return rxjs.of();
|
|
773
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
|
+
};
|
|
774
795
|
return CheckoutSubscriptionService;
|
|
775
796
|
}());
|
|
776
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 });
|
|
@@ -1117,10 +1138,10 @@
|
|
|
1117
1138
|
},
|
|
1118
1139
|
{
|
|
1119
1140
|
provide: 'CategoryRepository',
|
|
1120
|
-
useFactory: function (firestore
|
|
1141
|
+
useFactory: function (firestore) {
|
|
1121
1142
|
return new i2.CategoryFirestoreRepository(firestore.firestore);
|
|
1122
1143
|
},
|
|
1123
|
-
deps: [i1$1.AngularFirestore
|
|
1144
|
+
deps: [i1$1.AngularFirestore],
|
|
1124
1145
|
},
|
|
1125
1146
|
{
|
|
1126
1147
|
provide: 'CheckoutRepository',
|
|
@@ -1277,10 +1298,10 @@
|
|
|
1277
1298
|
},
|
|
1278
1299
|
{
|
|
1279
1300
|
provide: 'CategoryRepository',
|
|
1280
|
-
useFactory: function (firestore
|
|
1301
|
+
useFactory: function (firestore) {
|
|
1281
1302
|
return new i2.CategoryFirestoreRepository(firestore.firestore);
|
|
1282
1303
|
},
|
|
1283
|
-
deps: [i1$1.AngularFirestore
|
|
1304
|
+
deps: [i1$1.AngularFirestore],
|
|
1284
1305
|
},
|
|
1285
1306
|
{
|
|
1286
1307
|
provide: 'CheckoutRepository',
|