Skip to content

Commit

Permalink
feat: add background request to castledkp2 beta service (#133)
Browse files Browse the repository at this point in the history
* feat: add castledkp2 upload background request

* fix logging
  • Loading branch information
sgoodrow authored Jul 30, 2024
1 parent 0eaade6 commit c2af23d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const {
token,
clientId,
castleDkpTokenRO,
castleDkp2TokenRW,
castleDkpAuctionRaidId,
castleDkpBonusesCharId,
sharedCharactersGoogleSheetId,
Expand Down Expand Up @@ -88,6 +89,11 @@ export const {
*/
castleDkpTokenRO?: string;

/**
* castledkp.vercel.app read/write token
*/
castleDkp2TokenRW?: string;

/**
* CastleDKP.com Raid ID for DKP auctions.
*/
Expand Down
19 changes: 16 additions & 3 deletions src/services/castledkp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import axiosRetry from "axios-retry";
import { partition } from "lodash";
import LRUCache from "lru-cache";
import moment from "moment";
import { castleDkpTokenRO, dkpBonusesChannelId } from "../config";
import { castleDkpTokenRO } from "../config";
import {
AdjustmentData,
RaidTick,
UPLOAD_DATE_FORMAT,
} from "../features/dkp-records/raid-tick";
import { MINUTES, MONTHS } from "../shared/time";
import { castledkp2 } from "./castledkp2";

const route = (f: string) => `api.php?function=${f}`;

Expand Down Expand Up @@ -163,7 +164,10 @@ export const castledkp = {
raid_note: `${name} ${threadUrl}`,
};
console.log("Creating new raid:", payload);
const { data } = await client.post<{ raid_id: number }>(route("add_raid"), payload);
const { data } = await client.post<{ raid_id: number }>(
route("add_raid"),
payload
);

return {
eventUrlSlug: event.name
Expand Down Expand Up @@ -203,7 +207,10 @@ export const castledkp = {
raid_note: `${tick.name} ${threadUrl}`,
};
console.log("Creating raid tick", payload);
const { data } = await client.post<{ raid_id: number }>(route("add_raid"), payload);
const { data } = await client.post<{ raid_id: number }>(
route("add_raid"),
payload
);

// add items to raid
console.log("Adding items to raid", tick.data.loot);
Expand All @@ -219,6 +226,12 @@ export const castledkp = {
) || []
);

// Temporarily create data using the beta service as well; this is not async and is fault tolerant.
// Primarily, this is being used to test the beta service by ingesting real data.
castledkp2.createRaid(tick).catch((error) => {
console.log(`Failed to create raid in beta service: ${error}`);
});

return {
eventUrlSlug: tick.data.event.name
.toLowerCase()
Expand Down
46 changes: 46 additions & 0 deletions src/services/castledkp2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import axios from "axios";
import { castleDkp2TokenRW } from "../config";
import { RaidTick } from "../features/dkp-records/raid-tick";

const client = axios.create({
baseURL: "https://castledkp.vercel.app",
});

client.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${castleDkp2TokenRW}`;
return config;
});

export const castledkp2 = {
createRaid: async (raidTick: RaidTick) => {
if (!castleDkp2TokenRW) {
console.error("Cannot query CastleDKP2 without an RW token.");
return;
}
return client.post("/api/v1/raid", {
activity: {
typeId: 1,
payout: raidTick.data.value,
note: raidTick.note,
},
attendees: raidTick.data.attendees.map((name) => ({
characterName: name,
pilotCharacterName: name,
})),
adjustments: raidTick.data.adjustments?.map(
({ player, value, reason }) => ({
characterName: player,
pilotCharacterName: player,
amount: value,
reason,
})
),
purchases: raidTick.data.loot.map(({ buyer, item, price }) => ({
characterName: buyer,
pilotCharacterName: buyer,
amount: price,
itemName: item,
})),
});
},
};

0 comments on commit c2af23d

Please sign in to comment.