@infrab4a/connect-angular 4.0.0-beta.4 → 4.0.0-beta.41

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.
Files changed (64) hide show
  1. package/angular-connect.module.d.ts +2 -0
  2. package/angular-firestore.module.d.ts +2 -1
  3. package/consts/backend-url.const.d.ts +1 -0
  4. package/consts/category-structure.d.ts +1 -0
  5. package/consts/index.d.ts +2 -0
  6. package/esm2020/angular-connect.module.mjs +17 -3
  7. package/esm2020/angular-elastic-search.module.mjs +3 -7
  8. package/esm2020/angular-firestore.module.mjs +235 -139
  9. package/esm2020/angular-hasura-graphql.module.mjs +111 -23
  10. package/esm2020/consts/backend-url.const.mjs +2 -0
  11. package/esm2020/consts/category-structure.mjs +2 -0
  12. package/esm2020/consts/index.mjs +3 -1
  13. package/esm2020/index.mjs +3 -3
  14. package/esm2020/services/auth.service.mjs +3 -3
  15. package/esm2020/services/cart.service.mjs +161 -23
  16. package/esm2020/services/catalog/adapters/category-structure.adapter.mjs +2 -0
  17. package/esm2020/services/catalog/adapters/index.mjs +4 -0
  18. package/esm2020/services/catalog/adapters/new-category-structure.adapter.mjs +42 -0
  19. package/esm2020/services/catalog/adapters/old-category-structure.adapter.mjs +23 -0
  20. package/esm2020/services/catalog/catalog.service.mjs +98 -0
  21. package/esm2020/services/catalog/category.service.mjs +51 -0
  22. package/esm2020/services/catalog/enums/index.mjs +2 -0
  23. package/esm2020/services/catalog/enums/product-sorts.enum.mjs +11 -0
  24. package/esm2020/services/catalog/index.mjs +6 -0
  25. package/esm2020/services/catalog/models/category-with-tree.model.mjs +10 -0
  26. package/esm2020/services/catalog/models/index.mjs +2 -0
  27. package/esm2020/services/catalog/types/index.mjs +2 -0
  28. package/esm2020/services/catalog/types/product-sort.type.mjs +2 -0
  29. package/esm2020/services/checkout-subscription.service.mjs +5 -3
  30. package/esm2020/services/checkout.service.mjs +3 -5
  31. package/esm2020/services/coupon.service.mjs +128 -140
  32. package/esm2020/services/index.mjs +5 -3
  33. package/esm2020/services/shipping.service.mjs +96 -0
  34. package/esm2020/services/types/shipping-methods.type.mjs +2 -0
  35. package/fesm2015/infrab4a-connect-angular.mjs +1686 -1059
  36. package/fesm2015/infrab4a-connect-angular.mjs.map +1 -1
  37. package/fesm2020/infrab4a-connect-angular.mjs +1816 -1202
  38. package/fesm2020/infrab4a-connect-angular.mjs.map +1 -1
  39. package/index.d.ts +2 -2
  40. package/package.json +2 -3
  41. package/services/cart.service.d.ts +9 -2
  42. package/services/catalog/adapters/category-structure.adapter.d.ts +4 -0
  43. package/services/catalog/adapters/index.d.ts +3 -0
  44. package/services/catalog/adapters/new-category-structure.adapter.d.ts +14 -0
  45. package/services/catalog/adapters/old-category-structure.adapter.d.ts +10 -0
  46. package/services/catalog/catalog.service.d.ts +54 -0
  47. package/services/catalog/category.service.d.ts +15 -0
  48. package/services/catalog/enums/index.d.ts +1 -0
  49. package/services/catalog/enums/product-sorts.enum.d.ts +9 -0
  50. package/services/catalog/index.d.ts +5 -0
  51. package/services/catalog/models/category-with-tree.model.d.ts +4 -0
  52. package/services/catalog/models/index.d.ts +1 -0
  53. package/services/catalog/types/index.d.ts +1 -0
  54. package/services/catalog/types/product-sort.type.d.ts +2 -0
  55. package/services/coupon.service.d.ts +13 -10
  56. package/services/index.d.ts +4 -2
  57. package/services/shipping.service.d.ts +19 -0
  58. package/services/types/shipping-methods.type.d.ts +12 -0
  59. package/esm2020/services/errors/group-invalid-coupon.error.mjs +0 -8
  60. package/esm2020/services/errors/index.mjs +0 -3
  61. package/esm2020/services/errors/invalid-coupon.error.mjs +0 -7
  62. package/services/errors/group-invalid-coupon.error.d.ts +0 -6
  63. package/services/errors/index.d.ts +0 -2
  64. package/services/errors/invalid-coupon.error.d.ts +0 -4
@@ -1,49 +1,25 @@
1
1
  import * as i0 from '@angular/core';
2
- import { Injectable, Inject, InjectionToken, NgModule, PLATFORM_ID } from '@angular/core';
3
- import * as i1 from '@angular/fire/auth';
4
- import { getIdToken, authState, Auth, provideAuth, getAuth } from '@angular/fire/auth';
5
- import { combineLatest, of, from, throwError, Subject, iif, forkJoin } from 'rxjs';
2
+ import { InjectionToken, NgModule, PLATFORM_ID, Injectable, Inject } from '@angular/core';
3
+ import * as i1$3 from '@infrab4a/connect';
4
+ import { 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, CategoryCollectionChildrenHasuraGraphQLRepository, Where, Shops, CheckoutTypes, CouponTypes, Exclusivities, isNil, NotFoundError, Checkout, pick, LineItem, RoundProductPricesHelper, set, Category, CheckoutSubscription, Product, RequiredArgumentError, add, Order } from '@infrab4a/connect';
5
+ import * as i1 from '@angular/fire/app';
6
+ import { provideFirebaseApp, initializeApp, FirebaseApp } from '@angular/fire/app';
7
+ import * as i1$1 from '@angular/fire/auth';
8
+ import { Auth, provideAuth, getAuth, getIdToken, authState } from '@angular/fire/auth';
9
+ import { isPlatformBrowser } from '@angular/common';
10
+ import * as i1$2 from '@angular/fire/firestore';
11
+ import { Firestore, provideFirestore, initializeFirestore, enableIndexedDbPersistence, docSnapshots, doc } from '@angular/fire/firestore';
12
+ import { initializeApp as initializeApp$1 } from 'firebase/app';
13
+ import { combineLatest, from, of, throwError, Subject, iif, forkJoin } from 'rxjs';
6
14
  import { map, mergeMap, catchError, concatMap, tap } from 'rxjs/operators';
7
- import * as i2 from '@infrab4a/connect';
8
- import { Where, Shops, CheckoutTypes, CouponTypes, Exclusivities, Status, isNil, NotFoundError, Checkout, pick, LineItem, CheckoutSubscription, Order, Category, Product, RequiredArgumentError, add, Authentication, AuthenticationFirebaseAuthService, Register, RegisterFirebaseAuthService, SignOut, RecoveryPassword, ProductsIndex, AxiosAdapter, UserBeautyProfileFirestoreRepository, Buy2WinFirestoreRepository, CategoryFirestoreRepository, CheckoutFirestoreRepository, CheckoutSubscriptionFirestoreRepository, CouponFirestoreRepository, SubscriptionEditionFirestoreRepository, HomeFirestoreRepository, LeadFirestoreRepository, LegacyOrderFirestoreRepository, ShopMenuFirestoreRepository, OrderFirestoreRepository, PaymentFirestoreRepository, ProductFirestoreRepository, SubscriptionPaymentFirestoreRepository, SubscriptionPlanFirestoreRepository, SubscriptionProductFirestoreRepository, SubscriptionFirestoreRepository, UserFirestoreRepository, UserAddressFirestoreRepository, UserPaymentMethodFirestoreRepository, ProductVariantFirestoreRepository, CategoryHasuraGraphQLRepository, ProductHasuraGraphQLRepository, VariantHasuraGraphQLRepository } from '@infrab4a/connect';
9
15
  import cookie from 'js-cookie';
10
- import { CustomError } from 'ts-custom-error';
11
- import * as i1$1 from '@angular/fire/firestore';
12
- import { docSnapshots, doc, Firestore, provideFirestore, getFirestore } from '@angular/fire/firestore';
13
- import * as i1$2 from '@angular/fire/app';
14
- import { provideFirebaseApp, initializeApp } from '@angular/fire/app';
15
- import { isPlatformServer } from '@angular/common';
16
+ import { __decorate, __metadata } from 'tslib';
17
+ import { Type } from 'class-transformer';
18
+ import * as i1$4 from '@angular/common/http';
16
19
 
17
- class AuthService {
18
- constructor(angularFireAuth, userRepository) {
19
- this.angularFireAuth = angularFireAuth;
20
- this.userRepository = userRepository;
21
- }
22
- getAuthstate() {
23
- const observables = [this.getFireUser(), this.getUser()];
24
- return combineLatest(observables).pipe(map(([fireUser, user]) => ({
25
- user,
26
- isAnonymous: fireUser?.isAnonymous,
27
- })));
28
- }
29
- getUser() {
30
- return this.getFireUser().pipe(map((user) => user?.uid), mergeMap((id) => (id ? this.userRepository.get({ id }) : of(null))), catchError(() => of(null)));
31
- }
32
- getTokenId() {
33
- return from(getIdToken(this.angularFireAuth.currentUser));
34
- }
35
- getFireUser() {
36
- return authState(this.angularFireAuth).pipe(catchError(() => of(null)));
37
- }
38
- }
39
- AuthService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AuthService, deps: [{ token: i1.Auth }, { token: 'UserRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
40
- AuthService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AuthService });
41
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AuthService, decorators: [{
42
- type: Injectable
43
- }], ctorParameters: function () { return [{ type: i1.Auth }, { type: undefined, decorators: [{
44
- type: Inject,
45
- args: ['UserRepository']
46
- }] }]; } });
20
+ const BACKEND_URL = 'BACKEND_URL';
21
+
22
+ const CATEGORY_STRUCTURE = 'CATEGORY_STRUCTURE';
47
23
 
48
24
  const DEFAULT_SHOP = 'DEFAULT_SHOP';
49
25
 
@@ -54,1245 +30,1877 @@ const FIREBASE_OPTIONS = new InjectionToken('firebaseOptions');
54
30
 
55
31
  const HASURA_OPTIONS = 'HASURA_OPTIONS';
56
32
 
57
- class InvalidCouponError extends CustomError {
58
- constructor(message) {
59
- super(message);
33
+ class AngularFirebaseAuthModule {
34
+ static initializeApp(options, nameOrConfig) {
35
+ return {
36
+ ngModule: AngularFirebaseAuthModule,
37
+ providers: [
38
+ { provide: FIREBASE_OPTIONS, useValue: options },
39
+ { provide: FIREBASE_APP_NAME, useValue: nameOrConfig },
40
+ ],
41
+ };
60
42
  }
61
43
  }
44
+ AngularFirebaseAuthModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirebaseAuthModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
45
+ AngularFirebaseAuthModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.0", ngImport: i0, type: AngularFirebaseAuthModule, imports: [i1.FirebaseAppModule, i1$1.AuthModule] });
46
+ AngularFirebaseAuthModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirebaseAuthModule, providers: [
47
+ {
48
+ provide: 'Authentication',
49
+ useFactory: (authenticationService, userRepository) => {
50
+ return new Authentication(authenticationService, userRepository);
51
+ },
52
+ deps: ['AuthenticationService', 'UserRepository'],
53
+ },
54
+ {
55
+ provide: 'AuthenticationService',
56
+ useFactory: (angularFireAuth) => {
57
+ return new AuthenticationFirebaseAuthService(angularFireAuth);
58
+ },
59
+ deps: [Auth],
60
+ },
61
+ {
62
+ provide: 'Register',
63
+ useFactory: (registerService, userRepository) => {
64
+ return new Register(registerService, userRepository);
65
+ },
66
+ deps: ['RegisterService', 'UserRepository'],
67
+ },
68
+ {
69
+ provide: 'RegisterService',
70
+ useFactory: (angularFireAuth) => {
71
+ return new RegisterFirebaseAuthService(angularFireAuth);
72
+ },
73
+ deps: [Auth],
74
+ },
75
+ {
76
+ provide: 'SignOut',
77
+ useFactory: (authenticationService) => {
78
+ return new SignOut(authenticationService);
79
+ },
80
+ deps: ['AuthenticationService'],
81
+ },
82
+ {
83
+ provide: 'RecoveryPassword',
84
+ useFactory: (authenticationService) => {
85
+ return new RecoveryPassword(authenticationService);
86
+ },
87
+ deps: ['AuthenticationService'],
88
+ },
89
+ ], imports: [provideFirebaseApp((injector) => {
90
+ const appName = injector.get(FIREBASE_APP_NAME);
91
+ return appName
92
+ ? initializeApp(injector.get(FIREBASE_OPTIONS), appName)
93
+ : initializeApp(injector.get(FIREBASE_OPTIONS));
94
+ }),
95
+ provideAuth(() => getAuth())] });
96
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirebaseAuthModule, decorators: [{
97
+ type: NgModule,
98
+ args: [{
99
+ imports: [
100
+ provideFirebaseApp((injector) => {
101
+ const appName = injector.get(FIREBASE_APP_NAME);
102
+ return appName
103
+ ? initializeApp(injector.get(FIREBASE_OPTIONS), appName)
104
+ : initializeApp(injector.get(FIREBASE_OPTIONS));
105
+ }),
106
+ provideAuth(() => getAuth()),
107
+ ],
108
+ providers: [
109
+ {
110
+ provide: 'Authentication',
111
+ useFactory: (authenticationService, userRepository) => {
112
+ return new Authentication(authenticationService, userRepository);
113
+ },
114
+ deps: ['AuthenticationService', 'UserRepository'],
115
+ },
116
+ {
117
+ provide: 'AuthenticationService',
118
+ useFactory: (angularFireAuth) => {
119
+ return new AuthenticationFirebaseAuthService(angularFireAuth);
120
+ },
121
+ deps: [Auth],
122
+ },
123
+ {
124
+ provide: 'Register',
125
+ useFactory: (registerService, userRepository) => {
126
+ return new Register(registerService, userRepository);
127
+ },
128
+ deps: ['RegisterService', 'UserRepository'],
129
+ },
130
+ {
131
+ provide: 'RegisterService',
132
+ useFactory: (angularFireAuth) => {
133
+ return new RegisterFirebaseAuthService(angularFireAuth);
134
+ },
135
+ deps: [Auth],
136
+ },
137
+ {
138
+ provide: 'SignOut',
139
+ useFactory: (authenticationService) => {
140
+ return new SignOut(authenticationService);
141
+ },
142
+ deps: ['AuthenticationService'],
143
+ },
144
+ {
145
+ provide: 'RecoveryPassword',
146
+ useFactory: (authenticationService) => {
147
+ return new RecoveryPassword(authenticationService);
148
+ },
149
+ deps: ['AuthenticationService'],
150
+ },
151
+ ],
152
+ }]
153
+ }] });
62
154
 
63
- class GroupInvalidCouponError extends CustomError {
64
- constructor(errors) {
65
- super('Many coupon errors throw');
66
- this.errors = errors;
155
+ class AngularElasticSeachModule {
156
+ static initializeApp(options) {
157
+ return {
158
+ ngModule: AngularElasticSeachModule,
159
+ providers: [{ provide: ES_CONFIG, useValue: options }],
160
+ };
67
161
  }
68
162
  }
163
+ AngularElasticSeachModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularElasticSeachModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
164
+ AngularElasticSeachModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.0", ngImport: i0, type: AngularElasticSeachModule });
165
+ AngularElasticSeachModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularElasticSeachModule, providers: [
166
+ {
167
+ provide: ProductsIndex,
168
+ useFactory: (configuration) => new ProductsIndex(new AxiosAdapter(configuration)),
169
+ deps: [ES_CONFIG],
170
+ },
171
+ ] });
172
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularElasticSeachModule, decorators: [{
173
+ type: NgModule,
174
+ args: [{
175
+ providers: [
176
+ {
177
+ provide: ProductsIndex,
178
+ useFactory: (configuration) => new ProductsIndex(new AxiosAdapter(configuration)),
179
+ deps: [ES_CONFIG],
180
+ },
181
+ ],
182
+ }]
183
+ }] });
69
184
 
70
- class CouponService {
71
- constructor(couponRepository, defaultShop, orderRepository, subscriptionRepository, categoryRepository) {
72
- this.couponRepository = couponRepository;
73
- this.defaultShop = defaultShop;
74
- this.orderRepository = orderRepository;
75
- this.subscriptionRepository = subscriptionRepository;
76
- this.categoryRepository = categoryRepository;
77
- this.emailIsFromCollaborator = (userEmail) => !!userEmail?.match(/@b4a.com.br/g);
185
+ class AngularFirestoreModule {
186
+ static initializeApp(options, nameOrConfig) {
187
+ return {
188
+ ngModule: AngularFirestoreModule,
189
+ providers: [
190
+ { provide: FIREBASE_OPTIONS, useValue: options.firebase },
191
+ { provide: FIREBASE_APP_NAME, useValue: nameOrConfig },
192
+ { provide: ES_CONFIG, useValue: options.elasticSearch },
193
+ ],
194
+ };
78
195
  }
79
- checkCoupon(nickname, userEmail, checkoutType, plan, checkout, isSubscription) {
80
- return from(this.couponRepository.find({
81
- filters: {
82
- nickname: { operator: Where.EQUALS, value: nickname },
83
- active: { operator: Where.EQUALS, value: true },
196
+ }
197
+ AngularFirestoreModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirestoreModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
198
+ AngularFirestoreModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.0", ngImport: i0, type: AngularFirestoreModule, imports: [AngularElasticSeachModule, i1.FirebaseAppModule, i1$2.FirestoreModule] });
199
+ AngularFirestoreModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirestoreModule, providers: [
200
+ {
201
+ provide: 'FirestoreOptions',
202
+ useFactory: (firestore, platformId) => ({
203
+ firestore,
204
+ interceptors: {
205
+ request: (request) => {
206
+ if (isPlatformBrowser(platformId))
207
+ return request;
208
+ const interval = setInterval(() => { }, 100);
209
+ request.interval = interval;
210
+ return request;
211
+ },
212
+ response: (response, request) => {
213
+ if (isPlatformBrowser(platformId))
214
+ return response;
215
+ clearInterval(request.interval);
216
+ return response;
217
+ },
218
+ },
219
+ }),
220
+ deps: [Firestore, PLATFORM_ID],
221
+ },
222
+ {
223
+ provide: 'BeautyProfileRepository',
224
+ useFactory: (config, userRepository) => {
225
+ return new UserBeautyProfileFirestoreRepository(config, userRepository);
84
226
  },
85
- })).pipe(concatMap((coupons) => this.checkCouponRules(coupons, checkoutType, plan, checkout, isSubscription)), concatMap((coupon) => this.checkCouponUseAndLimit(coupon, userEmail, checkout)), map((coupon) => this.isValidCoupon(coupon, userEmail)), map((coupon) => coupon));
86
- }
87
- checkCouponRules(coupons, checkoutType, plan, checkout, isSubscription) {
88
- // Caso não ache nenhum cupom, retorna erro
89
- if (coupons.count < 1) {
90
- return throwError('Cupom inválido.');
91
- }
92
- // Get Primeiro Cupom (o find do repository retorna um array)
93
- const coupon = coupons.data.shift();
94
- // Verifica se o cupom é aplicavel na loja
95
- const isInShop = coupon.shopAvailability === Shops.ALL || coupon.shopAvailability === this.defaultShop;
96
- // Cupon não aplicavel a loja retorna erro
97
- if (!isInShop)
98
- return throwError('Cupom inválido para loja.');
99
- // Verifica se o coupon é aplicado no checkout que está sendo realizado
100
- const isCheckoutType = coupon.checkoutType === CheckoutTypes.ALL || coupon.checkoutType === checkoutType;
101
- // Cupon não aplicavel ao checkout retorna erro
102
- if (!isCheckoutType)
103
- return throwError('Cupom inválido. Erro de checkout.');
104
- // Verifica se o cupom é ou pode ser aplicado para subscription
105
- if (checkoutType === CheckoutTypes.ALL || checkoutType === CheckoutTypes.SUBSCRIPTION) {
106
- // Se o cupom tiver um plano associado, verifica se é o mesmo plano do checkout da assinatura
107
- if (coupon.plan && coupon.plan.toUpperCase() !== plan.toUpperCase())
108
- return throwError('Cupom inválido para sua assinatura.');
109
- }
110
- if (isSubscription)
111
- return of(coupon);
112
- // Verifica se possui o valor minimo de compra para utilização do cupom
113
- const hasMinSubTotal = this.hasMinSubTotal(coupon, checkout);
114
- // Se não tem valor mínimo atingido, retorna erro
115
- if (!hasMinSubTotal)
116
- return throwError('Valor mínimo não atingido');
117
- return of(coupon);
118
- }
119
- isValidCoupon(coupon, userEmail) {
120
- // Verifica a data de inicio de validade do cupom
121
- if (coupon?.beginAt > new Date())
122
- throw new InvalidCouponError('Cupom ainda não liberado.');
123
- // Verifica a data de validade do cupom
124
- if (coupon?.expiresIn < new Date())
125
- throw new InvalidCouponError('Cupom expirado.');
126
- return coupon;
127
- }
128
- async checkCouponUseAndLimit(coupon, userEmail, checkout) {
129
- const orders = await this.orderRepository.find({
130
- filters: {
131
- coupon: { id: coupon.id },
132
- payment: { status: 'paid' },
227
+ deps: ['FirestoreOptions', 'UserRepository'],
228
+ },
229
+ {
230
+ provide: 'Buy2WinRepository',
231
+ useFactory: (options) => {
232
+ return new Buy2WinFirestoreRepository(options);
133
233
  },
134
- });
135
- // orders que usuario ja fez com o cupom
136
- const ordersUserCoupon = orders.data.filter((o) => o.user.email == userEmail);
137
- // Verifica o limite de uso de cupom por usuario
138
- if (coupon.useLimitPerUser && ordersUserCoupon.length)
139
- throw new InvalidCouponError('Limite de uso por usuário atingido.');
140
- // Verifica o limite de uso geral por usuario
141
- if (coupon.useLimit && orders.data.length >= coupon.useLimit)
142
- throw new InvalidCouponError('Limite de uso atingido.');
143
- const validUser = await this.userValidationAndSubscriptionStatus(coupon, userEmail);
144
- if (!validUser)
145
- throw new InvalidCouponError('Usuário não elegível.');
146
- const hasProductCategories = await this.hasProductCategories(coupon, checkout);
147
- if (!hasProductCategories)
148
- throw 'Seu carrinho não possui produtos elegíveis para desconto.';
149
- return coupon;
150
- }
151
- calcDiscountSubscription(coupon, checkout) {
152
- //
153
- let discount = 0;
154
- if (coupon.type === CouponTypes.ABSOLUTE)
155
- discount = coupon.discount;
156
- else if (coupon.type === CouponTypes.PERCENTAGE)
157
- discount = checkout.subscriptionPlan.recurrencePrice * (coupon.discount / 100);
158
- return of(discount);
159
- }
160
- async calcDiscountShopping(coupon, checkout) {
161
- let discount = 0;
162
- switch (coupon.type) {
163
- case CouponTypes.ABSOLUTE: {
164
- discount = coupon.discount;
165
- break;
166
- }
167
- case CouponTypes.PERCENTAGE: {
168
- discount = await this.calcShoppingPercentageDiscount(coupon, checkout);
169
- break;
170
- }
171
- }
172
- return discount;
173
- }
174
- async calcShoppingPercentageDiscount(coupon, checkout) {
175
- let discount = 0;
176
- const shop = checkout.shop;
177
- let lineItensDiscount = [];
178
- const couponCategories = await this.getCouponCategoriesId(coupon);
179
- if (coupon.productsCategories && coupon.productsCategories.length) {
180
- lineItensDiscount = checkout.lineItems?.filter((i) => {
181
- if (i.categories?.length) {
182
- return i.categories.some((c) => couponCategories.some((cat) => cat.id == c || cat.firestoreId == c));
183
- }
184
- return true;
185
- });
186
- }
187
- else {
188
- lineItensDiscount = checkout.lineItems;
189
- }
190
- const subTotal = lineItensDiscount.reduce((acc, curr) => checkout.user?.isSubscriber && curr.price?.subscriberPrice
191
- ? acc + curr.price?.subscriberPrice * curr.quantity
192
- : acc + curr.pricePaid * curr.quantity, 0) || 0;
193
- discount = subTotal * (coupon.discount / 100);
194
- return discount;
195
- }
196
- hasMinSubTotal(coupon, checkout) {
197
- if (!coupon.minSubTotalValue)
198
- return true;
199
- const shop = checkout.shop;
200
- let subTotal = checkout.lineItems?.reduce((acc, curr) => checkout.user?.isSubscriber && curr.price?.subscriberPrice
201
- ? acc + curr.price?.subscriberPrice * curr.quantity
202
- : acc + curr.pricePaid * curr.quantity, 0) || 0;
203
- if (coupon.minSubTotalValue <= subTotal)
204
- return true;
205
- return false;
206
- }
207
- async hasProductCategories(coupon, checkout) {
208
- if (!coupon.productsCategories || !coupon.productsCategories.length) {
209
- return true;
210
- }
211
- const couponCategories = await this.getCouponCategoriesId(coupon);
212
- const hasCategories = checkout.lineItems?.filter((i) => {
213
- if (!i.categories || !i.categories?.length)
214
- return true;
215
- return i.categories.some((c) => couponCategories.some((cat) => cat.id == c || cat.firestoreId == c));
216
- });
217
- return hasCategories.length ? true : false;
218
- }
219
- async userValidationAndSubscriptionStatus(coupon, userEmail) {
220
- // Verifica se o email do usuário é coorporativo
221
- if (!this.emailIsFromCollaborator(userEmail) && coupon.exclusivityType === Exclusivities.COLLABORATORS)
222
- throw new InvalidCouponError('Você não é colaborador.');
223
- // Verifica se o email do usuário é associado ao cupom de uso por usuario
224
- if (coupon.exclusivityType === Exclusivities.SPECIFIC_USER && coupon.userExclusiveEmail !== userEmail)
225
- throw new InvalidCouponError('Cupom não é válido para este usuário.');
226
- const couponRuleSubscription = coupon.exclusivityType === Exclusivities.ACTIVE_SUBSCRIBER ||
227
- coupon.exclusivityType === Exclusivities.INACTIVE_SUBSCRIBER ||
228
- coupon.exclusivityType === Exclusivities.NON_SUBSCRIBER;
229
- if (couponRuleSubscription && userEmail) {
230
- const sub = await this.subscriptionRepository
231
- .find({
232
- filters: {
233
- user: {
234
- email: { operator: Where.EQUALS, value: userEmail },
235
- },
236
- },
237
- })
238
- .then((sub) => sub.data);
239
- const activeSubs = sub?.filter((s) => s.status === Status.ACTIVE);
240
- switch (coupon.exclusivityType) {
241
- case Exclusivities.ACTIVE_SUBSCRIBER:
242
- return activeSubs.length > 0;
243
- case Exclusivities.INACTIVE_SUBSCRIBER:
244
- return activeSubs.length === 0;
245
- case Exclusivities.NON_SUBSCRIBER:
246
- return sub.length === 0;
247
- default:
248
- return false;
249
- }
250
- }
251
- return true;
252
- }
253
- async getCouponCategoriesId(coupon) {
254
- let couponCategories = [];
255
- for (let index = 0; index < coupon.productsCategories.length; index++) {
256
- let c = await this.categoryRepository.get({
257
- id: coupon.productsCategories[index],
258
- });
259
- couponCategories.push({ id: c.id, firestoreId: c.firestoreId });
260
- }
261
- return couponCategories;
262
- }
263
- }
264
- CouponService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CouponService, deps: [{ token: 'CouponRepository' }, { token: DEFAULT_SHOP }, { token: 'OrderRepository' }, { token: 'SubscriptionRepository' }, { token: 'CategoryRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
265
- CouponService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CouponService, providedIn: 'root' });
266
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CouponService, decorators: [{
267
- type: Injectable,
268
- args: [{
269
- providedIn: 'root',
270
- }]
271
- }], ctorParameters: function () { return [{ type: undefined, decorators: [{
272
- type: Inject,
273
- args: ['CouponRepository']
274
- }] }, { type: i2.Shops, decorators: [{
275
- type: Inject,
276
- args: [DEFAULT_SHOP]
277
- }] }, { type: undefined, decorators: [{
278
- type: Inject,
279
- args: ['OrderRepository']
280
- }] }, { type: undefined, decorators: [{
281
- type: Inject,
282
- args: ['SubscriptionRepository']
283
- }] }, { type: undefined, decorators: [{
284
- type: Inject,
285
- args: ['CategoryRepository']
286
- }] }]; } });
287
-
288
- class CheckoutService {
289
- constructor(couponService, checkoutRepository, orderRepository, userRepository, defaultShop) {
290
- this.couponService = couponService;
291
- this.checkoutRepository = checkoutRepository;
292
- this.orderRepository = orderRepository;
293
- this.userRepository = userRepository;
294
- this.defaultShop = defaultShop;
295
- }
296
- getCheckout(checkoutData) {
297
- const checkoutId = cookie.get('checkoutId');
298
- if (!isNil(checkoutId))
299
- return from(this.checkoutRepository.get({ id: checkoutId }));
300
- return from(this.createCheckout(checkoutData));
301
- }
302
- getUserByCheckout(checkoutId) {
303
- return from(this.checkoutRepository.get({ id: checkoutId })).pipe(concatMap((checkout) => checkout?.user?.id ? of(checkout.user) : from(this.userRepository.get({ id: checkout.user.id }))), concatMap((user) => of(user) || throwError(() => new NotFoundError('User is not found'))));
304
- }
305
- updateCheckoutLineItems(checkout) {
306
- return from(this.checkoutRepository.update(Checkout.toInstance({ id: checkout.id, lineItems: checkout.lineItems })));
307
- }
308
- updateCheckoutUser(checkout) {
309
- return from(this.checkoutRepository.update(Checkout.toInstance({ id: checkout.id, user: checkout.user })));
310
- }
311
- clearCheckoutFromSession() {
312
- cookie.remove('checkoutId');
313
- return of();
314
- }
315
- calcDiscount(coupon) {
316
- return this.getCheckout().pipe(concatMap(async (checkout) => await this.couponService.calcDiscountShopping(coupon, checkout)));
317
- }
318
- checkCoupon(nickname, checkoutType) {
319
- return this.getCheckout().pipe(concatMap((checkout) => this.couponService
320
- .checkCoupon(nickname, checkout.user?.email, CheckoutTypes.ECOMMERCE, checkout.user.subscriptionPlan, checkout, false)
321
- .pipe()));
322
- }
323
- async createCheckout(checkoutData) {
324
- const checkout = await this.checkoutRepository.create({
325
- createdAt: new Date(),
326
- ...Checkout.toInstance(pick(checkoutData, ['user', 'shop'])).toPlain(),
327
- shop: checkoutData?.shop || this.defaultShop,
328
- });
329
- cookie.set('checkoutId', checkout.id);
330
- return checkout;
331
- }
332
- }
333
- CheckoutService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutService, deps: [{ token: CouponService }, { token: 'CheckoutRepository' }, { token: 'OrderRepository' }, { token: 'UserRepository' }, { token: DEFAULT_SHOP }], target: i0.ɵɵFactoryTarget.Injectable });
334
- CheckoutService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutService });
335
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutService, decorators: [{
336
- type: Injectable
337
- }], ctorParameters: function () { return [{ type: CouponService }, { type: undefined, decorators: [{
338
- type: Inject,
339
- args: ['CheckoutRepository']
340
- }] }, { type: undefined, decorators: [{
341
- type: Inject,
342
- args: ['OrderRepository']
343
- }] }, { type: undefined, decorators: [{
344
- type: Inject,
345
- args: ['UserRepository']
346
- }] }, { type: i2.Shops, decorators: [{
347
- type: Inject,
348
- args: [DEFAULT_SHOP]
349
- }] }]; } });
350
-
351
- class CartService {
352
- constructor(authService, checkoutService, defaultShop, productRepository) {
353
- this.authService = authService;
354
- this.checkoutService = checkoutService;
355
- this.defaultShop = defaultShop;
356
- this.productRepository = productRepository;
357
- this.cartSubject = new Subject();
358
- this.updateLineItemInCart = (lineItem, quantity, checkout) => (isNil(checkout) ? this.checkoutService.getCheckout() : of(checkout)).pipe(concatMap((checkoutLoaded) => {
359
- const items = [];
360
- const index = checkoutLoaded.lineItems?.map((checkoutItem) => checkoutItem.sku).indexOf(lineItem.sku);
361
- if (index > -1) {
362
- checkoutLoaded.lineItems[index].quantity += quantity;
363
- checkoutLoaded.lineItems[index].pricePaid = lineItem.pricePaid;
364
- }
365
- else
366
- checkoutLoaded.lineItems = items.concat(checkoutLoaded.lineItems ? checkoutLoaded.lineItems.concat([lineItem]) : [lineItem]);
367
- return this.checkoutService
368
- .updateCheckoutLineItems(checkoutLoaded)
369
- .pipe(map((updatedCheckout) => this.generateCartObject(updatedCheckout.lineItems)));
370
- }));
371
- this.generateCartObject = (items) => {
372
- const cart = {};
373
- items?.forEach((item) => (cart[item.sku] = LineItem.toInstance({
374
- ...(cart[item.sku] || item),
375
- quantity: (cart[item.sku]?.quantity || 0) + (item.quantity ? item.quantity : 1),
376
- })));
377
- return cart;
378
- };
379
- this.buildLineItem = async ({ checkout, item, quantity, }) => {
380
- const product = await this.productRepository.get({ id: item.id });
381
- item.quantity = item?.quantity || checkout?.lineItems?.find((lineItem) => lineItem.sku === item.sku)?.quantity || 0;
382
- if (this.checkMaxStock(item, quantity || 0))
383
- throw new Error('Desculpe! Temos apenas ' + item.stock?.quantity + ' em estoque.');
384
- const image = item.image || item.images?.shift();
385
- const { id, name, EAN, brand, slug, stock, price, weight, categories, sku, type } = item;
386
- const isGift = item.isGift || null;
387
- const pricePaid = this.getProductPrice({
388
- product: item,
389
- shop: checkout.shop || this.defaultShop,
390
- isSubscriber: checkout.user?.isSubscriber,
391
- });
392
- return {
393
- checkout,
394
- lineItem: LineItem.toInstance({
395
- id,
396
- name: name ?? product.name,
397
- EAN: EAN ?? product.EAN,
398
- brand: product.brand,
399
- slug: slug ?? product.slug,
400
- sku: sku ?? product.sku,
401
- stock,
402
- price,
403
- image,
404
- weight: weight ?? product.weight,
405
- quantity: (item.quantity || 0) + (quantity || 0),
406
- pricePaid,
407
- categories: product.categories ?? [],
408
- isGift: isGift ?? null,
409
- costPrice: product.costPrice ?? 0,
410
- type,
411
- }),
412
- };
413
- };
414
- this.getProductPrice = ({ product, isSubscriber, }) => {
415
- const info = product.price;
416
- if (product.isGift)
417
- return 0;
418
- return isSubscriber && info.subscriberPrice > 0 ? info.subscriberPrice : info.price;
419
- };
420
- this.checkMaxStock = (item, quantity) => {
421
- const maxStock = item.stock?.quantity || 0;
422
- const currentItemAmount = item.quantity || 0;
423
- return currentItemAmount + quantity > maxStock;
424
- };
425
- }
426
- addItem(item, quantity = 1) {
427
- return from(this.checkoutService.getCheckout()).pipe(concatMap(async (checkout) => await this.buildLineItem({ checkout, item, quantity: quantity || 1 })), mergeMap(({ checkout, lineItem }) => this.updateLineItemInCart(lineItem, quantity || 1, checkout)), tap((cart) => this.cartSubject.next(cart)));
428
- }
429
- decreaseItem(item) {
430
- return this.checkoutService.getCheckout().pipe(map((checkout) => {
431
- const checkoutItem = checkout.lineItems?.find((lineItem) => lineItem.sku === item.sku);
432
- if (!isNil(checkoutItem))
433
- checkoutItem.quantity -= checkoutItem.quantity > 1 ? 1 : 0;
434
- return checkout;
435
- }), concatMap((checkout) => this.checkoutService.updateCheckoutLineItems(checkout)), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
436
- }
437
- getCart(checkout) {
438
- this.buildCartFromCheckout(checkout).subscribe((cart) => this.cartSubject.next(cart));
439
- return this.cartSubject;
440
- }
441
- /**
442
- * @deprecated The method should not be used
443
- */
444
- getVariantPriceDiscount(item) {
445
- return this.authService.getUser().pipe(concatMap((user) => iif(() => user.isSubscriber && !!item.price.subscriberPrice, of(item.price.subscriberPrice), of(item.price.price))), catchError(() => of(item.price.price)));
446
- }
447
- removeItem(item) {
448
- return this.checkoutService.getCheckout().pipe(map((checkout) => {
449
- const index = checkout.lineItems.findIndex((lineItem) => lineItem.sku === item.sku);
450
- if (index >= 0)
451
- checkout.lineItems.splice(index, 1);
452
- return checkout;
453
- }), concatMap((checkout) => this.checkoutService.updateCheckoutLineItems(checkout)), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
454
- }
455
- updateUserCart(user) {
456
- return this.checkoutService.getCheckout().pipe(concatMap((checkout) => this.checkoutService.updateCheckoutUser(Checkout.toInstance({ ...checkout.toPlain(), user }))), concatMap(async (checkout) => this.checkoutService.updateCheckoutLineItems(Checkout.toInstance({
457
- ...checkout.toPlain(),
458
- lineItems: checkout.lineItems?.length ? await Promise.all(checkout.lineItems?.map(async (item) => (await this.buildLineItem({ checkout, item })).lineItem)) : [],
459
- })).toPromise()), map(checkout => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
460
- }
461
- clearCart() {
462
- return this.checkoutService.getCheckout().pipe(map((checkout) => {
463
- this.checkoutService.clearCheckoutFromSession();
464
- return checkout;
465
- }), concatMap((oldCheckout) => this.buildCartFromCheckout(oldCheckout)), tap((cart) => this.cartSubject.next(cart)));
466
- }
467
- buildCartFromCheckout(checkoutData) {
468
- return this.checkoutService.getCheckout(checkoutData).pipe(map((checkout) => checkout.lineItems), concatMap((lineItems) => of(this.generateCartObject(lineItems))));
469
- }
470
- }
471
- CartService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CartService, deps: [{ token: AuthService }, { token: CheckoutService }, { token: DEFAULT_SHOP }, { token: 'ProductRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
472
- CartService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CartService });
473
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CartService, decorators: [{
474
- type: Injectable
475
- }], ctorParameters: function () { return [{ type: AuthService }, { type: CheckoutService }, { type: i2.Shops, decorators: [{
476
- type: Inject,
477
- args: [DEFAULT_SHOP]
478
- }] }, { type: undefined, decorators: [{
479
- type: Inject,
480
- args: ['ProductRepository']
481
- }] }]; } });
482
-
483
- class CheckoutSubscriptionService {
484
- constructor(checkoutSubscriptionRepository, subscriptionRepository, couponService) {
485
- this.checkoutSubscriptionRepository = checkoutSubscriptionRepository;
486
- this.subscriptionRepository = subscriptionRepository;
487
- this.couponService = couponService;
488
- }
489
- getCheckoutSubscription(checkoutData) {
490
- const checkoutId = cookie.get('checkoutSubscriptionId');
491
- if (!isNil(checkoutId))
492
- return from(this.checkoutSubscriptionRepository.get({ id: checkoutId }));
493
- return from(this.createCheckoutSubscription(checkoutData));
494
- }
495
- async createCheckoutSubscription(checkoutData) {
496
- const checkout = await this.checkoutSubscriptionRepository.create({
497
- createdAt: new Date(),
498
- ...CheckoutSubscription.toInstance(pick(checkoutData, ['user', 'shop'])).toPlain(),
499
- });
500
- cookie.set('checkoutSubscriptionId', checkout.id);
501
- return checkout;
502
- }
503
- clearCheckoutSubscriptionFromSession() {
504
- cookie.remove('checkoutSubscriptionId');
505
- return of();
506
- }
507
- checkCoupon(nickname, userEmail) {
508
- return this.getCheckoutSubscription().pipe(concatMap((checkout) => this.couponService.checkCoupon(nickname, userEmail, CheckoutTypes.SUBSCRIPTION, checkout.subscriptionPlan.name, null, true).pipe()));
509
- }
510
- calcDiscountSubscription(coupon) {
511
- return this.getCheckoutSubscription().pipe(concatMap((checkout) => this.couponService.calcDiscountSubscription(coupon, checkout).pipe()));
512
- }
513
- }
514
- CheckoutSubscriptionService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutSubscriptionService, deps: [{ token: 'CheckoutSubscriptionRepository' }, { token: 'SubscriptionRepository' }, { token: CouponService }], target: i0.ɵɵFactoryTarget.Injectable });
515
- CheckoutSubscriptionService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutSubscriptionService });
516
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutSubscriptionService, decorators: [{
517
- type: Injectable
518
- }], ctorParameters: function () { return [{ type: undefined, decorators: [{
519
- type: Inject,
520
- args: ['CheckoutSubscriptionRepository']
521
- }] }, { type: undefined, decorators: [{
522
- type: Inject,
523
- args: ['SubscriptionRepository']
524
- }] }, { type: CouponService }]; } });
525
-
526
- class OrderService {
527
- constructor(angularFirestore, orderRepository) {
528
- this.angularFirestore = angularFirestore;
529
- this.orderRepository = orderRepository;
530
- this.orderSubject = new Subject();
531
- }
532
- getOrder(id) {
533
- docSnapshots(doc(this.angularFirestore, `${this.orderRepository.collectionName}/${id}`))
534
- .pipe(map((doc) => Order.toInstance(doc.data())))
535
- .subscribe((doc) => this.orderSubject.next(doc));
536
- return this.orderSubject;
537
- }
538
- }
539
- OrderService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: OrderService, deps: [{ token: i1$1.Firestore }, { token: 'OrderRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
540
- OrderService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: OrderService });
541
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: OrderService, decorators: [{
542
- type: Injectable
543
- }], ctorParameters: function () { return [{ type: i1$1.Firestore }, { type: i2.OrderFirestoreRepository, decorators: [{
544
- type: Inject,
545
- args: ['OrderRepository']
546
- }] }]; } });
547
-
548
- class HomeShopService {
549
- constructor(categoryRepository, homeRepository, productRepository, defaultShop) {
550
- this.categoryRepository = categoryRepository;
551
- this.homeRepository = homeRepository;
552
- this.productRepository = productRepository;
553
- this.defaultShop = defaultShop;
554
- this.buildCategoryGroupWithRequiredData = (group) => ({
555
- category: Category.toInstance(pick(group?.category?.toPlain() || {}, ['id', 'name', 'slug', 'conditions'])),
556
- products: group?.products?.map((product) => Product.toInstance(pick(product?.toPlain() || {}, [
557
- 'id',
558
- 'price',
559
- 'reviews',
560
- 'hasVariants',
561
- 'slug',
562
- 'sku',
563
- 'stock',
564
- 'costPrice',
565
- 'images',
566
- 'miniatures',
567
- 'name',
568
- 'weight',
569
- 'rate',
570
- 'type',
571
- ]))) || [],
572
- });
573
- }
574
- get homeId() {
575
- if (this.defaultShop === Shops.GLAMSHOP)
576
- return 'glamshop';
577
- if (this.defaultShop === Shops.MENSMARKET)
578
- return 'mens_market';
579
- return null;
580
- }
581
- getHomeData() {
582
- return this.getHomeConfiguration().pipe(map((home) => (home?.data?.expiresAt > new Date() ? home : null)), concatMap((home) => home
583
- ? of(home)
584
- : forkJoin([this.getDiscoverProducts(), this.getFeaturedProducts(), this.getVerticalProducts()]).pipe(map(([discoverProducts, featuredProducts, verticalProducts]) => ({
585
- discoverProducts,
586
- featuredProducts,
587
- verticalProducts,
588
- })), concatMap((data) => this.saveHomeData(data)))));
589
- }
590
- getBanners(type) {
591
- return this.getHomeConfiguration().pipe(map((home) => {
592
- if (type === 'brand')
593
- return home.brandsCarousel;
594
- if (type === 'buyToWin')
595
- return [home.buyToWinBanner];
596
- if (type === 'block')
597
- return home.blockBanners;
598
- if (type === 'blog')
599
- return [home.blogBanner];
600
- return [];
601
- }));
602
- }
603
- getMinValueForFreeShipping() {
604
- return this.getHomeConfiguration().pipe(map((home) => home.minValueForFreeShipping));
605
- }
606
- getDiscoverProducts() {
607
- return this.getHomeConfiguration().pipe(concatMap((home) => from(this.categoryRepository.getCategoriesForHome(home.discoverCategories)).pipe(map((groups) => groups.map(this.buildCategoryGroupWithRequiredData)))));
608
- }
609
- getFeaturedProducts() {
610
- return this.getHomeConfiguration().pipe(concatMap((home) => from(this.categoryRepository.getCategoriesForHome(home.featuredCategories)).pipe(map((groups) => groups.map(this.buildCategoryGroupWithRequiredData)))));
611
- }
612
- getVerticalProducts() {
613
- return this.getHomeConfiguration().pipe(concatMap((home) => forkJoin(home.verticalCarousels.filter(Boolean).map((id) => from(this.categoryRepository.get({ id })).pipe(concatMap((category) => from(this.productRepository.find({
614
- filters: { categories: { operator: Where.IN, value: [category.id] } },
615
- limits: { limit: 12 },
616
- })).pipe(map((products) => ({ category, products })))), map(({ category, products }) => ({ category, products: products.data })), map(this.buildCategoryGroupWithRequiredData))))));
617
- }
618
- getHomeConfiguration() {
619
- return of(this.homeConfiguration).pipe(concatMap((home) => home
620
- ? of(home)
621
- : !this.homeId
622
- ? throwError(new RequiredArgumentError(['homeId']))
623
- : from(this.homeRepository.get({ id: this.homeId })).pipe(tap((homeLoaded) => (this.homeConfiguration = homeLoaded)))));
624
- }
625
- saveHomeData(homeData) {
626
- const data = {
627
- createdAt: new Date(),
628
- expiresAt: add(new Date(), { hours: 1 }),
629
- data: homeData,
630
- };
631
- return from(this.homeRepository.update({
632
- id: this.homeId,
633
- data,
634
- })).pipe(tap(() => (this.homeConfiguration.data = data)), map(() => this.homeConfiguration));
635
- }
636
- }
637
- HomeShopService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: HomeShopService, deps: [{ token: 'CategoryRepository' }, { token: 'HomeRepository' }, { token: 'ProductRepository' }, { token: DEFAULT_SHOP }], target: i0.ɵɵFactoryTarget.Injectable });
638
- HomeShopService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: HomeShopService });
639
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: HomeShopService, decorators: [{
640
- type: Injectable
641
- }], ctorParameters: function () { return [{ type: undefined, decorators: [{
642
- type: Inject,
643
- args: ['CategoryRepository']
644
- }] }, { type: undefined, decorators: [{
645
- type: Inject,
646
- args: ['HomeRepository']
647
- }] }, { type: undefined, decorators: [{
648
- type: Inject,
649
- args: ['ProductRepository']
650
- }] }, { type: i2.Shops, decorators: [{
651
- type: Inject,
652
- args: [DEFAULT_SHOP]
653
- }] }]; } });
234
+ deps: ['FirestoreOptions'],
235
+ },
236
+ {
237
+ provide: CategoryFirestoreRepository,
238
+ useFactory: (options) => {
239
+ return new CategoryFirestoreRepository(options);
240
+ },
241
+ deps: ['FirestoreOptions'],
242
+ },
243
+ {
244
+ provide: 'CheckoutRepository',
245
+ useFactory: (options) => {
246
+ return new CheckoutFirestoreRepository(options);
247
+ },
248
+ deps: ['FirestoreOptions'],
249
+ },
250
+ {
251
+ provide: 'CheckoutSubscriptionRepository',
252
+ useFactory: (options) => {
253
+ return new CheckoutSubscriptionFirestoreRepository(options);
254
+ },
255
+ deps: ['FirestoreOptions'],
256
+ },
257
+ {
258
+ provide: 'CouponRepository',
259
+ useFactory: (options) => {
260
+ return new CouponFirestoreRepository(options);
261
+ },
262
+ deps: ['FirestoreOptions'],
263
+ },
264
+ {
265
+ provide: 'CampaignHashtagRepository',
266
+ useFactory: (options) => {
267
+ return new CampaignHashtagFirestoreRepository(options);
268
+ },
269
+ deps: ['FirestoreOptions'],
270
+ },
271
+ {
272
+ provide: 'CampaignDashboardRepository',
273
+ useFactory: (options) => {
274
+ return new CampaignDashboardFirestoreRepository(options);
275
+ },
276
+ deps: ['FirestoreOptions'],
277
+ },
278
+ {
279
+ provide: 'EditionRepository',
280
+ useFactory: (options, subscriptionRepository) => {
281
+ return new SubscriptionEditionFirestoreRepository(options, subscriptionRepository);
282
+ },
283
+ deps: ['FirestoreOptions', 'SubscriptionRepository'],
284
+ },
285
+ {
286
+ provide: 'HomeRepository',
287
+ useFactory: (options) => {
288
+ return new HomeFirestoreRepository(options);
289
+ },
290
+ deps: ['FirestoreOptions'],
291
+ },
292
+ {
293
+ provide: 'LeadRepository',
294
+ useFactory: (options) => {
295
+ return new LeadFirestoreRepository(options);
296
+ },
297
+ deps: ['FirestoreOptions'],
298
+ },
299
+ {
300
+ provide: 'LegacyOrderRepository',
301
+ useFactory: (options) => {
302
+ return new LegacyOrderFirestoreRepository(options);
303
+ },
304
+ deps: ['FirestoreOptions'],
305
+ },
306
+ {
307
+ provide: 'ShopMenuRepository',
308
+ useFactory: (options) => {
309
+ return new ShopMenuFirestoreRepository(options);
310
+ },
311
+ deps: ['FirestoreOptions'],
312
+ },
313
+ {
314
+ provide: 'OrderRepository',
315
+ useFactory: (options) => {
316
+ return new OrderFirestoreRepository(options);
317
+ },
318
+ deps: ['FirestoreOptions'],
319
+ },
320
+ {
321
+ provide: 'PaymentRepository',
322
+ useFactory: (options) => {
323
+ return new PaymentFirestoreRepository(options);
324
+ },
325
+ deps: ['FirestoreOptions'],
326
+ },
327
+ {
328
+ provide: ProductFirestoreRepository,
329
+ useFactory: (options) => {
330
+ return new ProductFirestoreRepository(options);
331
+ },
332
+ deps: ['FirestoreOptions'],
333
+ },
334
+ {
335
+ provide: 'ShopSettingsRepository',
336
+ useFactory: (options) => {
337
+ return new ShopSettingsFirestoreRepository(options);
338
+ },
339
+ deps: ['FirestoreOptions'],
340
+ },
341
+ {
342
+ provide: 'SubscriptionPaymentRepository',
343
+ useFactory: (options, subscriptionRepository) => {
344
+ return new SubscriptionPaymentFirestoreRepository(options, subscriptionRepository);
345
+ },
346
+ deps: ['FirestoreOptions', 'SubscriptionRepository'],
347
+ },
348
+ {
349
+ provide: 'SubscriptionPlanRepository',
350
+ useFactory: (options) => {
351
+ return new SubscriptionPlanFirestoreRepository(options);
352
+ },
353
+ deps: ['FirestoreOptions'],
354
+ },
355
+ {
356
+ provide: 'SubscriptionProductRepository',
357
+ useFactory: (options) => {
358
+ return new SubscriptionProductFirestoreRepository(options);
359
+ },
360
+ deps: ['FirestoreOptions'],
361
+ },
362
+ {
363
+ provide: 'SubscriptionRepository',
364
+ useFactory: (options) => {
365
+ return new SubscriptionFirestoreRepository(options);
366
+ },
367
+ deps: ['FirestoreOptions'],
368
+ },
369
+ {
370
+ provide: 'UserRepository',
371
+ useFactory: (options) => {
372
+ return new UserFirestoreRepository(options);
373
+ },
374
+ deps: ['FirestoreOptions'],
375
+ },
376
+ {
377
+ provide: 'UserAddressRepository',
378
+ useFactory: (options, userRepository) => {
379
+ return new UserAddressFirestoreRepository(options, userRepository);
380
+ },
381
+ deps: ['FirestoreOptions', 'UserRepository'],
382
+ },
383
+ {
384
+ provide: 'UserPaymentMethodRepository',
385
+ useFactory: (options, userRepository) => {
386
+ return new UserPaymentMethodFirestoreRepository(options, userRepository);
387
+ },
388
+ deps: ['FirestoreOptions', 'UserRepository'],
389
+ },
390
+ {
391
+ provide: ProductVariantFirestoreRepository,
392
+ useFactory: (options, productRepository) => {
393
+ return new ProductVariantFirestoreRepository(options, productRepository);
394
+ },
395
+ deps: ['FirestoreOptions', ProductFirestoreRepository],
396
+ },
397
+ ], imports: [AngularElasticSeachModule,
398
+ provideFirebaseApp((injector) => {
399
+ const appName = injector.get(FIREBASE_APP_NAME);
400
+ return appName
401
+ ? initializeApp$1(injector.get(FIREBASE_OPTIONS), appName)
402
+ : initializeApp$1(injector.get(FIREBASE_OPTIONS));
403
+ }),
404
+ provideFirestore((injector) => {
405
+ const firestore = initializeFirestore(injector.get(FirebaseApp), { experimentalForceLongPolling: true });
406
+ enableIndexedDbPersistence(firestore).catch(console.error);
407
+ return firestore;
408
+ })] });
409
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirestoreModule, decorators: [{
410
+ type: NgModule,
411
+ args: [{
412
+ imports: [
413
+ AngularElasticSeachModule,
414
+ provideFirebaseApp((injector) => {
415
+ const appName = injector.get(FIREBASE_APP_NAME);
416
+ return appName
417
+ ? initializeApp$1(injector.get(FIREBASE_OPTIONS), appName)
418
+ : initializeApp$1(injector.get(FIREBASE_OPTIONS));
419
+ }),
420
+ provideFirestore((injector) => {
421
+ const firestore = initializeFirestore(injector.get(FirebaseApp), { experimentalForceLongPolling: true });
422
+ enableIndexedDbPersistence(firestore).catch(console.error);
423
+ return firestore;
424
+ }),
425
+ ],
426
+ providers: [
427
+ {
428
+ provide: 'FirestoreOptions',
429
+ useFactory: (firestore, platformId) => ({
430
+ firestore,
431
+ interceptors: {
432
+ request: (request) => {
433
+ if (isPlatformBrowser(platformId))
434
+ return request;
435
+ const interval = setInterval(() => { }, 100);
436
+ request.interval = interval;
437
+ return request;
438
+ },
439
+ response: (response, request) => {
440
+ if (isPlatformBrowser(platformId))
441
+ return response;
442
+ clearInterval(request.interval);
443
+ return response;
444
+ },
445
+ },
446
+ }),
447
+ deps: [Firestore, PLATFORM_ID],
448
+ },
449
+ {
450
+ provide: 'BeautyProfileRepository',
451
+ useFactory: (config, userRepository) => {
452
+ return new UserBeautyProfileFirestoreRepository(config, userRepository);
453
+ },
454
+ deps: ['FirestoreOptions', 'UserRepository'],
455
+ },
456
+ {
457
+ provide: 'Buy2WinRepository',
458
+ useFactory: (options) => {
459
+ return new Buy2WinFirestoreRepository(options);
460
+ },
461
+ deps: ['FirestoreOptions'],
462
+ },
463
+ {
464
+ provide: CategoryFirestoreRepository,
465
+ useFactory: (options) => {
466
+ return new CategoryFirestoreRepository(options);
467
+ },
468
+ deps: ['FirestoreOptions'],
469
+ },
470
+ {
471
+ provide: 'CheckoutRepository',
472
+ useFactory: (options) => {
473
+ return new CheckoutFirestoreRepository(options);
474
+ },
475
+ deps: ['FirestoreOptions'],
476
+ },
477
+ {
478
+ provide: 'CheckoutSubscriptionRepository',
479
+ useFactory: (options) => {
480
+ return new CheckoutSubscriptionFirestoreRepository(options);
481
+ },
482
+ deps: ['FirestoreOptions'],
483
+ },
484
+ {
485
+ provide: 'CouponRepository',
486
+ useFactory: (options) => {
487
+ return new CouponFirestoreRepository(options);
488
+ },
489
+ deps: ['FirestoreOptions'],
490
+ },
491
+ {
492
+ provide: 'CampaignHashtagRepository',
493
+ useFactory: (options) => {
494
+ return new CampaignHashtagFirestoreRepository(options);
495
+ },
496
+ deps: ['FirestoreOptions'],
497
+ },
498
+ {
499
+ provide: 'CampaignDashboardRepository',
500
+ useFactory: (options) => {
501
+ return new CampaignDashboardFirestoreRepository(options);
502
+ },
503
+ deps: ['FirestoreOptions'],
504
+ },
505
+ {
506
+ provide: 'EditionRepository',
507
+ useFactory: (options, subscriptionRepository) => {
508
+ return new SubscriptionEditionFirestoreRepository(options, subscriptionRepository);
509
+ },
510
+ deps: ['FirestoreOptions', 'SubscriptionRepository'],
511
+ },
512
+ {
513
+ provide: 'HomeRepository',
514
+ useFactory: (options) => {
515
+ return new HomeFirestoreRepository(options);
516
+ },
517
+ deps: ['FirestoreOptions'],
518
+ },
519
+ {
520
+ provide: 'LeadRepository',
521
+ useFactory: (options) => {
522
+ return new LeadFirestoreRepository(options);
523
+ },
524
+ deps: ['FirestoreOptions'],
525
+ },
526
+ {
527
+ provide: 'LegacyOrderRepository',
528
+ useFactory: (options) => {
529
+ return new LegacyOrderFirestoreRepository(options);
530
+ },
531
+ deps: ['FirestoreOptions'],
532
+ },
533
+ {
534
+ provide: 'ShopMenuRepository',
535
+ useFactory: (options) => {
536
+ return new ShopMenuFirestoreRepository(options);
537
+ },
538
+ deps: ['FirestoreOptions'],
539
+ },
540
+ {
541
+ provide: 'OrderRepository',
542
+ useFactory: (options) => {
543
+ return new OrderFirestoreRepository(options);
544
+ },
545
+ deps: ['FirestoreOptions'],
546
+ },
547
+ {
548
+ provide: 'PaymentRepository',
549
+ useFactory: (options) => {
550
+ return new PaymentFirestoreRepository(options);
551
+ },
552
+ deps: ['FirestoreOptions'],
553
+ },
554
+ {
555
+ provide: ProductFirestoreRepository,
556
+ useFactory: (options) => {
557
+ return new ProductFirestoreRepository(options);
558
+ },
559
+ deps: ['FirestoreOptions'],
560
+ },
561
+ {
562
+ provide: 'ShopSettingsRepository',
563
+ useFactory: (options) => {
564
+ return new ShopSettingsFirestoreRepository(options);
565
+ },
566
+ deps: ['FirestoreOptions'],
567
+ },
568
+ {
569
+ provide: 'SubscriptionPaymentRepository',
570
+ useFactory: (options, subscriptionRepository) => {
571
+ return new SubscriptionPaymentFirestoreRepository(options, subscriptionRepository);
572
+ },
573
+ deps: ['FirestoreOptions', 'SubscriptionRepository'],
574
+ },
575
+ {
576
+ provide: 'SubscriptionPlanRepository',
577
+ useFactory: (options) => {
578
+ return new SubscriptionPlanFirestoreRepository(options);
579
+ },
580
+ deps: ['FirestoreOptions'],
581
+ },
582
+ {
583
+ provide: 'SubscriptionProductRepository',
584
+ useFactory: (options) => {
585
+ return new SubscriptionProductFirestoreRepository(options);
586
+ },
587
+ deps: ['FirestoreOptions'],
588
+ },
589
+ {
590
+ provide: 'SubscriptionRepository',
591
+ useFactory: (options) => {
592
+ return new SubscriptionFirestoreRepository(options);
593
+ },
594
+ deps: ['FirestoreOptions'],
595
+ },
596
+ {
597
+ provide: 'UserRepository',
598
+ useFactory: (options) => {
599
+ return new UserFirestoreRepository(options);
600
+ },
601
+ deps: ['FirestoreOptions'],
602
+ },
603
+ {
604
+ provide: 'UserAddressRepository',
605
+ useFactory: (options, userRepository) => {
606
+ return new UserAddressFirestoreRepository(options, userRepository);
607
+ },
608
+ deps: ['FirestoreOptions', 'UserRepository'],
609
+ },
610
+ {
611
+ provide: 'UserPaymentMethodRepository',
612
+ useFactory: (options, userRepository) => {
613
+ return new UserPaymentMethodFirestoreRepository(options, userRepository);
614
+ },
615
+ deps: ['FirestoreOptions', 'UserRepository'],
616
+ },
617
+ {
618
+ provide: ProductVariantFirestoreRepository,
619
+ useFactory: (options, productRepository) => {
620
+ return new ProductVariantFirestoreRepository(options, productRepository);
621
+ },
622
+ deps: ['FirestoreOptions', ProductFirestoreRepository],
623
+ },
624
+ ],
625
+ }]
626
+ }] });
654
627
 
655
- class AngularFirebaseAuthModule {
656
- static initializeApp(options, nameOrConfig) {
628
+ class AngularHasuraGraphQLModule {
629
+ static initializeApp(options) {
657
630
  return {
658
- ngModule: AngularFirebaseAuthModule,
659
- providers: [
660
- { provide: FIREBASE_OPTIONS, useValue: options },
661
- { provide: FIREBASE_APP_NAME, useValue: nameOrConfig },
662
- ],
631
+ ngModule: AngularHasuraGraphQLModule,
632
+ providers: [{ provide: HASURA_OPTIONS, useValue: options }],
663
633
  };
664
634
  }
665
635
  }
666
- AngularFirebaseAuthModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirebaseAuthModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
667
- AngularFirebaseAuthModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.0", ngImport: i0, type: AngularFirebaseAuthModule, imports: [i1$2.FirebaseAppModule, i1.AuthModule] });
668
- AngularFirebaseAuthModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirebaseAuthModule, providers: [
636
+ AngularHasuraGraphQLModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularHasuraGraphQLModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
637
+ AngularHasuraGraphQLModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.0", ngImport: i0, type: AngularHasuraGraphQLModule });
638
+ AngularHasuraGraphQLModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularHasuraGraphQLModule, providers: [
669
639
  {
670
- provide: 'Authentication',
671
- useFactory: (authenticationService, userRepository) => {
672
- return new Authentication(authenticationService, userRepository);
640
+ provide: 'HasuraConfig',
641
+ useFactory: (options, platformId) => ({
642
+ endpoint: options.endpoint,
643
+ authOptions: options.credentials,
644
+ interceptors: {
645
+ request: (request) => {
646
+ if (isPlatformBrowser(platformId))
647
+ return request;
648
+ const interval = setInterval(() => { }, 100);
649
+ request.interval = interval;
650
+ return request;
651
+ },
652
+ response: (response, request) => {
653
+ if (isPlatformBrowser(platformId))
654
+ return response;
655
+ clearInterval(request.interval);
656
+ return response;
657
+ },
658
+ },
659
+ }),
660
+ deps: [HASURA_OPTIONS, PLATFORM_ID],
661
+ },
662
+ {
663
+ provide: 'CategoryRepository',
664
+ useExisting: CategoryHasuraGraphQLRepository,
665
+ },
666
+ {
667
+ provide: CategoryHasuraGraphQLRepository,
668
+ useFactory: (options, productRepository, categoryFilterRepository) => {
669
+ return new CategoryHasuraGraphQLRepository(options, productRepository, categoryFilterRepository);
673
670
  },
674
- deps: ['AuthenticationService', 'UserRepository'],
671
+ deps: ['HasuraConfig', ProductHasuraGraphQLRepository, CategoryFilterHasuraGraphQLRepository],
675
672
  },
676
673
  {
677
- provide: 'AuthenticationService',
678
- useFactory: (angularFireAuth) => {
679
- return new AuthenticationFirebaseAuthService(angularFireAuth);
674
+ provide: 'ProductRepository',
675
+ useExisting: ProductHasuraGraphQLRepository,
676
+ },
677
+ {
678
+ provide: ProductHasuraGraphQLRepository,
679
+ useFactory: (hasuraConfig) => {
680
+ return new ProductHasuraGraphQLRepository(hasuraConfig);
680
681
  },
681
- deps: [Auth],
682
+ deps: ['HasuraConfig'],
682
683
  },
683
684
  {
684
- provide: 'Register',
685
- useFactory: (registerService, userRepository) => {
686
- return new Register(registerService, userRepository);
685
+ provide: 'VariantRepository',
686
+ useExisting: VariantHasuraGraphQLRepository,
687
+ },
688
+ {
689
+ provide: VariantHasuraGraphQLRepository,
690
+ useFactory: (hasuraConfig) => {
691
+ return new VariantHasuraGraphQLRepository(hasuraConfig);
687
692
  },
688
- deps: ['RegisterService', 'UserRepository'],
693
+ deps: ['HasuraConfig'],
689
694
  },
690
695
  {
691
- provide: 'RegisterService',
692
- useFactory: (angularFireAuth) => {
693
- return new RegisterFirebaseAuthService(angularFireAuth);
696
+ provide: 'CategoryFilterRepository',
697
+ useExisting: CategoryFilterHasuraGraphQLRepository,
698
+ },
699
+ {
700
+ provide: CategoryFilterHasuraGraphQLRepository,
701
+ useFactory: (options) => {
702
+ return new CategoryFilterHasuraGraphQLRepository(options);
694
703
  },
695
- deps: [Auth],
704
+ deps: ['HasuraConfig'],
696
705
  },
697
706
  {
698
- provide: 'SignOut',
699
- useFactory: (authenticationService) => {
700
- return new SignOut(authenticationService);
707
+ provide: 'FilterOptionRepository',
708
+ useExisting: FilterOptionHasuraGraphQLRepository,
709
+ },
710
+ {
711
+ provide: FilterOptionHasuraGraphQLRepository,
712
+ useFactory: (options) => {
713
+ return new FilterOptionHasuraGraphQLRepository(options);
701
714
  },
702
- deps: ['AuthenticationService'],
715
+ deps: ['HasuraConfig'],
716
+ },
717
+ {
718
+ provide: 'FilterRepository',
719
+ useExisting: FilterHasuraGraphQLRepository,
703
720
  },
704
721
  {
705
- provide: 'RecoveryPassword',
706
- useFactory: (authenticationService) => {
707
- return new RecoveryPassword(authenticationService);
722
+ provide: FilterHasuraGraphQLRepository,
723
+ useFactory: (options, filterOptionRepository, categoryFilterRepository) => {
724
+ return new FilterHasuraGraphQLRepository(options, filterOptionRepository, categoryFilterRepository);
708
725
  },
709
- deps: ['AuthenticationService'],
726
+ deps: ['HasuraConfig', FilterOptionHasuraGraphQLRepository, CategoryFilterHasuraGraphQLRepository],
710
727
  },
711
- ], imports: [provideFirebaseApp((injector) => {
712
- const appName = injector.get(FIREBASE_APP_NAME);
713
- return appName
714
- ? initializeApp(injector.get(FIREBASE_OPTIONS), appName)
715
- : initializeApp(injector.get(FIREBASE_OPTIONS));
716
- }),
717
- provideAuth(() => getAuth())] });
718
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirebaseAuthModule, decorators: [{
728
+ {
729
+ provide: CategoryCollectionChildrenHasuraGraphQLRepository,
730
+ useFactory: (options) => new CategoryCollectionChildrenHasuraGraphQLRepository(options.endpoint, options.credentials),
731
+ },
732
+ {
733
+ provide: 'CategoryCollectionChildrenRepository',
734
+ useExisting: CategoryCollectionChildrenHasuraGraphQLRepository,
735
+ },
736
+ ] });
737
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularHasuraGraphQLModule, decorators: [{
719
738
  type: NgModule,
720
739
  args: [{
721
- imports: [
722
- provideFirebaseApp((injector) => {
723
- const appName = injector.get(FIREBASE_APP_NAME);
724
- return appName
725
- ? initializeApp(injector.get(FIREBASE_OPTIONS), appName)
726
- : initializeApp(injector.get(FIREBASE_OPTIONS));
727
- }),
728
- provideAuth(() => getAuth()),
729
- ],
730
740
  providers: [
731
741
  {
732
- provide: 'Authentication',
733
- useFactory: (authenticationService, userRepository) => {
734
- return new Authentication(authenticationService, userRepository);
742
+ provide: 'HasuraConfig',
743
+ useFactory: (options, platformId) => ({
744
+ endpoint: options.endpoint,
745
+ authOptions: options.credentials,
746
+ interceptors: {
747
+ request: (request) => {
748
+ if (isPlatformBrowser(platformId))
749
+ return request;
750
+ const interval = setInterval(() => { }, 100);
751
+ request.interval = interval;
752
+ return request;
753
+ },
754
+ response: (response, request) => {
755
+ if (isPlatformBrowser(platformId))
756
+ return response;
757
+ clearInterval(request.interval);
758
+ return response;
759
+ },
760
+ },
761
+ }),
762
+ deps: [HASURA_OPTIONS, PLATFORM_ID],
763
+ },
764
+ {
765
+ provide: 'CategoryRepository',
766
+ useExisting: CategoryHasuraGraphQLRepository,
767
+ },
768
+ {
769
+ provide: CategoryHasuraGraphQLRepository,
770
+ useFactory: (options, productRepository, categoryFilterRepository) => {
771
+ return new CategoryHasuraGraphQLRepository(options, productRepository, categoryFilterRepository);
735
772
  },
736
- deps: ['AuthenticationService', 'UserRepository'],
773
+ deps: ['HasuraConfig', ProductHasuraGraphQLRepository, CategoryFilterHasuraGraphQLRepository],
737
774
  },
738
775
  {
739
- provide: 'AuthenticationService',
740
- useFactory: (angularFireAuth) => {
741
- return new AuthenticationFirebaseAuthService(angularFireAuth);
776
+ provide: 'ProductRepository',
777
+ useExisting: ProductHasuraGraphQLRepository,
778
+ },
779
+ {
780
+ provide: ProductHasuraGraphQLRepository,
781
+ useFactory: (hasuraConfig) => {
782
+ return new ProductHasuraGraphQLRepository(hasuraConfig);
742
783
  },
743
- deps: [Auth],
784
+ deps: ['HasuraConfig'],
744
785
  },
745
786
  {
746
- provide: 'Register',
747
- useFactory: (registerService, userRepository) => {
748
- return new Register(registerService, userRepository);
787
+ provide: 'VariantRepository',
788
+ useExisting: VariantHasuraGraphQLRepository,
789
+ },
790
+ {
791
+ provide: VariantHasuraGraphQLRepository,
792
+ useFactory: (hasuraConfig) => {
793
+ return new VariantHasuraGraphQLRepository(hasuraConfig);
749
794
  },
750
- deps: ['RegisterService', 'UserRepository'],
795
+ deps: ['HasuraConfig'],
751
796
  },
752
797
  {
753
- provide: 'RegisterService',
754
- useFactory: (angularFireAuth) => {
755
- return new RegisterFirebaseAuthService(angularFireAuth);
798
+ provide: 'CategoryFilterRepository',
799
+ useExisting: CategoryFilterHasuraGraphQLRepository,
800
+ },
801
+ {
802
+ provide: CategoryFilterHasuraGraphQLRepository,
803
+ useFactory: (options) => {
804
+ return new CategoryFilterHasuraGraphQLRepository(options);
756
805
  },
757
- deps: [Auth],
806
+ deps: ['HasuraConfig'],
758
807
  },
759
808
  {
760
- provide: 'SignOut',
761
- useFactory: (authenticationService) => {
762
- return new SignOut(authenticationService);
809
+ provide: 'FilterOptionRepository',
810
+ useExisting: FilterOptionHasuraGraphQLRepository,
811
+ },
812
+ {
813
+ provide: FilterOptionHasuraGraphQLRepository,
814
+ useFactory: (options) => {
815
+ return new FilterOptionHasuraGraphQLRepository(options);
763
816
  },
764
- deps: ['AuthenticationService'],
817
+ deps: ['HasuraConfig'],
765
818
  },
766
819
  {
767
- provide: 'RecoveryPassword',
768
- useFactory: (authenticationService) => {
769
- return new RecoveryPassword(authenticationService);
820
+ provide: 'FilterRepository',
821
+ useExisting: FilterHasuraGraphQLRepository,
822
+ },
823
+ {
824
+ provide: FilterHasuraGraphQLRepository,
825
+ useFactory: (options, filterOptionRepository, categoryFilterRepository) => {
826
+ return new FilterHasuraGraphQLRepository(options, filterOptionRepository, categoryFilterRepository);
770
827
  },
771
- deps: ['AuthenticationService'],
828
+ deps: ['HasuraConfig', FilterOptionHasuraGraphQLRepository, CategoryFilterHasuraGraphQLRepository],
829
+ },
830
+ {
831
+ provide: CategoryCollectionChildrenHasuraGraphQLRepository,
832
+ useFactory: (options) => new CategoryCollectionChildrenHasuraGraphQLRepository(options.endpoint, options.credentials),
833
+ },
834
+ {
835
+ provide: 'CategoryCollectionChildrenRepository',
836
+ useExisting: CategoryCollectionChildrenHasuraGraphQLRepository,
772
837
  },
773
838
  ],
774
839
  }]
775
840
  }] });
776
841
 
777
- class AngularElasticSeachModule {
778
- static initializeApp(options) {
779
- return {
780
- ngModule: AngularElasticSeachModule,
781
- providers: [{ provide: ES_CONFIG, useValue: options }],
782
- };
842
+ class AuthService {
843
+ constructor(angularFireAuth, userRepository) {
844
+ this.angularFireAuth = angularFireAuth;
845
+ this.userRepository = userRepository;
846
+ }
847
+ getAuthstate() {
848
+ const observables = [this.getFireUser(), this.getUser()];
849
+ return combineLatest(observables).pipe(map(([fireUser, user]) => ({
850
+ user,
851
+ isAnonymous: fireUser?.isAnonymous,
852
+ })));
853
+ }
854
+ getUser() {
855
+ return this.getFireUser().pipe(map((user) => user?.uid), mergeMap((id) => (id ? from(this.userRepository.get({ id })).pipe(catchError(() => of(null))) : of(null))));
856
+ }
857
+ getTokenId() {
858
+ return from(getIdToken(this.angularFireAuth.currentUser));
859
+ }
860
+ getFireUser() {
861
+ return authState(this.angularFireAuth).pipe(catchError(() => of(null)));
862
+ }
863
+ }
864
+ AuthService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AuthService, deps: [{ token: i1$1.Auth }, { token: 'UserRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
865
+ AuthService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AuthService });
866
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AuthService, decorators: [{
867
+ type: Injectable
868
+ }], ctorParameters: function () { return [{ type: i1$1.Auth }, { type: undefined, decorators: [{
869
+ type: Inject,
870
+ args: ['UserRepository']
871
+ }] }]; } });
872
+
873
+ class CouponService {
874
+ constructor(couponRepository, defaultShop, orderRepository, categoryRepository) {
875
+ this.couponRepository = couponRepository;
876
+ this.defaultShop = defaultShop;
877
+ this.orderRepository = orderRepository;
878
+ this.categoryRepository = categoryRepository;
879
+ this.emailIsFromCollaborator = (userEmail) => !!userEmail?.match(/@b4a.com.br/g);
880
+ }
881
+ checkCoupon(nickname, checkoutType, checkout, plan) {
882
+ return from(this.couponRepository
883
+ .find({
884
+ filters: {
885
+ nickname: { operator: Where.EQUALS, value: nickname },
886
+ active: { operator: Where.EQUALS, value: true },
887
+ },
888
+ })
889
+ .then((result) => result.data[0])).pipe(concatMap((coupon) => this.couponValidation(coupon, checkoutType)), concatMap((couponValid) => this.couponRulesValidation(couponValid, checkoutType, checkout, plan)), map((couponValidated) => couponValidated));
890
+ }
891
+ async couponValidation(coupon, checkoutType) {
892
+ if (!coupon)
893
+ throw 'Cupom inválido.';
894
+ if (coupon?.beginAt && coupon?.beginAt.getTime() > new Date().getTime())
895
+ throw 'Cupom inválido.';
896
+ if (coupon?.expiresIn && coupon?.expiresIn.getTime() < new Date().getTime())
897
+ throw 'Cupom expirado.';
898
+ const isInShop = coupon.shopAvailability === Shops.ALL || coupon.shopAvailability === this.defaultShop;
899
+ if (!isInShop)
900
+ throw 'Cupom inválido para loja.';
901
+ const isCheckoutType = coupon.checkoutType === CheckoutTypes.ALL || coupon.checkoutType === checkoutType;
902
+ if (!isCheckoutType)
903
+ throw 'Cupom inválido. Erro de checkout.';
904
+ return coupon;
905
+ }
906
+ async couponRulesValidation(coupon, checkoutType, checkout, plan) {
907
+ if (checkoutType == CheckoutTypes.SUBSCRIPTION) {
908
+ if (coupon.plan && coupon.plan.toUpperCase() !== plan.toUpperCase())
909
+ throw 'Cupom inválido para sua assinatura.';
910
+ return coupon;
911
+ }
912
+ const validUser = this.coupomUserValidation(coupon, checkout?.user);
913
+ if (!validUser)
914
+ throw 'Usuário não elegível.';
915
+ const orders = await this.getOrdersWithCoupon(coupon);
916
+ const ordersWithUser = this.countOrdersWithUser(orders, checkout.user.email);
917
+ const couponUseLimits = this.getCouponUseLimits(coupon, checkoutType, checkout.user);
918
+ if (couponUseLimits.limitedPerUser && ordersWithUser > 0)
919
+ throw 'Limite de uso por usuário atingido.';
920
+ if (!couponUseLimits.unlimited && couponUseLimits.total && orders.length >= couponUseLimits.total)
921
+ throw 'Limite de uso atingido.';
922
+ const hasProductCategories = await this.hasProductCategories(coupon, checkout);
923
+ if (!hasProductCategories)
924
+ throw 'Seu carrinho não possui produtos elegíveis para desconto.';
925
+ const hasMinSubTotal = await this.hasMinSubTotal(coupon, checkout);
926
+ if (!hasMinSubTotal)
927
+ throw `Valor mínimo de ${Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(coupon.minSubTotalValue)} não atingido`;
928
+ return coupon;
929
+ }
930
+ calcDiscountSubscription(coupon, checkout) {
931
+ let discount = 0;
932
+ if (coupon.discount.subscription.type == CouponTypes.ABSOLUTE)
933
+ discount = coupon.discount.subscription.value;
934
+ else
935
+ discount = checkout.subscriptionPlan.recurrencePrice * (coupon.discount.subscription.value / 100);
936
+ return of(discount);
937
+ }
938
+ async calcDiscountShopping(coupon, checkout) {
939
+ let discount = 0;
940
+ if (checkout.user.isSubscriber && coupon.discount.subscriber.value) {
941
+ discount = await this.calcDiscountByType(coupon.discount.subscriber.type, coupon.discount.subscriber.value, coupon.productsCategories, checkout);
942
+ }
943
+ else {
944
+ discount = await this.calcDiscountByType(coupon.discount.non_subscriber.type, coupon.discount.non_subscriber.value, coupon.productsCategories, checkout);
945
+ }
946
+ return discount;
947
+ }
948
+ async calcDiscountByType(type, value, categories, checkout) {
949
+ let discount = 0;
950
+ let lineItensDiscount = await this.getLineItensEligebleForDiscount(categories, checkout);
951
+ const subTotal = this.calcCheckoutSubtotal(lineItensDiscount, checkout.user, checkout.shop);
952
+ if (type == CouponTypes.ABSOLUTE) {
953
+ discount = value > subTotal ? subTotal : value;
954
+ }
955
+ else {
956
+ discount = subTotal * (value / 100);
957
+ }
958
+ return discount;
959
+ }
960
+ async hasMinSubTotal(coupon, checkout) {
961
+ if (!coupon.minSubTotalValue)
962
+ return true;
963
+ let lineItensDiscount = await this.getLineItensEligebleForDiscount(coupon.productsCategories, checkout);
964
+ const subTotal = this.calcCheckoutSubtotal(lineItensDiscount, checkout.user, checkout.shop);
965
+ if (coupon.minSubTotalValue <= subTotal)
966
+ return true;
967
+ return false;
968
+ }
969
+ async hasProductCategories(coupon, checkout) {
970
+ if (!coupon.productsCategories || !coupon.productsCategories.length) {
971
+ return true;
972
+ }
973
+ const couponCategories = await this.getCouponCategoriesId(coupon.productsCategories);
974
+ const hasCategories = checkout.lineItems?.filter((i) => {
975
+ if (!i.categories || !i.categories?.length)
976
+ return true;
977
+ return i.categories.some((c) => couponCategories.some((cat) => cat == c));
978
+ });
979
+ return hasCategories.length ? true : false;
980
+ }
981
+ coupomUserValidation(coupon, user) {
982
+ if (!user || coupon.exclusivityType.includes(Exclusivities.ALL_USERS))
983
+ return true;
984
+ let userTypes = [];
985
+ if (coupon.exclusivityType.includes(Exclusivities.COLLABORATORS) && this.emailIsFromCollaborator(user.email))
986
+ userTypes.push(Exclusivities.COLLABORATORS);
987
+ if (coupon.exclusivityType.includes(Exclusivities.SPECIFIC_USER) && coupon.userExclusiveEmail.includes(user.email))
988
+ userTypes.push(Exclusivities.SPECIFIC_USER);
989
+ if (coupon.exclusivityType.includes(Exclusivities.ACTIVE_SUBSCRIBER) &&
990
+ user.isSubscriber &&
991
+ user.subscriptionPlan != '')
992
+ userTypes.push(Exclusivities.ACTIVE_SUBSCRIBER);
993
+ if (user.isSubscriber &&
994
+ user.subscriptionPlan == '' &&
995
+ coupon.exclusivityType.includes(Exclusivities.INACTIVE_SUBSCRIBER))
996
+ userTypes.push(Exclusivities.INACTIVE_SUBSCRIBER);
997
+ if (coupon.exclusivityType.includes(Exclusivities.NON_SUBSCRIBER) && !user.isSubscriber)
998
+ userTypes.push(Exclusivities.NON_SUBSCRIBER);
999
+ return coupon.exclusivityType.some((r) => userTypes.includes(r));
1000
+ }
1001
+ async getCouponCategoriesId(productsCategories) {
1002
+ const couponCategories = [];
1003
+ for (let index = 0; index < productsCategories.length; index++) {
1004
+ const category = await this.categoryRepository.get({
1005
+ id: productsCategories[index],
1006
+ });
1007
+ if (category) {
1008
+ const children = await this.categoryRepository.getChildren(parseInt(productsCategories[index]));
1009
+ couponCategories.push(category.id, ...children.map((c) => c.id.toString()));
1010
+ }
1011
+ }
1012
+ return [...new Set(couponCategories)];
1013
+ }
1014
+ async getLineItensEligebleForDiscount(productsCategories, checkout) {
1015
+ let lineItensDiscount = [];
1016
+ const couponCategories = await this.getCouponCategoriesId(productsCategories);
1017
+ if (productsCategories && productsCategories.length) {
1018
+ lineItensDiscount = checkout.lineItems?.filter((i) => {
1019
+ if (i.categories?.length) {
1020
+ return i.categories.some((c) => couponCategories.some((cat) => cat == c));
1021
+ }
1022
+ return true;
1023
+ });
1024
+ }
1025
+ else {
1026
+ lineItensDiscount = checkout.lineItems;
1027
+ }
1028
+ return lineItensDiscount;
1029
+ }
1030
+ calcCheckoutSubtotal(lineItens, user, shop) {
1031
+ return (lineItens?.reduce((acc, curr) => user?.isSubscriber && curr.price.subscriberPrice
1032
+ ? acc + curr.price?.subscriberPrice * curr.quantity
1033
+ : acc + curr.pricePaid * curr.quantity, 0) || 0);
1034
+ }
1035
+ async getOrdersWithCoupon(coupon) {
1036
+ return await this.orderRepository
1037
+ .find({
1038
+ filters: {
1039
+ coupon: { id: coupon.id },
1040
+ payment: { status: 'paid' },
1041
+ },
1042
+ })
1043
+ .then((result) => result.data);
1044
+ }
1045
+ countOrdersWithUser(orders, email) {
1046
+ return orders.filter((o) => o.user.email == email).length;
1047
+ }
1048
+ getCouponUseLimits(coupon, checkoutType, user) {
1049
+ let couponUseLimits;
1050
+ if (checkoutType == CheckoutTypes.ECOMMERCE || checkoutType == CheckoutTypes.ALL) {
1051
+ couponUseLimits = user && user.isSubscriber ? coupon.useLimits.subscriber : coupon.useLimits.non_subscriber;
1052
+ }
1053
+ else {
1054
+ couponUseLimits = coupon.useLimits.subscription;
1055
+ }
1056
+ return couponUseLimits;
1057
+ }
1058
+ }
1059
+ CouponService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CouponService, deps: [{ token: 'CouponRepository' }, { token: DEFAULT_SHOP }, { token: 'OrderRepository' }, { token: 'CategoryRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
1060
+ CouponService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CouponService, providedIn: 'root' });
1061
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CouponService, decorators: [{
1062
+ type: Injectable,
1063
+ args: [{
1064
+ providedIn: 'root',
1065
+ }]
1066
+ }], ctorParameters: function () { return [{ type: undefined, decorators: [{
1067
+ type: Inject,
1068
+ args: ['CouponRepository']
1069
+ }] }, { type: i1$3.Shops, decorators: [{
1070
+ type: Inject,
1071
+ args: [DEFAULT_SHOP]
1072
+ }] }, { type: undefined, decorators: [{
1073
+ type: Inject,
1074
+ args: ['OrderRepository']
1075
+ }] }, { type: undefined, decorators: [{
1076
+ type: Inject,
1077
+ args: ['CategoryRepository']
1078
+ }] }]; } });
1079
+
1080
+ class CheckoutService {
1081
+ constructor(couponService, checkoutRepository, orderRepository, userRepository, defaultShop) {
1082
+ this.couponService = couponService;
1083
+ this.checkoutRepository = checkoutRepository;
1084
+ this.orderRepository = orderRepository;
1085
+ this.userRepository = userRepository;
1086
+ this.defaultShop = defaultShop;
1087
+ }
1088
+ getCheckout(checkoutData) {
1089
+ const checkoutId = cookie.get('checkoutId');
1090
+ if (!isNil(checkoutId))
1091
+ return from(this.checkoutRepository.get({ id: checkoutId }));
1092
+ return from(this.createCheckout(checkoutData));
1093
+ }
1094
+ getUserByCheckout(checkoutId) {
1095
+ return from(this.checkoutRepository.get({ id: checkoutId })).pipe(concatMap((checkout) => checkout?.user?.id ? of(checkout.user) : from(this.userRepository.get({ id: checkout.user.id }))), concatMap((user) => of(user) || throwError(() => new NotFoundError('User is not found'))));
1096
+ }
1097
+ updateCheckoutLineItems(checkout) {
1098
+ return from(this.checkoutRepository.update(Checkout.toInstance({ id: checkout.id, lineItems: checkout.lineItems })));
1099
+ }
1100
+ updateCheckoutUser(checkout) {
1101
+ return from(this.checkoutRepository.update(Checkout.toInstance({ id: checkout.id, user: checkout.user })));
1102
+ }
1103
+ clearCheckoutFromSession() {
1104
+ cookie.remove('checkoutId');
1105
+ return of();
1106
+ }
1107
+ calcDiscount(coupon) {
1108
+ return this.getCheckout().pipe(concatMap(async (checkout) => await this.couponService.calcDiscountShopping(coupon, checkout)));
1109
+ }
1110
+ checkCoupon(nickname, checkoutType) {
1111
+ return this.getCheckout().pipe(concatMap((checkout) => this.couponService.checkCoupon(nickname, CheckoutTypes.ECOMMERCE, checkout, null).pipe()));
1112
+ }
1113
+ async createCheckout(checkoutData) {
1114
+ const checkout = await this.checkoutRepository.create({
1115
+ createdAt: new Date(),
1116
+ ...Checkout.toInstance(pick(checkoutData, ['user', 'shop'])).toPlain(),
1117
+ shop: checkoutData?.shop || this.defaultShop,
1118
+ });
1119
+ cookie.set('checkoutId', checkout.id);
1120
+ return checkout;
783
1121
  }
784
1122
  }
785
- AngularElasticSeachModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularElasticSeachModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
786
- AngularElasticSeachModulemod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.0", ngImport: i0, type: AngularElasticSeachModule });
787
- AngularElasticSeachModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularElasticSeachModule, providers: [
788
- {
789
- provide: ProductsIndex,
790
- useFactory: (configuration) => {
791
- return new ProductsIndex(new AxiosAdapter(configuration));
792
- },
793
- deps: [ES_CONFIG],
794
- },
795
- ] });
796
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularElasticSeachModule, decorators: [{
797
- type: NgModule,
798
- args: [{
799
- providers: [
800
- {
801
- provide: ProductsIndex,
802
- useFactory: (configuration) => {
803
- return new ProductsIndex(new AxiosAdapter(configuration));
804
- },
805
- deps: [ES_CONFIG],
806
- },
807
- ],
808
- }]
809
- }] });
1123
+ CheckoutService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutService, deps: [{ token: CouponService }, { token: 'CheckoutRepository' }, { token: 'OrderRepository' }, { token: 'UserRepository' }, { token: DEFAULT_SHOP }], target: i0.ɵɵFactoryTarget.Injectable });
1124
+ CheckoutServiceprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutService });
1125
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutService, decorators: [{
1126
+ type: Injectable
1127
+ }], ctorParameters: function () { return [{ type: CouponService }, { type: undefined, decorators: [{
1128
+ type: Inject,
1129
+ args: ['CheckoutRepository']
1130
+ }] }, { type: undefined, decorators: [{
1131
+ type: Inject,
1132
+ args: ['OrderRepository']
1133
+ }] }, { type: undefined, decorators: [{
1134
+ type: Inject,
1135
+ args: ['UserRepository']
1136
+ }] }, { type: i1$3.Shops, decorators: [{
1137
+ type: Inject,
1138
+ args: [DEFAULT_SHOP]
1139
+ }] }]; } });
810
1140
 
811
- class AngularFirestoreModule {
812
- static initializeApp(options, nameOrConfig) {
1141
+ class CartService {
1142
+ constructor(authService, checkoutService, defaultShop, productRepository, categoryRepository, variantRepository, buy2WinRepository) {
1143
+ this.authService = authService;
1144
+ this.checkoutService = checkoutService;
1145
+ this.defaultShop = defaultShop;
1146
+ this.productRepository = productRepository;
1147
+ this.categoryRepository = categoryRepository;
1148
+ this.variantRepository = variantRepository;
1149
+ this.buy2WinRepository = buy2WinRepository;
1150
+ this.cartSubject = new Subject();
1151
+ this.updateLineItemInCart = (lineItem, quantity, checkout) => (isNil(checkout) ? this.checkoutService.getCheckout() : of(checkout)).pipe(concatMap((checkoutLoaded) => {
1152
+ const items = [];
1153
+ const index = checkoutLoaded.lineItems?.map((checkoutItem) => checkoutItem.id).indexOf(lineItem.id);
1154
+ if (index > -1) {
1155
+ checkoutLoaded.lineItems[index].quantity += quantity;
1156
+ checkoutLoaded.lineItems[index].pricePaid = lineItem.pricePaid;
1157
+ }
1158
+ else
1159
+ checkoutLoaded.lineItems = items.concat(checkoutLoaded.lineItems ? checkoutLoaded.lineItems.concat([lineItem]) : [lineItem]);
1160
+ return this.checkoutService
1161
+ .updateCheckoutLineItems(checkoutLoaded)
1162
+ .pipe(map((updatedCheckout) => this.generateCartObject(updatedCheckout.lineItems)));
1163
+ }));
1164
+ this.generateCartObject = (items) => items?.reduce((cart, item) => ({
1165
+ ...cart,
1166
+ [item.id]: LineItem.toInstance({
1167
+ ...(cart[item.id] || item),
1168
+ quantity: (cart[item.id]?.quantity || 0) + (item.quantity ? item.quantity : 1),
1169
+ }),
1170
+ }), {}) || {};
1171
+ this.buildLineItem = async ({ checkout, item, quantity, }) => {
1172
+ const product = await this.getProductData(item.id);
1173
+ item.quantity = item?.quantity || checkout?.lineItems?.find((lineItem) => lineItem.id === item.id)?.quantity || 0;
1174
+ if (this.checkMaxStock(item, quantity || 0))
1175
+ throw new Error('Desculpe! Temos apenas ' + item.stock?.quantity + ' em estoque.');
1176
+ const image = item.image || item.images?.shift();
1177
+ const { id, name, EAN, slug, stock, price, weight, sku, type } = item;
1178
+ const isGift = item.isGift || null;
1179
+ const pricePaid = this.getProductPrice({
1180
+ product: item,
1181
+ shop: checkout.shop || this.defaultShop,
1182
+ isSubscriber: checkout.user?.isSubscriber,
1183
+ });
1184
+ RoundProductPricesHelper.roundProductPrices(item);
1185
+ return {
1186
+ checkout,
1187
+ lineItem: LineItem.toInstance({
1188
+ id,
1189
+ name: name ?? product.name,
1190
+ EAN: EAN ?? product.EAN,
1191
+ brand: product.brand,
1192
+ slug: slug ?? product.slug,
1193
+ sku: sku ?? product.sku,
1194
+ stock,
1195
+ price: this.roundPrice(price),
1196
+ image,
1197
+ weight: weight ?? product.weight,
1198
+ quantity: (item.quantity || 0) + (quantity || 0),
1199
+ pricePaid,
1200
+ categories: product.categories ?? [],
1201
+ isGift: isGift ?? null,
1202
+ costPrice: product.costPrice ?? 0,
1203
+ type,
1204
+ }),
1205
+ };
1206
+ };
1207
+ this.getProductPrice = ({ product, isSubscriber, }) => {
1208
+ const info = product.price;
1209
+ if (product.isGift)
1210
+ return 0;
1211
+ return isSubscriber && info.subscriberPrice > 0
1212
+ ? Number(info.subscriberPrice.toFixed(2))
1213
+ : Number(info.price.toFixed(2));
1214
+ };
1215
+ this.checkMaxStock = (item, quantity) => {
1216
+ const maxStock = item.stock?.quantity || 0;
1217
+ const currentItemAmount = item.quantity || 0;
1218
+ return currentItemAmount + quantity > maxStock;
1219
+ };
1220
+ }
1221
+ addItem(item, quantity = 1) {
1222
+ return from(this.checkoutService.getCheckout()).pipe(concatMap(async (checkout) => await this.buildLineItem({ checkout, item, quantity: quantity || 1 })), mergeMap(({ checkout, lineItem }) => this.updateLineItemInCart(lineItem, quantity || 1, checkout)), tap((cart) => this.cartSubject.next(cart)));
1223
+ }
1224
+ decreaseItem(item) {
1225
+ return this.checkoutService.getCheckout().pipe(map((checkout) => {
1226
+ const checkoutItem = checkout.lineItems?.find((lineItem) => lineItem.id === item.id);
1227
+ if (!isNil(checkoutItem))
1228
+ checkoutItem.quantity -= checkoutItem.quantity > 1 ? 1 : 0;
1229
+ return checkout;
1230
+ }), concatMap((checkout) => this.checkoutService.updateCheckoutLineItems(checkout)), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
1231
+ }
1232
+ getCart(checkout) {
1233
+ this.buildCartFromCheckout(checkout).subscribe((cart) => this.cartSubject.next(cart));
1234
+ return this.cartSubject;
1235
+ }
1236
+ /**
1237
+ * @deprecated The method should not be used
1238
+ */
1239
+ getVariantPriceDiscount(item) {
1240
+ return this.authService.getUser().pipe(concatMap((user) => iif(() => user.isSubscriber && !!item.price.subscriberPrice, of(item.price.subscriberPrice), of(item.price.price))), catchError(() => of(item.price.price)));
1241
+ }
1242
+ removeItem(item) {
1243
+ return this.checkoutService.getCheckout().pipe(map((checkout) => {
1244
+ const index = checkout.lineItems.findIndex((lineItem) => lineItem.id === item.id);
1245
+ if (index >= 0)
1246
+ checkout.lineItems.splice(index, 1);
1247
+ return checkout;
1248
+ }), concatMap((checkout) => this.checkoutService.updateCheckoutLineItems(checkout)), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
1249
+ }
1250
+ updateUserCart(user) {
1251
+ return this.checkoutService.getCheckout().pipe(concatMap((checkout) => this.checkoutService.updateCheckoutUser(Checkout.toInstance({ ...checkout.toPlain(), user }))), concatMap(async (checkout) => this.checkoutService
1252
+ .updateCheckoutLineItems(Checkout.toInstance({
1253
+ ...checkout.toPlain(),
1254
+ lineItems: checkout.lineItems?.length
1255
+ ? await Promise.all(checkout.lineItems?.map(async (item) => (await this.buildLineItem({ checkout, item })).lineItem))
1256
+ : [],
1257
+ }))
1258
+ .toPromise()), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
1259
+ }
1260
+ clearCart() {
1261
+ return this.checkoutService.getCheckout().pipe(map((checkout) => {
1262
+ this.checkoutService.clearCheckoutFromSession();
1263
+ return checkout;
1264
+ }), concatMap((oldCheckout) => this.buildCartFromCheckout(oldCheckout)), tap((cart) => this.cartSubject.next(cart)));
1265
+ }
1266
+ buildCartFromCheckout(checkoutData) {
1267
+ return this.checkoutService.getCheckout(checkoutData).pipe(map((checkout) => checkout.lineItems), concatMap((lineItems) => of(this.generateCartObject(lineItems))));
1268
+ }
1269
+ roundPrice(productPrice) {
1270
+ const { price, fullPrice, subscriberPrice } = productPrice;
813
1271
  return {
814
- ngModule: AngularFirestoreModule,
815
- providers: [
816
- { provide: FIREBASE_OPTIONS, useValue: options.firebase },
817
- { provide: FIREBASE_APP_NAME, useValue: nameOrConfig },
818
- { provide: ES_CONFIG, useValue: options.elasticSearch },
819
- ],
1272
+ ...productPrice,
1273
+ price: Number(price.toFixed(2)),
1274
+ fullPrice: Number(fullPrice.toFixed(2)),
1275
+ ...(subscriberPrice && { subscriberPrice: Number(subscriberPrice.toFixed(2)) }),
820
1276
  };
821
1277
  }
822
- }
823
- AngularFirestoreModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirestoreModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
824
- AngularFirestoreModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.0", ngImport: i0, type: AngularFirestoreModule, imports: [AngularElasticSeachModule, i1$2.FirebaseAppModule, i1$1.FirestoreModule] });
825
- AngularFirestoreModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirestoreModule, providers: [
826
- {
827
- provide: 'BeautyProfileRepository',
828
- useFactory: (firestore, userRepository) => {
829
- return new UserBeautyProfileFirestoreRepository(firestore, userRepository);
830
- },
831
- deps: [Firestore, 'UserRepository'],
832
- },
833
- {
834
- provide: 'Buy2WinRepository',
835
- useFactory: (firestore) => {
836
- return new Buy2WinFirestoreRepository(firestore);
837
- },
838
- deps: [Firestore],
839
- },
840
- {
841
- provide: CategoryFirestoreRepository,
842
- useFactory: (firestore) => {
843
- return new CategoryFirestoreRepository(firestore);
844
- },
845
- deps: [Firestore],
846
- },
847
- {
848
- provide: 'CheckoutRepository',
849
- useFactory: (firestore) => {
850
- return new CheckoutFirestoreRepository(firestore);
851
- },
852
- deps: [Firestore],
853
- },
854
- {
855
- provide: 'CheckoutSubscriptionRepository',
856
- useFactory: (firestore) => {
857
- return new CheckoutSubscriptionFirestoreRepository(firestore);
858
- },
859
- deps: [Firestore],
860
- },
861
- {
862
- provide: 'CouponRepository',
863
- useFactory: (firestore) => {
864
- return new CouponFirestoreRepository(firestore);
865
- },
866
- deps: [Firestore],
867
- },
868
- {
869
- provide: 'EditionRepository',
870
- useFactory: (firestore, subscriptionRepository) => {
871
- return new SubscriptionEditionFirestoreRepository(firestore, subscriptionRepository);
872
- },
873
- deps: [Firestore, 'SubscriptionRepository'],
874
- },
875
- {
876
- provide: 'HomeRepository',
877
- useFactory: (firestore) => {
878
- return new HomeFirestoreRepository(firestore);
879
- },
880
- deps: [Firestore],
881
- },
882
- {
883
- provide: 'LeadRepository',
884
- useFactory: (firestore) => {
885
- return new LeadFirestoreRepository(firestore);
886
- },
887
- deps: [Firestore],
888
- },
889
- {
890
- provide: 'LegacyOrderRepository',
891
- useFactory: (firestore) => {
892
- return new LegacyOrderFirestoreRepository(firestore);
893
- },
894
- deps: [Firestore],
895
- },
896
- {
897
- provide: 'ShopMenuRepository',
898
- useFactory: (firestore) => {
899
- return new ShopMenuFirestoreRepository(firestore);
900
- },
901
- deps: [Firestore],
902
- },
903
- {
904
- provide: 'OrderRepository',
905
- useFactory: (firestore) => {
906
- return new OrderFirestoreRepository(firestore);
907
- },
908
- deps: [Firestore],
909
- },
910
- {
911
- provide: 'PaymentRepository',
912
- useFactory: (firestore) => {
913
- return new PaymentFirestoreRepository(firestore);
914
- },
915
- deps: [Firestore],
916
- },
917
- {
918
- provide: ProductFirestoreRepository,
919
- useFactory: (firestore) => {
920
- return new ProductFirestoreRepository(firestore);
921
- },
922
- deps: [Firestore],
923
- },
924
- {
925
- provide: 'SubscriptionPaymentRepository',
926
- useFactory: (firestore, subscriptionRepository) => {
927
- return new SubscriptionPaymentFirestoreRepository(firestore, subscriptionRepository);
928
- },
929
- deps: [Firestore, 'SubscriptionRepository'],
930
- },
931
- {
932
- provide: 'SubscriptionPlanRepository',
933
- useFactory: (firestore) => {
934
- return new SubscriptionPlanFirestoreRepository(firestore);
935
- },
936
- deps: [Firestore],
937
- },
938
- {
939
- provide: 'SubscriptionProductRepository',
940
- useFactory: (firestore) => {
941
- return new SubscriptionProductFirestoreRepository(firestore);
942
- },
943
- deps: [Firestore],
944
- },
945
- {
946
- provide: 'SubscriptionRepository',
947
- useFactory: (firestore) => {
948
- return new SubscriptionFirestoreRepository(firestore);
949
- },
950
- deps: [Firestore],
951
- },
952
- {
953
- provide: 'UserRepository',
954
- useFactory: (firestore) => {
955
- return new UserFirestoreRepository(firestore);
956
- },
957
- deps: [Firestore],
958
- },
959
- {
960
- provide: 'UserAddressRepository',
961
- useFactory: (firestore, userRepository) => {
962
- return new UserAddressFirestoreRepository(firestore, userRepository);
963
- },
964
- deps: [Firestore, 'UserRepository'],
965
- },
966
- {
967
- provide: 'UserPaymentMethodRepository',
968
- useFactory: (firestore, userRepository) => {
969
- return new UserPaymentMethodFirestoreRepository(firestore, userRepository);
1278
+ async getProductData(productId) {
1279
+ let product;
1280
+ let variant;
1281
+ try {
1282
+ product = await this.productRepository.get({ id: productId });
1283
+ }
1284
+ catch (error) {
1285
+ if (!(error instanceof NotFoundError))
1286
+ throw error;
1287
+ const { data: variants } = await this.variantRepository.find({ filters: { id: productId } });
1288
+ variant = variants.shift();
1289
+ if (!variant)
1290
+ throw error;
1291
+ product = await this.productRepository.get({ id: variant.productId });
1292
+ }
1293
+ return {
1294
+ ...product.toPlain(),
1295
+ ...(variant && { ...variant.toPlain() }),
1296
+ };
1297
+ }
1298
+ getGifts() {
1299
+ return this.checkoutService.getCheckout().pipe(mergeMap(async (checkout) => {
1300
+ const notGiftItems = checkout.lineItems ? checkout.lineItems.filter((item) => !item.isGift) : [];
1301
+ if (!notGiftItems.length)
1302
+ return { ...checkout, lineItems: [] };
1303
+ const cartTotal = notGiftItems.reduce((a, b) => a + b.pricePaid * b.quantity, 0);
1304
+ const campaigns = await this.buy2WinRepository
1305
+ .find({
1306
+ filters: {
1307
+ active: { operator: Where.EQUALS, value: true },
1308
+ shop: { operator: Where.EQUALS, value: this.defaultShop },
1309
+ },
1310
+ })
1311
+ .then((data) => data.data);
1312
+ if (!campaigns.length)
1313
+ return { ...checkout, lineItems: notGiftItems };
1314
+ const elegibleCampaigns = [];
1315
+ for (const campaign of campaigns) {
1316
+ const today = new Date().getDate();
1317
+ if (!(campaign.startDate.getDate() <= today) && !(campaign.endDate.getDate() >= today))
1318
+ continue;
1319
+ if (campaign.activeCategory) {
1320
+ const categoriesCampaing = campaign.categories.map((c) => c.id.toString());
1321
+ const categoriesCampaingFullTree = [];
1322
+ for (const id of categoriesCampaing) {
1323
+ const children = await this.categoryRepository.getChildren(parseInt(id));
1324
+ categoriesCampaingFullTree.push(id, ...children.map((c) => c.id.toString()));
1325
+ }
1326
+ const categoriesCampaingTree = [...new Set(categoriesCampaingFullTree)];
1327
+ const filterProductsCategories = checkout.lineItems.filter((l) => {
1328
+ if (!l.categories || !l.categories?.length)
1329
+ return true;
1330
+ return l.categories.some((c) => categoriesCampaingTree.some((cat) => cat == c));
1331
+ });
1332
+ if (filterProductsCategories.length) {
1333
+ const cartTotalCategories = filterProductsCategories.reduce((a, b) => a + b.pricePaid * b.quantity, 0);
1334
+ if (cartTotalCategories >= campaign.cartValueMin)
1335
+ elegibleCampaigns.push(campaign);
1336
+ }
1337
+ }
1338
+ else {
1339
+ if (campaign.cartValue && campaign.cartValue > 0) {
1340
+ if (campaign.cartValue <= cartTotal)
1341
+ elegibleCampaigns.push(campaign);
1342
+ }
1343
+ }
1344
+ }
1345
+ if (!elegibleCampaigns.length)
1346
+ return { ...checkout, lineItems: notGiftItems };
1347
+ const campaingnProducts = [];
1348
+ for (const campaign of elegibleCampaigns) {
1349
+ let elegibleProducts = [];
1350
+ for (const product of campaign.products) {
1351
+ const { data: productData } = await this.productRepository.find({ filters: { sku: product } });
1352
+ if (!productData.length)
1353
+ continue;
1354
+ const gift = productData.shift();
1355
+ if (gift.stock.quantity < 1)
1356
+ continue;
1357
+ elegibleProducts.push(gift);
1358
+ }
1359
+ campaingnProducts.push(elegibleProducts);
1360
+ }
1361
+ if (!campaingnProducts.length)
1362
+ return { ...checkout, lineItems: notGiftItems };
1363
+ const gifts = this.giftToLineItems([].concat(...campaingnProducts));
1364
+ return { ...checkout, lineItems: notGiftItems.concat(gifts) };
1365
+ }), concatMap((checkout) => this.checkoutService.updateCheckoutLineItems(checkout)), map((checkout) => this.generateCartObject(checkout.lineItems)), tap((cart) => this.cartSubject.next(cart)));
1366
+ }
1367
+ giftToLineItems(items) {
1368
+ return items.map((item) => {
1369
+ const { brand, categories, id, name, price, sku, slug, stock, weight, EAN } = item;
1370
+ const image = item?.miniatures?.length ? item.miniatures[0] : item.images[0];
1371
+ return LineItem.toInstance({
1372
+ brand,
1373
+ categories,
1374
+ id: id.toString(),
1375
+ name,
1376
+ price,
1377
+ sku,
1378
+ slug,
1379
+ stock,
1380
+ weight,
1381
+ EAN,
1382
+ image,
1383
+ pricePaid: 0,
1384
+ quantity: 1,
1385
+ isGift: true,
1386
+ });
1387
+ });
1388
+ }
1389
+ }
1390
+ CartService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CartService, deps: [{ token: AuthService }, { token: CheckoutService }, { token: DEFAULT_SHOP }, { token: 'ProductRepository' }, { token: 'CategoryRepository' }, { token: 'VariantRepository' }, { token: 'Buy2WinRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
1391
+ CartService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CartService });
1392
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CartService, decorators: [{
1393
+ type: Injectable
1394
+ }], ctorParameters: function () { return [{ type: AuthService }, { type: CheckoutService }, { type: i1$3.Shops, decorators: [{
1395
+ type: Inject,
1396
+ args: [DEFAULT_SHOP]
1397
+ }] }, { type: undefined, decorators: [{
1398
+ type: Inject,
1399
+ args: ['ProductRepository']
1400
+ }] }, { type: undefined, decorators: [{
1401
+ type: Inject,
1402
+ args: ['CategoryRepository']
1403
+ }] }, { type: undefined, decorators: [{
1404
+ type: Inject,
1405
+ args: ['VariantRepository']
1406
+ }] }, { type: i1$3.Buy2WinFirestoreRepository, decorators: [{
1407
+ type: Inject,
1408
+ args: ['Buy2WinRepository']
1409
+ }] }]; } });
1410
+
1411
+ class CatalogService {
1412
+ constructor(productRepository, categoryStructureAdapter, shop) {
1413
+ this.productRepository = productRepository;
1414
+ this.categoryStructureAdapter = categoryStructureAdapter;
1415
+ this.shop = shop;
1416
+ this.buildFilterQuery = ({ clubDiscount, brands, prices, gender, tags, rate, customOptions, }) => {
1417
+ const filters = {};
1418
+ if (clubDiscount?.length)
1419
+ set(filters, 'price.subscriberDiscountPercentage', { operator: Where.IN, value: clubDiscount });
1420
+ if (brands?.length)
1421
+ filters.brand = { operator: Where.IN, value: brands };
1422
+ if (gender?.length)
1423
+ filters.tags = {
1424
+ operator: Where.IN,
1425
+ value: gender
1426
+ .map((genderOptions) => genderOptions === 'female' ? 'feminino' : genderOptions === 'male' ? 'masculino' : null)
1427
+ .filter(Boolean),
1428
+ };
1429
+ if (prices?.min || prices?.max)
1430
+ set(filters, prices.subscriberPrice ? 'price.subscriberPrice' : 'price.price', [
1431
+ ...(prices.min ? [{ operator: Where.GTE, value: Math.round(prices.min) }] : []),
1432
+ ...(prices.max ? [{ operator: Where.LTE, value: Math.ceil(prices.max) }] : []),
1433
+ ]);
1434
+ if (rate)
1435
+ filters.rate = { operator: Where.GTE, value: rate };
1436
+ if (tags?.length)
1437
+ filters.tags = { operator: Where.LIKE, value: tags };
1438
+ if (customOptions?.length)
1439
+ filters.filters = { operator: Where.LIKE, value: customOptions };
1440
+ return filters;
1441
+ };
1442
+ this.buildSortQuery = (sort) => {
1443
+ if (!sort || sort === 'most-relevant')
1444
+ return {};
1445
+ if (sort === 'best-sellers')
1446
+ return { shoppingCount: 'desc' };
1447
+ if (sort === 'biggest-price')
1448
+ return { price: 'desc' };
1449
+ if (sort === 'lowest-price')
1450
+ return { price: 'asc' };
1451
+ if (sort === 'best-rating')
1452
+ return { rate: 'desc' };
1453
+ if (sort === 'news')
1454
+ return { createdAt: 'desc' };
1455
+ };
1456
+ this.buildLimitQuery = (options) => {
1457
+ const limit = options?.perPage || 20;
1458
+ return {
1459
+ limit,
1460
+ offset: ((options?.page || 1) - 1) * limit,
1461
+ };
1462
+ };
1463
+ }
1464
+ async fetchProducts(category, options) {
1465
+ const limits = this.buildLimitQuery(options);
1466
+ return await this.productRepository
1467
+ .findCatalog({
1468
+ filters: {
1469
+ ...(await this.categoryStructureAdapter.buildProductFilterByCategory(category)),
1470
+ ...this.buildFilterQuery(options?.filters || {}),
970
1471
  },
971
- deps: [Firestore, 'UserRepository'],
972
- },
973
- {
974
- provide: ProductVariantFirestoreRepository,
975
- useFactory: (firestore, productRepository) => {
976
- return new ProductVariantFirestoreRepository(firestore, productRepository);
1472
+ ...(options?.sort ? { orderBy: this.buildSortQuery(options?.sort) } : {}),
1473
+ limits,
1474
+ options: { minimal: ['price'], maximum: ['price'] },
1475
+ }, options?.mainGender || this.shop === Shops.MENSMARKET ? 'male' : 'female')
1476
+ .then(({ data, count: total, maximum, minimal }) => ({
1477
+ products: { data: data.map((product) => RoundProductPricesHelper.roundProductPrices(product)), total },
1478
+ pages: Math.ceil(total / limits.limit),
1479
+ prices: {
1480
+ price: { min: +minimal?.price?.price?.toFixed(2), max: +maximum?.price?.price?.toFixed(2) },
1481
+ subscriberPrice: {
1482
+ min: +minimal?.price?.subscriberPrice?.toFixed(2),
1483
+ max: +maximum?.price?.subscriberPrice?.toFixed(2),
1484
+ },
977
1485
  },
978
- deps: [Firestore, ProductFirestoreRepository],
979
- },
980
- ], imports: [AngularElasticSeachModule,
981
- provideFirebaseApp((injector) => {
982
- const appName = injector.get(FIREBASE_APP_NAME);
983
- return appName
984
- ? initializeApp(injector.get(FIREBASE_OPTIONS), appName)
985
- : initializeApp(injector.get(FIREBASE_OPTIONS));
986
- }),
987
- provideFirestore(() => getFirestore())] });
988
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularFirestoreModule, decorators: [{
989
- type: NgModule,
990
- args: [{
991
- imports: [
992
- AngularElasticSeachModule,
993
- provideFirebaseApp((injector) => {
994
- const appName = injector.get(FIREBASE_APP_NAME);
995
- return appName
996
- ? initializeApp(injector.get(FIREBASE_OPTIONS), appName)
997
- : initializeApp(injector.get(FIREBASE_OPTIONS));
998
- }),
999
- provideFirestore(() => getFirestore()),
1000
- ],
1001
- providers: [
1002
- {
1003
- provide: 'BeautyProfileRepository',
1004
- useFactory: (firestore, userRepository) => {
1005
- return new UserBeautyProfileFirestoreRepository(firestore, userRepository);
1006
- },
1007
- deps: [Firestore, 'UserRepository'],
1008
- },
1009
- {
1010
- provide: 'Buy2WinRepository',
1011
- useFactory: (firestore) => {
1012
- return new Buy2WinFirestoreRepository(firestore);
1013
- },
1014
- deps: [Firestore],
1015
- },
1016
- {
1017
- provide: CategoryFirestoreRepository,
1018
- useFactory: (firestore) => {
1019
- return new CategoryFirestoreRepository(firestore);
1020
- },
1021
- deps: [Firestore],
1022
- },
1023
- {
1024
- provide: 'CheckoutRepository',
1025
- useFactory: (firestore) => {
1026
- return new CheckoutFirestoreRepository(firestore);
1027
- },
1028
- deps: [Firestore],
1029
- },
1030
- {
1031
- provide: 'CheckoutSubscriptionRepository',
1032
- useFactory: (firestore) => {
1033
- return new CheckoutSubscriptionFirestoreRepository(firestore);
1034
- },
1035
- deps: [Firestore],
1036
- },
1037
- {
1038
- provide: 'CouponRepository',
1039
- useFactory: (firestore) => {
1040
- return new CouponFirestoreRepository(firestore);
1041
- },
1042
- deps: [Firestore],
1043
- },
1044
- {
1045
- provide: 'EditionRepository',
1046
- useFactory: (firestore, subscriptionRepository) => {
1047
- return new SubscriptionEditionFirestoreRepository(firestore, subscriptionRepository);
1048
- },
1049
- deps: [Firestore, 'SubscriptionRepository'],
1050
- },
1051
- {
1052
- provide: 'HomeRepository',
1053
- useFactory: (firestore) => {
1054
- return new HomeFirestoreRepository(firestore);
1055
- },
1056
- deps: [Firestore],
1057
- },
1058
- {
1059
- provide: 'LeadRepository',
1060
- useFactory: (firestore) => {
1061
- return new LeadFirestoreRepository(firestore);
1062
- },
1063
- deps: [Firestore],
1064
- },
1065
- {
1066
- provide: 'LegacyOrderRepository',
1067
- useFactory: (firestore) => {
1068
- return new LegacyOrderFirestoreRepository(firestore);
1069
- },
1070
- deps: [Firestore],
1071
- },
1072
- {
1073
- provide: 'ShopMenuRepository',
1074
- useFactory: (firestore) => {
1075
- return new ShopMenuFirestoreRepository(firestore);
1076
- },
1077
- deps: [Firestore],
1078
- },
1079
- {
1080
- provide: 'OrderRepository',
1081
- useFactory: (firestore) => {
1082
- return new OrderFirestoreRepository(firestore);
1083
- },
1084
- deps: [Firestore],
1085
- },
1086
- {
1087
- provide: 'PaymentRepository',
1088
- useFactory: (firestore) => {
1089
- return new PaymentFirestoreRepository(firestore);
1090
- },
1091
- deps: [Firestore],
1092
- },
1093
- {
1094
- provide: ProductFirestoreRepository,
1095
- useFactory: (firestore) => {
1096
- return new ProductFirestoreRepository(firestore);
1097
- },
1098
- deps: [Firestore],
1099
- },
1100
- {
1101
- provide: 'SubscriptionPaymentRepository',
1102
- useFactory: (firestore, subscriptionRepository) => {
1103
- return new SubscriptionPaymentFirestoreRepository(firestore, subscriptionRepository);
1104
- },
1105
- deps: [Firestore, 'SubscriptionRepository'],
1106
- },
1107
- {
1108
- provide: 'SubscriptionPlanRepository',
1109
- useFactory: (firestore) => {
1110
- return new SubscriptionPlanFirestoreRepository(firestore);
1111
- },
1112
- deps: [Firestore],
1113
- },
1114
- {
1115
- provide: 'SubscriptionProductRepository',
1116
- useFactory: (firestore) => {
1117
- return new SubscriptionProductFirestoreRepository(firestore);
1118
- },
1119
- deps: [Firestore],
1120
- },
1121
- {
1122
- provide: 'SubscriptionRepository',
1123
- useFactory: (firestore) => {
1124
- return new SubscriptionFirestoreRepository(firestore);
1125
- },
1126
- deps: [Firestore],
1127
- },
1128
- {
1129
- provide: 'UserRepository',
1130
- useFactory: (firestore) => {
1131
- return new UserFirestoreRepository(firestore);
1132
- },
1133
- deps: [Firestore],
1134
- },
1135
- {
1136
- provide: 'UserAddressRepository',
1137
- useFactory: (firestore, userRepository) => {
1138
- return new UserAddressFirestoreRepository(firestore, userRepository);
1139
- },
1140
- deps: [Firestore, 'UserRepository'],
1141
- },
1142
- {
1143
- provide: 'UserPaymentMethodRepository',
1144
- useFactory: (firestore, userRepository) => {
1145
- return new UserPaymentMethodFirestoreRepository(firestore, userRepository);
1146
- },
1147
- deps: [Firestore, 'UserRepository'],
1148
- },
1149
- {
1150
- provide: ProductVariantFirestoreRepository,
1151
- useFactory: (firestore, productRepository) => {
1152
- return new ProductVariantFirestoreRepository(firestore, productRepository);
1153
- },
1154
- deps: [Firestore, ProductFirestoreRepository],
1155
- },
1156
- ],
1157
- }]
1158
- }] });
1486
+ }));
1487
+ }
1488
+ }
1489
+ CatalogService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CatalogService, deps: [{ token: 'ProductRepository' }, { token: CATEGORY_STRUCTURE }, { token: DEFAULT_SHOP }], target: i0.ɵɵFactoryTarget.Injectable });
1490
+ CatalogService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CatalogService });
1491
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CatalogService, decorators: [{
1492
+ type: Injectable
1493
+ }], ctorParameters: function () { return [{ type: undefined, decorators: [{
1494
+ type: Inject,
1495
+ args: ['ProductRepository']
1496
+ }] }, { type: undefined, decorators: [{
1497
+ type: Inject,
1498
+ args: [CATEGORY_STRUCTURE]
1499
+ }] }, { type: i1$3.Shops, decorators: [{
1500
+ type: Inject,
1501
+ args: [DEFAULT_SHOP]
1502
+ }] }]; } });
1159
1503
 
1160
- class AngularHasuraGraphQLModule {
1161
- static initializeApp(options) {
1162
- return {
1163
- ngModule: AngularHasuraGraphQLModule,
1164
- providers: [{ provide: HASURA_OPTIONS, useValue: options }],
1504
+ class CategoryService {
1505
+ constructor(productRepository, categoryRepository, categoryFilterRepository, categoryStructureAdapter, shop) {
1506
+ this.productRepository = productRepository;
1507
+ this.categoryRepository = categoryRepository;
1508
+ this.categoryFilterRepository = categoryFilterRepository;
1509
+ this.categoryStructureAdapter = categoryStructureAdapter;
1510
+ this.shop = shop;
1511
+ }
1512
+ async fetchBrands(category, mainGender) {
1513
+ const brands = await this.productRepository
1514
+ .findCatalog({
1515
+ filters: await this.categoryStructureAdapter.buildProductFilterByCategory(category),
1516
+ fields: ['brand'],
1517
+ }, mainGender || this.shop === Shops.MENSMARKET ? 'male' : 'female')
1518
+ .then(({ data }) => Object.keys(data.map((product) => product.brand).reduce((brands, brand) => ({ ...brands, [brand]: true }), {})));
1519
+ return this.categoryRepository
1520
+ .find({ filters: { brandCategory: true }, orderBy: { name: 'asc' } })
1521
+ .then(({ data }) => data.filter((category) => brands.includes(category.conditions.brand)));
1522
+ }
1523
+ async fetchFilterOptions(category, filters) {
1524
+ return await this.categoryFilterRepository
1525
+ .find({ filters: { categoryId: +category.id } })
1526
+ .then(({ data }) => data.map((categoryFilter) => categoryFilter.filter));
1527
+ }
1528
+ }
1529
+ CategoryService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CategoryService, deps: [{ token: 'ProductRepository' }, { token: 'CategoryRepository' }, { token: 'CategoryFilterRepository' }, { token: CATEGORY_STRUCTURE }, { token: DEFAULT_SHOP }], target: i0.ɵɵFactoryTarget.Injectable });
1530
+ CategoryService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CategoryService });
1531
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CategoryService, decorators: [{
1532
+ type: Injectable
1533
+ }], ctorParameters: function () { return [{ type: undefined, decorators: [{
1534
+ type: Inject,
1535
+ args: ['ProductRepository']
1536
+ }] }, { type: undefined, decorators: [{
1537
+ type: Inject,
1538
+ args: ['CategoryRepository']
1539
+ }] }, { type: undefined, decorators: [{
1540
+ type: Inject,
1541
+ args: ['CategoryFilterRepository']
1542
+ }] }, { type: undefined, decorators: [{
1543
+ type: Inject,
1544
+ args: [CATEGORY_STRUCTURE]
1545
+ }] }, { type: i1$3.Shops, decorators: [{
1546
+ type: Inject,
1547
+ args: [DEFAULT_SHOP]
1548
+ }] }]; } });
1549
+
1550
+ var ProductSorts;
1551
+ (function (ProductSorts) {
1552
+ ProductSorts["MOST_RELEVANT"] = "most-relevant";
1553
+ ProductSorts["BEST_SELLER"] = "best-sellers";
1554
+ ProductSorts["BIGGEST_PRICE"] = "biggest-price";
1555
+ ProductSorts["LOWEST_PRICE"] = "lowest-price";
1556
+ ProductSorts["BIGGEST_DISCOUNT"] = "biggest-discount";
1557
+ ProductSorts["BEST_RATING"] = "best-rating";
1558
+ ProductSorts["NEWS"] = "news";
1559
+ })(ProductSorts || (ProductSorts = {}));
1560
+
1561
+ class CategoryWithTree extends Category {
1562
+ }
1563
+ __decorate([
1564
+ Type(() => CategoryWithTree),
1565
+ __metadata("design:type", Array)
1566
+ ], CategoryWithTree.prototype, "children", void 0);
1567
+
1568
+ class CheckoutSubscriptionService {
1569
+ constructor(checkoutSubscriptionRepository, subscriptionRepository, couponService) {
1570
+ this.checkoutSubscriptionRepository = checkoutSubscriptionRepository;
1571
+ this.subscriptionRepository = subscriptionRepository;
1572
+ this.couponService = couponService;
1573
+ }
1574
+ getCheckoutSubscription(checkoutData) {
1575
+ const checkoutId = cookie.get('checkoutSubscriptionId');
1576
+ if (!isNil(checkoutId))
1577
+ return from(this.checkoutSubscriptionRepository.get({ id: checkoutId }));
1578
+ return from(this.createCheckoutSubscription(checkoutData));
1579
+ }
1580
+ async createCheckoutSubscription(checkoutData) {
1581
+ const checkout = await this.checkoutSubscriptionRepository.create({
1582
+ createdAt: new Date(),
1583
+ ...CheckoutSubscription.toInstance(pick(checkoutData, ['user', 'shop'])).toPlain(),
1584
+ });
1585
+ cookie.set('checkoutSubscriptionId', checkout.id);
1586
+ return checkout;
1587
+ }
1588
+ clearCheckoutSubscriptionFromSession() {
1589
+ cookie.remove('checkoutSubscriptionId');
1590
+ return of();
1591
+ }
1592
+ checkCoupon(nickname, userEmail) {
1593
+ return this.getCheckoutSubscription().pipe(concatMap((checkout) => this.couponService
1594
+ .checkCoupon(nickname, CheckoutTypes.SUBSCRIPTION, checkout, checkout.subscriptionPlan.name)
1595
+ .pipe()));
1596
+ }
1597
+ calcDiscountSubscription(coupon) {
1598
+ return this.getCheckoutSubscription().pipe(concatMap((checkout) => this.couponService.calcDiscountSubscription(coupon, checkout).pipe()));
1599
+ }
1600
+ }
1601
+ CheckoutSubscriptionService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutSubscriptionService, deps: [{ token: 'CheckoutSubscriptionRepository' }, { token: 'SubscriptionRepository' }, { token: CouponService }], target: i0.ɵɵFactoryTarget.Injectable });
1602
+ CheckoutSubscriptionService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutSubscriptionService });
1603
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: CheckoutSubscriptionService, decorators: [{
1604
+ type: Injectable
1605
+ }], ctorParameters: function () { return [{ type: undefined, decorators: [{
1606
+ type: Inject,
1607
+ args: ['CheckoutSubscriptionRepository']
1608
+ }] }, { type: undefined, decorators: [{
1609
+ type: Inject,
1610
+ args: ['SubscriptionRepository']
1611
+ }] }, { type: CouponService }]; } });
1612
+
1613
+ class HomeShopService {
1614
+ constructor(categoryRepository, homeRepository, productRepository, defaultShop) {
1615
+ this.categoryRepository = categoryRepository;
1616
+ this.homeRepository = homeRepository;
1617
+ this.productRepository = productRepository;
1618
+ this.defaultShop = defaultShop;
1619
+ this.buildCategoryGroupWithRequiredData = (group) => ({
1620
+ category: Category.toInstance(pick(group?.category?.toPlain() || {}, ['id', 'name', 'slug', 'conditions'])),
1621
+ products: group?.products?.map((product) => Product.toInstance(pick(product?.toPlain() || {}, [
1622
+ 'id',
1623
+ 'price',
1624
+ 'reviews',
1625
+ 'hasVariants',
1626
+ 'slug',
1627
+ 'sku',
1628
+ 'stock',
1629
+ 'costPrice',
1630
+ 'images',
1631
+ 'miniatures',
1632
+ 'name',
1633
+ 'weight',
1634
+ 'rate',
1635
+ 'type',
1636
+ ]))) || [],
1637
+ });
1638
+ }
1639
+ get homeId() {
1640
+ if (this.defaultShop === Shops.GLAMSHOP)
1641
+ return 'glamshop';
1642
+ if (this.defaultShop === Shops.MENSMARKET)
1643
+ return 'mens_market';
1644
+ return null;
1645
+ }
1646
+ getHomeData() {
1647
+ return this.getHomeConfiguration().pipe(map((home) => (home?.data?.expiresAt > new Date() ? home : null)), concatMap((home) => home
1648
+ ? of(home)
1649
+ : forkJoin([this.getDiscoverProducts(), this.getFeaturedProducts(), this.getVerticalProducts()]).pipe(map(([discoverProducts, featuredProducts, verticalProducts]) => ({
1650
+ discoverProducts,
1651
+ featuredProducts,
1652
+ verticalProducts,
1653
+ })), concatMap((data) => this.saveHomeData(data)))));
1654
+ }
1655
+ getBanners(type) {
1656
+ return this.getHomeConfiguration().pipe(map((home) => {
1657
+ if (type === 'brand')
1658
+ return home.brandsCarousel;
1659
+ if (type === 'buyToWin')
1660
+ return [home.buyToWinBanner];
1661
+ if (type === 'block')
1662
+ return home.blockBanners;
1663
+ if (type === 'blog')
1664
+ return [home.blogBanner];
1665
+ return [];
1666
+ }));
1667
+ }
1668
+ getMinValueForFreeShipping() {
1669
+ return this.getHomeConfiguration().pipe(map((home) => home.minValueForFreeShipping));
1670
+ }
1671
+ getDiscoverProducts() {
1672
+ return this.getHomeConfiguration().pipe(concatMap((home) => from(this.categoryRepository.getCategoriesForHome(home.discoverCategories)).pipe(map((groups) => groups.map(this.buildCategoryGroupWithRequiredData)))));
1673
+ }
1674
+ getFeaturedProducts() {
1675
+ return this.getHomeConfiguration().pipe(concatMap((home) => from(this.categoryRepository.getCategoriesForHome(home.featuredCategories)).pipe(map((groups) => groups.map(this.buildCategoryGroupWithRequiredData)))));
1676
+ }
1677
+ getVerticalProducts() {
1678
+ return this.getHomeConfiguration().pipe(concatMap((home) => forkJoin(home.verticalCarousels.filter(Boolean).map((id) => from(this.categoryRepository.get({ id })).pipe(concatMap((category) => from(this.productRepository.find({
1679
+ filters: { categories: { operator: Where.IN, value: [category.id] } },
1680
+ limits: { limit: 12 },
1681
+ })).pipe(map((products) => ({ category, products })))), map(({ category, products }) => ({ category, products: products.data })), map(this.buildCategoryGroupWithRequiredData))))));
1682
+ }
1683
+ getHomeConfiguration() {
1684
+ return of(this.homeConfiguration).pipe(concatMap((home) => home
1685
+ ? of(home)
1686
+ : !this.homeId
1687
+ ? throwError(new RequiredArgumentError(['homeId']))
1688
+ : from(this.homeRepository.get({ id: this.homeId })).pipe(tap((homeLoaded) => (this.homeConfiguration = homeLoaded)))));
1689
+ }
1690
+ saveHomeData(homeData) {
1691
+ const data = {
1692
+ createdAt: new Date(),
1693
+ expiresAt: add(new Date(), { hours: 1 }),
1694
+ data: homeData,
1165
1695
  };
1696
+ return from(this.homeRepository.update({
1697
+ id: this.homeId,
1698
+ data,
1699
+ })).pipe(tap(() => (this.homeConfiguration.data = data)), map(() => this.homeConfiguration));
1166
1700
  }
1167
1701
  }
1168
- AngularHasuraGraphQLModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularHasuraGraphQLModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
1169
- AngularHasuraGraphQLModulemod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.0", ngImport: i0, type: AngularHasuraGraphQLModule });
1170
- AngularHasuraGraphQLModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularHasuraGraphQLModule, providers: [
1171
- {
1172
- provide: 'HasuraConfig',
1173
- useFactory: (options, platformId) => ({
1174
- endpoint: options.endpoint,
1175
- authOptions: options.credentials,
1176
- interceptors: {
1177
- request: (request) => {
1178
- console.log('request', request);
1179
- if (isPlatformServer(platformId))
1180
- console.log('request:server', request);
1181
- },
1182
- response: (response) => {
1183
- console.log('response', response);
1184
- if (isPlatformServer(platformId))
1185
- console.log('response:server', response);
1186
- },
1187
- },
1188
- }),
1189
- deps: [HASURA_OPTIONS, PLATFORM_ID],
1190
- },
1191
- {
1192
- provide: 'CategoryRepository',
1193
- useExisting: CategoryHasuraGraphQLRepository,
1194
- },
1195
- {
1196
- provide: CategoryHasuraGraphQLRepository,
1197
- useFactory: (hasuraConfig, productRepository) => {
1198
- return new CategoryHasuraGraphQLRepository(hasuraConfig, productRepository);
1199
- },
1200
- deps: ['HasuraConfig', ProductHasuraGraphQLRepository],
1201
- },
1202
- {
1203
- provide: 'ProductRepository',
1204
- useExisting: ProductHasuraGraphQLRepository,
1205
- },
1206
- {
1207
- provide: ProductHasuraGraphQLRepository,
1208
- useFactory: (hasuraConfig) => {
1209
- return new ProductHasuraGraphQLRepository(hasuraConfig);
1210
- },
1211
- deps: ['HasuraConfig'],
1212
- },
1213
- {
1214
- provide: 'VariantRepository',
1215
- useExisting: VariantHasuraGraphQLRepository,
1216
- },
1217
- {
1218
- provide: VariantHasuraGraphQLRepository,
1219
- useFactory: (hasuraConfig) => {
1220
- return new VariantHasuraGraphQLRepository(hasuraConfig);
1221
- },
1222
- deps: ['HasuraConfig'],
1223
- },
1224
- ] });
1225
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularHasuraGraphQLModule, decorators: [{
1226
- type: NgModule,
1227
- args: [{
1228
- providers: [
1229
- {
1230
- provide: 'HasuraConfig',
1231
- useFactory: (options, platformId) => ({
1232
- endpoint: options.endpoint,
1233
- authOptions: options.credentials,
1234
- interceptors: {
1235
- request: (request) => {
1236
- console.log('request', request);
1237
- if (isPlatformServer(platformId))
1238
- console.log('request:server', request);
1239
- },
1240
- response: (response) => {
1241
- console.log('response', response);
1242
- if (isPlatformServer(platformId))
1243
- console.log('response:server', response);
1244
- },
1245
- },
1246
- }),
1247
- deps: [HASURA_OPTIONS, PLATFORM_ID],
1248
- },
1249
- {
1250
- provide: 'CategoryRepository',
1251
- useExisting: CategoryHasuraGraphQLRepository,
1252
- },
1253
- {
1254
- provide: CategoryHasuraGraphQLRepository,
1255
- useFactory: (hasuraConfig, productRepository) => {
1256
- return new CategoryHasuraGraphQLRepository(hasuraConfig, productRepository);
1257
- },
1258
- deps: ['HasuraConfig', ProductHasuraGraphQLRepository],
1259
- },
1260
- {
1261
- provide: 'ProductRepository',
1262
- useExisting: ProductHasuraGraphQLRepository,
1263
- },
1264
- {
1265
- provide: ProductHasuraGraphQLRepository,
1266
- useFactory: (hasuraConfig) => {
1267
- return new ProductHasuraGraphQLRepository(hasuraConfig);
1268
- },
1269
- deps: ['HasuraConfig'],
1270
- },
1271
- {
1272
- provide: 'VariantRepository',
1273
- useExisting: VariantHasuraGraphQLRepository,
1274
- },
1275
- {
1276
- provide: VariantHasuraGraphQLRepository,
1277
- useFactory: (hasuraConfig) => {
1278
- return new VariantHasuraGraphQLRepository(hasuraConfig);
1279
- },
1280
- deps: ['HasuraConfig'],
1281
- },
1282
- ],
1283
- }]
1284
- }] });
1702
+ HomeShopService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: HomeShopService, deps: [{ token: 'CategoryRepository' }, { token: 'HomeRepository' }, { token: 'ProductRepository' }, { token: DEFAULT_SHOP }], target: i0.ɵɵFactoryTarget.Injectable });
1703
+ HomeShopServiceprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: HomeShopService });
1704
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: HomeShopService, decorators: [{
1705
+ type: Injectable
1706
+ }], ctorParameters: function () { return [{ type: undefined, decorators: [{
1707
+ type: Inject,
1708
+ args: ['CategoryRepository']
1709
+ }] }, { type: undefined, decorators: [{
1710
+ type: Inject,
1711
+ args: ['HomeRepository']
1712
+ }] }, { type: undefined, decorators: [{
1713
+ type: Inject,
1714
+ args: ['ProductRepository']
1715
+ }] }, { type: i1$3.Shops, decorators: [{
1716
+ type: Inject,
1717
+ args: [DEFAULT_SHOP]
1718
+ }] }]; } });
1719
+
1720
+ class OrderService {
1721
+ constructor(angularFirestore, orderRepository) {
1722
+ this.angularFirestore = angularFirestore;
1723
+ this.orderRepository = orderRepository;
1724
+ this.orderSubject = new Subject();
1725
+ }
1726
+ getOrder(id) {
1727
+ docSnapshots(doc(this.angularFirestore, `${this.orderRepository.collectionName}/${id}`))
1728
+ .pipe(map((doc) => Order.toInstance(doc.data())))
1729
+ .subscribe((doc) => this.orderSubject.next(doc));
1730
+ return this.orderSubject;
1731
+ }
1732
+ }
1733
+ OrderService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: OrderService, deps: [{ token: i1$2.Firestore }, { token: 'OrderRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
1734
+ OrderService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: OrderService });
1735
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: OrderService, decorators: [{
1736
+ type: Injectable
1737
+ }], ctorParameters: function () { return [{ type: i1$2.Firestore }, { type: i1$3.OrderFirestoreRepository, decorators: [{
1738
+ type: Inject,
1739
+ args: ['OrderRepository']
1740
+ }] }]; } });
1741
+
1742
+ class ShippingService {
1743
+ constructor(http, apiUrl, homeService) {
1744
+ this.http = http;
1745
+ this.apiUrl = apiUrl;
1746
+ this.homeService = homeService;
1747
+ }
1748
+ getShippingMethods(shop, zip, weightGrams, totalPrice, personId, subscriptionPlan) {
1749
+ return combineLatest([
1750
+ this.homeService.getHomeData(),
1751
+ this.http.get(`${this.apiUrl}open/checkshippingcompany?personId=${personId}&postalCode=${zip}&weightGrams=${weightGrams}`),
1752
+ ]).pipe(map(([datas, shippingMethodsResponse]) => {
1753
+ let shippingMethods = shippingMethodsResponse.result;
1754
+ if (!shippingMethods.length)
1755
+ return [];
1756
+ shippingMethods = shippingMethods.map((shippingMethod) => {
1757
+ if (shippingMethod.ShippingCompanyName == 'Same Day EG')
1758
+ shippingMethod.ShippingCompanyName = 'Same Day';
1759
+ return shippingMethod;
1760
+ });
1761
+ const datasSameDayNotAvaliable = datas.sameDayNotAvaliable;
1762
+ if (this.isHolidays(datasSameDayNotAvaliable)) {
1763
+ shippingMethods = shippingMethods.filter((method) => method.serviceName !== 'Same Day');
1764
+ }
1765
+ if (totalPrice >= 200) {
1766
+ shippingMethods = shippingMethods.map((s) => {
1767
+ if (s.serviceName !== 'Same Day')
1768
+ return { ...s, totalPrice: 0 };
1769
+ else
1770
+ return s;
1771
+ });
1772
+ }
1773
+ if (shop == Shops.GLAMSHOP)
1774
+ return shippingMethods;
1775
+ if (this.isFreeShippingBySubscription(shop, subscriptionPlan)) {
1776
+ shippingMethods = shippingMethods.map((s) => {
1777
+ if (s.serviceName == 'Same Day')
1778
+ return { ...s, totalPrice: s.totalPrice / 2 };
1779
+ else
1780
+ return { ...s, totalPrice: 0 };
1781
+ });
1782
+ }
1783
+ if (this.isHalfShippingBySubscription(shop, subscriptionPlan)) {
1784
+ shippingMethods = shippingMethods.map((s) => {
1785
+ return { ...s, totalPrice: s.totalPrice / 2 };
1786
+ });
1787
+ }
1788
+ return shippingMethods;
1789
+ }));
1790
+ }
1791
+ isFreeShippingBySubscription(shop, subscriptionPlan) {
1792
+ if (!subscriptionPlan)
1793
+ return false;
1794
+ if (shop == Shops.MENSMARKET && subscriptionPlan == 'SELECT')
1795
+ return true;
1796
+ return false;
1797
+ }
1798
+ isHalfShippingBySubscription(shop, subscriptionPlan) {
1799
+ if (!subscriptionPlan)
1800
+ return false;
1801
+ if (shop == Shops.MENSMARKET && subscriptionPlan == 'PRIME') {
1802
+ return true;
1803
+ }
1804
+ return false;
1805
+ }
1806
+ isHolidays(datas) {
1807
+ const today = new Date();
1808
+ for (const key in datas) {
1809
+ let start = new Date(`${today.getFullYear()}-${datas[key].beginDate}`);
1810
+ let end = new Date(`${today.getFullYear()}-${datas[key].endDate}`);
1811
+ if (start > end)
1812
+ end = new Date(`${today.getFullYear() + 1}-${datas[key].endDate}`);
1813
+ if (today >= start && today <= end)
1814
+ return true;
1815
+ }
1816
+ return false;
1817
+ }
1818
+ }
1819
+ ShippingService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: ShippingService, deps: [{ token: i1$4.HttpClient }, { token: BACKEND_URL }, { token: HomeShopService }], target: i0.ɵɵFactoryTarget.Injectable });
1820
+ ShippingService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: ShippingService });
1821
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: ShippingService, decorators: [{
1822
+ type: Injectable
1823
+ }], ctorParameters: function () { return [{ type: i1$4.HttpClient }, { type: undefined, decorators: [{
1824
+ type: Inject,
1825
+ args: [BACKEND_URL]
1826
+ }] }, { type: HomeShopService }]; } });
1827
+
1828
+ class NewCategoryStructureAdapter {
1829
+ constructor(categoryRepository, categoryCollectionChildrenRepository) {
1830
+ this.categoryRepository = categoryRepository;
1831
+ this.categoryCollectionChildrenRepository = categoryCollectionChildrenRepository;
1832
+ }
1833
+ async buildProductFilterByCategory(category) {
1834
+ const categoriesIds = (await this.isCollection(category))
1835
+ ? await this.getAllCategoriesIdFromCollection(category)
1836
+ : [...(await this.getAllCategoriesIdFromCategory(category)), category.id.toString()];
1837
+ return { category: { id: { operator: Where.IN, value: categoriesIds } } };
1838
+ }
1839
+ async getAllCategoriesIdFromCategory(category) {
1840
+ return this.categoryRepository
1841
+ .getChildren(+category.id)
1842
+ .then((categories) => categories.map((category) => category.id.toString()));
1843
+ }
1844
+ async getAllCategoriesIdFromCollection(category) {
1845
+ return this.categoryCollectionChildrenRepository
1846
+ .find({ filters: { collectionId: +category.id } })
1847
+ .then(({ data }) => data.map((categoryCollection) => categoryCollection.categoryId.toString()));
1848
+ }
1849
+ async isCollection(category) {
1850
+ return !isNil(category.isCollection)
1851
+ ? category.isCollection
1852
+ : this.categoryRepository.get({ id: category.id }).then((category) => category.isCollection);
1853
+ }
1854
+ }
1855
+ NewCategoryStructureAdapter.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: NewCategoryStructureAdapter, deps: [{ token: 'CategoryRepository' }, { token: 'CategoryCollectionChildrenRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
1856
+ NewCategoryStructureAdapter.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: NewCategoryStructureAdapter });
1857
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: NewCategoryStructureAdapter, decorators: [{
1858
+ type: Injectable
1859
+ }], ctorParameters: function () { return [{ type: undefined, decorators: [{
1860
+ type: Inject,
1861
+ args: ['CategoryRepository']
1862
+ }] }, { type: undefined, decorators: [{
1863
+ type: Inject,
1864
+ args: ['CategoryCollectionChildrenRepository']
1865
+ }] }]; } });
1866
+
1867
+ class OldCategoryStructureAdapter {
1868
+ constructor(categoryRepository) {
1869
+ this.categoryRepository = categoryRepository;
1870
+ }
1871
+ async buildProductFilterByCategory(category) {
1872
+ const productsIds = category.products?.length
1873
+ ? category.products
1874
+ : await this.categoryRepository.get({ id: category.id }).then((categoryFound) => categoryFound.products);
1875
+ return { id: { operator: Where.IN, value: productsIds } };
1876
+ }
1877
+ }
1878
+ OldCategoryStructureAdapter.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: OldCategoryStructureAdapter, deps: [{ token: 'CategoryRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
1879
+ OldCategoryStructureAdapter.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: OldCategoryStructureAdapter });
1880
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: OldCategoryStructureAdapter, decorators: [{
1881
+ type: Injectable
1882
+ }], ctorParameters: function () { return [{ type: undefined, decorators: [{
1883
+ type: Inject,
1884
+ args: ['CategoryRepository']
1885
+ }] }]; } });
1285
1886
 
1286
1887
  class AngularConnectModule {
1287
1888
  static initializeApp(defaultShop, options, nameOrConfig) {
1288
1889
  return {
1289
1890
  ngModule: AngularConnectModule,
1290
1891
  providers: [
1892
+ {
1893
+ provide: CATEGORY_STRUCTURE,
1894
+ useClass: isNil(options?.oldCategoryStructure) || options?.oldCategoryStructure
1895
+ ? OldCategoryStructureAdapter
1896
+ : NewCategoryStructureAdapter,
1897
+ },
1291
1898
  ...(isNil(defaultShop) ? [] : [{ provide: DEFAULT_SHOP, useValue: defaultShop }]),
1292
1899
  ...(isNil(options?.firebase) ? [] : [{ provide: FIREBASE_OPTIONS, useValue: options?.firebase }]),
1293
1900
  ...(isNil(options?.firebase) ? [] : [{ provide: FIREBASE_APP_NAME, useValue: nameOrConfig }]),
1294
1901
  ...(isNil(options?.elasticSearch) ? [] : [{ provide: ES_CONFIG, useValue: options.elasticSearch }]),
1295
1902
  ...(isNil(options?.hasura) ? [] : [{ provide: HASURA_OPTIONS, useValue: options.hasura }]),
1903
+ ...(isNil(options?.backendUrl) ? [] : [{ provide: BACKEND_URL, useValue: options.backendUrl }]),
1296
1904
  ],
1297
1905
  };
1298
1906
  }
@@ -1302,11 +1910,14 @@ AngularConnectModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", ve
1302
1910
  AngularConnectModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularConnectModule, providers: [
1303
1911
  AuthService,
1304
1912
  CartService,
1913
+ CatalogService,
1914
+ CategoryService,
1305
1915
  CheckoutService,
1306
1916
  CheckoutSubscriptionService,
1307
1917
  CouponService,
1308
1918
  HomeShopService,
1309
1919
  OrderService,
1920
+ ShippingService,
1310
1921
  ], imports: [AngularFirebaseAuthModule, AngularFirestoreModule, AngularHasuraGraphQLModule] });
1311
1922
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: AngularConnectModule, decorators: [{
1312
1923
  type: NgModule,
@@ -1315,11 +1926,14 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImpor
1315
1926
  providers: [
1316
1927
  AuthService,
1317
1928
  CartService,
1929
+ CatalogService,
1930
+ CategoryService,
1318
1931
  CheckoutService,
1319
1932
  CheckoutSubscriptionService,
1320
1933
  CouponService,
1321
1934
  HomeShopService,
1322
1935
  OrderService,
1936
+ ShippingService,
1323
1937
  ],
1324
1938
  }]
1325
1939
  }] });
@@ -1328,5 +1942,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImpor
1328
1942
  * Generated bundle index. Do not edit.
1329
1943
  */
1330
1944
 
1331
- export { AngularConnectModule, AngularFirebaseAuthModule, AngularFirestoreModule, AngularHasuraGraphQLModule, AuthService, CartService, CheckoutService, CheckoutSubscriptionService, CouponService, HomeShopService, OrderService };
1945
+ export { AngularConnectModule, AngularFirebaseAuthModule, AngularFirestoreModule, AngularHasuraGraphQLModule, AuthService, CartService, CatalogService, CategoryService, CategoryWithTree, CheckoutService, CheckoutSubscriptionService, CouponService, HomeShopService, OrderService, ProductSorts, ShippingService };
1332
1946
  //# sourceMappingURL=infrab4a-connect-angular.mjs.map