изменил структуру под аб тестирование, сделал 2 различных вариант (один дефолтный, другой с красным текстом) модули загружаются лениво, для оптимизации загрузки приложения пофиксил подсчет суммы транзакций за периоды
129 lines
3.8 KiB
TypeScript
129 lines
3.8 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import moment from 'moment';
|
|
import { Moment, Purchase } from '../interface/data';
|
|
|
|
export interface IPurchaseData {
|
|
currentPeriod: Moment[];
|
|
lastPeriod: Moment[];
|
|
lastPurchases: Purchase[];
|
|
currentPurchases: Purchase[];
|
|
lastAmount?: number;
|
|
currentAmount?: number;
|
|
$loading: boolean;
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class LoyaltyProgramService {
|
|
public purchaseData: IPurchaseData = {
|
|
currentPeriod: [],
|
|
lastPeriod: [],
|
|
lastPurchases: [],
|
|
currentPurchases: [],
|
|
$loading: true,
|
|
get currentAmount(): number {
|
|
const amount = this.currentPurchases.reduce(
|
|
(accumulator, currentValue) => {
|
|
if (['CancelPayFromWallet', 'CancelRefillWalletFromOrder'].includes(currentValue.transactionType || '')) {
|
|
return accumulator - currentValue.orderSum!;
|
|
} else if (['PayFromWallet', 'RefillWalletFromOrder'].includes(currentValue.transactionType || '')) {
|
|
return accumulator + currentValue.orderSum!;
|
|
}
|
|
return accumulator
|
|
},
|
|
0
|
|
);
|
|
|
|
return amount * 1;
|
|
},
|
|
get lastAmount(): number {
|
|
const amount = this.lastPurchases.reduce((accumulator, currentValue) => {
|
|
if (['CancelPayFromWallet', 'CancelRefillWalletFromOrder'].includes(currentValue.transactionType || '')) {
|
|
return accumulator - currentValue.orderSum!;
|
|
} else if (['PayFromWallet', 'RefillWalletFromOrder'].includes(currentValue.transactionType || '')) {
|
|
return accumulator + currentValue.orderSum!;
|
|
}
|
|
return accumulator
|
|
}, 0);
|
|
return amount * 1;
|
|
},
|
|
};
|
|
|
|
constructor() {
|
|
this.getCurrentQuarterOfYear();
|
|
}
|
|
|
|
getCurrentQuarterOfYear() {
|
|
const quarters = [
|
|
[
|
|
moment().subtract(1, 'years').endOf('year').subtract(3, 'months'),
|
|
moment().startOf('year').add(10, 'days'),
|
|
],
|
|
[moment().startOf('year').add(10, 'days'), moment().startOf('year').add(3, 'months')],
|
|
[
|
|
moment().startOf('year').add(3, 'months'),
|
|
moment().startOf('year').add(6, 'months'),
|
|
],
|
|
[
|
|
moment().startOf('year').add(6, 'months'),
|
|
moment().startOf('year').add(9, 'months'),
|
|
],
|
|
[
|
|
moment().startOf('year').add(9, 'months'),
|
|
moment().startOf('year').add(12, 'months'),
|
|
],
|
|
];
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
if (moment().isBetween(quarters[i][0], quarters[i][1])) {
|
|
this.purchaseData.lastPeriod = quarters[i - 1];
|
|
this.purchaseData.currentPeriod = quarters[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
getBalanceAmount(loyaltyPrograms: any[]) {
|
|
return loyaltyPrograms.reduce((accumulator, currentValue) => {
|
|
return accumulator + currentValue.balance;
|
|
}, 0);
|
|
}
|
|
|
|
setLastPurchases(purchases: Purchase[]) {
|
|
this.purchaseData.lastPurchases = purchases.filter((value: Purchase) => {
|
|
return moment(value.transactionCreateDate).isBetween(
|
|
this.purchaseData.lastPeriod[0],
|
|
this.purchaseData.lastPeriod[1]
|
|
);
|
|
});
|
|
}
|
|
|
|
setCurrentPurchases(purchases: Purchase[]) {
|
|
this.purchaseData.currentPurchases = purchases.filter((value: Purchase) => {
|
|
return moment(value.transactionCreateDate).isBetween(
|
|
this.purchaseData.currentPeriod[0],
|
|
this.purchaseData.currentPeriod[1]
|
|
);
|
|
});
|
|
}
|
|
|
|
filterPurchases(purchases: Purchase[]) {
|
|
return purchases.filter((purchase: Purchase) =>
|
|
[
|
|
'PayFromWallet',
|
|
'RefillWalletFromOrder',
|
|
'CancelPayFromWallet',
|
|
'CancelRefillWalletFromOrder',
|
|
].includes(purchase.transactionType || '')
|
|
);
|
|
}
|
|
|
|
getLastPurchase(purchases: Purchase[]) {
|
|
return purchases.filter((purchase: Purchase) =>
|
|
['PayFromWallet', 'RefillWalletFromOrder'].includes(
|
|
purchase.transactionType || ''
|
|
)
|
|
)[0];
|
|
}
|
|
}
|