refactor(match-service): pass region outside of account object

This commit is contained in:
Kalane 2021-09-19 22:41:56 +02:00
parent b1466c4063
commit 0703174cfa
3 changed files with 27 additions and 13 deletions

View file

@ -26,7 +26,6 @@ export default class SummonersController {
if (!account) {
return response.json(null)
}
account.region = region
finalJSON.account = account
// Summoner in DB
@ -36,7 +35,7 @@ export default class SummonersController {
finalJSON.account.names = await SummonerService.getAllSummonerNames(account, summonerDB)
// MATCH LIST
finalJSON.matchList = await MatchService.updateMatchList(account, summonerDB)
finalJSON.matchList = await MatchService.updateMatchList(account, region, summonerDB)
// All seasons the summoner has played
// TODO: check if there is a way to do that with V5...

View file

@ -11,7 +11,6 @@ export interface SummonerDTO {
id: string
puuid: string
summonerLevel: number
region?: string
}
export default class SummonerEndpoint {
@ -23,6 +22,16 @@ export default class SummonerEndpoint {
this.limiter = limiter
}
public accountId(accountId: string, region: string): Promise<SummonerDTO> {
return new JaxRequest(
region,
this.config,
`summoner/v4/summoners/by-account/${accountId}`,
this.limiter,
36000
).execute()
}
public summonerId(summonerId: string, region: string): Promise<SummonerDTO> {
return new JaxRequest(
region,

View file

@ -13,14 +13,15 @@ class MatchService {
/**
* Add 100 matches at a time to MatchList until the stopFetching condition is true
* @param account of the summoner
* @param region of the summoner
* @param stopFetching condition to stop fetching the MatchList
*/
private async _fetchMatchListUntil(account: SummonerDTO, stopFetching: any) {
private async _fetchMatchListUntil(account: SummonerDTO, region: string, stopFetching: any) {
let matchList: MatchlistDto = []
let alreadyIn = false
let index = 0
do {
let newMatchList = await Jax.Matchlist.puuid(account.puuid, account.region as string, index)
let newMatchList = await Jax.Matchlist.puuid(account.puuid, region as string, index)
// Error while fetching Riot API
if (!newMatchList) {
return matchList
@ -28,10 +29,7 @@ class MatchService {
matchList = [...matchList, ...newMatchList]
alreadyIn = newMatchList.length === 0 || stopFetching(newMatchList)
// If the match is made in another region : we stop fetching
if (
matchList[matchList.length - 1].split('_')[0].toLowerCase() !==
account.region?.toLowerCase()
) {
if (matchList[matchList.length - 1].split('_')[0].toLowerCase() !== region.toLowerCase()) {
alreadyIn = true
}
index += 100
@ -41,15 +39,23 @@ class MatchService {
/**
* Update the full MatchList of the summoner
*/
public async updateMatchList(account: SummonerDTO, summonerDB: Summoner): Promise<MatchlistDto> {
public async updateMatchList(
account: SummonerDTO,
region: string,
summonerDB: Summoner
): Promise<MatchlistDto> {
console.time('matchList')
const currentMatchList = await summonerDB.related('matchList').query().orderBy('matchId', 'asc')
const currentMatchListIds = currentMatchList.map((m) => m.matchId)
const newMatchList = await this._fetchMatchListUntil(account, (newMatchList: MatchlistDto) => {
const newMatchList = await this._fetchMatchListUntil(
account,
region,
(newMatchList: MatchlistDto) => {
return currentMatchListIds.some((id) => id === newMatchList[newMatchList.length - 1])
})
}
)
const matchListToSave: MatchlistDto = []
for (const matchId of newMatchList.reverse()) {