@memori.ai/memori-react 7.4.1 โ†’ 7.4.2

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.
@@ -1,158 +1,4 @@
1
- import {
2
- Stats,
3
- GamificationLevel,
4
- EventLog,
5
- UsersLog,
6
- Memori,
7
- Invitation,
8
- } from '@memori.ai/memori-api-client/dist/types';
9
- import memoriApiClient from '@memori.ai/memori-api-client';
10
-
11
- const { deleteSession, getStatistics, initSession } = memoriApiClient();
12
-
13
- const conversionTable: { [key: string]: number } = {
14
- totalReceivers: 0.2,
15
- receiversWithMemories: 0.4,
16
- totalMemories: 0.2,
17
- publicMemories: 0.4,
18
- memoriesWithMedia: 0.4,
19
- totalQuestions: 0.2,
20
- publicQuestions: 0.3,
21
- questionsWithMoreThanOneAnswer: 0.4,
22
- totalStories: 0.2,
23
- publicStories: 0.4,
24
- storiesWithDate: 0.5,
25
- storiesWithPlace: 0.4,
26
- storiesWithDateAndPlace: 0.6,
27
- unansweredQuestions: 0,
28
- successfulCorrelations: 0,
29
- failedCorrelations: 0,
30
- };
31
-
32
- export const BADGES_MAP: Map<number, string> = new Map([
33
- [0, '๐Ÿ†•'],
34
- [10, '๐Ÿน'],
35
- [20, '๐Ÿ‡'],
36
- [30, '๐Ÿฐ'],
37
- [40, '๐Ÿ’ก'],
38
- [50, '๐Ÿ†'],
39
- [60, '๐Ÿ…'],
40
- [70, '๐Ÿฅ‰'],
41
- [80, '๐Ÿฅˆ'],
42
- [90, '๐Ÿฅ‡'],
43
- [100, '๐Ÿ”ฅ'],
44
- [110, '๐ŸŽ“'],
45
- [120, '๐Ÿค–'],
46
- [130, '๐Ÿ‘พ'],
47
- [150, '๐ŸŒ'],
48
- [180, '๐Ÿ’œ'],
49
- [200, '๐Ÿ’™'],
50
- [250, '๐Ÿงก'],
51
- [300, '๐Ÿ’š'],
52
- [350, '๐Ÿ’›'],
53
- [400, '๐Ÿ’–'],
54
- [450, '๐Ÿ’'],
55
- [500, '๐Ÿ–ค'],
56
- [550, 'โค๏ธ'],
57
- [600, '๐Ÿง '],
58
- ]);
59
-
60
- export const getBadge = (points: number): string => {
61
- const unlockedBadges = Array.from(BADGES_MAP.keys()).filter(
62
- (k: number) => k <= points
63
- );
64
-
65
- if (unlockedBadges.length > 0) {
66
- const lastBadge = unlockedBadges[unlockedBadges.length - 1];
67
- return BADGES_MAP.get(lastBadge) as string;
68
- }
69
-
70
- return '';
71
- };
72
-
73
- export const getNextBadge = (
74
- badge: string
75
- ): { points: number; badge: string } | undefined => {
76
- if (badge === '๐Ÿ†•') {
77
- return { points: 10, badge: '๐Ÿน' };
78
- }
79
- let nextBadgePoints;
80
- let nextBadge;
81
-
82
- let currentSeen = false;
83
- for (const [key, value] of BADGES_MAP) {
84
- if (currentSeen) {
85
- nextBadgePoints = key;
86
- nextBadge = value;
87
- break;
88
- }
89
-
90
- if (value === badge) {
91
- currentSeen = true;
92
- }
93
- }
94
-
95
- return nextBadge && nextBadgePoints
96
- ? {
97
- points: nextBadgePoints,
98
- badge: nextBadge,
99
- }
100
- : undefined;
101
- };
102
-
103
- export const getPointsForBadge = (badge: string): number => {
104
- let points = 0;
105
-
106
- for (const [key, value] of BADGES_MAP) {
107
- if (value === badge) {
108
- points = key;
109
- break;
110
- }
111
- }
112
-
113
- return points;
114
- };
115
-
116
- export const getPointsFromStats = (stats: Stats): number => {
117
- let points = 0;
118
- Object.entries(stats).forEach(stat => {
119
- if (stat[0] in conversionTable) {
120
- points += stat[1] * conversionTable[stat[0]];
121
- }
122
- });
123
- return Math.trunc(points);
124
- };
125
-
126
- export const getGamificationLevel = (points: number): GamificationLevel => {
127
- const roundedPoints = Math.round(points);
128
- const badge = getBadge(roundedPoints);
129
- const pointsForCurrentBadge = getPointsForBadge(badge);
130
- const nextBadge = getNextBadge(badge);
131
-
132
- return {
133
- points: roundedPoints,
134
- badge,
135
- pointsForCurrentBadge,
136
- nextBadge,
137
- };
138
- };
139
-
140
- export const getGamificationLevelByStats = (
141
- stats: Stats
142
- ): GamificationLevel => {
143
- const floatPoints = getPointsFromStats(stats);
144
- const points = Math.round(floatPoints);
145
- const badge = getBadge(points);
146
- const pointsForCurrentBadge = getPointsForBadge(badge);
147
- const nextBadge = getNextBadge(badge);
148
-
149
- return {
150
- points,
151
- badge,
152
- pointsForCurrentBadge,
153
- nextBadge,
154
- };
155
- };
1
+ import { EventLog, UsersLog } from '@memori.ai/memori-api-client/dist/types';
156
2
 
157
3
  const groupBy = <T>(array: T[], predicate: (v: T) => string) =>
158
4
  array.reduce((acc, value) => {
@@ -210,43 +56,3 @@ export const eventLogGroupUsersByDate = (eventLogs: EventLog[]): UsersLog[] => {
210
56
  });
211
57
  return userLogs;
212
58
  };
213
-
214
- export const getMemoriUnansQuestions = async (memori: Memori) => {
215
- if (memori.privacyType === 'SECRET' || !memori.isGiver) return 0;
216
- try {
217
- if (!memori.giverTag && !!memori.receivedInvitations?.length) {
218
- let giverInvitation = memori.receivedInvitations.find(
219
- (i: Invitation) => i.type === 'GIVER' && i.state === 'ACCEPTED'
220
- );
221
-
222
- if (giverInvitation) {
223
- memori.giverTag = giverInvitation.tag;
224
- memori.giverPIN = giverInvitation.pin;
225
- }
226
- }
227
-
228
- const { sessionID, currentState, ...response } = await initSession({
229
- memoriID: memori.engineMemoriID ?? '',
230
- password: memori.secretToken,
231
- tag: memori.giverTag,
232
- pin: memori.giverPIN,
233
- });
234
-
235
- if (response.resultCode !== 0 || !sessionID) {
236
- return 0;
237
- }
238
-
239
- const { statistics, ...resp } = await getStatistics(sessionID);
240
-
241
- await deleteSession(sessionID);
242
-
243
- if (statistics && resp.resultCode === 0) {
244
- return statistics.unansweredQuestions;
245
- }
246
-
247
- return 0;
248
- } catch (e) {
249
- console.log(e);
250
- return 0;
251
- }
252
- };
@@ -1,213 +0,0 @@
1
- import { EventLog, Stats } from '@memori.ai/memori-api-client/dist/types';
2
- import {
3
- getBadge,
4
- getNextBadge,
5
- getPointsForBadge,
6
- getPointsFromStats,
7
- getGamificationLevel,
8
- eventLogGroupUsersByDate,
9
- getGamificationLevelByStats,
10
- } from './statistics';
11
-
12
- export const stats: Stats = {
13
- totalReceivers: 2,
14
- receiversWithMemories: 1,
15
- totalMemories: 50,
16
- publicMemories: 30,
17
- memoriesWithMedia: 25,
18
- totalQuestions: 35,
19
- publicQuestions: 30,
20
- questionsWithMoreThanOneAnswer: 20,
21
- totalStories: 15,
22
- publicStories: 10,
23
- storiesWithDate: 5,
24
- storiesWithPlace: 3,
25
- storiesWithDateAndPlace: 2,
26
- unansweredQuestions: 10,
27
- successfulCorrelations: 5,
28
- failedCorrelations: 1,
29
- };
30
-
31
- export const eventLogs: EventLog[] = [
32
- {
33
- eventLogID: '28c9f4c8-d7d5-44c4-9f79-418944b03e82',
34
- eventType: 'MemoriOpened',
35
- ipAddress: '::ffff:10.11.12.14',
36
- memoriID: '1afe57c6-1b69-4a61-96ea-52bf7b8d158e',
37
- timestamp: '2022-04-15T15:00:27.534191',
38
- userAgent: 'RestSharp/106.13.0.0',
39
- },
40
- {
41
- eventLogID: '28c9f4c8-d7d5-44c4-9f79-418944b03e83',
42
- eventType: 'MemoriOpened',
43
- ipAddress: '::ffff:10.11.12.13',
44
- memoriID: '1afe57c6-1b69-4a61-96ea-52bf7b8d158e',
45
- timestamp: '2022-04-15T15:00:27.534191',
46
- userAgent: 'RestSharp/106.13.0.0',
47
- },
48
- {
49
- eventLogID: '28c9f4c8-d7d5-44c4-9f79-418944b03e84',
50
- eventType: 'MemoriClosed',
51
- ipAddress: '::ffff:10.11.12.13',
52
- memoriID: '1afe57c6-1b69-4a61-96ea-52bf7b8d158e',
53
- timestamp: '2022-04-15T15:10:27.534191',
54
- userAgent: 'RestSharp/106.13.0.0',
55
- },
56
- {
57
- eventLogID: '28c9f4c8-d7d5-44c4-9f79-418944b03e85',
58
- eventType: 'MemoriOpened',
59
- ipAddress: '::ffff:10.11.12.13',
60
- memoriID: '1afe57c6-1b69-4a61-96ea-52bf7b8d158e',
61
- timestamp: '2022-04-15T15:15:27.534191',
62
- userAgent: 'RestSharp/106.13.0.0',
63
- },
64
- {
65
- eventLogID: '28c9f4c8-d7d5-44c4-9f79-418944b03e86',
66
- eventType: 'MemoriClosed',
67
- ipAddress: '::ffff:10.11.12.13',
68
- memoriID: '1afe57c6-1b69-4a61-96ea-52bf7b8d158e',
69
- timestamp: '2022-04-15T15:20:27.534191',
70
- userAgent: 'RestSharp/106.13.0.0',
71
- },
72
- {
73
- eventLogID: '28c9f4c8-d7d5-44c4-9f79-418944b03e88',
74
- eventType: 'MemoriClosed',
75
- ipAddress: undefined,
76
- memoriID: '1afe57c6-1b69-4a61-96ea-52bf7b8d158e',
77
- timestamp: '2022-04-15T15:28:27.534191',
78
- userAgent: 'RestSharp/106.13.0.0',
79
- },
80
- ];
81
-
82
- describe('getBadge', () => {
83
- it('should return ๐Ÿ†• badge for 0 points', () => {
84
- expect(getBadge(0)).toBe('๐Ÿ†•');
85
- });
86
- it('should return ๐Ÿน for 10 points', () => {
87
- expect(getBadge(10)).toBe('๐Ÿน');
88
- });
89
- it('should return ๐Ÿฅ‡ for 99 points', () => {
90
- expect(getBadge(99)).toBe('๐Ÿฅ‡');
91
- });
92
- it('should return ๐Ÿฅ‡ for 99.9 points with round', () => {
93
- expect(getBadge(99.9)).toBe('๐Ÿฅ‡');
94
- });
95
- it('should return ๐Ÿ”ฅ for 100 points', () => {
96
- expect(getBadge(100)).toBe('๐Ÿ”ฅ');
97
- });
98
- it('should return ๐Ÿ”ฅ for 101 points', () => {
99
- expect(getBadge(101)).toBe('๐Ÿ”ฅ');
100
- });
101
- it('should return ๐ŸŒŽ for 150 points', () => {
102
- expect(getBadge(150)).toBe('๐ŸŒ');
103
- });
104
- it('should return ๐Ÿง  for 1000 points', () => {
105
- expect(getBadge(1000)).toBe('๐Ÿง ');
106
- });
107
- });
108
-
109
- describe('getNextBadge', () => {
110
- it('should return { points: 10, badge: "๐Ÿน" } for ๐Ÿ†• badge', () => {
111
- expect(getNextBadge('๐Ÿ†•')).toEqual({ points: 10, badge: '๐Ÿน' });
112
- });
113
- it('should return { points: 150, badge: "๐ŸŒ" } for ๐Ÿ‘พ', () => {
114
- expect(getNextBadge('๐Ÿ‘พ')).toEqual({ points: 150, badge: '๐ŸŒ' });
115
- });
116
- it('should return { points: 600, badge: "๐Ÿง " } for โค', () => {
117
- expect(getNextBadge('โค๏ธ')).toEqual({ points: 600, badge: '๐Ÿง ' });
118
- });
119
- });
120
-
121
- describe('getPointsForBadge', () => {
122
- it('should return 0 for "new" badge', () => {
123
- expect(getPointsForBadge('๐Ÿ†•')).toBe(0);
124
- });
125
- it('should return 10 for ๐Ÿน', () => {
126
- expect(getPointsForBadge('๐Ÿน')).toBe(10);
127
- });
128
- it('should return 150 for ๐ŸŒŽ', () => {
129
- expect(getPointsForBadge('๐ŸŒ')).toBe(150);
130
- });
131
- it('should return 600 for ๐Ÿง ', () => {
132
- expect(getPointsForBadge('๐Ÿง ')).toBe(600);
133
- });
134
- });
135
-
136
- describe('getPointsFromStats', () => {
137
- it('should return 0 for no stats', () => {
138
- expect(getPointsFromStats({} as Stats)).toBe(0);
139
- });
140
- it('should return x for stats', () => {
141
- expect(getPointsFromStats(stats)).toBe(68);
142
- });
143
- });
144
-
145
- describe('getGamificationLevel', () => {
146
- it('should return correct level for 0 points', () => {
147
- expect(getGamificationLevel(0)).toEqual({
148
- points: 0,
149
- badge: '๐Ÿ†•',
150
- pointsForCurrentBadge: 0,
151
- nextBadge: { points: 10, badge: '๐Ÿน' },
152
- });
153
- });
154
- it('should return correct level with 68 points', () => {
155
- expect(getGamificationLevel(68.34)).toEqual({
156
- points: 68,
157
- badge: '๐Ÿ…',
158
- pointsForCurrentBadge: 60,
159
- nextBadge: { points: 70, badge: '๐Ÿฅ‰' },
160
- });
161
- });
162
- it('should return correct level with 100 points', () => {
163
- expect(getGamificationLevel(100)).toEqual({
164
- points: 100,
165
- badge: '๐Ÿ”ฅ',
166
- pointsForCurrentBadge: 100,
167
- nextBadge: { points: 110, badge: '๐ŸŽ“' },
168
- });
169
- });
170
- it('should return correct level with 68 points and correct float round', () => {
171
- expect(getGamificationLevel(67.84)).toEqual({
172
- points: 68,
173
- badge: '๐Ÿ…',
174
- pointsForCurrentBadge: 60,
175
- nextBadge: { points: 70, badge: '๐Ÿฅ‰' },
176
- });
177
- });
178
- it('should return correct level with 99.9 points', () => {
179
- expect(getGamificationLevel(99.9)).toEqual({
180
- points: 100,
181
- badge: '๐Ÿ”ฅ',
182
- pointsForCurrentBadge: 100,
183
- nextBadge: { points: 110, badge: '๐ŸŽ“' },
184
- });
185
- });
186
- it('should return correct level with overflow 9999 points', () => {
187
- expect(getGamificationLevel(9999)).toEqual({
188
- points: 9999,
189
- badge: '๐Ÿง ',
190
- pointsForCurrentBadge: 600,
191
- nextBadge: undefined,
192
- });
193
- });
194
- });
195
-
196
- describe('getGamificationLevelByStats', () => {
197
- it('should return correct level for no stats', () => {
198
- expect(getGamificationLevelByStats({} as Stats)).toEqual({
199
- points: 0,
200
- badge: '๐Ÿ†•',
201
- pointsForCurrentBadge: 0,
202
- nextBadge: { points: 10, badge: '๐Ÿน' },
203
- });
204
- });
205
- it('should return correct level for stats', () => {
206
- expect(getGamificationLevelByStats(stats)).toEqual({
207
- points: 68,
208
- badge: '๐Ÿ…',
209
- pointsForCurrentBadge: 60,
210
- nextBadge: { points: 70, badge: '๐Ÿฅ‰' },
211
- });
212
- });
213
- });