init
This commit is contained in:
69
src/lib/pocketbase/util/transform-types.ts
Normal file
69
src/lib/pocketbase/util/transform-types.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { Player } from "@/features/players/types";
|
||||
import { Team } from "@/features/teams/types";
|
||||
import { Tournament } from "@/features/tournaments/types";
|
||||
|
||||
// pocketbase does this weird thing with relations where it puts them under a seperate "expand" field
|
||||
// this file transforms raw pocketbase results to our types
|
||||
|
||||
export function transformPlayer(record: any): Player {
|
||||
const sadf: string[] = [];
|
||||
const teams = record.expand?.teams
|
||||
?.sort((a: Team, b: Team) => new Date(a.created) < new Date(b.created) ? -1 : 0)
|
||||
?.map(transformTeam) ?? [];
|
||||
|
||||
return {
|
||||
id: record.id!,
|
||||
first_name: record.first_name,
|
||||
last_name: record.last_name,
|
||||
auth_id: record.auth_id,
|
||||
created: record.created,
|
||||
updated: record.updated,
|
||||
teams,
|
||||
};
|
||||
}
|
||||
|
||||
export function transformTeam(record: any): Team {
|
||||
const players = record.expand?.players
|
||||
?.sort((a: Player, b: Player) => new Date(a.created!) < new Date(b.created!) ? -1 : 0)
|
||||
?.map(transformPlayer) ?? [];
|
||||
|
||||
return {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
logo_url: record.logo_url,
|
||||
primary_color: record.primary_color,
|
||||
accent_color: record.accent_color,
|
||||
song_id: record.song_id,
|
||||
song_name: record.song_name,
|
||||
song_artist: record.song_artist,
|
||||
song_album: record.song_album,
|
||||
song_year: record.song_year,
|
||||
song_start: record.song_start,
|
||||
song_end: 0,
|
||||
song_image_url: record.song_image_url,
|
||||
created: record.created,
|
||||
updated: record.updated,
|
||||
players,
|
||||
};
|
||||
}
|
||||
|
||||
export function transformTournament(record: any): Tournament {
|
||||
const teams = record.expand?.teams
|
||||
?.sort((a: Team, b: Team) => new Date(a.created) < new Date(b.created) ? -1 : 0)
|
||||
?.map(transformTeam) ?? [];
|
||||
|
||||
return {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
location: record.location,
|
||||
desc: record.desc,
|
||||
rules: record.rules,
|
||||
logo_url: record.logo_url,
|
||||
enroll_time: record.enroll_time,
|
||||
start_time: record.start_time,
|
||||
end_time: record.end_time,
|
||||
created: record.created,
|
||||
updated: record.updated,
|
||||
teams,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user