Skip to content

Commit

Permalink
feat: Load Raid info for max level characters
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenKirschbaum committed Aug 10, 2024
1 parent 8005f6e commit 01f9409
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions lambda/character-list/src/characters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<APIGatewayProxyResultV2> {
const region = request.pathParameters?.region;
Expand All @@ -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
}

Expand All @@ -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,
}
}
Expand Down

0 comments on commit 01f9409

Please sign in to comment.