Fix linking when game loaded is an expansion itself.

This commit is contained in:
Bradley Shellnut 2023-10-16 10:07:16 +13:00
parent 66c9ef5c93
commit 707daf9f35
2 changed files with 55 additions and 7 deletions

View file

@ -1,7 +1,15 @@
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import prisma from '$lib/prisma.js'; import prisma from '$lib/prisma.js';
import type { GameType } from '$lib/types.js'; import type { GameType } from '$lib/types.js';
import { createArtist, createCategory, createDesigner, createExpansion, createMechanic, createOrUpdateGame, createPublisher } from '$lib/utils/dbUtils.js'; import {
createArtist,
createCategory,
createDesigner,
createExpansion,
createMechanic,
createOrUpdateGame,
createPublisher
} from '$lib/utils/dbUtils.js';
import { mapAPIGameToBoredGame } from '$lib/utils/gameMapper.js'; import { mapAPIGameToBoredGame } from '$lib/utils/gameMapper.js';
import type { Game } from '@prisma/client'; import type { Game } from '@prisma/client';
@ -18,7 +26,12 @@ export const load = async ({ params, setHeaders, locals, fetch }) => {
designers: true, designers: true,
publishers: true, publishers: true,
mechanics: true, mechanics: true,
categories: true categories: true,
expansions: {
include: {
base_game: true
}
}
} }
}); });
console.log('found game', game); console.log('found game', game);
@ -28,7 +41,10 @@ export const load = async ({ params, setHeaders, locals, fetch }) => {
} }
const currentDate = new Date(); const currentDate = new Date();
if (game.last_sync_at === null || currentDate.getDate() - game.last_sync_at.getDate() > 7 * 24 * 60 * 60 * 1000) { if (
game.last_sync_at === null ||
currentDate.getDate() - game.last_sync_at.getDate() > 7 * 24 * 60 * 60 * 1000
) {
await syncGameAndConnectedData(game, fetch); await syncGameAndConnectedData(game, fetch);
} }
@ -62,6 +78,8 @@ export const load = async ({ params, setHeaders, locals, fetch }) => {
}); });
} }
console.log('Returning game', game);
return { return {
game, game,
user, user,
@ -119,10 +137,22 @@ async function syncGameAndConnectedData(game: Game, eventFetch: Function) {
console.log('Inbound?', externalExpansion.inbound); console.log('Inbound?', externalExpansion.inbound);
if (externalExpansion?.inbound === true) { if (externalExpansion?.inbound === true) {
expansion = await createExpansion(game, externalExpansion, false, eventFetch); expansion = await createExpansion(game, externalExpansion, false, eventFetch);
await prisma.game.update({
where: {
external_id: externalExpansion.id
},
data: {
expansions: {
connect: {
id: expansion.id
}
}
}
})
} else { } else {
expansion = await createExpansion(game, externalExpansion, true, eventFetch); expansion = await createExpansion(game, externalExpansion, true, eventFetch);
expansions.push({ id: expansion.id });
} }
expansions.push({ id: expansion.id });
} }
let boredGame = mapAPIGameToBoredGame(externalGame); let boredGame = mapAPIGameToBoredGame(externalGame);
@ -135,4 +165,4 @@ async function syncGameAndConnectedData(game: Game, eventFetch: Function) {
boredGame.expansions = expansions; boredGame.expansions = expansions;
return createOrUpdateGame(boredGame); return createOrUpdateGame(boredGame);
} }
} }

View file

@ -11,8 +11,8 @@
$: existsInCollection = $collectionStore.find((item: SavedGameType) => item.id === game.id); $: existsInCollection = $collectionStore.find((item: SavedGameType) => item.id === game.id);
$: existsInWishlist = $wishlistStore.find((item: SavedGameType) => item.id === game.id); $: existsInWishlist = $wishlistStore.find((item: SavedGameType) => item.id === game.id);
$: collectionText = existsInCollection ? 'Remove from collection' : 'Add to collection'; // $: collectionText = existsInCollection ? 'Remove from collection' : 'Add to collection';
$: wishlistText = existsInWishlist ? 'Remove from wishlist' : 'Add to wishlist'; // $: wishlistText = existsInWishlist ? 'Remove from wishlist' : 'Add to wishlist';
export let data: PageData; export let data: PageData;
console.log('data', data); console.log('data', data);
@ -67,6 +67,24 @@
{/if} {/if}
</div> </div>
</section> </section>
<section>
<p>Categories</p>
{#each game?.categories as category}
<span>{category.name}</span>
{/each}
</section>
<section>
<p>Mechanics</p>
{#each game?.mechanics as mechanic}
<span>{mechanic.name}</span>
{/each}
</section>
<section>
<p>Expansions</p>
{#each game?.expansions as expansions}
<span>{expansions?.base_game?.name}</span>
{/each}
</section>
<section class="description" class:show={seeMore} class:hide={!seeMore} style="margin-top: 2rem;"> <section class="description" class:show={seeMore} class:hide={!seeMore} style="margin-top: 2rem;">
{@html game?.description} {@html game?.description}
</section> </section>