Skip to content

Commit c2af23d

Browse files
authored
feat: add background request to castledkp2 beta service (#133)
* feat: add castledkp2 upload background request * fix logging
1 parent 0eaade6 commit c2af23d

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const {
99
token,
1010
clientId,
1111
castleDkpTokenRO,
12+
castleDkp2TokenRW,
1213
castleDkpAuctionRaidId,
1314
castleDkpBonusesCharId,
1415
sharedCharactersGoogleSheetId,
@@ -88,6 +89,11 @@ export const {
8889
*/
8990
castleDkpTokenRO?: string;
9091

92+
/**
93+
* castledkp.vercel.app read/write token
94+
*/
95+
castleDkp2TokenRW?: string;
96+
9197
/**
9298
* CastleDKP.com Raid ID for DKP auctions.
9399
*/

src/services/castledkp.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import axiosRetry from "axios-retry";
33
import { partition } from "lodash";
44
import LRUCache from "lru-cache";
55
import moment from "moment";
6-
import { castleDkpTokenRO, dkpBonusesChannelId } from "../config";
6+
import { castleDkpTokenRO } from "../config";
77
import {
88
AdjustmentData,
99
RaidTick,
1010
UPLOAD_DATE_FORMAT,
1111
} from "../features/dkp-records/raid-tick";
1212
import { MINUTES, MONTHS } from "../shared/time";
13+
import { castledkp2 } from "./castledkp2";
1314

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

@@ -163,7 +164,10 @@ export const castledkp = {
163164
raid_note: `${name} ${threadUrl}`,
164165
};
165166
console.log("Creating new raid:", payload);
166-
const { data } = await client.post<{ raid_id: number }>(route("add_raid"), payload);
167+
const { data } = await client.post<{ raid_id: number }>(
168+
route("add_raid"),
169+
payload
170+
);
167171

168172
return {
169173
eventUrlSlug: event.name
@@ -203,7 +207,10 @@ export const castledkp = {
203207
raid_note: `${tick.name} ${threadUrl}`,
204208
};
205209
console.log("Creating raid tick", payload);
206-
const { data } = await client.post<{ raid_id: number }>(route("add_raid"), payload);
210+
const { data } = await client.post<{ raid_id: number }>(
211+
route("add_raid"),
212+
payload
213+
);
207214

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

229+
// Temporarily create data using the beta service as well; this is not async and is fault tolerant.
230+
// Primarily, this is being used to test the beta service by ingesting real data.
231+
castledkp2.createRaid(tick).catch((error) => {
232+
console.log(`Failed to create raid in beta service: ${error}`);
233+
});
234+
222235
return {
223236
eventUrlSlug: tick.data.event.name
224237
.toLowerCase()

src/services/castledkp2.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import axios from "axios";
2+
import { castleDkp2TokenRW } from "../config";
3+
import { RaidTick } from "../features/dkp-records/raid-tick";
4+
5+
const client = axios.create({
6+
baseURL: "https://castledkp.vercel.app",
7+
});
8+
9+
client.interceptors.request.use((config) => {
10+
config.headers.Authorization = `Bearer ${castleDkp2TokenRW}`;
11+
return config;
12+
});
13+
14+
export const castledkp2 = {
15+
createRaid: async (raidTick: RaidTick) => {
16+
if (!castleDkp2TokenRW) {
17+
console.error("Cannot query CastleDKP2 without an RW token.");
18+
return;
19+
}
20+
return client.post("/api/v1/raid", {
21+
activity: {
22+
typeId: 1,
23+
payout: raidTick.data.value,
24+
note: raidTick.note,
25+
},
26+
attendees: raidTick.data.attendees.map((name) => ({
27+
characterName: name,
28+
pilotCharacterName: name,
29+
})),
30+
adjustments: raidTick.data.adjustments?.map(
31+
({ player, value, reason }) => ({
32+
characterName: player,
33+
pilotCharacterName: player,
34+
amount: value,
35+
reason,
36+
})
37+
),
38+
purchases: raidTick.data.loot.map(({ buyer, item, price }) => ({
39+
characterName: buyer,
40+
pilotCharacterName: buyer,
41+
amount: price,
42+
itemName: item,
43+
})),
44+
});
45+
},
46+
};

0 commit comments

Comments
 (0)