Updated Series/Sets fetchers to filter and sort

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2021-05-03 00:56:45 +02:00
parent 91f968a01e
commit 6e517efd0f
Signed by: Florian Bouillon
GPG Key ID: 50BD648F12C86AB6
4 changed files with 45 additions and 16 deletions

View File

@ -27,6 +27,6 @@ export default class implements Endpoint<SerieList, SerieSingle, {}, Array<Serie
} }
public async common() { public async common() {
return getSeries() return getSeries(this.lang)
} }
} }

View File

@ -32,8 +32,7 @@ export default class implements Endpoint<SetList, SetSingle, CardSingle, Array<S
} }
public async common() { public async common() {
return (await getSets()) return (await getSets(undefined, this.lang))
.filter((set) => isSetAvailable(set, this.lang))
} }
public async sub(common: Array<Set>, item: string) { public async sub(common: Array<Set>, item: string) {

View File

@ -1,16 +1,29 @@
import { smartGlob } from "./util" import { smartGlob } from "./util"
import { setToSetSimple, getSets } from "./setUtil" import { setToSetSimple, getSets, getSet, isSetAvailable } from "./setUtil"
import { Serie, SupportedLanguages, Set } from 'db/interfaces' import { Serie, SupportedLanguages, Set } from 'db/interfaces'
import { Serie as SerieSingle, SerieResume } from '@tcgdex/sdk/interfaces' import { Serie as SerieSingle, SerieResume } from '@tcgdex/sdk/interfaces'
export async function getSeries(): Promise<Array<Serie>> { export async function getSeries(lang: SupportedLanguages): Promise<Array<Serie>> {
const series = await Promise.all((await smartGlob('./db/data/*.js')) let series: Array<Serie> = (await Promise.all((await smartGlob('./db/data/*.js'))
//Find Serie's name
.map((it) => it.substring(it.lastIndexOf('/') + 1, it.length - 3)) .map((it) => it.substring(it.lastIndexOf('/') + 1, it.length - 3))
.map((it) => getSerie(it))) // Fetch the Serie
const tmp: Array<[Serie, Set | undefined]> = await Promise.all(series.map( async (it) => { .map((it) => getSerie(it))))
return [it, (await getSets(it.name.en)).reduce<Set | undefined>((p, c) => p ? p.releaseDate < c.releaseDate ? p : c : c, undefined) as Set] as [Serie, Set] // Filter the serie if no name's exists in the selected lang
.filter((serie) => !!serie.name[lang])
// Filter available series
const isAvailable = await Promise.all(series.map((serie) => isSerieAvailable(serie, lang)))
series = series.filter((_, index) => isAvailable[index])
// Sort series by the first set release date
const tmp: Array<[Serie, Set | undefined]> = await Promise.all(series.map(async (it) => {
return [
it,
(await getSets(it.name.en, lang))
.reduce<Set | undefined>((p, c) => p ? p.releaseDate < c.releaseDate ? p : c : c, undefined) as Set] as [Serie, Set]
})) }))
tmp.forEach((t) => !t[1] && console.log(t[0].name) )
return tmp.sort((a, b) => (a[1] ? a[1].releaseDate : '0') > (b[1] ? b[1].releaseDate : '0') ? 1 : -1).map((it) => it[0]) return tmp.sort((a, b) => (a[1] ? a[1].releaseDate : '0') > (b[1] ? b[1].releaseDate : '0') ? 1 : -1).map((it) => it[0])
} }
@ -18,6 +31,14 @@ export async function getSerie(name: string): Promise<Serie> {
return (await import(`../db/data/${name}.js`)).default return (await import(`../db/data/${name}.js`)).default
} }
export async function isSerieAvailable(serie: Serie, lang: SupportedLanguages) {
if (!serie.name[lang]) {
return false
}
const sets = (await getSets(serie.name.en, lang))
return sets.length > 0
}
export async function serieToSerieSimple(serie: Serie, lang: SupportedLanguages): Promise<SerieResume> { export async function serieToSerieSimple(serie: Serie, lang: SupportedLanguages): Promise<SerieResume> {
return { return {
id: serie.id, id: serie.id,
@ -26,7 +47,7 @@ export async function serieToSerieSimple(serie: Serie, lang: SupportedLanguages)
} }
export async function serieToSerieSingle(serie: Serie, lang: SupportedLanguages): Promise<SerieSingle> { export async function serieToSerieSingle(serie: Serie, lang: SupportedLanguages): Promise<SerieSingle> {
const setsTmp = await getSets(serie.name.en) const setsTmp = await getSets(serie.name.en, lang)
const sets = await Promise.all(setsTmp const sets = await Promise.all(setsTmp
.sort((a, b) => { .sort((a, b) => {
return a.releaseDate > b.releaseDate ? 1 : -1 return a.releaseDate > b.releaseDate ? 1 : -1

View File

@ -10,22 +10,31 @@ interface t {
const setCache: t = {} const setCache: t = {}
// Dont use cache as it wont necessary have them all // Dont use cache as it wont necessary have them all
export async function getSets(serie = '*'): Promise<Array<Set>> { export async function getSets(serie = '*', lang: SupportedLanguages): Promise<Array<Set>> {
const sets = (await smartGlob(`./db/data/${serie}/*.js`)).map((set) => set.substring(set.lastIndexOf('/')+1, set.lastIndexOf('.'))) // list sets names
return Promise.all(sets.map((set) => getSet(set, serie))) const rawSets = (await smartGlob(`./db/data/${serie}/*.js`)).map((set) => set.substring(set.lastIndexOf('/')+1, set.lastIndexOf('.')))
// Fetch sets
const sets = (await Promise.all(rawSets.map((set) => getSet(set, serie, lang))))
// Filter sets
.filter((set) => isSetAvailable(set, lang))
// Sort sets by release date
.sort((a, b) => {
return a.releaseDate > b.releaseDate ? 1 : -1
})
return sets
} }
/** /**
* Return the set * Return the set
* @param name the name of the set (don't include.js/.ts) * @param name the name of the set (don't include.js/.ts)
*/ */
export async function getSet(name: string, serie = '*'): Promise<Set> { export async function getSet(name: string, serie = '*', lang: SupportedLanguages): Promise<Set> {
if (!setCache[name]) { if (!setCache[name]) {
try { try {
const [path] = await smartGlob(`./db/data/${serie}/${name}.js`) const [path] = await smartGlob(`./db/data/${serie}/${name}.js`)
setCache[name] = (await import(path.replace('./', '../'))).default setCache[name] = (await import(path.replace('./', '../'))).default
} catch (e) { } catch (e) {
const set = (await getSets()).find((s) => s.id === name) const set = (await getSets(undefined, lang)).find((s) => s.id === name)
if (set) { if (set) {
return set return set
} }