diff --git a/lambda/character-list/src/characters.ts b/lambda/character-list/src/characters.ts index de1e308..1b1f233 100644 --- a/lambda/character-list/src/characters.ts +++ b/lambda/character-list/src/characters.ts @@ -11,6 +11,8 @@ import httpContentNegotiation from "@middy/http-content-negotiation"; import httpResponseSerializerMiddleware from "@middy/http-response-serializer"; const REGIONS = ["eu", "us", "kr", "tw"]; +const MAX_LEVEL = 70; +const RELEVANT_EXPANSION = 503; const lambdaHandler = async function (request: APIGatewayProxyEventV2 & SessionData): Promise { const region = request.pathParameters?.region; @@ -30,7 +32,9 @@ const lambdaHandler = async function (request: APIGatewayProxyEventV2 & SessionD if(profileResponse.status === 404) return { statusCode: 200, body: { - wow_accounts: [] + profile: { + wow_accounts: [] + } } as any } @@ -44,13 +48,52 @@ const lambdaHandler = async function (request: APIGatewayProxyEventV2 & SessionD const profileData = await profileResponse.json(); + const maxLevelCharacters = profileData.wow_accounts.map((account: any) => + account.characters.filter((character: any) => character.level === MAX_LEVEL) + ).flat(); + + let charactersRaidInfo; + try { + const characterRaidsResponses = await Promise.all( + maxLevelCharacters.map(async (character: any) => { + const raidResponse = await fetch(`https://${region}.api.blizzard.com/profile/wow/character/${character.realm.slug}/${character.name.toLowerCase()}/encounters/raids?namespace=profile-${region}&locale=en_US`, { + headers: { + Authorization: `Bearer ${request.session.battleNet.access_token}`, + } + }); + + if(!raidResponse.ok) { + logger.error(`Failed to fetch raid info for ${character.name}-${character.realm.slug}`, { + status: raidResponse.status, + text: await raidResponse.text(), + }); + throw new Error("Failed to fetch raid info"); + } + + const raidResponseData = await raidResponse.json(); + + return ( + (raidResponseData.expansions || []) + .filter((expansion: any) => expansion.expansion.id === RELEVANT_EXPANSION) + .map((expansion: any) => ({[`${character.name.toLowerCase()}-${character.realm.slug}`]: expansion.instances})) + .shift() + ) + }) + ); + + charactersRaidInfo = characterRaidsResponses.reduce((acc, character) => ({...acc, ...character}), {}); + } catch (error) { + logger.error("Failed to fetch raid info", error as Error); + } + return { statusCode: 200, headers: { "Cache-Control": "max-age=300, s-maxage=3600", }, body: { - profile: profileData + profile: profileData, + raids: charactersRaidInfo, } as any, } }