330 lines
9.9 KiB
TypeScript
Executable File
330 lines
9.9 KiB
TypeScript
Executable File
import { getRandomUserAgent, getULID, sleep } from "$lib/utils";
|
|
import { constants } from "$lib/utils/constants";
|
|
import type { BookingEntry, Draw, LooseApiUser } from "$lib/utils/data.types";
|
|
import { rng } from "$lib/utils/rng";
|
|
// import fs from "fs";
|
|
|
|
// function dumpDistributors(distributors: LooseApiUser[]) {
|
|
// fs.writeFileSync("distributors.json", JSON.stringify(distributors, null, 2));
|
|
// }
|
|
|
|
// function dumpDealers(dealers: LooseApiUser[]) {
|
|
// fs.writeFileSync("dealers.json", JSON.stringify(dealers, null, 2));
|
|
// }
|
|
|
|
export const testIfSessionIsValid = async (jwt: string) => {
|
|
try {
|
|
const res = await fetch(
|
|
`${constants.SCRAP_API_URL}/v1/user/get-balance?userId=6339`,
|
|
{
|
|
headers: {
|
|
...constants.SCRAP_API_BASE_HEADERS,
|
|
Authorization: jwt,
|
|
"User-Agent": getRandomUserAgent(),
|
|
},
|
|
},
|
|
);
|
|
if (res.status !== 200) {
|
|
return false;
|
|
}
|
|
const rj = (await res.json()) as {
|
|
code: number;
|
|
success: boolean;
|
|
message: string;
|
|
data: any;
|
|
time: string;
|
|
};
|
|
return rj.code == 200 && rj.success;
|
|
} catch (err) {
|
|
console.log(err);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export const getSessionToken = async (payload: {
|
|
userId: string;
|
|
password: string;
|
|
verifyToken: string;
|
|
code: string;
|
|
userType: number;
|
|
}): Promise<{ ok: boolean; message: string }> => {
|
|
console.log("Requesting...");
|
|
const res = await fetch(`${constants.SCRAP_API_URL}/v1/auth/login`, {
|
|
method: "POST",
|
|
headers: {
|
|
...constants.SCRAP_API_BASE_HEADERS,
|
|
"User-Agent": getRandomUserAgent(),
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(payload),
|
|
});
|
|
const out = await res.json();
|
|
if (out.code !== 200) {
|
|
return { ok: false, message: out.message };
|
|
}
|
|
return { ok: true, message: out.data.token };
|
|
};
|
|
|
|
export async function getUsersBalance(userId: number, jwt: string) {
|
|
try {
|
|
const res = await fetch(
|
|
`${constants.SCRAP_API_URL}/v1/user/get-balance?userId=${userId}`,
|
|
{
|
|
headers: {
|
|
...constants.SCRAP_API_BASE_HEADERS,
|
|
Authorization: jwt,
|
|
"User-Agent": getRandomUserAgent(),
|
|
},
|
|
},
|
|
);
|
|
const rj = (await res.json()) as {
|
|
code: number;
|
|
success: boolean;
|
|
message: string;
|
|
data: { allowedBalance: number; balance: number };
|
|
time: string;
|
|
};
|
|
if (res.status !== 200 || rj.code !== 200 || !rj.success) {
|
|
console.log(
|
|
`[!] Error getting balance for ${userId} :: ${JSON.stringify(rj)}`,
|
|
);
|
|
return false;
|
|
}
|
|
return rj.data.balance;
|
|
} catch (err) {
|
|
console.log(err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export const getDealers = async (jwt: string, distributor_ids: string[]) => {
|
|
try {
|
|
// // Create an array of promises for each fetch request
|
|
const requests = distributor_ids.map(async (did) => {
|
|
await sleep(rng(100, 10000));
|
|
const res = await fetch(
|
|
`${constants.SCRAP_API_URL}/v1/user/dealer-list`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
...constants.SCRAP_API_BASE_HEADERS,
|
|
"Content-Type": "application/json",
|
|
"User-Agent": getRandomUserAgent(),
|
|
Authorization: jwt,
|
|
},
|
|
body: JSON.stringify({
|
|
page: 1,
|
|
pageSize: 999999,
|
|
parentDistributor: parseInt(did),
|
|
}),
|
|
},
|
|
);
|
|
const data = (await res.json()) as {
|
|
code: number;
|
|
success: boolean;
|
|
message: string;
|
|
data: {
|
|
items: any[];
|
|
total: number;
|
|
};
|
|
};
|
|
if (data.code !== 200 || !data.success) {
|
|
return {
|
|
dealers: [],
|
|
ok: false,
|
|
code: data.code,
|
|
message: data.message,
|
|
};
|
|
}
|
|
const dealers = data.data.items.map((item) => item.dealer);
|
|
|
|
// dumpDealers(dealers);
|
|
|
|
return {
|
|
dealers,
|
|
ok: res.status === 200 && data.success,
|
|
code: data.code,
|
|
message: data.message,
|
|
};
|
|
});
|
|
// // Wait for all promises to resolve
|
|
const responses = await Promise.all(requests);
|
|
const dealers: LooseApiUser[] = [];
|
|
const errors: { message: string }[] = [];
|
|
for (const res of responses) {
|
|
if (res.code !== 200 || !res.ok) {
|
|
errors.push({ message: res.message });
|
|
continue;
|
|
}
|
|
for (const dealer of res.dealers) {
|
|
dealers.push(dealer);
|
|
}
|
|
}
|
|
|
|
// fs.writeFileSync("dealers.json", JSON.stringify(dealers, null, 2));
|
|
|
|
return { dealers, errors };
|
|
} catch (err) {
|
|
console.error(err);
|
|
return {
|
|
dealers: [],
|
|
errors: [{ message: "An error occured during fetching dealers" }],
|
|
};
|
|
}
|
|
};
|
|
|
|
export const getDistributors = async (jwt: string) => {
|
|
const res = await fetch(
|
|
`${constants.SCRAP_API_URL}/v1/user/distributor-list`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
...constants.SCRAP_API_BASE_HEADERS,
|
|
Authorization: jwt,
|
|
"Content-Type": "application/json",
|
|
"User-Agent": getRandomUserAgent(),
|
|
},
|
|
body: JSON.stringify({
|
|
page: 1,
|
|
pageSize: 999999,
|
|
parentDistributor: 15,
|
|
}),
|
|
},
|
|
);
|
|
const json = (await res.json()) as {
|
|
code: number;
|
|
success: boolean;
|
|
message: string;
|
|
data: { total: number; items: any[] };
|
|
};
|
|
|
|
if (!json.data.items || json.code !== 200 || !json.success) {
|
|
return { ok: false, message: json.message, data: [] };
|
|
}
|
|
|
|
// fs.writeFileSync(
|
|
// "distributors.json",
|
|
// JSON.stringify(json.data.items, null, 2),
|
|
// );
|
|
|
|
// dumpDistributors(json.data.items.map((item) => item.distributor));
|
|
|
|
return {
|
|
ok: true,
|
|
message: "",
|
|
data: json.data.items.map((item) => item.distributor),
|
|
};
|
|
};
|
|
|
|
export const getDraws = async (jwt: string) => {
|
|
const res = await fetch(
|
|
`${constants.SCRAP_API_URL}/v1/draw/list-my?userId=15`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
...constants.SCRAP_API_BASE_HEADERS,
|
|
Authorization: jwt,
|
|
"Content-Type": "application/json",
|
|
"User-Agent": getRandomUserAgent(),
|
|
},
|
|
},
|
|
);
|
|
type J = {
|
|
code: number;
|
|
success: boolean;
|
|
message: string;
|
|
data: { draw: Draw }[];
|
|
};
|
|
let decoded = (await res.json()) as { data: J };
|
|
const json = (decoded.data.success ? decoded.data : decoded) as any as J;
|
|
if (json.code !== 200 || !json.success || !json.data) {
|
|
return { ok: false, message: json.message, data: [] };
|
|
}
|
|
return {
|
|
ok: true,
|
|
message: "",
|
|
data: json.data.map((item) => item.draw),
|
|
};
|
|
};
|
|
|
|
export const getData = async (
|
|
jwt: string,
|
|
userIds: number[],
|
|
drawId: number,
|
|
chosenDate: string,
|
|
) => {
|
|
const res = await fetch(`${constants.SCRAP_API_URL}/v1/book/list2`, {
|
|
method: "POST",
|
|
headers: {
|
|
...constants.SCRAP_API_BASE_HEADERS,
|
|
Authorization: jwt,
|
|
"Content-Type": "application/json",
|
|
"User-Agent": getRandomUserAgent(),
|
|
},
|
|
body: JSON.stringify({
|
|
userType: 3,
|
|
userIds,
|
|
drawId: drawId,
|
|
startDate: chosenDate,
|
|
endDate: chosenDate,
|
|
beAdmin: false,
|
|
containImported: false,
|
|
keyword: "",
|
|
}),
|
|
});
|
|
type J = {
|
|
code: number;
|
|
success: boolean;
|
|
message: string;
|
|
data: { book: BookingEntry; user: any }[];
|
|
};
|
|
let decoded = (await res.json()) as { data: J };
|
|
const json = (decoded.data.success ? decoded.data : decoded) as any as J;
|
|
if (json.code !== 200 || !json.success || !json.data) {
|
|
return { ok: false, message: json.message, data: [] };
|
|
}
|
|
return { ok: true, message: "", data: json.data.map((e) => e.book) };
|
|
};
|
|
|
|
export const mockGetUserData = async (
|
|
jwt: string,
|
|
userIds: number[],
|
|
drawId: number,
|
|
chosenDate: string,
|
|
) => {
|
|
const entries = [] as BookingEntry[];
|
|
|
|
const rng = (min: number, max: number) => {
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
};
|
|
const randomCeil = rng(10_000, 200_000);
|
|
|
|
await sleep(rng(100, 1000));
|
|
|
|
for (let i = 0; i < randomCeil; i++) {
|
|
const _f = rng(5, 50);
|
|
const _s = rng(5, 50);
|
|
const f = _f - (_f % 5);
|
|
const s = _s - (_s % 5);
|
|
|
|
entries.push({
|
|
id: getULID(),
|
|
bookDate: chosenDate,
|
|
changedBalance: f + s,
|
|
first: f,
|
|
second: s,
|
|
dealerId: userIds[rng(0, userIds.length - 1)],
|
|
distributorId: 6339,
|
|
drawId: drawId,
|
|
number: rng(0, 9999).toString(),
|
|
requestId: new Date().getTime().toString(),
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
sheetId: "0",
|
|
sheetName: "",
|
|
});
|
|
}
|
|
|
|
return { ok: true, message: "", data: entries };
|
|
};
|