@procraft/widget-order 2.32.1 → 2.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CourseItem/AltItem/index.js +3 -3
- package/dist/CourseItem/AltItem/index.js.map +1 -1
- package/dist/CourseItem/AltItemText/index.js +1 -1
- package/dist/CourseItem/AltItemText/index.js.map +1 -1
- package/dist/CourseItem/FieldValues/index.js +6 -6
- package/dist/CourseItem/FieldValues/index.js.map +1 -1
- package/dist/CourseItem/FieldValuesByType/index.js +2 -2
- package/dist/CourseItem/FieldValuesByType/index.js.map +1 -1
- package/dist/CourseItem/Materials/index.js +5 -5
- package/dist/CourseItem/Materials/index.js.map +1 -1
- package/dist/CourseItem/Materials/styles.js +1 -1
- package/dist/CourseItem/Materials/styles.js.map +1 -1
- package/dist/CourseItem/OrderButton/index.js +24 -20
- package/dist/CourseItem/OrderButton/index.js.map +1 -1
- package/dist/CourseItem/OrderButton/interfaces.d.ts +1 -0
- package/dist/CourseItem/OrderButton/styles.js +1 -1
- package/dist/CourseItem/OrderButton/styles.js.map +1 -1
- package/dist/CourseItem/PlacesLeft/index.js +1 -1
- package/dist/CourseItem/PlacesLeft/index.js.map +1 -1
- package/dist/CourseItem/Reviews/ReviewsPopup/Filters/index.js +7 -7
- package/dist/CourseItem/Reviews/ReviewsPopup/Filters/index.js.map +1 -1
- package/dist/CourseItem/Reviews/ReviewsPopup/ProgressBar/index.js +1 -1
- package/dist/CourseItem/Reviews/ReviewsPopup/ProgressBar/index.js.map +1 -1
- package/dist/CourseItem/Reviews/ReviewsPopup/Review/index.js +1 -1
- package/dist/CourseItem/Reviews/ReviewsPopup/Review/index.js.map +1 -1
- package/dist/CourseItem/Reviews/ReviewsPopup/Stats/index.js +6 -6
- package/dist/CourseItem/Reviews/ReviewsPopup/Stats/index.js.map +1 -1
- package/dist/CourseItem/Reviews/ReviewsPopup/index.js +10 -10
- package/dist/CourseItem/Reviews/ReviewsPopup/index.js.map +1 -1
- package/dist/CourseItem/Reviews/Stars/index.js +1 -1
- package/dist/CourseItem/Reviews/Stars/index.js.map +1 -1
- package/dist/CourseItem/Reviews/index.js +5 -5
- package/dist/CourseItem/Reviews/index.js.map +1 -1
- package/dist/CourseItem/Sales/Sale/index.js +24 -13
- package/dist/CourseItem/Sales/Sale/index.js.map +1 -1
- package/dist/CourseItem/Sales/index.js +1 -1
- package/dist/CourseItem/Sales/index.js.map +1 -1
- package/dist/CourseItem/Subscriptions/index.js +3 -3
- package/dist/CourseItem/Subscriptions/index.js.map +1 -1
- package/dist/CourseItem/Subscriptions/styles.js +1 -1
- package/dist/CourseItem/Subscriptions/styles.js.map +1 -1
- package/dist/CourseItem/index.js +12 -11
- package/dist/CourseItem/index.js.map +1 -1
- package/dist/components/DateFormat/index.js +1 -1
- package/dist/components/DateFormat/index.js.map +1 -1
- package/dist/components/OldPrice/index.js +19 -2
- package/dist/components/OldPrice/index.js.map +1 -1
- package/dist/components/OldPrice/interfaces.d.ts +7 -0
- package/dist/components/OldPrice/styles.d.ts +8 -0
- package/dist/components/OldPrice/styles.js +15 -2
- package/dist/components/OldPrice/styles.js.map +1 -1
- package/dist/components/ProlongationWidget/index.js +26 -16
- package/dist/components/ProlongationWidget/index.js.map +1 -1
- package/dist/components/ProlongationWidget/styles.js +1 -1
- package/dist/components/ProlongationWidget/styles.js.map +1 -1
- package/dist/components/RadioGroup/index.js +3 -3
- package/dist/components/RadioGroup/index.js.map +1 -1
- package/dist/components/RadioGroup/styles.js +1 -1
- package/dist/components/RadioGroup/styles.js.map +1 -1
- package/dist/components/Tabs/index.js +2 -2
- package/dist/components/Tabs/index.js.map +1 -1
- package/dist/components/Tabs/styles.js +1 -1
- package/dist/components/Tabs/styles.js.map +1 -1
- package/dist/components/Typography/index.js +1 -1
- package/dist/components/Typography/index.js.map +1 -1
- package/dist/components/Typography/styles.js +2 -2
- package/dist/components/Typography/styles.js.map +1 -1
- package/dist/helpers/getCatalogItems/index.js +1 -1
- package/dist/helpers/getCatalogItems/index.js.map +1 -1
- package/dist/hooks/useCatalogItemSubscription/index.js +3 -3
- package/dist/hooks/useCatalogItemSubscription/index.js.map +1 -1
- package/dist/hooks/useOrderLink/index.d.ts +1 -1
- package/dist/hooks/useOrderLink/index.js +17 -5
- package/dist/hooks/useOrderLink/index.js.map +1 -1
- package/dist/hooks/useOrderLink/interfaces.d.ts +1 -0
- package/dist/hooks/usePrices/index.js +1 -1
- package/dist/hooks/usePrices/index.js.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/models/Course.js +4 -4
- package/dist/models/Course.js.map +1 -1
- package/dist/modules/gql/cli/generateTypes/generateTypes.js +2 -2
- package/dist/modules/gql/cli/generateTypes/generateTypes.js.map +1 -1
- package/dist/modules/gql/cli/generateTypes/run.js +1 -1
- package/dist/modules/gql/cli/generateTypes/run.js.map +1 -1
- package/dist/modules/gql/cli/generateTypes/types.js +7 -7
- package/dist/modules/gql/cli/generateTypes/types.js.map +1 -1
- package/dist/modules/gql/generated/helpers/apollo-helpers.d.ts +88 -12
- package/dist/modules/gql/generated/types.d.ts +77 -1
- package/dist/modules/gql/generated/types.js +2 -0
- package/dist/modules/gql/generated/types.js.map +1 -1
- package/dist/money/SecondaryPrices.d.ts +26 -0
- package/dist/money/SecondaryPrices.js +58 -0
- package/dist/money/SecondaryPrices.js.map +1 -0
- package/dist/money/convert.d.ts +37 -0
- package/dist/money/convert.js +114 -0
- package/dist/money/convert.js.map +1 -0
- package/dist/styles/GlobalStyle/index.js +1 -1
- package/dist/styles/GlobalStyle/index.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/utils.js +5 -5
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/CourseItem/OrderButton/index.tsx +12 -0
- package/src/CourseItem/OrderButton/interfaces.ts +1 -0
- package/src/CourseItem/Sales/Sale/index.tsx +35 -6
- package/src/CourseItem/index.tsx +3 -0
- package/src/components/OldPrice/index.tsx +40 -2
- package/src/components/OldPrice/interfaces.ts +8 -0
- package/src/components/OldPrice/styles.ts +15 -0
- package/src/components/ProlongationWidget/__tests__/ProlongationWidget.test.tsx +101 -0
- package/src/components/ProlongationWidget/index.tsx +29 -34
- package/src/hooks/useOrderLink/index.tsx +14 -1
- package/src/hooks/useOrderLink/interfaces.ts +1 -0
- package/src/index.tsx +3 -1
- package/src/interfaces/index.ts +17 -0
- package/src/modules/gql/generated/helpers/apollo-helpers.ts +95 -19
- package/src/modules/gql/generated/schema.json +704 -25
- package/src/modules/gql/generated/types.ts +88 -1
- package/src/money/SecondaryPrices.tsx +99 -0
- package/src/money/__tests__/SecondaryPrices.test.tsx +110 -0
- package/src/money/__tests__/convert.test.ts +181 -0
- package/src/money/convert.ts +127 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Валюты показа (DisplayCurrency) — арифметика, парная к Scala-стороне.
|
|
3
|
+
*
|
|
4
|
+
* Парная реализация: lms-back `FinanceProfile.getCurrencyAmount` (+ `MoneyUtils.round`)
|
|
5
|
+
* и лендинг `lms-landing/src/utils/displayCurrency.ts` — дословный оригинал этого файла
|
|
6
|
+
* (пакет не может импортировать из lms-landing, поэтому копия, а не импорт).
|
|
7
|
+
*
|
|
8
|
+
* Канонический набор паритетных векторов — контракт SL-13080 §8, все 19 штук.
|
|
9
|
+
* Менять алгоритм можно только вместе с контрактом и обеими соседними реализациями.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type DisplayCurrencyRate = { currency: string; rate: number }
|
|
13
|
+
|
|
14
|
+
/** Деньги: не более 2 знаков после запятой. */
|
|
15
|
+
const AMOUNT_SCALE = 100
|
|
16
|
+
/** `currency_rate decimal(20, 10)` — контракт §3.4. */
|
|
17
|
+
const RATE_SCALE = 1e10
|
|
18
|
+
/** Столько нулей дописываем к числителю, чтобы скомпенсировать масштабы: 1e10 / 100. */
|
|
19
|
+
const DIVISION_ZEROS = 8
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Предел на делитель: инвариант длинного деления — `rem < divisor`, поэтому
|
|
23
|
+
* `rem * 10 + 9` обязано оставаться целым в double. 9e14 * 10 + 9 < 2^53.
|
|
24
|
+
* В пересчёте на курс это `rate < 9e4` — 90 000 единиц валюты Organization
|
|
25
|
+
* за единицу другой валюты. Выше — деградация к double-делению.
|
|
26
|
+
*/
|
|
27
|
+
const MAX_DIVISOR = 9e14
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Парная к Scala `FinanceProfile.getCurrencyAmount`.
|
|
31
|
+
*
|
|
32
|
+
* - `rate === 1` → сумма как есть, БЕЗ округления (в родной валюте школы «там может быть 7.99»);
|
|
33
|
+
* - иначе `amount / rate`, HALF_UP **от нуля**, до целых, посчитанное точно.
|
|
34
|
+
*
|
|
35
|
+
* Прямое `Math.round(amount / rate)` не подходит по двум независимым причинам:
|
|
36
|
+
* `17 / 0.272` — точно `62.5` → `63`, а в double `62.49999999999999` → `62` (§8 №18);
|
|
37
|
+
* `-5 / 2` — HALF_UP даёт `-3`, а `Math.round(-2.5)` даёт `-2` (§8 №10).
|
|
38
|
+
* Обе ошибки воспроизводят исходный дефект SL-13080 — расхождение витрины и страницы оплаты.
|
|
39
|
+
*
|
|
40
|
+
* `BigInt` недоступен (`tsconfig.json` → `target: "ES6"`), поэтому деление
|
|
41
|
+
* целочисленное длинное на обычных `number`.
|
|
42
|
+
*/
|
|
43
|
+
export function convertMoney(amount: number, rate: number): number {
|
|
44
|
+
if (!isFinite(amount) || !isFinite(rate) || rate <= 0) {
|
|
45
|
+
// Защитно: некорректный курс не роняет экран, показываем исходную сумму.
|
|
46
|
+
return amount
|
|
47
|
+
}
|
|
48
|
+
if (rate === 1) {
|
|
49
|
+
// Контракт §3.3: округления нет вообще.
|
|
50
|
+
return amount
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const negative = amount < 0
|
|
54
|
+
// A = |amount| * 100, R = rate * 1e10 → |amount| / rate === A * 1e8 / R
|
|
55
|
+
const a = Math.round(Math.abs(amount) * AMOUNT_SCALE)
|
|
56
|
+
const r = Math.round(rate * RATE_SCALE)
|
|
57
|
+
if (r <= 0) {
|
|
58
|
+
return amount
|
|
59
|
+
}
|
|
60
|
+
if (r > MAX_DIVISOR || !Number.isSafeInteger(a)) {
|
|
61
|
+
return degrade(amount, rate)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const value = divideHalfUp(a, DIVISION_ZEROS, r)
|
|
65
|
+
if (value === null) {
|
|
66
|
+
return degrade(amount, rate)
|
|
67
|
+
}
|
|
68
|
+
// `value !== 0` обязательно: `-0` в JS существует, а в Scala BigDecimal — нет,
|
|
69
|
+
// и formatMoney отрисовал бы его как «-0 ₽».
|
|
70
|
+
return negative && value !== 0 ? -value : value
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Запасной путь за пределами точного диапазона: считаем в double и округляем
|
|
75
|
+
* HALF_UP от нуля. Расхождение со Scala здесь возможно, поэтому громко сообщаем.
|
|
76
|
+
*/
|
|
77
|
+
function degrade(amount: number, rate: number): number {
|
|
78
|
+
// eslint-disable-next-line no-console
|
|
79
|
+
console.warn(
|
|
80
|
+
`[displayCurrency] курс ${rate} или сумма ${amount} вне точного диапазона; ` +
|
|
81
|
+
'результат может разойтись с расчётом на бэкенде'
|
|
82
|
+
)
|
|
83
|
+
const abs = Math.abs(amount) / rate
|
|
84
|
+
const rounded = Math.round(abs)
|
|
85
|
+
return amount < 0 && rounded !== 0 ? -rounded : rounded
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* `(dividend * 10^zeros) / divisor` с округлением HALF_UP.
|
|
90
|
+
* Возвращает `null`, если частное выходит за безопасные целые.
|
|
91
|
+
*/
|
|
92
|
+
function divideHalfUp(
|
|
93
|
+
dividend: number,
|
|
94
|
+
zeros: number,
|
|
95
|
+
divisor: number
|
|
96
|
+
): number | null {
|
|
97
|
+
const digits = String(dividend) + '0'.repeat(zeros)
|
|
98
|
+
let quotient = 0
|
|
99
|
+
let rem = 0
|
|
100
|
+
for (let i = 0; i < digits.length; i++) {
|
|
101
|
+
// Инвариант rem < divisor <= MAX_DIVISOR делает эту строку точной в double.
|
|
102
|
+
rem = rem * 10 + Number(digits[i])
|
|
103
|
+
const d = Math.floor(rem / divisor)
|
|
104
|
+
rem -= d * divisor
|
|
105
|
+
quotient = quotient * 10 + d
|
|
106
|
+
if (!Number.isSafeInteger(quotient)) {
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return 2 * rem >= divisor ? quotient + 1 : quotient
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Хвост списка валют показа — те, что рисуются мелким шрифтом рядом с основной суммой.
|
|
115
|
+
*
|
|
116
|
+
* `slice(1)` — первый элемент по контракту §3.2 п.6 всегда валюта Organization с `rate = 1`.
|
|
117
|
+
* Фильтр по `primary` — защита от расхождения `OrgProfile.currency` и deprecated
|
|
118
|
+
* `MasterProfileCommon.currency`, из которого корзина и витрина берут основную валюту сегодня.
|
|
119
|
+
*/
|
|
120
|
+
export function secondaryCurrencies(
|
|
121
|
+
list: ReadonlyArray<DisplayCurrencyRate> | null | undefined,
|
|
122
|
+
primary: string
|
|
123
|
+
): DisplayCurrencyRate[] {
|
|
124
|
+
return (list ?? [])
|
|
125
|
+
.slice(1)
|
|
126
|
+
.filter((n) => !!n && !!n.currency && n.currency !== primary && n.rate > 0)
|
|
127
|
+
}
|