@infrab4a/connect-angular 0.17.0-beta.2 → 0.17.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundles/infrab4a-connect-angular.umd.js +85 -40
- package/bundles/infrab4a-connect-angular.umd.js.map +1 -1
- package/esm2015/services/checkout-subscription.service.js +2 -17
- package/esm2015/services/checkout.service.js +8 -3
- package/esm2015/services/coupon.service.js +72 -25
- package/fesm2015/infrab4a-connect-angular.js +78 -41
- package/fesm2015/infrab4a-connect-angular.js.map +1 -1
- package/package.json +2 -2
- package/services/coupon.service.d.ts +5 -3
|
@@ -5,7 +5,7 @@ import { AngularFireAuth } from '@angular/fire/auth';
|
|
|
5
5
|
import { of, combineLatest, from, throwError, Subject, iif, forkJoin } from 'rxjs';
|
|
6
6
|
import { catchError, map, mergeMap, concatMap, tap } from 'rxjs/operators';
|
|
7
7
|
import * as i2 from '@infrab4a/connect';
|
|
8
|
-
import { Coupon, Where, Exclusivities,
|
|
8
|
+
import { Coupon, Where, Exclusivities, Shops, CheckoutTypes, CouponTypes, 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 } from '@infrab4a/connect';
|
|
9
9
|
import { __awaiter } from 'tslib';
|
|
10
10
|
import cookie from 'js-cookie';
|
|
11
11
|
import * as i1$1 from '@angular/fire/firestore';
|
|
@@ -67,16 +67,17 @@ class GroupInvalidCouponError extends Error {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
class CouponService {
|
|
70
|
-
constructor(couponRepository, defaultShop, orderRepository) {
|
|
70
|
+
constructor(couponRepository, defaultShop, orderRepository, subscriptionRepository) {
|
|
71
71
|
this.couponRepository = couponRepository;
|
|
72
72
|
this.defaultShop = defaultShop;
|
|
73
73
|
this.orderRepository = orderRepository;
|
|
74
|
+
this.subscriptionRepository = subscriptionRepository;
|
|
74
75
|
this.emailIsFromCollaborator = (userEmail) => !!(userEmail === null || userEmail === void 0 ? void 0 : userEmail.match(/@b4a.com.br/g));
|
|
75
76
|
this.separateValidCoupons = (coupons, userEmail) => coupons
|
|
76
77
|
.map((coupon) => {
|
|
77
78
|
try {
|
|
78
79
|
if (!(coupon instanceof Coupon))
|
|
79
|
-
throw new InvalidCouponError('
|
|
80
|
+
throw new InvalidCouponError('Cupom inválido.');
|
|
80
81
|
if (this.isValidCoupon(coupon, userEmail))
|
|
81
82
|
return coupon;
|
|
82
83
|
}
|
|
@@ -91,60 +92,66 @@ class CouponService {
|
|
|
91
92
|
invalids: [],
|
|
92
93
|
});
|
|
93
94
|
}
|
|
94
|
-
checkCoupon(nickname, userEmail, checkoutType, plan, checkout) {
|
|
95
|
+
checkCoupon(nickname, userEmail, checkoutType, plan, checkout, isSubscription) {
|
|
95
96
|
return from(this.couponRepository.find([
|
|
96
97
|
{
|
|
97
98
|
nickname: { operator: Where.EQUALS, value: nickname },
|
|
98
99
|
},
|
|
99
|
-
])).pipe(concatMap((coupons) => this.checkCouponRules(coupons, checkoutType, plan, checkout)), concatMap((coupon) => this.checkCouponUseLimit(coupon, userEmail)), map((coupon) => this.isValidCoupon(coupon, userEmail)), map((coupon) => coupon));
|
|
100
|
+
])).pipe(concatMap((coupons) => this.checkCouponRules(coupons, checkoutType, plan, checkout, isSubscription)), concatMap((coupon) => this.checkCouponUseLimit(coupon, userEmail)), map((coupon) => this.isValidCoupon(coupon, userEmail)), map((coupon) => coupon));
|
|
100
101
|
}
|
|
101
102
|
isValidCoupon(coupon, userEmail) {
|
|
102
103
|
// Verifica se o email do usuário é coorporativo
|
|
103
104
|
if (!this.emailIsFromCollaborator(userEmail) && coupon.exclusivityType === Exclusivities.COLLABORATORS)
|
|
104
|
-
throw new InvalidCouponError('
|
|
105
|
+
throw new InvalidCouponError('Você não é colaborador.');
|
|
105
106
|
// Verifica se o email do usuário é associado ao cupom de uso por usuario
|
|
106
107
|
if (coupon.exclusivityType === Exclusivities.SPECIFIC_USER && coupon.userExclusiveEmail !== userEmail)
|
|
107
|
-
throw new InvalidCouponError('
|
|
108
|
+
throw new InvalidCouponError('Cupom não é válido para este usuário.');
|
|
108
109
|
// Verifica a data de inicio de validade do cupom
|
|
109
110
|
if ((coupon === null || coupon === void 0 ? void 0 : coupon.beginAt) > new Date())
|
|
110
|
-
throw new InvalidCouponError('
|
|
111
|
+
throw new InvalidCouponError('Cupom inválido.');
|
|
111
112
|
// Verifica a data de validade do cupom
|
|
112
113
|
if ((coupon === null || coupon === void 0 ? void 0 : coupon.expiresIn) < new Date())
|
|
113
|
-
throw new InvalidCouponError('
|
|
114
|
+
throw new InvalidCouponError('Cupom expirado.');
|
|
114
115
|
return coupon;
|
|
115
116
|
}
|
|
116
|
-
checkCouponRules(coupons, checkoutType, plan, checkout) {
|
|
117
|
+
checkCouponRules(coupons, checkoutType, plan, checkout, isSubscription) {
|
|
117
118
|
// Caso não ache nenhum cupom, retorna erro
|
|
118
119
|
if (coupons.count < 1)
|
|
119
|
-
return throwError(
|
|
120
|
+
return throwError('Cupom inválido.');
|
|
120
121
|
// Get Primeiro Cupom (o find do repository retorna um array)
|
|
121
122
|
const coupon = coupons.data.shift();
|
|
122
123
|
// Verifica se o cupom é aplicavel na loja
|
|
123
124
|
const isInShop = coupon.shopAvailability === Shops.ALL || coupon.shopAvailability === this.defaultShop;
|
|
124
125
|
// Cupon não aplicavel a loja retorna erro
|
|
125
126
|
if (!isInShop)
|
|
126
|
-
return throwError(
|
|
127
|
+
return throwError('Cupom inválido.');
|
|
127
128
|
// Verifica se o coupon é aplicado no checkout que está sendo realizado
|
|
128
129
|
const isCheckoutType = coupon.checkoutType === CheckoutTypes.ALL || coupon.checkoutType === checkoutType;
|
|
129
130
|
// Cupon não aplicavel ao checkout retorna erro
|
|
130
131
|
if (!isCheckoutType)
|
|
131
|
-
return throwError(
|
|
132
|
+
return throwError('Cupom inválido.');
|
|
132
133
|
// Verifica se o cupom é ou pode ser aplicado para subscription
|
|
133
134
|
if (checkoutType === CheckoutTypes.ALL || checkoutType === CheckoutTypes.SUBSCRIPTION) {
|
|
134
135
|
// Se o cupom tiver um plano associado, verifica se é o mesmo plano do checkout da assinatura
|
|
136
|
+
console.log('1', coupon.plan, plan.toUpperCase());
|
|
135
137
|
if (coupon.plan && coupon.plan.toUpperCase() !== plan.toUpperCase())
|
|
136
|
-
return throwError(
|
|
138
|
+
return throwError('Cupom inválido.');
|
|
137
139
|
}
|
|
140
|
+
if (isSubscription)
|
|
141
|
+
return of(coupon);
|
|
138
142
|
// Verifica se possui o valor minimo de compra para utilização do cupom
|
|
139
143
|
const hasMinSubTotal = this.hasMinSubTotal(coupon, checkout);
|
|
140
144
|
// Se não tem valor mínimo atingido, retorna erro
|
|
141
145
|
if (!hasMinSubTotal)
|
|
142
|
-
return throwError(
|
|
146
|
+
return throwError('Valor mínimo não atingido');
|
|
143
147
|
// Verifica se a compra possui produtos elegíveis para desconto
|
|
144
148
|
const hasProductCategories = this.hasProductCategories(coupon, checkout);
|
|
145
149
|
// Se não tem produtos elegíveis, retorna erro
|
|
146
150
|
if (!hasProductCategories)
|
|
147
|
-
return throwError(
|
|
151
|
+
return throwError('Seu carrinho não possui produtos elegíveis para desconto.');
|
|
152
|
+
// const validUser = this.userSubscriberStatus(coupon, checkout)
|
|
153
|
+
// console.log(validUser)
|
|
154
|
+
// if (!validUser) return throwError('Usuário não elegível.')
|
|
148
155
|
return of(coupon);
|
|
149
156
|
}
|
|
150
157
|
checkCouponUseLimit(coupon, userEmail) {
|
|
@@ -155,10 +162,10 @@ class CouponService {
|
|
|
155
162
|
const ordersUserCoupon = orders.data.filter((o) => o.user.email == userEmail);
|
|
156
163
|
// Verifica o limite de uso de cupom por usuario
|
|
157
164
|
if (coupon.useLimitPerUser && ordersUserCoupon.length)
|
|
158
|
-
throw new InvalidCouponError('
|
|
165
|
+
throw new InvalidCouponError('Limite de uso por usuário atingido.');
|
|
159
166
|
// Verifica o limite de uso geral por usuario
|
|
160
167
|
if (coupon.useLimit && orders.data.length >= coupon.useLimit)
|
|
161
|
-
throw new InvalidCouponError('
|
|
168
|
+
throw new InvalidCouponError('Limite de uso atingido.');
|
|
162
169
|
return coupon;
|
|
163
170
|
});
|
|
164
171
|
}
|
|
@@ -190,8 +197,14 @@ class CouponService {
|
|
|
190
197
|
let discount = 0;
|
|
191
198
|
const shop = checkout.shop;
|
|
192
199
|
let lineItensDiscount = [];
|
|
193
|
-
if (coupon.productsCategories) {
|
|
194
|
-
lineItensDiscount = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter((i) =>
|
|
200
|
+
if (coupon.productsCategories && coupon.productsCategories.length) {
|
|
201
|
+
lineItensDiscount = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter((i) => {
|
|
202
|
+
var _a;
|
|
203
|
+
if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
|
|
204
|
+
return i.categories.some((c) => coupon.productsCategories.includes(c));
|
|
205
|
+
}
|
|
206
|
+
return true;
|
|
207
|
+
});
|
|
195
208
|
}
|
|
196
209
|
else {
|
|
197
210
|
lineItensDiscount = checkout.lineItems;
|
|
@@ -222,16 +235,47 @@ class CouponService {
|
|
|
222
235
|
}
|
|
223
236
|
hasProductCategories(coupon, checkout) {
|
|
224
237
|
var _a;
|
|
225
|
-
if (coupon.productsCategories) {
|
|
226
|
-
const hasCategories = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter((i) =>
|
|
227
|
-
|
|
238
|
+
if (coupon.productsCategories && coupon.productsCategories.length) {
|
|
239
|
+
const hasCategories = (_a = checkout.lineItems) === null || _a === void 0 ? void 0 : _a.filter((i) => {
|
|
240
|
+
var _a;
|
|
241
|
+
if ((_a = i.categories) === null || _a === void 0 ? void 0 : _a.length) {
|
|
242
|
+
return i.categories.some((c) => coupon.productsCategories.includes(c));
|
|
243
|
+
}
|
|
244
|
+
return true;
|
|
245
|
+
});
|
|
246
|
+
return hasCategories.length ? true : false;
|
|
228
247
|
}
|
|
229
248
|
else {
|
|
230
249
|
return true;
|
|
231
250
|
}
|
|
232
251
|
}
|
|
252
|
+
userSubscriberStatus(coupon, checkout) {
|
|
253
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
254
|
+
const sub = yield this.subscriptionRepository
|
|
255
|
+
.find([
|
|
256
|
+
{
|
|
257
|
+
user: {
|
|
258
|
+
email: { operator: Where.EQUALS, value: checkout.user.email },
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
])
|
|
262
|
+
.then((sub) => sub.data);
|
|
263
|
+
const active = sub === null || sub === void 0 ? void 0 : sub.filter((s) => s.status === Status.ACTIVE);
|
|
264
|
+
console.log(`active`, active, active.length, sub);
|
|
265
|
+
switch (coupon.exclusivityType) {
|
|
266
|
+
case Exclusivities.ACTIVE_SUBSCRIBER:
|
|
267
|
+
return active.length > 0;
|
|
268
|
+
case Exclusivities.INACTIVE_SUBSCRIBER:
|
|
269
|
+
return active.length === 0;
|
|
270
|
+
case Exclusivities.NON_SUBSCRIBER:
|
|
271
|
+
return sub.length === 0;
|
|
272
|
+
default:
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
233
277
|
}
|
|
234
|
-
CouponService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: CouponService, deps: [{ token: 'CouponRepository' }, { token: DEFAULT_SHOP }, { token: 'OrderRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
278
|
+
CouponService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: CouponService, deps: [{ token: 'CouponRepository' }, { token: DEFAULT_SHOP }, { token: 'OrderRepository' }, { token: 'SubscriptionRepository' }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
235
279
|
CouponService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: CouponService, providedIn: 'root' });
|
|
236
280
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImport: i0, type: CouponService, decorators: [{
|
|
237
281
|
type: Injectable,
|
|
@@ -247,6 +291,9 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.2.12", ngImpo
|
|
|
247
291
|
}] }, { type: undefined, decorators: [{
|
|
248
292
|
type: Inject,
|
|
249
293
|
args: ['OrderRepository']
|
|
294
|
+
}] }, { type: undefined, decorators: [{
|
|
295
|
+
type: Inject,
|
|
296
|
+
args: ['SubscriptionRepository']
|
|
250
297
|
}] }]; } });
|
|
251
298
|
|
|
252
299
|
class CheckoutService {
|
|
@@ -280,7 +327,12 @@ class CheckoutService {
|
|
|
280
327
|
return this.getCheckout().pipe(concatMap((checkout) => this.couponService.calcDiscountShopping(coupon, checkout).pipe()));
|
|
281
328
|
}
|
|
282
329
|
checkCoupon(nickname, checkoutType) {
|
|
283
|
-
return this.getCheckout().pipe(concatMap((checkout) => {
|
|
330
|
+
return this.getCheckout().pipe(concatMap((checkout) => {
|
|
331
|
+
var _a;
|
|
332
|
+
return this.couponService
|
|
333
|
+
.checkCoupon(nickname, (_a = checkout.user) === null || _a === void 0 ? void 0 : _a.email, CheckoutTypes.ECOMMERCE, checkout.user.subscriptionPlan, checkout, false)
|
|
334
|
+
.pipe();
|
|
335
|
+
}));
|
|
284
336
|
}
|
|
285
337
|
createCheckout(checkoutData) {
|
|
286
338
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -471,22 +523,7 @@ class CheckoutSubscriptionService {
|
|
|
471
523
|
return of();
|
|
472
524
|
}
|
|
473
525
|
checkCoupon(nickname, userEmail) {
|
|
474
|
-
return this.getCheckoutSubscription().pipe(concatMap((checkout) => this.couponService.checkCoupon(nickname, userEmail, CheckoutTypes.SUBSCRIPTION, checkout.subscriptionPlan.name).pipe()
|
|
475
|
-
// .pipe(
|
|
476
|
-
// concatMap((coupon: Coupon) => {
|
|
477
|
-
// const couponsWithSamePlan = coupons.filter((coupon) => checkout.subscriptionPlan.name === coupon.plan)
|
|
478
|
-
// const couponsWithNoPlan = coupons.filter((coupon) => !coupon.plan)
|
|
479
|
-
// if (couponsWithSamePlan.length > 0) return of(couponsWithSamePlan)
|
|
480
|
-
// if (couponsWithNoPlan.length > 0) return of(couponsWithNoPlan)
|
|
481
|
-
// throw new Error(`Coupon subscription plan is invalid.`)
|
|
482
|
-
// })
|
|
483
|
-
// ,
|
|
484
|
-
// concatMap((coupons) =>
|
|
485
|
-
// !!checkout.user?.email ? this.checkCouponsWithCheckout(coupons, checkout) : of(coupons),
|
|
486
|
-
// )
|
|
487
|
-
// map((validatedCoupons) => validatedCoupons.shift()),
|
|
488
|
-
// ),
|
|
489
|
-
));
|
|
526
|
+
return this.getCheckoutSubscription().pipe(concatMap((checkout) => this.couponService.checkCoupon(nickname, userEmail, CheckoutTypes.SUBSCRIPTION, checkout.subscriptionPlan.name, null, true).pipe()));
|
|
490
527
|
}
|
|
491
528
|
calcDiscountSubscription(coupon) {
|
|
492
529
|
return this.getCheckoutSubscription().pipe(concatMap((checkout) => this.couponService.calcDiscountSubscription(coupon, checkout).pipe()));
|