@sportsbroker/firebase-structure 2.1.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.
Files changed (41) hide show
  1. package/README.md +200 -0
  2. package/database-rules.json +56 -0
  3. package/index.d.ts +2 -0
  4. package/models/MAIN_CHANGES +21 -0
  5. package/models/_index.d.ts +11 -0
  6. package/models/config/_index.d.ts +3 -0
  7. package/models/details/_index.d.ts +13 -0
  8. package/models/details/matches/_index.d.ts +15 -0
  9. package/models/details/matches/commentaries.d.ts +21 -0
  10. package/models/details/matches/events.d.ts +62 -0
  11. package/models/details/matches/overviews.d.ts +25 -0
  12. package/models/details/matches/scores.d.ts +20 -0
  13. package/models/details/matches/shares.d.ts +14 -0
  14. package/models/details/matches/stats.d.ts +29 -0
  15. package/models/details/matches/teams.d.ts +17 -0
  16. package/models/details/matches/venueOverviews.d.ts +14 -0
  17. package/models/details/ranking/_index.d.ts +8 -0
  18. package/models/details/tables/_index.d.ts +9 -0
  19. package/models/details/tables/groups.d.ts +17 -0
  20. package/models/details/tables/ranking.d.ts +31 -0
  21. package/models/details/tables/teams.d.ts +13 -0
  22. package/models/details/teams/_index.d.ts +7 -0
  23. package/models/details/teams/overviews.d.ts +17 -0
  24. package/models/details/teams/shares.d.ts +22 -0
  25. package/models/details/users/_index.d.ts +15 -0
  26. package/models/details/users/notifications.d.ts +33 -0
  27. package/models/details/users/orders.d.ts +36 -0
  28. package/models/details/users/overviews.d.ts +39 -0
  29. package/models/details/users/ownedShares.d.ts +19 -0
  30. package/models/details/users/wallet.d.ts +13 -0
  31. package/models/details/users/watchlist.d.ts +9 -0
  32. package/models/global.d.ts +44 -0
  33. package/models/queries/_index.d.ts +9 -0
  34. package/models/queries/matches.d.ts +40 -0
  35. package/models/queries/orders.d.ts +27 -0
  36. package/models/queries/shares.d.ts +17 -0
  37. package/models/snapshots/_index.d.ts +7 -0
  38. package/models/snapshots/matchOverviews.d.ts +46 -0
  39. package/models/snapshots/tableSummary.d.ts +47 -0
  40. package/package.json +17 -0
  41. package/screen-queries +28 -0
package/README.md ADDED
@@ -0,0 +1,200 @@
1
+ # Firebase structure schema for SportsBroker
2
+
3
+ Repository is documentation for data structures used in firebase database for SportsBroker. It's also communication channel for any change requests to that model in order to notify every platform (backend, web, Android and iOS) about changes.
4
+
5
+ **Due to that, every pull request should be approved at least by one person from each platform, which, if there is such need, should create appropriate ticket in platform's Jira, and link the pull request to the ticket.**
6
+
7
+ Note that Firebase Relational Database shouldn't be a source of true for project data. It's not structured to keep good data structure in meaning of it's storage. It's designed to be access point for data for frontend clients, so all structures are built in way, which let build screens easily, having regard to firebase queries constraints.
8
+
9
+ ## Formatting conventions
10
+
11
+ Structures are written in TypeScript type system which should be understandable for every developer.
12
+
13
+ There is one exception - TypeScript doesn't allow to have dynamic object key other than number or string,
14
+ even if key is string wrapped in other type. However convention was kept to reference different types, mostly IDs,
15
+ to distinguish MongoID, stats.com ID etc.
16
+
17
+ Typescript types reference:
18
+
19
+ https://www.typescriptlang.org/docs/handbook/basic-types.html
20
+
21
+ https://www.typescriptlang.org/docs/handbook/advanced-types.html
22
+
23
+ ## Firebase specific traits
24
+
25
+ Before starting working with firebase structure it's necessary to get familiar with Firebase Relatime Database documentation https://firebase.google.com/docs/database/
26
+
27
+ Although to understand some solutions introduced in SportsBroker structure there is necessity to learn about some additional firebase traits, tricks and problems developer needs to handle using Firebase realtime database.
28
+
29
+ This section contains the most important facts from perspective of structure creation process.
30
+
31
+ ### Ids
32
+
33
+ Id's of objects are always held as it's keys in order to let build better queries for such objects data. Id's held as values should be only references to another objects defined in another collections.
34
+
35
+ ### Queries
36
+
37
+ #### Path
38
+ In order to build Firebase query it's necessary to know full path to retrieved object. There are no path queries which allow to find some node without knowing names of it's ancestor. So for such structure:
39
+
40
+ ```javascript
41
+ {
42
+ "sportId": {
43
+ "matches": {
44
+ "matchId": {
45
+ "date": "01.01.2000",
46
+ "someHeavyObject": {}
47
+ }
48
+ },
49
+ "teams": {
50
+ "teamId" {
51
+ "name": "Arsenal",
52
+ "someHeavyObject": {}
53
+ }
54
+ }
55
+ },
56
+ "sport2Id": {
57
+ "matches": {
58
+ "matchId": {
59
+ "date": "01.01.2000",
60
+ "someHeavyObject": {}
61
+ }
62
+ },
63
+ "players": {
64
+ "playerId" {
65
+ "name": "Novak",
66
+ "someHeavyObject": {}
67
+ }
68
+ }
69
+ }
70
+ }
71
+ ```
72
+
73
+ If there is necessity to retrieve object *matches* it's necessary to know id of its' parent node (*sportId*) and use it with full path `sportId/matches`. There is no sensible possibility though to retrieve matches from both *sportId* and *sport2Id* (the only way to do it is to retrieve full object with *teams*, *players* and all other siblings there could be)
74
+
75
+ ##### Finding path nodes
76
+
77
+ Having just such structure as above it's impossible to get *sportId* and *sport2Id* without loading whole objects and getting their keys.
78
+
79
+ It's why to retrieve such key there is necessity to build additional object where, *sportId* will contain only lightweight sorting and querying data, such as:
80
+
81
+ ```javascript
82
+ {
83
+ "sports": {
84
+ "sportId": {
85
+ "pos": 0,
86
+ "name": "football"
87
+ },
88
+ "someID2": {
89
+ "pos": 1,
90
+ "name": "tennis"
91
+ }
92
+ }
93
+ }
94
+ ```
95
+
96
+ Such list might be used either to build UI for choosing some id, or to simply load all ids and use them to create next queries for another data.
97
+
98
+ #### Querying lists
99
+
100
+ Querying objects in firebase has a lot of constraints in comparision to relational databases queries or even other object oriented databases.
101
+
102
+ Building query starts from creating path giving name of every child on it.
103
+
104
+ The node which is last on the path is the node which will be returned once starting Value subscription, and it's first level descendants with Child subscription.
105
+
106
+ Starting from that node we choose which child, or key will be used for sorting and constraining a query (**it has to be the same node!**). Sorting is made only in descendant order. After ordering it is possibility to set starting and ending values of chosen child, and limit of items which will be observed.
107
+
108
+ There is also possibility to use equals method on the same child, what is translated into setting start and end constraints into given value.
109
+
110
+ The most important thing is that **each constraint can be set only once to single query**. So there is no possibility to sort list with 2 children, or sort it with one and do constraint with another.
111
+
112
+ There is also problem with making descendant sorting, which firebase doesn't support. The solution for that is to add elements to the list in reverse order. Although there is also problem that with **ChildListener** children will be added in ascending order, so if adding items to the view won't be delayed animation will behave in unexpected way - adding items to the top of the screen. For such case the only solution is to create additional **ValueListener** with **limitToLast(1)** which will return last key, and stop running animation once all data is loaded with **ChildListener**. Although it can't be applied to all kinds of paging, and if those types are not possible then there is need to load all data and do such reversing there, or just do sorting on client side after loading all data.
113
+
114
+ ##### Queries helpers
115
+
116
+ Because of lack of possibility to put multiple constraints and sorts on one query there are some tricks to do it other way:
117
+
118
+ ###### Multiple constraints helpers
119
+
120
+ In order to use some additional constraints there is need to create additional objects which use constraints as path nodes and have only sorting parameters as final nodes. Such objects lets to do queries for items ids which let to build next queries for actual data.
121
+
122
+ ###### Multiple sorting
123
+
124
+ Unfortunately there is no way to do 2 sorts in firebase. The only solution is to load everything and sort it on client side. If our intention isn't loading everything with such queries, or do paging, then it's necessary to create some lightweight helper object just with data needed for such sorting and setting constraints, and then build additional queries for full data. Note that there has to be Value listener used.
125
+
126
+ #### Queries paging
127
+
128
+ There are several ways it is possible to do paging in firebase. The particular paging method depends on type of data, ways of sorting and constraining, mutability of data. Some of them also require to use **ChildListeners**, but for some other it is possible (and worth) using **ValueListeners**.
129
+
130
+ ##### By page constraints
131
+
132
+ If query is sorted by some value which lets setting boundaries for page of data, and such pages won't have too big gaps of no data pages, then it's possible to define query to which there will be **startAt** and **endAt** constraints added creating query for each page.
133
+
134
+ As such constraints there might be used date, where one page might be assumed to be 1 week. The problem is that there is need to establish then when all data is already loaded and paging should stop. There might be set up limit of empty pages (which should be checked with ValueListener limited to 1 item), ValueListener might check last item for a whole list and data loading might continue until last item is loaded. Also in firebase there might be added counter for amount of items in a list which would work as max constraint.
135
+
136
+ ##### By Keys
137
+
138
+ If there is no particular order in which data is needed there is possibility to use **orderByKey** and set limit to required page size + 1. Last item on a size shouldn't be added to the list, but should be used to set it's key as next page **startAt** parameter.
139
+
140
+ ###### With an additional list
141
+
142
+ Another way is to create in firebase additional collection just with keys and sorting data inside. Costs of loading such lightweight data should allow to load all items and do paging just by running additional queries for full data while needed.
143
+
144
+ ### Reference lists
145
+
146
+ In Firebase there is convention for lists that don't have any sorting order, or constraints and are just referencing to objects defined in bigger dictionary objects. Such lists contain just ids as keys and `true` values assigned to them. Such objects can be easily used to build queries for actual data which doesn't have to be doubled.
147
+
148
+ ## Structure assumptions
149
+
150
+ All models should be constructed in a way which forces developers to subscribe into the smallest possible pieces of data. It's why first nodes are meant to build descriptive path to collections of small pieces of actual data.
151
+
152
+ Structure is divided into sections: **details**, **queries**, **snapshots**, and **backendHelpers**. Some of them have also additional subsection, although all of them are introduced only to increase readability of structure.
153
+
154
+ Every section have on some level descendants which are list of ids of objects. Subscription should always happen on the last id node in a path in order to retrieve singular object, or on last but one for retrieving lists of data.
155
+
156
+ Most of sections contain object **overviews**. It's goal is to gather data defining its' first ancestor (such as for example *name*).
157
+
158
+ Each of sections stores slightly different types of data:
159
+
160
+ ### details
161
+
162
+ It is sections which is main source of truth for data. If anything can be found in another sections should be also possible to retrieve it from **details**. Nevertheless it's possible to double data inside of **details** section - in cases when doubling simplifies client logic and doesn't make maintaining doubling difficult for a backend.
163
+
164
+ All data in details are divided into logical types sections such as **matches**, **teams**, **users** and others which increases readability of structure.
165
+
166
+ If some data is duplicated between sections then it shouldn't be updated the same way. For example **teams** and **shares** subsections in **matches** are not updated once match is finished, but in section **teams** they should be always actual.
167
+
168
+ **not_in_use_yet** section is added in order to not misguide as it's not used yet, and prepared mainly to remind about some structure which needs to be handled one app will grow.
169
+
170
+ #### matches
171
+
172
+ Contains data necessary to display detailed data of a singular match. It assumes that once client app enters such detailed match screen it has to already have id of a match.
173
+
174
+ Important thing to notice is that once match is over no data is going to change for such match. It's why data for teams shares, venue are duplicated here (even if venue or team name changes in the future in match screen should be displayed old one). It helps to simplify logic of finding proper names depending on dates.
175
+
176
+ Worth to notice that shares in this model relate to shares from particular game instead of actual ones. The only actual value should be displayed on buy/sell buttons and have to be retrieved from teams collection.
177
+
178
+ #### teams
179
+
180
+ Contains data related to teams which are active in SportsBroker - actual in given season or tournament and available for buying their shares.
181
+
182
+ #### users
183
+
184
+ Contains all data which is assigned to users. In order to retrieve them client app needs to authenticate user through FirebaseAuthentication and use userId to retrieve data. Rules in firebase have to forbid reading data by unauthorized users, and data of other users.
185
+
186
+ ### snapshots
187
+
188
+ Section for objects which are needed to create lists of data containing data combined with a lot of various models in **details** section. Snapshots should be only used for creating UI lists which let to select item for opening detailed screen, for which data should be taken from **details** section with usage of snapshot id.
189
+
190
+ Ids should be the same both in snapshot and in models from which it takes data. Any extra data should be relatively static and might be used in details screen to avoid additional unnecessary requests.
191
+
192
+ It shouldn't also contain data which would cause too big effort for backend to update it in all items.
193
+
194
+ ### queries
195
+
196
+ Section for objects which are helpers (described in [Queries helpers](#queries-helpers)). In cases when in either **details** or in **snapshot** is some list of objects, for which there is a need to set more then one constraints, or do some other complex operations before loading whole object data, then such small helper objects, which contains constraints in path instead of values, and only sorting data as final children, should be placed in **queries** section.
197
+
198
+ They should be only used to create ordered queries for retrieving id's of objects which are then used to get proper data which is about to be displayed.
199
+
200
+ In queries section besides logical types sections level there is introduced also some level of sentence building words (such as `queries/matches/of/teams` for matches played by some teams which need to be constrained by `teamId` and sorted by `startDate`) in order to increase query purpose readability.
@@ -0,0 +1,56 @@
1
+ {
2
+ "rules": {
3
+ ".read": false,
4
+ ".write": false,
5
+ "queries": {
6
+ ".read": true
7
+ },
8
+ "snapshots": {
9
+ ".read": true
10
+ },
11
+ "details": {
12
+ "matches": {
13
+ ".read": true
14
+ },
15
+ "ranking": {
16
+ ".read": true
17
+ },
18
+ "seasons": {
19
+ ".read": true
20
+ },
21
+ "tables": {
22
+ ".read": true
23
+ },
24
+ "teams": {
25
+ ".read": true
26
+ },
27
+ "users": {
28
+ "notifications": {
29
+ "$uid": {
30
+ ".read": "$uid === auth.uid"
31
+ }
32
+ },
33
+ "orders": {
34
+ "$uid": {
35
+ ".read": "$uid === auth.uid"
36
+ }
37
+ },
38
+ "overviews": {
39
+ "$uid": {
40
+ ".read": "$uid === auth.uid"
41
+ }
42
+ },
43
+ "ownedShares": {
44
+ "$uid": {
45
+ ".read": "$uid === auth.uid"
46
+ }
47
+ },
48
+ "wallet": {
49
+ "$uid": {
50
+ ".read": "$uid === auth.uid"
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./models/global";
2
+ export * from "./models/_index";
@@ -0,0 +1,21 @@
1
+ originalStartDate renamed to startDate and startDate renamed to realStartDate, because sheduled date is main date which is used, and making it's name with some kind of prefix makes it easier to use it wrongly
2
+ Email added to user overview
3
+
4
+ users, moved to details node and renamed to users->overviews
5
+ Users watchlist removed from user item and created separate list for that
6
+ usersOrders, userShares, userWallet and rank moved to users
7
+
8
+ Matches restructured in matches node into overviews, teams, shares, scores, venueOverviews + moved there lineups, commentaries, events, photos and stats
9
+
10
+ countries, leagues, seasons, teams, venues moved to details node and restructured to keep same data structure
11
+ teamShares, moved to activeTeams
12
+
13
+ participants completly removed as they need to be built from scratch once we'll know what we need
14
+
15
+ teamMatches, teamOpponentMatches and seasonMatches moved to queries,
16
+
17
+ matchOverviews moved to snapshots
18
+
19
+ shareValueEvents completly removed as it's now counted on backend
20
+
21
+ name added to shares in "teams" and ownedShares to allow alphabetic sorting
@@ -0,0 +1,11 @@
1
+ import { Details } from "./details/_index";
2
+ import { Queries } from "./queries/_index";
3
+ import { Snapshots } from "./snapshots/_index";
4
+ import { Config } from "./config/_index";
5
+
6
+ export interface Firebase {
7
+ details: Details;
8
+ queries: Queries;
9
+ snapshots: Snapshots;
10
+ config: Config;
11
+ }
@@ -0,0 +1,3 @@
1
+ export interface Config {
2
+ statusCheckRequired: Boolean;
3
+ }
@@ -0,0 +1,13 @@
1
+ import { Users } from "./users/_index";
2
+ import { Teams } from "./teams/_index";
3
+ import { Matches } from "./matches/_index";
4
+ import { Tables } from "./tables/_index";
5
+ import { Ranking } from "./ranking/_index";
6
+
7
+ export interface Details {
8
+ users: Users;
9
+ teams: Teams;
10
+ matches: Matches;
11
+ tables: Tables;
12
+ ranking: Ranking;
13
+ }
@@ -0,0 +1,15 @@
1
+ import { Events } from "./events";
2
+ import { Overviews } from "./overviews";
3
+ import { Scores } from "./scores";
4
+ import { Shares } from "./shares";
5
+ import { Stats } from "./stats";
6
+ import { Teams } from "./teams";
7
+
8
+ export interface Matches {
9
+ events: Events;
10
+ overviews: Overviews;
11
+ scores: Scores;
12
+ shares: Shares;
13
+ stats: Stats;
14
+ teams: Teams;
15
+ }
@@ -0,0 +1,21 @@
1
+ import { MatchPeriod } from "../../global";
2
+
3
+ /**
4
+ * Not implemented yet
5
+ */
6
+ export interface Commentaries {
7
+ [sportID: string]: {
8
+ [matchID: string]: {
9
+ [commentaryID: string]: {
10
+ period: MatchPeriod;
11
+ headline: string;
12
+ text: string;
13
+ time: {
14
+ minutes: number;
15
+ seconds: number;
16
+ additionalMinutes: number;
17
+ };
18
+ };
19
+ };
20
+ };
21
+ }
@@ -0,0 +1,62 @@
1
+ import { TeamID, TeamType } from "../../global";
2
+
3
+ type EventType = "YELLOW_CARD" | "RED_CARD" | "GOAL" | "SUBSTITUTION";
4
+
5
+ type PlayerID = string;
6
+
7
+ interface EventTime {
8
+ minutes: number;
9
+ seconds: number;
10
+ additionalMinutes: number;
11
+ }
12
+
13
+ interface BaseEvent {
14
+ type: EventType;
15
+ time: EventTime;
16
+ teamType: TeamType;
17
+ teamId: TeamID;
18
+ }
19
+
20
+ interface RedCardEvent extends BaseEvent {
21
+ type: "RED_CARD";
22
+ isSecondYellow?: boolean;
23
+ playerId?: PlayerID;
24
+ playerName: string;
25
+ }
26
+
27
+ interface YellowCardEvent extends BaseEvent {
28
+ type: "YELLOW_CARD";
29
+ playerId?: PlayerID;
30
+ playerName: string;
31
+ }
32
+
33
+ interface GoalEvent extends BaseEvent {
34
+ type: "GOAL";
35
+ assistingPlayerId?: PlayerID;
36
+ assistingPlayerName: string;
37
+ goalNumber: number;
38
+ penalty?: boolean;
39
+ ownGoal?: boolean;
40
+ playerId?: PlayerID;
41
+ playerName: string;
42
+ }
43
+
44
+ interface SubstitutionEvent extends BaseEvent {
45
+ type: "SUBSTITUTION";
46
+ playerInId?: PlayerID;
47
+ playerInName: string;
48
+ playerOutId?: PlayerID;
49
+ playerOutName: string;
50
+ }
51
+
52
+ export interface Events {
53
+ [sportID: string]: {
54
+ [matchID: string]: {
55
+ [eventID: string]:
56
+ | YellowCardEvent
57
+ | RedCardEvent
58
+ | GoalEvent
59
+ | SubstitutionEvent;
60
+ };
61
+ };
62
+ }
@@ -0,0 +1,25 @@
1
+ import { Date, MatchPeriod, MatchStatus } from "../../global";
2
+
3
+ export interface MatchTime {
4
+ minutes: number;
5
+ additionalMinutes: number;
6
+ }
7
+
8
+ export interface EventStatus {
9
+ matchStatus: MatchStatus;
10
+ period: MatchPeriod;
11
+ isActive: boolean;
12
+ matchTime?: MatchTime;
13
+ }
14
+
15
+ export interface Overviews {
16
+ [sportId: string]: {
17
+ [matchId: string]: {
18
+ startDate: Date;
19
+ roundNb: number;
20
+ originalRoundNb: number;
21
+ isTba: boolean;
22
+ eventStatus: EventStatus;
23
+ };
24
+ };
25
+ }
@@ -0,0 +1,20 @@
1
+ export interface Score {
2
+ home: number;
3
+ away: number;
4
+ }
5
+
6
+ export interface Scores {
7
+ [sportID: string]: {
8
+ [matchID: string]: {
9
+ home: number;
10
+ away: number;
11
+ periods: {
12
+ firstHalf: Score;
13
+ secondHalf?: Score;
14
+ firstOvertime?: Score;
15
+ secondOvertime?: Score;
16
+ };
17
+ shootoutPenalties?: Score;
18
+ };
19
+ };
20
+ }
@@ -0,0 +1,14 @@
1
+ export interface MatchShares {
2
+ price: number;
3
+ openingPrice: number;
4
+ priceChangePercentage: number;
5
+ }
6
+
7
+ export interface Shares {
8
+ [sportID: string]: {
9
+ [matchID: string]: {
10
+ home: MatchShares;
11
+ away: MatchShares;
12
+ };
13
+ };
14
+ }
@@ -0,0 +1,29 @@
1
+ export type StatsType =
2
+ | "possession"
3
+ | "shotsOnTarget"
4
+ | "clearances"
5
+ | "corners"
6
+ | "offsides"
7
+ | "passes"
8
+ | "penalties"
9
+ | "shots"
10
+ | "tackles"
11
+ | "touches"
12
+ | "yellowCards"
13
+ | "redCards";
14
+
15
+ interface TeamsStats {
16
+ home: number;
17
+ away: number;
18
+ }
19
+
20
+ export interface Stats {
21
+ [sportID: string]: {
22
+ [matchID: string]: {
23
+ /**
24
+ * possible values: @see StatsType
25
+ */
26
+ [statsType: string]: TeamsStats;
27
+ };
28
+ };
29
+ }
@@ -0,0 +1,17 @@
1
+ import { Image, TeamID } from "../../global";
2
+
3
+ export interface MatchTeam {
4
+ id: TeamID;
5
+ name: string;
6
+ abbreviation: string;
7
+ logo?: Image;
8
+ }
9
+
10
+ export interface Teams {
11
+ [sportID: string]: {
12
+ [matchID: string]: {
13
+ home: MatchTeam;
14
+ away: MatchTeam;
15
+ };
16
+ };
17
+ }
@@ -0,0 +1,14 @@
1
+ export type VenueID = string;
2
+
3
+ /**
4
+ * Not implemented yet
5
+ */
6
+ export interface VenueOverviews {
7
+ [sportID: string]: {
8
+ [matchID: string]: {
9
+ id: VenueID;
10
+ name: string;
11
+ city: string;
12
+ };
13
+ };
14
+ }
@@ -0,0 +1,8 @@
1
+ export interface Ranking {
2
+ [userId: string]: {
3
+ change: number;
4
+ equity: number;
5
+ position: number;
6
+ username: string;
7
+ };
8
+ }
@@ -0,0 +1,9 @@
1
+ import { Groups } from "./groups";
2
+ import { Ranking } from "./ranking";
3
+ import { Teams } from "./teams";
4
+
5
+ export interface Tables {
6
+ groups: Groups;
7
+ ranking: Ranking;
8
+ teams: Teams;
9
+ }
@@ -0,0 +1,17 @@
1
+ import { GroupID, SeasonID, SportID } from "../../global";
2
+
3
+ /**
4
+ * sportID: SportID
5
+ * seasonID: SeasonID
6
+ * groupID: GroupID
7
+ */
8
+ export interface Groups {
9
+ [sportID: string]: {
10
+ [seasonID: string]: {
11
+ [groupID: string]: {
12
+ name: string;
13
+ groupNb: string;
14
+ };
15
+ };
16
+ };
17
+ }
@@ -0,0 +1,31 @@
1
+ interface TeamRanking {
2
+ position: number;
3
+ positionChange: number;
4
+ points: number;
5
+ played: number;
6
+ won: number;
7
+ draw: number;
8
+ lost: number;
9
+ goalsDifference: number;
10
+ goalsFor: number;
11
+ goalsAgainst: number;
12
+ }
13
+
14
+ export interface Ranking {
15
+ groups: {
16
+ [sportID: string]: {
17
+ [seasonID: string]: {
18
+ [groupID: string]: {
19
+ [teamID: string]: TeamRanking;
20
+ };
21
+ };
22
+ };
23
+ };
24
+ leagues: {
25
+ [sportID: string]: {
26
+ [seasonID: string]: {
27
+ [teamID: string]: TeamRanking;
28
+ };
29
+ };
30
+ };
31
+ }
@@ -0,0 +1,13 @@
1
+ import { Image } from "../../global";
2
+
3
+ export interface Teams {
4
+ [sportID: string]: {
5
+ [seasonID: string]: {
6
+ [teamID: string]: {
7
+ name: string;
8
+ abbreviation: string;
9
+ logo?: Image;
10
+ };
11
+ };
12
+ };
13
+ }
@@ -0,0 +1,7 @@
1
+ import { Overviews } from "./overviews";
2
+ import { Shares } from "./shares";
3
+
4
+ export interface Teams {
5
+ overviews: Overviews;
6
+ shares: Shares;
7
+ }
@@ -0,0 +1,17 @@
1
+ import { Image, TeamID } from "../../global";
2
+
3
+ /**
4
+ * sportId: SportID
5
+ * teamId: TeamID
6
+ */
7
+ export interface Overviews {
8
+ [sportId: string]: {
9
+ [teamId: string]: {
10
+ name: string;
11
+ abbreviation: string;
12
+ id: TeamID;
13
+ logo?: Image;
14
+ isCurrentlyPlaying: Boolean;
15
+ };
16
+ };
17
+ }
@@ -0,0 +1,22 @@
1
+ import { TeamID } from "../../global";
2
+
3
+ interface MarketIndicators {
4
+ weeklyHigh: number;
5
+ weeklyLow: number;
6
+ weeklyVolume: number;
7
+ capacity: number;
8
+ }
9
+
10
+ export interface Shares {
11
+ [sportId: string]: {
12
+ [teamId: string]: {
13
+ price: number;
14
+ openingPrice: number;
15
+ priceChangePercentage: number;
16
+ buyPrice: number;
17
+ sellPrice: number;
18
+ id: TeamID;
19
+ market: MarketIndicators;
20
+ };
21
+ };
22
+ }
@@ -0,0 +1,15 @@
1
+ import { Orders } from "./orders";
2
+ import { Overviews } from "./overviews";
3
+ import { Wallet } from "./wallet";
4
+ import { Watchlist } from "./watchlist";
5
+ import { Notifications } from "./notifications";
6
+ import { OwnedShares } from "./ownedShares";
7
+
8
+ export interface Users {
9
+ orders: Orders;
10
+ overviews: Overviews;
11
+ wallet: Wallet;
12
+ watchlist: Watchlist;
13
+ notifications: Notifications;
14
+ ownedShares: OwnedShares;
15
+ }
@@ -0,0 +1,33 @@
1
+ import { UserID, Language } from "../../global";
2
+
3
+ export type NotificationID = string;
4
+
5
+ export type NotificationType =
6
+ | "TRANSACTION_BUY"
7
+ | "TRANSACTION_SELL"
8
+ | "TEAM_ELIMINATED"
9
+ | "KICK_OFF";
10
+
11
+ /**
12
+ * notificationId: NotificationID
13
+ * language: Language
14
+ * userId: UserID
15
+ */
16
+ export interface Notifications {
17
+ [userId: string]: {
18
+ unreadCount: number;
19
+ [language: string]: {
20
+ entries: {
21
+ [notificationId: string]: {
22
+ type: NotificationType;
23
+ title: string;
24
+ description?: string;
25
+ shown: boolean;
26
+ createdAt: Date;
27
+ updatedAt: Date;
28
+ id: NotificationID;
29
+ };
30
+ };
31
+ };
32
+ };
33
+ }
@@ -0,0 +1,36 @@
1
+ import { SportID, TeamID } from "../../global";
2
+
3
+ export type OrderActionType = "BUY" | "SELL";
4
+
5
+ export type OrderTransactionType = "MARKET" | "LIMIT";
6
+
7
+ export type OrderStatus = "ACTIVE" | "COMPLETED" | "ABORTED" | "CANCELLED";
8
+
9
+ type OrderID = string;
10
+
11
+ /**
12
+ * userID: UserID
13
+ * orderID: OrderID
14
+ */
15
+ export interface Orders {
16
+ [userID: string]: {
17
+ [orderID: string]: {
18
+ action: OrderActionType;
19
+ allOrNothing: boolean;
20
+ amount: number;
21
+ createdAt: Date;
22
+ limit?: number;
23
+ processedAmount: number;
24
+ processedMoney: number;
25
+ processedFee: number;
26
+ processedTotalMoney: number; // processedMoney + processedFee
27
+ sportId: SportID;
28
+ teamId: TeamID;
29
+ transactionFee: number;
30
+ type: OrderTransactionType;
31
+ updatedAt: Date;
32
+ status: OrderStatus;
33
+ id: OrderID;
34
+ };
35
+ };
36
+ }
@@ -0,0 +1,39 @@
1
+ import { UserID } from "../../global";
2
+
3
+ /**
4
+ * This stateful structure is required because of asynchronous matter of id verification.
5
+ * The values might change during integration with 3rd party provider.
6
+ */
7
+ type IDVerification = "SUBMITTED" | "VERIFIED" | "REJECTED";
8
+
9
+ /**
10
+ * userID: UserID
11
+ */
12
+ export interface Overviews {
13
+ [userID: string]: {
14
+ username: string;
15
+ email: string;
16
+ rank: number;
17
+ id: UserID;
18
+ status: {
19
+ emailVerified: boolean;
20
+ idVerification: IDVerification | null;
21
+ phoneVerified: boolean;
22
+ banned: boolean;
23
+ deactivated: boolean;
24
+ };
25
+ profile?: {
26
+ firstName?: string;
27
+ lastName?: string;
28
+ birthdate?: string;
29
+ };
30
+ address?: {
31
+ addressLine1?: string;
32
+ addressLine2?: string;
33
+ county?: string;
34
+ city?: string;
35
+ postalCode?: string;
36
+ country?: string;
37
+ };
38
+ };
39
+ }
@@ -0,0 +1,19 @@
1
+ import { SportID } from "../../global";
2
+
3
+ /**
4
+ * teamID: TeamID
5
+ * userID: userID
6
+ */
7
+ export interface OwnedShares {
8
+ [userID: string]: {
9
+ [teamID: string]: {
10
+ sportId: SportID;
11
+ amount: number;
12
+ availableAmount: number;
13
+ value: number;
14
+ profit: number;
15
+ invested: number;
16
+ averagePricePerShare: number;
17
+ };
18
+ };
19
+ }
@@ -0,0 +1,13 @@
1
+ import { UserID } from "../../global";
2
+
3
+ /**
4
+ * userID: UserID
5
+ */
6
+ export interface Wallet {
7
+ [userId: string]: {
8
+ available: number;
9
+ invested: number;
10
+ profit: number;
11
+ equity: number;
12
+ };
13
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * userId: UserID
3
+ * teamID: TeamID
4
+ */
5
+ export interface Watchlist {
6
+ [userId: string]: {
7
+ [teamID: string]: boolean;
8
+ };
9
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 24-char ID
3
+ */
4
+ export type MongoID = string;
5
+
6
+ export type UserID = MongoID;
7
+
8
+ export type TeamID = MongoID;
9
+
10
+ /**
11
+ * @type ISO Date
12
+ */
13
+ export type Date = string;
14
+
15
+ /**
16
+ * Currently support only soccer
17
+ */
18
+ export type SportID = "soccer" | string;
19
+
20
+ export type Language = "en" | "de" | string;
21
+
22
+ export type MatchID = MongoID;
23
+
24
+ export type SeasonID = string;
25
+
26
+ export type GroupID = string;
27
+
28
+ export interface Image {
29
+ default: string;
30
+ }
31
+
32
+ export type TeamType = "home" | "away";
33
+
34
+ export type MatchPeriod =
35
+ | "FIRST_HALF"
36
+ | "SECOND_HALF"
37
+ | "FIRST_OVERTIME_HALF"
38
+ | "SECOND_OVERTIME_HALF"
39
+ | "SHOOTOUT";
40
+
41
+ /**
42
+ * PENDING means that match hasn't started yet
43
+ */
44
+ export type MatchStatus = "PENDING" | "STARTED" | "FINISHED";
@@ -0,0 +1,9 @@
1
+ import { MatchesQueries } from "./matches";
2
+ import { SharesQueries } from "./shares";
3
+ import { OrdersQueries } from "./orders";
4
+
5
+ export interface Queries {
6
+ matches: MatchesQueries;
7
+ shares: SharesQueries;
8
+ orders: OrdersQueries;
9
+ }
@@ -0,0 +1,40 @@
1
+ import { Date, MatchStatus } from "../global";
2
+
3
+ interface MatchQueryData {
4
+ startDate: Date; // "Official", planned time of start
5
+ realStartDate: Date; // Real start time, f.e match was delayed
6
+ roundNb: number;
7
+ matchStatus: MatchStatus;
8
+ }
9
+
10
+ interface Matches {
11
+ in: {
12
+ seasons: {
13
+ [sportID: string]: {
14
+ [seasonID: string]: {
15
+ [matchID: string]: MatchQueryData;
16
+ };
17
+ };
18
+ };
19
+ };
20
+ of: {
21
+ teams: {
22
+ [sportID: string]: {
23
+ [teamID: string]: {
24
+ [matchID: string]: MatchQueryData;
25
+ };
26
+ };
27
+ };
28
+ teamVsOpponents: {
29
+ [sportID: string]: {
30
+ [teamID: string]: {
31
+ [opponentTeamID: string]: {
32
+ [matchID: string]: MatchQueryData;
33
+ };
34
+ };
35
+ };
36
+ };
37
+ };
38
+ }
39
+
40
+ export { Matches as MatchesQueries };
@@ -0,0 +1,27 @@
1
+ import { Date } from "../global";
2
+ import { OrderStatus } from "../details/users/orders";
3
+
4
+ interface OrderQueryData {
5
+ status: OrderStatus;
6
+ createdAt: Date;
7
+ updatedAt: Date;
8
+ executedAt?: Date;
9
+ id: string;
10
+ }
11
+
12
+ interface Orders {
13
+ where: {
14
+ active: {
15
+ [userID: string]: {
16
+ [orderID: string]: OrderQueryData; //only with status Active
17
+ };
18
+ };
19
+ executed: {
20
+ [userID: string]: {
21
+ [orderID: string]: OrderQueryData; //any status except Active
22
+ };
23
+ };
24
+ };
25
+ }
26
+
27
+ export { Orders as OrdersQueries };
@@ -0,0 +1,17 @@
1
+ interface Shares {
2
+ of: {
3
+ teams: {
4
+ [sportID: string]: {
5
+ /**
6
+ * Sort by name / abbreviation, take team ID to access share
7
+ */
8
+ [teamID: string]: {
9
+ name: string;
10
+ abbreviation: string;
11
+ };
12
+ };
13
+ };
14
+ };
15
+ }
16
+
17
+ export { Shares as SharesQueries };
@@ -0,0 +1,7 @@
1
+ import { MatchOverviews } from "./matchOverviews";
2
+ import { TableSummary } from "./tableSummary";
3
+
4
+ export interface Snapshots {
5
+ matchOverviews: MatchOverviews;
6
+ tableSummary: TableSummary;
7
+ }
@@ -0,0 +1,46 @@
1
+ import { Image, MatchPeriod, MatchStatus, TeamID } from "../global";
2
+
3
+ interface Team {
4
+ id: TeamID;
5
+ name: string;
6
+ abbreviation: string;
7
+ logo?: Image;
8
+ }
9
+
10
+ interface Share {
11
+ price: number;
12
+ openingPrice: number;
13
+ priceChangePercentage: number;
14
+ }
15
+
16
+ interface EventStatus {
17
+ matchStatus: MatchStatus;
18
+ period: MatchPeriod;
19
+ isActive: boolean;
20
+ matchTime: {
21
+ minutes: number;
22
+ additionalMinutes: number;
23
+ };
24
+ }
25
+
26
+ export interface MatchOverviews {
27
+ [sportID: string]: {
28
+ [matchID: string]: {
29
+ teams: {
30
+ home: Team;
31
+ away: Team;
32
+ };
33
+ score: {
34
+ home: number;
35
+ away: number;
36
+ };
37
+ share?: {
38
+ home: Share;
39
+ away: Share;
40
+ };
41
+ startDate: Date;
42
+ roundNb: number;
43
+ eventStatus: EventStatus;
44
+ };
45
+ };
46
+ }
@@ -0,0 +1,47 @@
1
+ import { Image } from "../global";
2
+ import { Team } from "./matchOverviews";
3
+ import { TeamRanking } from "../details/tables/ranking";
4
+
5
+ interface MatchOutcome {
6
+ vsTeam: Team;
7
+ result?: MatchResult;
8
+ matchId: number;
9
+ startDate: string;
10
+ }
11
+
12
+ export enum MatchResult {
13
+ WON = "WON",
14
+ DRAW = "DRAW",
15
+ LOST = "LOST"
16
+ }
17
+
18
+ interface TeamTable {
19
+ name: string;
20
+ abbreviation: string;
21
+ logo: Image;
22
+ ranking: TeamRanking;
23
+ lastMatchesResults: {
24
+ [matchId: string]: MatchOutcome;
25
+ };
26
+ previousMatch: MatchOutcome;
27
+ nextMatch: MatchOutcome;
28
+ }
29
+
30
+ export interface TableSummary {
31
+ groups: {
32
+ [sportID: string]: {
33
+ [seasonID: string]: {
34
+ [groupID: string]: {
35
+ [teamID: string]: TeamTable;
36
+ };
37
+ };
38
+ };
39
+ };
40
+ leagues: {
41
+ [sportID: string]: {
42
+ [seasonID: string]: {
43
+ [teamID: string]: TeamTable;
44
+ };
45
+ };
46
+ };
47
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@sportsbroker/firebase-structure",
3
+ "version": "2.1.0",
4
+ "description": "Firebase structure typings",
5
+ "main": "index.d.ts",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/SportsBroker-HQ/firebase-structure"
9
+ },
10
+ "author": "Lukasz Ostrowski <lukasz.ostrowski@sportsbroker.com>",
11
+ "contributors": ["Mikolaj :)"],
12
+ "license": "ISC",
13
+ "bugs": {
14
+ "url": "https://github.com/BonFireITLTD/firebase-structure/issues"
15
+ },
16
+ "homepage": "https://github.com/BonFireITLTD/firebase-structure#readme"
17
+ }
package/screen-queries ADDED
@@ -0,0 +1,28 @@
1
+ MATCHES https://zpl.io/2poYgA8
2
+
3
+ List query:
4
+ queries/matches/in/seasons/_seasonId_
5
+ .orderByChild("startDate")
6
+
7
+ Paging for example with separate query for each page with startAt() and endAt() constraints - 1 week should work fine
8
+ As listener it's better to use ValueListener to avoid effect of adding previous items from top to bottom above first item of previous list - it shouldn't happen too often that query list will change
9
+
10
+ For each children in value snapshot need to take key which is _matchId_ and query:
11
+ snapshots/matchOverviews/_sportId_/_matchId_
12
+
13
+ Then need to take from teams/home/id (same from away) and subscribe both to:
14
+
15
+ details/teams/shares/_sportId_/_teamId_
16
+
17
+ And use it to set Buy/Sell buttons - needs to be out of snapshot as it's updated too often
18
+
19
+ MATCH_EVENTS
20
+
21
+ In match details topbar and "ViewAll" dialog - https://zpl.io/a3zwQBd
22
+ For home team:
23
+
24
+ details/matches/events/_sportId_/_matchId_/home
25
+ .orderByChild("time/minutes")
26
+
27
+ For overview limitToLast(1)
28
+ For dialog can be subscribed without limits as items are so small that they don't need paging