mirror of
https://github.com/michivonah/themepark-assistant.git
synced 2025-12-22 22:16:29 +01:00
refactoring type usage in existing functions & refactor fetchAttractions into own file
This commit is contained in:
parent
0a3636fda8
commit
d377c68975
3 changed files with 57 additions and 50 deletions
|
|
@ -1,62 +1,22 @@
|
||||||
import { getDbEnv } from '../db/client'
|
import { getDbEnv } from '../db/client'
|
||||||
import { attraction, themepark } from '../db/schema'
|
import { attraction, themepark } from '../db/schema'
|
||||||
import { inArray } from 'drizzle-orm'
|
import { inArray } from 'drizzle-orm'
|
||||||
import httpRequest from '../lib/http-request'
|
import { Attraction } from '../types/attraction'
|
||||||
|
import { ThemeparkSelect } from '../types/themepark'
|
||||||
import asyncBatchJob from '../lib/async-batch-job'
|
import asyncBatchJob from '../lib/async-batch-job'
|
||||||
|
import fetchAttractions from '../lib/fetch-attractions'
|
||||||
|
|
||||||
interface AttractionImport {
|
type ThemeparkAPI = Pick<ThemeparkSelect, "apiName" | "id">;
|
||||||
code: string,
|
|
||||||
name: string,
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AttractionType {
|
|
||||||
name: string,
|
|
||||||
apiCode: string,
|
|
||||||
themeparkId: number
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Themepark {
|
|
||||||
apiName: string,
|
|
||||||
id: number
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetching the attractions from a specified park
|
|
||||||
* @param park API Code for request themepark
|
|
||||||
* @param endpoint Endpoint where to fetch data from (default: https://api.wartezeiten.app/v1/parks)
|
|
||||||
* @param lang Language used for API request
|
|
||||||
* @returns Interface with attraction code & name
|
|
||||||
*/
|
|
||||||
async function fetchAttractions(
|
|
||||||
park: string,
|
|
||||||
endpoint: string = "https://api.wartezeiten.app/v1/parks",
|
|
||||||
lang: string = 'de'
|
|
||||||
): Promise<AttractionImport[]>{
|
|
||||||
try{
|
|
||||||
const headers = {
|
|
||||||
'language':lang,
|
|
||||||
'park':park
|
|
||||||
};
|
|
||||||
|
|
||||||
const result = await httpRequest<AttractionImport[]>(endpoint, {
|
|
||||||
headers: headers
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
catch(e){
|
|
||||||
throw new Error(`Failed to fetch attractions: ${e}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an object of all themeparks saved in the database
|
* Return an object of all themeparks saved in the database
|
||||||
* @param env DB Connection
|
* @param env DB Connection
|
||||||
* @returns Object of themeparks with api name & id from internal DB
|
* @returns Object of themeparks with api name & id from internal DB
|
||||||
*/
|
*/
|
||||||
async function getThemeparks(env: Env): Promise<Themepark[]>{
|
async function getThemeparks(env: Env): Promise<ThemeparkAPI[]>{
|
||||||
try{
|
try{
|
||||||
const db = getDbEnv(env);
|
const db = getDbEnv(env);
|
||||||
const themeparks: Themepark[] = await db.select({
|
const themeparks: ThemeparkAPI[] = await db.select({
|
||||||
apiName: themepark.apiName,
|
apiName: themepark.apiName,
|
||||||
id: themepark.id
|
id: themepark.id
|
||||||
}).from(themepark);
|
}).from(themepark);
|
||||||
|
|
@ -74,13 +34,13 @@ async function getThemeparks(env: Env): Promise<Themepark[]>{
|
||||||
* @param parks Object of themeparks to get attractions from
|
* @param parks Object of themeparks to get attractions from
|
||||||
* @returns Object of attractions
|
* @returns Object of attractions
|
||||||
*/
|
*/
|
||||||
async function getAttractionsByParks(env: Env, parks: Themepark[]): Promise<AttractionType[]>{
|
async function getAttractionsByParks(env: Env, parks: ThemeparkAPI[]): Promise<Attraction[]>{
|
||||||
try{
|
try{
|
||||||
const db = getDbEnv(env);
|
const db = getDbEnv(env);
|
||||||
|
|
||||||
const parkIds: number[] = parks.map(p => p.id);
|
const parkIds: number[] = parks.map(p => p.id);
|
||||||
|
|
||||||
const attractions: AttractionType[] = await db.select({
|
const attractions: Attraction[] = await db.select({
|
||||||
name: attraction.name,
|
name: attraction.name,
|
||||||
apiCode: attraction.apiCode,
|
apiCode: attraction.apiCode,
|
||||||
themeparkId: attraction.themeparkId
|
themeparkId: attraction.themeparkId
|
||||||
|
|
@ -100,7 +60,7 @@ async function getAttractionsByParks(env: Env, parks: Themepark[]): Promise<Attr
|
||||||
* @param env DB Connection
|
* @param env DB Connection
|
||||||
* @param parks Object of the themeparks from which the attractions have to be imported
|
* @param parks Object of the themeparks from which the attractions have to be imported
|
||||||
*/
|
*/
|
||||||
async function importAttractionsByParks(env: Env, parks: Themepark[]): Promise<void>{
|
async function importAttractionsByParks(env: Env, parks: ThemeparkAPI[]): Promise<void>{
|
||||||
try{
|
try{
|
||||||
const db = getDbEnv(env);
|
const db = getDbEnv(env);
|
||||||
|
|
||||||
|
|
|
||||||
30
api/src/lib/fetch-attractions.ts
Normal file
30
api/src/lib/fetch-attractions.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import { AttractionImport } from '../types/attraction'
|
||||||
|
import httpRequest from '../lib/http-request'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetching the attractions from a specified park
|
||||||
|
* @param park API Code for request themepark
|
||||||
|
* @param endpoint Endpoint where to fetch data from (default: https://api.wartezeiten.app/v1/parks)
|
||||||
|
* @param lang Language used for API request
|
||||||
|
* @returns Interface with attraction code & name
|
||||||
|
*/
|
||||||
|
export default async function fetchAttractions(
|
||||||
|
park: string,
|
||||||
|
endpoint: string = "https://api.wartezeiten.app/v1/waitingtimes",
|
||||||
|
lang: string = 'de'
|
||||||
|
): Promise<AttractionImport[]>{
|
||||||
|
try{
|
||||||
|
const headers = {
|
||||||
|
'language':lang,
|
||||||
|
'park':park
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await httpRequest<AttractionImport[]>(endpoint, {
|
||||||
|
headers: headers
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch(e){
|
||||||
|
throw new Error(`Failed to fetch attractions: ${e}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,3 +3,20 @@ import { attraction } from '../db/schema';
|
||||||
|
|
||||||
export type Attraction = InferInsertModel<typeof attraction>
|
export type Attraction = InferInsertModel<typeof attraction>
|
||||||
export type AttractionSelect = InferSelectModel<typeof attraction>
|
export type AttractionSelect = InferSelectModel<typeof attraction>
|
||||||
|
|
||||||
|
// API Response format
|
||||||
|
export interface AttractionImport {
|
||||||
|
code: string,
|
||||||
|
name: string,
|
||||||
|
waitingtime: number,
|
||||||
|
status: "opened" | "virtualqueue" | "maintenance" | "closedice" | "closedweather" | "closed"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Waittime comparison
|
||||||
|
export interface AttractionChanges {
|
||||||
|
apiCode: string,
|
||||||
|
name: string,
|
||||||
|
waittime: number,
|
||||||
|
hasChanged: boolean,
|
||||||
|
increased: boolean
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue