fixed
This commit is contained in:
@@ -5,7 +5,6 @@ import { rng } from "$lib/utils/rng";
|
||||
|
||||
export const testIfSessionIsValid = async (jwt: string) => {
|
||||
try {
|
||||
console.log("Testing session validity...", jwt);
|
||||
const res = await fetch(
|
||||
`${constants.SCRAP_API_URL}/v1/user/get-balance?userId=6339`,
|
||||
{
|
||||
|
||||
@@ -33,6 +33,18 @@ async function removePostSession() {
|
||||
await redis.del(constants.POST_SESSION_KEY);
|
||||
}
|
||||
|
||||
const postDataCacheStore = {
|
||||
get: async (key: string) => {
|
||||
return JSON.parse((await redis.get(`pdcc:${key}`)) ?? "[]");
|
||||
},
|
||||
set: async (key: string, data: any) => {
|
||||
await redis.setex(`pdcc:${key}`, 60 * 60 * 1, JSON.stringify(data));
|
||||
},
|
||||
del: async (key: string) => {
|
||||
await redis.del(`pdcc:${key}`);
|
||||
},
|
||||
};
|
||||
|
||||
export const postDataApiRouter = createTRPCRouter({
|
||||
fetchPostDataHistory: protectedProcedure
|
||||
.input(zPostDataHistoryFilters)
|
||||
@@ -55,12 +67,14 @@ export const postDataApiRouter = createTRPCRouter({
|
||||
.input(zPostDataFilters)
|
||||
.query(async ({ input }) => {
|
||||
const date = input.date;
|
||||
const cacheKey = getULID();
|
||||
if (!input.draw) {
|
||||
return {
|
||||
ok: false,
|
||||
detail: "Draw is required",
|
||||
data: [],
|
||||
errors: undefined,
|
||||
key: cacheKey,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -69,24 +83,26 @@ export const postDataApiRouter = createTRPCRouter({
|
||||
await dbApiUser.getAllPostUsersWithParentUsers(),
|
||||
);
|
||||
if (!balOut.ok || !balOut.data) {
|
||||
return { ok: false, detail: balOut.detail, data: [], users: [] };
|
||||
return {
|
||||
ok: false,
|
||||
key: cacheKey,
|
||||
detail: balOut.detail,
|
||||
data: [],
|
||||
users: [],
|
||||
};
|
||||
}
|
||||
const users = balOut.data;
|
||||
console.log(`[=] ${users.length} users found`);
|
||||
console.log(users);
|
||||
|
||||
const result = await fetchDataForPosting(date, input, users);
|
||||
postDataCacheStore.set(cacheKey, result.data).catch(console.error);
|
||||
console.log("result.data.length = ", result.data.length);
|
||||
return result;
|
||||
return { ...result, key: cacheKey };
|
||||
}),
|
||||
|
||||
post: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
yes: zPostDataFilters,
|
||||
data: z.array(zPostDataEntry),
|
||||
}),
|
||||
)
|
||||
.input(z.object({ yes: zPostDataFilters, cachedDataKey: z.string() }))
|
||||
.mutation(async ({ input }) => {
|
||||
if (await hasPostSession()) {
|
||||
return {
|
||||
@@ -146,8 +162,9 @@ export const postDataApiRouter = createTRPCRouter({
|
||||
};
|
||||
}
|
||||
|
||||
let data = input.data;
|
||||
if (input.data.length < 1) {
|
||||
let data: any[] = await postDataCacheStore.get(input.cachedDataKey);
|
||||
console.log("cached.data.length = ", data.length);
|
||||
if (data.length < 1) {
|
||||
console.log("No data found from preview, generating a list");
|
||||
const _out = await fetchDataForPosting(date, input.yes, balOut.data);
|
||||
if (!_out.ok) {
|
||||
@@ -155,6 +172,7 @@ export const postDataApiRouter = createTRPCRouter({
|
||||
return _out;
|
||||
}
|
||||
data = _out.data;
|
||||
console.log("data.length = ", data.length);
|
||||
}
|
||||
|
||||
if (data.length < 1) {
|
||||
@@ -168,9 +186,8 @@ export const postDataApiRouter = createTRPCRouter({
|
||||
};
|
||||
}
|
||||
|
||||
console.log(`[+] Posting ${input.data.length} entries to the API`);
|
||||
console.log(`[+] Posting ${data.length} entries to the API`);
|
||||
|
||||
console.time("Time taken to post data to the API");
|
||||
const res = await postDataToApi({
|
||||
sessions: userSessions,
|
||||
data,
|
||||
@@ -200,6 +217,7 @@ export const postDataApiRouter = createTRPCRouter({
|
||||
|
||||
console.log("[+] Data saved to the database");
|
||||
|
||||
await postDataCacheStore.del(input.cachedDataKey);
|
||||
await removePostSession();
|
||||
return {
|
||||
ok: true,
|
||||
|
||||
Reference in New Issue
Block a user