@infrab4a/connect-angular 3.8.0-beta.9 → 3.8.1-beta.0

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.
@@ -5,13 +5,13 @@ import { AngularFireAuth } from '@angular/fire/auth';
5
5
  import { of, combineLatest, from, throwError, Subject, iif, forkJoin } from 'rxjs';
6
6
  import { catchError, map, mergeMap, concatMap, tap } from 'rxjs/operators';
7
7
  import { __awaiter } from 'tslib';
8
- import * as i1$1 from '@infrab4a/connect';
8
+ import * as i3 from '@infrab4a/connect';
9
9
  import { Coupon, Where, Shops, CheckoutTypes, CouponTypes, Exclusivities, isNil, NotFoundError, Checkout, pick, LineItem, CheckoutSubscription, Category, Product, RequiredArgumentError, add, Order, Authentication, AuthenticationFirebaseAuthService, Register, RegisterFirebaseAuthService, SignOut, RecoveryPassword, ProductsIndex, AxiosAdapter, UserBeautyProfileFirestoreRepository, Buy2WinFirestoreRepository, CategoryFirestoreRepository, CheckoutFirestoreRepository, CheckoutSubscriptionFirestoreRepository, CouponFirestoreRepository, CampaignHashtagFirestoreRepository, CampaignDashboardFirestoreRepository, SubscriptionEditionFirestoreRepository, HomeFirestoreRepository, LeadFirestoreRepository, LegacyOrderFirestoreRepository, ShopMenuFirestoreRepository, OrderFirestoreRepository, PaymentFirestoreRepository, ProductFirestoreRepository, ShopSettingsFirestoreRepository, SubscriptionPaymentFirestoreRepository, SubscriptionPlanFirestoreRepository, SubscriptionProductFirestoreRepository, SubscriptionFirestoreRepository, UserFirestoreRepository, UserAddressFirestoreRepository, UserPaymentMethodFirestoreRepository, ProductVariantFirestoreRepository, CategoryHasuraGraphQLRepository, ProductHasuraGraphQLRepository, CategoryFilterHasuraGraphQLRepository, VariantHasuraGraphQLRepository, FilterOptionHasuraGraphQLRepository, FilterHasuraGraphQLRepository } from '@infrab4a/connect';
10
10
  import cookie from 'js-cookie';
11
11
  import { CustomError } from 'ts-custom-error';
12
- import * as i1$2 from '@angular/fire/firestore';
12
+ import * as i1$1 from '@angular/fire/firestore';
13
13
  import { AngularFirestore } from '@angular/fire/firestore';
14
- import * as i1$3 from '@angular/common/http';
14
+ import * as i1$2 from '@angular/common/http';
15
15
  import { FIREBASE_OPTIONS, FIREBASE_APP_NAME, AngularFireModule } from '@angular/fire';
16
16
 
17
17
  class AuthService {
@@ -32,7 +32,7 @@ class AuthService {
32
32
  }));
33
33
  }
34
34
  getUser() {
35
- return this.getFireUser().pipe(map((user) => user === null || user === void 0 ? void 0 : user.uid), mergeMap((id) => (id ? this.userRepository.get({ id }) : of(null))));
35
+ return this.getFireUser().pipe(map((user) => user === null || user === void 0 ? void 0 : user.uid), mergeMap((id) => (id ? from(this.userRepository.get({ id })).pipe(catchError(() => of(null))) : of(null))));
36
36
  }
37
37
  getTokenId() {
38
38
  return this.angularFireAuth.idToken;
@@ -301,7 +301,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImpo
301
301
  }], ctorParameters: function () { return [{ type: undefined, decorators: [{
302
302
  type: Inject,
303
303
  args: ['CouponRepository']
304
- }] }, { type: i1$1.Shops, decorators: [{
304
+ }] }, { type: i3.Shops, decorators: [{
305
305
  type: Inject,
306
306
  args: [DEFAULT_SHOP]
307
307
  }] }, { type: undefined, decorators: [{
@@ -371,22 +371,24 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImpo
371
371
  }] }, { type: undefined, decorators: [{
372
372
  type: Inject,
373
373
  args: ['UserRepository']
374
- }] }, { type: i1$1.Shops, decorators: [{
374
+ }] }, { type: i3.Shops, decorators: [{
375
375
  type: Inject,
376
376
  args: [DEFAULT_SHOP]
377
377
  }] }]; } });
378
378
 
379
379
  class CartService {
380
- constructor(authService, checkoutService, defaultShop, productRepository) {
380
+ constructor(authService, checkoutService, defaultShop, productRepository, variantRepository, buy2WinRepository) {
381
381
  this.authService = authService;
382
382
  this.checkoutService = checkoutService;
383
383
  this.defaultShop = defaultShop;
384
384
  this.productRepository = productRepository;
385
+ this.variantRepository = variantRepository;
386
+ this.buy2WinRepository = buy2WinRepository;
385
387
  this.cartSubject = new Subject();
386
388
  this.updateLineItemInCart = (lineItem, quantity, checkout) => (isNil(checkout) ? this.checkoutService.getCheckout() : of(checkout)).pipe(concatMap((checkoutLoaded) => {
387
389
  var _a;
388
390
  const items = [];
389
- const index = (_a = checkoutLoaded.lineItems) === null || _a === void 0 ? void 0 : _a.map((checkoutItem) => checkoutItem.sku).indexOf(lineItem.sku);
391
+ const index = (_a = checkoutLoaded.lineItems) === null || _a === void 0 ? void 0 : _a.map((checkoutItem) => checkoutItem.id).indexOf(lineItem.id);
390
392
  if (index > -1) {
391
393
  checkoutLoaded.lineItems[index].quantity += quantity;
392
394
  checkoutLoaded.lineItems[index].pricePaid = lineItem.pricePaid;
@@ -397,22 +399,18 @@ class CartService {
397
399
  .updateCheckoutLineItems(checkoutLoaded)
398
400
  .pipe(map((updatedCheckout) => this.generateCartObject(updatedCheckout.lineItems)));
399
401
  }));
400
- this.generateCartObject = (items) => {
401
- const cart = {};
402
- items === null || items === void 0 ? void 0 : items.forEach((item) => {
403
- var _a;
404
- return (cart[item.sku] = LineItem.toInstance(Object.assign(Object.assign({}, (cart[item.sku] || item)), { quantity: (((_a = cart[item.sku]) === null || _a === void 0 ? void 0 : _a.quantity) || 0) + (item.quantity ? item.quantity : 1) })));
405
- });
406
- return cart;
407
- };
402
+ this.generateCartObject = (items) => items.reduce((cart, item) => {
403
+ var _a;
404
+ return (Object.assign(Object.assign({}, cart), { [item.id]: LineItem.toInstance(Object.assign(Object.assign({}, (cart[item.id] || item)), { quantity: (((_a = cart[item.id]) === null || _a === void 0 ? void 0 : _a.quantity) || 0) + (item.quantity ? item.quantity : 1) })) }));
405
+ }, {});
408
406
  this.buildLineItem = ({ checkout, item, quantity, }) => __awaiter(this, void 0, void 0, function* () {
409
407
  var _a, _b, _c, _d, _e, _f, _g;
410
- const product = yield this.productRepository.get({ id: item.id });
411
- item.quantity = (item === null || item === void 0 ? void 0 : item.quantity) || ((_b = (_a = checkout === null || checkout === void 0 ? void 0 : checkout.lineItems) === null || _a === void 0 ? void 0 : _a.find((lineItem) => lineItem.sku === item.sku)) === null || _b === void 0 ? void 0 : _b.quantity) || 0;
408
+ const product = yield this.getProductData(item.id);
409
+ item.quantity = (item === null || item === void 0 ? void 0 : item.quantity) || ((_b = (_a = checkout === null || checkout === void 0 ? void 0 : checkout.lineItems) === null || _a === void 0 ? void 0 : _a.find((lineItem) => lineItem.id === item.id)) === null || _b === void 0 ? void 0 : _b.quantity) || 0;
412
410
  if (this.checkMaxStock(item, quantity || 0))
413
411
  throw new Error('Desculpe! Temos apenas ' + ((_c = item.stock) === null || _c === void 0 ? void 0 : _c.quantity) + ' em estoque.');
414
412
  const image = item.image || ((_d = item.images) === null || _d === void 0 ? void 0 : _d.shift());
415
- const { id, name, EAN, brand, slug, stock, price, weight, categories, sku, type } = item;
413
+ const { id, name, EAN, slug, stock, price, weight, sku, type } = item;
416
414
  const isGift = item.isGift || null;
417
415
  const pricePaid = this.getProductPrice({
418
416
  product: item,
@@ -460,7 +458,7 @@ class CartService {
460
458
  decreaseItem(item) {
461
459
  return this.checkoutService.getCheckout().pipe(map((checkout) => {
462
460
  var _a;
463
- const checkoutItem = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.find((lineItem) => lineItem.sku === item.sku);
461
+ const checkoutItem = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.find((lineItem) => lineItem.id === item.id);
464
462
  if (!isNil(checkoutItem))
465
463
  checkoutItem.quantity -= checkoutItem.quantity > 1 ? 1 : 0;
466
464
  return checkout;
@@ -478,7 +476,7 @@ class CartService {
478
476
  }
479
477
  removeItem(item) {
480
478
  return this.checkoutService.getCheckout().pipe(map((checkout) => {
481
- const index = checkout.lineItems.findIndex((lineItem) => lineItem.sku === item.sku);
479
+ const index = checkout.lineItems.findIndex((lineItem) => lineItem.id === item.id);
482
480
  if (index >= 0)
483
481
  checkout.lineItems.splice(index, 1);
484
482
  return checkout;
@@ -487,8 +485,12 @@ class CartService {
487
485
  updateUserCart(user) {
488
486
  return this.checkoutService.getCheckout().pipe(concatMap((checkout) => this.checkoutService.updateCheckoutUser(Checkout.toInstance(Object.assign(Object.assign({}, checkout.toPlain()), { user })))), concatMap((checkout) => __awaiter(this, void 0, void 0, function* () {
489
487
  var _a, _b;
490
- return this.checkoutService.updateCheckoutLineItems(Checkout.toInstance(Object.assign(Object.assign({}, checkout.toPlain()), { lineItems: ((_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.length) ? yield Promise.all((_b = checkout.lineItems) === null || _b === void 0 ? void 0 : _b.map((item) => __awaiter(this, void 0, void 0, function* () { return (yield this.buildLineItem({ checkout, item })).lineItem; }))) : [] }))).toPromise();
491
- })), map(checkout => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
488
+ return this.checkoutService
489
+ .updateCheckoutLineItems(Checkout.toInstance(Object.assign(Object.assign({}, checkout.toPlain()), { lineItems: ((_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.length)
490
+ ? yield Promise.all((_b = checkout.lineItems) === null || _b === void 0 ? void 0 : _b.map((item) => __awaiter(this, void 0, void 0, function* () { return (yield this.buildLineItem({ checkout, item })).lineItem; })))
491
+ : [] })))
492
+ .toPromise();
493
+ })), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
492
494
  }
493
495
  clearCart() {
494
496
  return this.checkoutService.getCheckout().pipe(map((checkout) => {
@@ -499,17 +501,123 @@ class CartService {
499
501
  buildCartFromCheckout(checkoutData) {
500
502
  return this.checkoutService.getCheckout(checkoutData).pipe(map((checkout) => checkout.lineItems), concatMap((lineItems) => of(this.generateCartObject(lineItems))));
501
503
  }
504
+ getProductData(productId) {
505
+ return __awaiter(this, void 0, void 0, function* () {
506
+ let product;
507
+ let variant;
508
+ try {
509
+ product = yield this.productRepository.get({ id: productId });
510
+ }
511
+ catch (error) {
512
+ if (!(error instanceof NotFoundError))
513
+ throw error;
514
+ variant = yield this.variantRepository.get({ id: productId });
515
+ product = yield this.productRepository.get({ id: variant.productId });
516
+ }
517
+ return Object.assign(Object.assign({}, product.toPlain()), (variant && Object.assign({}, variant.toPlain())));
518
+ });
519
+ }
520
+ getGifts() {
521
+ return this.checkoutService.getCheckout().pipe(mergeMap((checkout) => __awaiter(this, void 0, void 0, function* () {
522
+ const notGiftItems = checkout.lineItems.filter((item) => !item.isGift);
523
+ if (!notGiftItems.length)
524
+ return Object.assign(Object.assign({}, checkout), { lineItems: [] });
525
+ const cartTotal = notGiftItems.reduce((a, b) => a + b.pricePaid * b.quantity, 0);
526
+ const campaigns = yield this.buy2WinRepository
527
+ .find({
528
+ filters: {
529
+ active: { operator: Where.EQUALS, value: true },
530
+ shop: { operator: Where.EQUALS, value: this.defaultShop },
531
+ },
532
+ })
533
+ .then((data) => data.data);
534
+ if (!campaigns.length)
535
+ return Object.assign(Object.assign({}, checkout), { lineItems: notGiftItems });
536
+ const elegibleCampaigns = [];
537
+ for (const campaign of campaigns) {
538
+ const today = new Date();
539
+ if (!(campaign.startDate <= today) && !(campaign.endDate >= today))
540
+ continue;
541
+ if (campaign.activeCategory) {
542
+ const categoriesCampaing = campaign.categories.map((c) => c.id);
543
+ const filterProductsCategories = checkout.lineItems.filter((l) => {
544
+ var _a;
545
+ if (!l.categories || !((_a = l.categories) === null || _a === void 0 ? void 0 : _a.length))
546
+ return true;
547
+ return l.categories.some((c) => categoriesCampaing.some((cat) => cat == c));
548
+ });
549
+ if (filterProductsCategories.length) {
550
+ const cartTotalCategories = filterProductsCategories.reduce((a, b) => a + b.pricePaid * b.quantity, 0);
551
+ if (cartTotalCategories >= campaign.cartValueMin)
552
+ elegibleCampaigns.push(campaign);
553
+ }
554
+ }
555
+ else {
556
+ if (campaign.cartValue && campaign.cartValue > 0) {
557
+ if (campaign.cartValue <= cartTotal)
558
+ elegibleCampaigns.push(campaign);
559
+ }
560
+ }
561
+ }
562
+ if (!elegibleCampaigns.length)
563
+ return Object.assign(Object.assign({}, checkout), { lineItems: notGiftItems });
564
+ const campaingnProducts = [];
565
+ for (const campaign of elegibleCampaigns) {
566
+ let elegibleProducts = [];
567
+ for (const product of campaign.products) {
568
+ const { data: productData } = yield this.productRepository.find({ filters: { sku: product } });
569
+ if (!productData.length)
570
+ continue;
571
+ elegibleProducts.push(productData.shift());
572
+ }
573
+ campaingnProducts.push(elegibleProducts);
574
+ }
575
+ if (!campaingnProducts.length)
576
+ return Object.assign(Object.assign({}, checkout), { lineItems: notGiftItems });
577
+ const gifts = this.giftToLineItems([].concat(...campaingnProducts));
578
+ return Object.assign(Object.assign({}, checkout), { lineItems: notGiftItems.concat(gifts) });
579
+ })), concatMap((checkout) => this.checkoutService.updateCheckoutLineItems(checkout)), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
580
+ }
581
+ giftToLineItems(items) {
582
+ return items.map((item) => {
583
+ var _a;
584
+ const { brand, categories, id, name, price, sku, slug, stock, weight, EAN } = item;
585
+ const image = ((_a = item === null || item === void 0 ? void 0 : item.miniatures) === null || _a === void 0 ? void 0 : _a.length) ? item.miniatures[0] : item.images[0];
586
+ return LineItem.toInstance({
587
+ brand,
588
+ categories,
589
+ id: id.toString(),
590
+ name,
591
+ price,
592
+ sku,
593
+ slug,
594
+ stock,
595
+ weight,
596
+ EAN,
597
+ image,
598
+ pricePaid: 0,
599
+ quantity: 1,
600
+ isGift: true,
601
+ });
602
+ });
603
+ }
502
604
  }
503
- CartService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: CartService, deps: [{ token: AuthService }, { token: CheckoutService }, { token: DEFAULT_SHOP }, { token: 'ProductRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
605
+ CartService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: CartService, deps: [{ token: AuthService }, { token: CheckoutService }, { token: DEFAULT_SHOP }, { token: 'ProductRepository' }, { token: 'VariantRepository' }, { token: 'Buy2WinRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
504
606
  CartService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: CartService });
505
607
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: CartService, decorators: [{
506
608
  type: Injectable
507
- }], ctorParameters: function () { return [{ type: AuthService }, { type: CheckoutService }, { type: i1$1.Shops, decorators: [{
609
+ }], ctorParameters: function () { return [{ type: AuthService }, { type: CheckoutService }, { type: i3.Shops, decorators: [{
508
610
  type: Inject,
509
611
  args: [DEFAULT_SHOP]
510
612
  }] }, { type: undefined, decorators: [{
511
613
  type: Inject,
512
614
  args: ['ProductRepository']
615
+ }] }, { type: undefined, decorators: [{
616
+ type: Inject,
617
+ args: ['VariantRepository']
618
+ }] }, { type: i3.Buy2WinFirestoreRepository, decorators: [{
619
+ type: Inject,
620
+ args: ['Buy2WinRepository']
513
621
  }] }]; } });
514
622
 
515
623
  class CheckoutSubscriptionService {
@@ -660,7 +768,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImpo
660
768
  }] }, { type: undefined, decorators: [{
661
769
  type: Inject,
662
770
  args: ['ProductRepository']
663
- }] }, { type: i1$1.Shops, decorators: [{
771
+ }] }, { type: i3.Shops, decorators: [{
664
772
  type: Inject,
665
773
  args: [DEFAULT_SHOP]
666
774
  }] }]; } });
@@ -680,11 +788,11 @@ class OrderService {
680
788
  return this.orderSubject;
681
789
  }
682
790
  }
683
- OrderService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: OrderService, deps: [{ token: i1$2.AngularFirestore }, { token: 'OrderRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
791
+ OrderService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: OrderService, deps: [{ token: i1$1.AngularFirestore }, { token: 'OrderRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
684
792
  OrderService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: OrderService });
685
793
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: OrderService, decorators: [{
686
794
  type: Injectable
687
- }], ctorParameters: function () { return [{ type: i1$2.AngularFirestore }, { type: i1$1.OrderFirestoreRepository, decorators: [{
795
+ }], ctorParameters: function () { return [{ type: i1$1.AngularFirestore }, { type: i3.OrderFirestoreRepository, decorators: [{
688
796
  type: Inject,
689
797
  args: ['OrderRepository']
690
798
  }] }]; } });
@@ -768,11 +876,11 @@ class ShippingService {
768
876
  return false;
769
877
  }
770
878
  }
771
- ShippingService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: ShippingService, deps: [{ token: i1$3.HttpClient }, { token: BACKEND_URL }, { token: HomeShopService }], target: i0.ɵɵFactoryTarget.Injectable });
879
+ ShippingService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: ShippingService, deps: [{ token: i1$2.HttpClient }, { token: BACKEND_URL }, { token: HomeShopService }], target: i0.ɵɵFactoryTarget.Injectable });
772
880
  ShippingService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: ShippingService });
773
881
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: ShippingService, decorators: [{
774
882
  type: Injectable
775
- }], ctorParameters: function () { return [{ type: i1$3.HttpClient }, { type: undefined, decorators: [{
883
+ }], ctorParameters: function () { return [{ type: i1$2.HttpClient }, { type: undefined, decorators: [{
776
884
  type: Inject,
777
885
  args: [BACKEND_URL]
778
886
  }] }, { type: HomeShopService }]; } });
@@ -898,9 +1006,7 @@ AngularElasticSeachModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0
898
1006
  AngularElasticSeachModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: AngularElasticSeachModule, providers: [
899
1007
  {
900
1008
  provide: ProductsIndex,
901
- useFactory: (configuration) => {
902
- return new ProductsIndex(new AxiosAdapter(configuration));
903
- },
1009
+ useFactory: (configuration) => new ProductsIndex(new AxiosAdapter(configuration)),
904
1010
  deps: [ES_CONFIG],
905
1011
  },
906
1012
  ] });
@@ -910,9 +1016,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImpo
910
1016
  providers: [
911
1017
  {
912
1018
  provide: ProductsIndex,
913
- useFactory: (configuration) => {
914
- return new ProductsIndex(new AxiosAdapter(configuration));
915
- },
1019
+ useFactory: (configuration) => new ProductsIndex(new AxiosAdapter(configuration)),
916
1020
  deps: [ES_CONFIG],
917
1021
  },
918
1022
  ],