@@ -24,6 +24,7 @@ import { GameEngineLogo } from "./entities/game-engine-logo.entity";
24
24
import { PartialGame } from "./game-repository.types" ;
25
25
import { StatisticsQueueService } from "../../statistics/statistics-queue/statistics-queue.service" ;
26
26
import { StatisticsSourceType } from "../../statistics/statistics.constants" ;
27
+ import { days } from "@nestjs/throttler" ;
27
28
28
29
/**
29
30
* Service responsible for data inserting and updating for all game-related models.
@@ -101,6 +102,39 @@ export class GameRepositoryCreateService {
101
102
private readonly statisticsQueueService : StatisticsQueueService ,
102
103
) { }
103
104
105
+ async shouldUpdate ( game : PartialGame ) {
106
+ if ( game . id == null || typeof game . id !== "number" ) {
107
+ return false ;
108
+ } else if (
109
+ game . name == undefined &&
110
+ ( game . alternativeNames == undefined ||
111
+ game . alternativeNames . length === 0 )
112
+ ) {
113
+ return false ;
114
+ } else if (
115
+ ( await this . gameRepository . existsBy ( { id : game . id } ) ) &&
116
+ game . updatedAt != undefined
117
+ ) {
118
+ const now = new Date ( ) ;
119
+ const lastUpdateDate = game . updatedAt as Date ;
120
+ const dayInMs = 1000 * 3600 * 24 ;
121
+
122
+ const differenceInTime = now . getTime ( ) - lastUpdateDate . getTime ( ) ;
123
+ const approximateDifferenceInDays = Math . round (
124
+ differenceInTime / dayInMs ,
125
+ ) ;
126
+
127
+ // game already exists and it has been more than thirty days since it's last update
128
+ // this logic only works if the 'updated_at' property is being returned by igdb-sync.
129
+ if ( approximateDifferenceInDays >= 30 ) {
130
+ return false ;
131
+ }
132
+ const one = 2 ;
133
+ }
134
+
135
+ return true ;
136
+ }
137
+
104
138
/**
105
139
* Creates or updates a game in our database. <br>
106
140
* ManyToMany models can't be easily upserted, since the junction table is not inserted/updated automatically (without .save).
@@ -109,16 +143,11 @@ export class GameRepositoryCreateService {
109
143
* @param game
110
144
*/
111
145
async createOrUpdate ( game : PartialGame ) {
112
- if ( game . id == null || typeof game . id !== "number" ) {
113
- throw new Error ( "Game ID must be a number." ) ;
114
- } else if (
115
- game . name == undefined &&
116
- ( game . alternativeNames == undefined ||
117
- game . alternativeNames . length === 0 )
118
- ) {
119
- throw new Error (
120
- "Game name or alternative names must be specified." ,
121
- ) ;
146
+ const shouldProcess = await this . shouldUpdate ( game ) ;
147
+
148
+ if ( ! shouldProcess ) {
149
+ // Do not log here, as most games are skipped after the first run.
150
+ return ;
122
151
}
123
152
124
153
const isUpdateAction = await this . gameRepository . existsBy ( {
@@ -163,9 +192,9 @@ export class GameRepositoryCreateService {
163
192
164
193
/**
165
194
* Builds child relationships which depend on the game being saved (e.g. alternative names, cover).
166
- e.g. Relationships where Game is on the OneToMany or ManyToMany side.<br>
167
-
168
- <strong> We assume the game is already persisted at this point, so we can use it as a parent.</strong>
195
+ * e.g. Relationships where Game is on the OneToMany or ManyToMany side.<br>
196
+ *
197
+ * <strong> We assume the game is already persisted at this point, so we can use it as a parent.</strong>
169
198
* @param game
170
199
*/
171
200
async buildChildRelationships ( game : PartialGame ) {
0 commit comments