done with upgrades..... I believe

This commit is contained in:
bootunloader
2025-01-25 23:41:57 +02:00
parent 15f2b238b0
commit aa73254a87
23 changed files with 2442 additions and 2368 deletions

View File

@@ -32,7 +32,7 @@
"ioredis": "^5.3.2", "ioredis": "^5.3.2",
"lucide-svelte": "^0.424.0", "lucide-svelte": "^0.424.0",
"node-fetch": "^3.3.2", "node-fetch": "^3.3.2",
"surrealdb.js": "^0.8.2", "surrealdb": "^1.1.0",
"svelte-french-toast": "^1.1.0", "svelte-french-toast": "^1.1.0",
"svelte-headlessui": "^0.0.20", "svelte-headlessui": "^0.0.20",
"tailwind-merge": "^2.4.0", "tailwind-merge": "^2.4.0",

50
pnpm-lock.yaml generated
View File

@@ -65,9 +65,9 @@ importers:
node-fetch: node-fetch:
specifier: ^3.3.2 specifier: ^3.3.2
version: 3.3.2 version: 3.3.2
surrealdb.js: surrealdb:
specifier: ^0.8.2 specifier: ^1.1.0
version: 0.8.4 version: 1.1.0(tslib@2.6.2)(typescript@5.2.2)(ws@8.14.2)
svelte-french-toast: svelte-french-toast:
specifier: ^1.1.0 specifier: ^1.1.0
version: 1.2.0(svelte@4.2.1) version: 1.2.0(svelte@4.2.1)
@@ -1141,6 +1141,11 @@ packages:
isexe@2.0.0: isexe@2.0.0:
resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==} resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==}
isows@1.0.6:
resolution: {integrity: sha512-lPHCayd40oW98/I0uvgaHKWCSvkzY27LjWLbtzOm64yQ+G3Q5npjjbdppU65iZXkK1Zt+kH9pfegli0AYfwYYw==}
peerDependencies:
ws: '*'
jiti@1.21.6: jiti@1.21.6:
resolution: {integrity: sha512-2yTgeWTWzMWkHu6Jp9NKgePDaYHbntiwvYuuJLbbN9vl7DC9DvXKOB2BC3ZZ92D3cvV/aflH0osDfwpHepQ53w==} resolution: {integrity: sha512-2yTgeWTWzMWkHu6Jp9NKgePDaYHbntiwvYuuJLbbN9vl7DC9DvXKOB2BC3ZZ92D3cvV/aflH0osDfwpHepQ53w==}
hasBin: true hasBin: true
@@ -1561,8 +1566,12 @@ packages:
resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==} resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==}
engines: {node: '>= 0.4'} engines: {node: '>= 0.4'}
surrealdb.js@0.8.4: surrealdb@1.1.0:
resolution: {integrity: sha512-ToCyBHxpVPGXth31ZktQvv+s7fvZG6+sR3mXHNAlhq0/43yYiYx3+3cYvCDGZQNBNUI42KENv8/aBQ5mGQZEEA==} resolution: {integrity: sha512-EAopFKeIo1lVCR+XEE/oMuh8LCPAIa2xC9MyVs5nw+BJyHc0Fr6F/UWVF9399LRtTBCoqe5G1W0hgxqLqkWaYA==}
engines: {node: '>=18.0.0'}
peerDependencies:
tslib: ^2.6.3
typescript: ^5.0.0
svelte-check@3.5.2: svelte-check@3.5.2:
resolution: {integrity: sha512-5a/YWbiH4c+AqAUP+0VneiV5bP8YOk9JL3jwvN+k2PEPLgpu85bjQc5eE67+eIZBBwUEJzmO3I92OqKcqbp3fw==} resolution: {integrity: sha512-5a/YWbiH4c+AqAUP+0VneiV5bP8YOk9JL3jwvN+k2PEPLgpu85bjQc5eE67+eIZBBwUEJzmO3I92OqKcqbp3fw==}
@@ -1760,12 +1769,6 @@ packages:
unplugin@1.5.0: unplugin@1.5.0:
resolution: {integrity: sha512-9ZdRwbh/4gcm1JTOkp9lAkIDrtOyOxgHmY7cjuwI8L/2RTikMcVG25GsZwNAgRuap3iDw2jeq7eoqtAsz5rW3A==} resolution: {integrity: sha512-9ZdRwbh/4gcm1JTOkp9lAkIDrtOyOxgHmY7cjuwI8L/2RTikMcVG25GsZwNAgRuap3iDw2jeq7eoqtAsz5rW3A==}
unws@0.2.4:
resolution: {integrity: sha512-/N1ajiqrSp0A/26/LBg7r10fOcPtGXCqJRJ61sijUFoGZMr6ESWGYn7i0cwr7fR7eEECY5HsitqtjGHDZLAu2w==}
engines: {node: '>=16.14.0'}
peerDependencies:
ws: '*'
update-browserslist-db@1.1.0: update-browserslist-db@1.1.0:
resolution: {integrity: sha512-EdRAaAyk2cUE1wOf2DkEhzxqOQvFOoRJFNS6NeyJ01Gp2beMRpBAINjM2iDXE3KCuKhwnvHIQCJm6ThL2Z+HzQ==} resolution: {integrity: sha512-EdRAaAyk2cUE1wOf2DkEhzxqOQvFOoRJFNS6NeyJ01Gp2beMRpBAINjM2iDXE3KCuKhwnvHIQCJm6ThL2Z+HzQ==}
hasBin: true hasBin: true
@@ -1779,6 +1782,10 @@ packages:
resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==} resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==}
hasBin: true hasBin: true
uuidv7@1.0.2:
resolution: {integrity: sha512-8JQkH4ooXnm1JCIhqTMbtmdnYEn6oKukBxHn1Ic9878jMkL7daTI7anTExfY18VRCX7tcdn5quzvCb6EWrR8PA==}
hasBin: true
vite-node@0.33.0: vite-node@0.33.0:
resolution: {integrity: sha512-19FpHYbwWWxDr73ruNahC+vtEdza52kA90Qb3La98yZ0xULqV8A5JLNPUff0f5zID4984tW7l3DH2przTJUZSw==} resolution: {integrity: sha512-19FpHYbwWWxDr73ruNahC+vtEdza52kA90Qb3La98yZ0xULqV8A5JLNPUff0f5zID4984tW7l3DH2przTJUZSw==}
engines: {node: '>=v14.18.0'} engines: {node: '>=v14.18.0'}
@@ -2830,6 +2837,10 @@ snapshots:
isexe@2.0.0: {} isexe@2.0.0: {}
isows@1.0.6(ws@8.14.2):
dependencies:
ws: 8.14.2
jiti@1.21.6: {} jiti@1.21.6: {}
jsonc-parser@3.2.0: {} jsonc-parser@3.2.0: {}
@@ -3202,13 +3213,14 @@ snapshots:
supports-preserve-symlinks-flag@1.0.0: {} supports-preserve-symlinks-flag@1.0.0: {}
surrealdb.js@0.8.4: surrealdb@1.1.0(tslib@2.6.2)(typescript@5.2.2)(ws@8.14.2):
dependencies: dependencies:
unws: 0.2.4(ws@8.14.2) isows: 1.0.6(ws@8.14.2)
ws: 8.14.2 tslib: 2.6.2
typescript: 5.2.2
uuidv7: 1.0.2
transitivePeerDependencies: transitivePeerDependencies:
- bufferutil - ws
- utf-8-validate
svelte-check@3.5.2(postcss-load-config@4.0.1(postcss@8.4.40))(postcss@8.4.40)(svelte@4.2.1): svelte-check@3.5.2(postcss-load-config@4.0.1(postcss@8.4.40))(postcss@8.4.40)(svelte@4.2.1):
dependencies: dependencies:
@@ -3426,10 +3438,6 @@ snapshots:
webpack-sources: 3.2.3 webpack-sources: 3.2.3
webpack-virtual-modules: 0.5.0 webpack-virtual-modules: 0.5.0
unws@0.2.4(ws@8.14.2):
dependencies:
ws: 8.14.2
update-browserslist-db@1.1.0(browserslist@4.23.3): update-browserslist-db@1.1.0(browserslist@4.23.3):
dependencies: dependencies:
browserslist: 4.23.3 browserslist: 4.23.3
@@ -3440,6 +3448,8 @@ snapshots:
uuid@9.0.1: {} uuid@9.0.1: {}
uuidv7@1.0.2: {}
vite-node@0.33.0(@types/node@20.6.4): vite-node@0.33.0(@types/node@20.6.4):
dependencies: dependencies:
cac: 6.7.14 cac: 6.7.14

View File

@@ -1,5 +1,5 @@
import Surreal from "surrealdb.js"; import Surreal, { RecordId, StringRecordId } from "surrealdb";
export type { QueryResult } from "surrealdb.js/script/types"; export type { QueryResult } from "surrealdb";
try { try {
if (document || window) { if (document || window) {
@@ -15,22 +15,18 @@ const CONFIG = {
db: process.env.SURREAL_DB ?? "", db: process.env.SURREAL_DB ?? "",
} as const; } as const;
// for (let key in CONFIG) { const db = new Surreal();
// if (
// !CONFIG[key as keyof typeof CONFIG] ||
// CONFIG[key as keyof typeof CONFIG] === ""
// ) {
// throw new Error(`Missing configuration for ${key}`);
// }
// }
let _surreal = export function parseToRID(idStr: string) {
CONFIG.url.length > 0 return new StringRecordId(idStr);
? new Surreal(`http://${CONFIG.url}/rpc`, { // const [a, b] = idStr.split(":");
auth: { user: CONFIG.user, pass: CONFIG.pass }, // return new RecordId(a, b);
ns: CONFIG.ns, }
db: CONFIG.db,
})
: undefined;
export const surreal = _surreal as Surreal; if (CONFIG.url.length > 0) {
await db.connect(`http://${CONFIG.url}/rpc`);
await db.use({ namespace: CONFIG.ns, database: CONFIG.db });
await db.signin({ username: CONFIG.user, password: CONFIG.pass });
}
export const surreal = db as Surreal;

View File

@@ -10,6 +10,77 @@ const upsertData = async (
data: BookingEntry[], data: BookingEntry[],
date: string, date: string,
tries: number = 0, tries: number = 0,
): Promise<void> => {
if (tries >= 3) {
console.log("Max tries exceeded for processing data");
return;
}
const tableName = getTableName(date);
const drawId = data[0].drawId;
console.log(`[...] Processing ${data.length} entries for ${tableName}`);
try {
// Delete existing entries for this date
console.log(
`[...] Deleting existing entries for ${date} in ${tableName}`,
);
console.time("deletion time");
await surreal.query(
`DELETE type::table($tableName) WHERE bookDate = $bookDate AND drawId = $drawId`,
{ tableName, bookDate: date, drawId },
);
console.timeEnd("deletion time");
// Prepare new entries
const entries = data.map((entry) => ({
...entry,
id: `${tableName}:${entry.id}`,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
bookDate: entry.bookDate.split(" ")[0],
requestId: entry.requestId ?? "",
}));
// Calculate chunk size (5 to 25% of total data length, max 10,000)
let chunkSize = Math.floor(
Math.random() * (data.length * 0.2 - data.length * 0.05) +
data.length * 0.05,
);
if (chunkSize > 1_000) {
chunkSize = 1_000;
}
console.log(`Chunk Size: ${chunkSize}`);
console.log(
`[+] Inserting ${entries.length} entries into ${tableName}`,
);
// Insert new entries in chunks
console.time("insertion time");
const chunks = chunkArray(entries, chunkSize).map(async (chunk) => {
await surreal.insert<BookingEntry>(tableName, chunk);
});
for (let i = 0; i < chunks.length; i += 2) {
await Promise.all(chunks.slice(i, i + 2));
}
console.timeEnd("insertion time");
console.log(
`[+] Successfully processed ${data.length} entries into ${tableName}`,
);
} catch (err) {
console.log("Failed to process data, attempting retry");
return await upsertData(data, date, tries + 1);
}
};
const upsertDataDED = async (
data: BookingEntry[],
date: string,
tries: number = 0,
): Promise<void> => { ): Promise<void> => {
const tableName = getTableName(date); const tableName = getTableName(date);
console.log(`[...] Upserting ${data.length} entries into ${tableName}`); console.log(`[...] Upserting ${data.length} entries into ${tableName}`);
@@ -19,13 +90,15 @@ const upsertData = async (
`select value id from type::table($tableName) where bookDate = $bookDate`, `select value id from type::table($tableName) where bookDate = $bookDate`,
{ tableName, bookDate: date }, { tableName, bookDate: date },
); );
for (let eId of alreadyPresent.result ?? []) { for (let eId of alreadyPresent ?? []) {
alreadyPresentIds.add(eId); alreadyPresentIds.add(eId);
} }
} catch (err) { } catch (err) {
console.log("Failed to fetch, seeing if can try again"); console.log("Failed to fetch, seeing if can try again");
if (tries >= 3) { if (tries >= 3) {
console.log("Max tries exceeded for initial fetch for upserting data"); console.log(
"Max tries exceeded for initial fetch for upserting data",
);
return; return;
} }
return await upsertData(data, date, tries++); return await upsertData(data, date, tries++);
@@ -143,10 +216,7 @@ const getBookingEntriesForDealer = async (
data, data,
)} entries for ${userId}, filters are ${date}, ${drawId} for ${tableName}`, )} entries for ${userId}, filters are ${date}, ${drawId} for ${tableName}`,
); );
if (data.status === "OK") { return data ?? [];
return data.result ?? [];
}
return [];
}; };
const getBookingEntriesByDraw = async (date: string, drawId: string) => { const getBookingEntriesByDraw = async (date: string, drawId: string) => {
@@ -156,13 +226,12 @@ const getBookingEntriesByDraw = async (date: string, drawId: string) => {
{ {
tableName, tableName,
date: date, date: date,
drawId: parseInt(drawId.includes(":") ? drawId.split(":")[1] : drawId), drawId: parseInt(
drawId.includes(":") ? drawId.split(":")[1] : drawId,
),
}, },
); );
if (data.status === "OK") { return data ?? [];
return data.result ?? [];
}
return [];
}; };
const deleteDataOlderThan2Weeks = async () => { const deleteDataOlderThan2Weeks = async () => {

View File

@@ -1,7 +1,7 @@
import { constants } from "$lib/utils/constants"; import { constants } from "$lib/utils/constants";
import type { Draw } from "$lib/utils/data.types"; import type { Draw } from "$lib/utils/data.types";
import { getDraws } from "../external/api.scraping.helpers"; import { getDraws } from "../external/api.scraping.helpers";
import { surreal } from "../connectors/surreal.db"; import { parseToRID, surreal } from "../connectors/surreal.db";
import { getSessionFromStore } from "../utils/session.service"; import { getSessionFromStore } from "../utils/session.service";
const tableName = "apidraw"; const tableName = "apidraw";
@@ -40,28 +40,27 @@ const getAllDraws = async (
skipOptional?: boolean, skipOptional?: boolean,
retry: number = 0, retry: number = 0,
): Promise<Draw[]> => { ): Promise<Draw[]> => {
let query = `select * from apidraw order by closeTime`; const [out] = await surreal.query<[Draw[]]>(
const [out] = await surreal.query<[Draw[]]>(query); `select * from apidraw order by closeTime`,
if (out.status === "OK") { );
const draws = out.result ?? []; if (out.length > 0) {
if (draws.length > 0) { return out;
return draws;
} }
await _populateDrawsTable(); await _populateDrawsTable();
if (retry < 3) { if (retry < 3) {
return getAllDraws(skipOptional, retry + 1); return getAllDraws(skipOptional, retry + 1);
} }
}
return []; return [];
}; };
async function setFilterDuplicatesFlag(drawId: string, flag: boolean) { async function setFilterDuplicatesFlag(drawId: string, flag: boolean) {
const [d] = await surreal.select<Draw>(drawId); const rid = parseToRID(drawId);
const d = await surreal.select<Draw>(rid);
if (!d || !d.id) { if (!d || !d.id) {
return; return;
} }
console.log("setFilterDuplicatesFlag :: ", drawId, flag); console.log("setFilterDuplicatesFlag :: ", rid, flag);
await surreal.update(drawId, { await surreal.update(rid, {
...d, ...d,
filterDuplicatesWhilePosting: flag, filterDuplicatesWhilePosting: flag,
updatedAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
@@ -69,12 +68,14 @@ async function setFilterDuplicatesFlag(drawId: string, flag: boolean) {
} }
async function updateDrawPresetInfo(draw: Draw) { async function updateDrawPresetInfo(draw: Draw) {
const drawId = draw.id; const drawId = parseToRID(draw.id);
const [d] = await surreal.select<Draw>(drawId); const d = await surreal.select<Draw>(drawId);
if (!d || !d.id) { if (!d || !d.id) {
console.log(`Draw not present for ${drawId}`);
return; return;
} }
await surreal.update(drawId, { console.log(`Updating draw info ${drawId}`);
const out = await surreal.update(drawId, {
...d, ...d,
filterDuplicatesWhilePosting: draw.filterDuplicatesWhilePosting, filterDuplicatesWhilePosting: draw.filterDuplicatesWhilePosting,
abRateF: draw.abRateF, abRateF: draw.abRateF,
@@ -87,9 +88,9 @@ async function updateDrawPresetInfo(draw: Draw) {
const getDraw = async (drawId: string): Promise<Draw | undefined> => { const getDraw = async (drawId: string): Promise<Draw | undefined> => {
const draws = await surreal.select<Draw>( const draws = await surreal.select<Draw>(
drawId.includes("apidraw") ? drawId : `apidraw:${drawId}`, parseToRID(drawId.includes("apidraw") ? drawId : `apidraw:${drawId}`),
); );
return draws[0]; return draws;
}; };
export const dbDraw = { export const dbDraw = {

View File

@@ -12,8 +12,8 @@ const upsertData = async (data: PostDataHistory) => {
{ tableName, bookDate: data.bookDate, drawId: data.drawId }, { tableName, bookDate: data.bookDate, drawId: data.drawId },
); );
console.log(check); console.log(check);
const firstOut = check.result ? check.result[0] : undefined; const firstOut = check && check.length > 0 ? check[0] : undefined;
if (check.status === "OK" && !!firstOut && !!firstOut.id) { if (check.length > 0 && !!firstOut && !!firstOut.id) {
console.log( console.log(
`Adding ${data.data.length} entries to ${firstOut.data.length} existing array`, `Adding ${data.data.length} entries to ${firstOut.data.length} existing array`,
); );
@@ -42,13 +42,15 @@ const getPostDataByDraw = async (date: string, drawId: string) => {
`select * from type::table($tableName) where bookDate = $date and drawId = $drawId`, `select * from type::table($tableName) where bookDate = $date and drawId = $drawId`,
{ {
tableName, tableName,
date: date, date,
drawId: parseInt(drawId.includes(":") ? drawId.split(":")[1] : drawId), drawId: parseInt(
drawId.includes(":") ? drawId.split(":")[1] : drawId,
),
}, },
); );
let out = [] as PostDataEntry[]; let out = [] as PostDataEntry[];
if (data.status === "OK" && data.result.length > 0) { if (data.length > 0) {
out = data.result[0].data; out = data[0].data;
} }
return out; return out;
}; };
@@ -60,14 +62,13 @@ async function doesPostHistoryDataExist(date: string, drawId: string) {
{ {
tableName, tableName,
date: date, date: date,
drawId: parseInt(drawId.includes(":") ? drawId.split(":")[1] : drawId), drawId: parseInt(
drawId.includes(":") ? drawId.split(":")[1] : drawId,
),
}, },
); );
if (data.status === "OK") { return data[0]?.id.length > 0;
return data.result[0]?.id.length > 0;
}
return false;
} }
async function deletePostDataByDraw(date: string, drawId: string) { async function deletePostDataByDraw(date: string, drawId: string) {
@@ -77,11 +78,13 @@ async function deletePostDataByDraw(date: string, drawId: string) {
{ {
tableName, tableName,
date: date, date: date,
drawId: parseInt(drawId.includes(":") ? drawId.split(":")[1] : drawId), drawId: parseInt(
drawId.includes(":") ? drawId.split(":")[1] : drawId,
),
}, },
); );
if (data.status === "OK") { if (data.length > 0) {
await surreal.delete(tableName); await surreal.delete(tableName);
return true; return true;
} }

View File

@@ -5,43 +5,41 @@ import {
type ApiPostUserWithParent, type ApiPostUserWithParent,
ApiUserTypes, ApiUserTypes,
DEFAULT_RANDOM_DISTRIBUTOR, DEFAULT_RANDOM_DISTRIBUTOR,
zApiPostUser,
} from "$lib/utils/data.types"; } from "$lib/utils/data.types";
import { surreal } from "../connectors/surreal.db"; import { parseToRID, surreal } from "../connectors/surreal.db";
const getUserById = async (userId: string) => { const getUserById = async (userId: string) => {
const query = `select * from apiuser where id = $id`; return await surreal.select<ApiUser>(parseToRID(userId));
const [rizzult] = await surreal.query<[ApiUser[]]>(query, { id: userId }); // const [rizzult] = await surreal.query<[ApiUser[]]>(query, { id: userId });
return rizzult.result?.[0]; // return rizzult[0];
}; };
const getAllIdsByUserType = async (userType: number) => { const getAllIdsByUserType = async (userType: number) => {
const query = `select value id from apiuser where userType = $userType`; const query = `select value id from apiuser where userType = $userType`;
const rizzult = (await surreal.query<[string[]]>(query, { userType }))[0]; const [rizzult] = await surreal.query<[any[]]>(query, { userType });
return (rizzult.result ?? []).map((e) => { return (rizzult ?? []).map((e) => {
return e.split(":")[1]; return e.id;
}); });
}; };
async function allUsersOfTypeLimitedInfo(userType: number) { async function allUsersOfTypeLimitedInfo(userType: number) {
const rizzult = ( const [rizzult] = await surreal.query<[ApiPostUser[]]>(
await surreal.query<[ApiPostUser[]]>(
`select id,userName,userId,postData from apiuser where userType = $userType`, `select id,userName,userId,postData from apiuser where userType = $userType`,
{ userType: userType }, { userType: userType },
) );
)[0]; return rizzult ?? [];
if (rizzult.status == "OK") {
return rizzult.result ?? [];
}
return [];
} }
async function setPostDataFlagForUsers(users: ApiPostUser[]) { async function setPostDataFlagForUsers(users: ApiPostUser[]) {
for (const user of users) { for (const user of users) {
const [u] = await surreal.select<ApiUser>(user.id); const uid = parseToRID(user.id);
const u = await surreal.select<ApiUser>(uid);
if (!u || !u.id) { if (!u || !u.id) {
continue; continue;
} }
await surreal.update<LooseApiUser>(user.id, { console.log("Updating user :: ", uid);
await surreal.update<LooseApiUser>(uid, {
...u, ...u,
postData: user.postData ?? false, postData: user.postData ?? false,
}); });
@@ -53,19 +51,21 @@ const getUserTypeCount = async (userType: number) => {
let query = `${queryBase} and disable = 0 group all`; let query = `${queryBase} and disable = 0 group all`;
let disabledQuery = `${queryBase} and disable = 1 group all`; let disabledQuery = `${queryBase} and disable = 1 group all`;
const enabledRizzult = ( const enabledRizzult = (
await surreal.query<[{ count: number }[]]>(query, { userType: userType }) await surreal.query<[{ count: number }[]]>(query, {
userType: userType,
})
)[0]; )[0];
const count = { enabled: 0, disabled: 0 }; const count = { enabled: 0, disabled: 0 };
if (enabledRizzult.status == "OK") { if (enabledRizzult.length > 0) {
count.enabled = enabledRizzult.result[0]?.count ?? 0; count.enabled = enabledRizzult[0]?.count ?? 0;
} }
const disabledRizzult = ( const disabledRizzult = (
await surreal.query<[{ count: number }[]]>(disabledQuery, { await surreal.query<[{ count: number }[]]>(disabledQuery, {
userType: userType, userType: userType,
}) })
)[0]; )[0];
if (disabledRizzult.status == "OK") { if (disabledRizzult.length > 0) {
count.disabled = disabledRizzult.result[0]?.count ?? 0; count.disabled = disabledRizzult[0]?.count ?? 0;
} }
return count; return count;
}; };
@@ -77,10 +77,7 @@ const allUsersOfType = async (userType: number) => {
{ userType: userType }, { userType: userType },
) )
)[0]; )[0];
if (rizzult.status == "OK") { return rizzult ?? [];
return rizzult.result ?? [];
}
return [];
}; };
async function updatePostUsersBalances( async function updatePostUsersBalances(
@@ -90,10 +87,10 @@ async function updatePostUsersBalances(
console.log(payload); console.log(payload);
for (const each of payload) { for (const each of payload) {
const [rizzult] = await surreal.query<[ApiUser[]]>( const [rizzult] = await surreal.query<[ApiUser[]]>(
`update $userId set balance = $balance`, `update type::table($tableName) set balance = $balance`,
{ userId: each.id, balance: each.balance }, { tableName: each.id, balance: each.balance },
); );
if (rizzult.status !== "OK") { if (rizzult.length < 1) {
console.error("updatePostUsersBalance :: ", rizzult); console.error("updatePostUsersBalance :: ", rizzult);
} }
} }
@@ -104,41 +101,27 @@ async function getAllPostUsers() {
const [rizzult] = await surreal.query<[ApiPostUser[]]>( const [rizzult] = await surreal.query<[ApiPostUser[]]>(
`select id,userName,userId,postData from apiuser where postData = true`, `select id,userName,userId,postData from apiuser where postData = true`,
); );
if (rizzult.status === "OK") { return rizzult ?? [];
return rizzult.result ?? [];
}
return [];
} }
async function getAllPostUsersWithParentUsers() { async function getAllPostUsersWithParentUsers() {
const [rizzult] = await surreal.query<[ApiPostUserWithParent[]]>( const [rizzult] = await surreal.query<[ApiPostUserWithParent[]]>(
`select id,userName,userId,postData,parentDistributor,parentAdmin from apiuser where postData = true`, `select id,userName,userId,postData,parentDistributor,parentAdmin from apiuser where postData = true`,
); );
if (rizzult.status === "OK") { const out = [];
return rizzult.result ?? []; for (const each of rizzult) {
out.push({ ...each, id: each.id.toString() });
} }
return []; return out;
// return rizzult ?? [];
} }
const getAllDistributorsWithTheirChildren = async () => {
const distributorIds = await getAllIdsByUserType(ApiUserTypes.DISTRIBUTOR);
const out = distributorIds.map(async (id) => {
const [rizzult] = await surreal.query<[ApiUser[]]>(
`select *, (select * from apiuser where parentDistributor = $id) as children from apiuser where id = $prefixedId`,
{ id, prefixedId: `apiuser:${id}` },
);
if (rizzult.status == "OK") {
return rizzult.result[0];
}
return undefined;
});
const responses = await Promise.all(out);
return responses;
};
const getRandomDistributor = async (): Promise<ApiUser> => { const getRandomDistributor = async (): Promise<ApiUser> => {
const ignoreList = ["001OP9"]; const ignoreList = ["001OP9"];
const randomUser = await _getRandomUser(ApiUserTypes.DISTRIBUTOR, ignoreList); const randomUser = await _getRandomUser(
ApiUserTypes.DISTRIBUTOR,
ignoreList,
);
if (!randomUser) { if (!randomUser) {
console.log("getting random distributor...."); console.log("getting random distributor....");
return DEFAULT_RANDOM_DISTRIBUTOR; return DEFAULT_RANDOM_DISTRIBUTOR;
@@ -156,16 +139,12 @@ const _getRandomUser = async (
ignoreList: string[], ignoreList: string[],
): Promise<ApiUser | undefined> => { ): Promise<ApiUser | undefined> => {
console.log("_getRandomUser :: ", userType); console.log("_getRandomUser :: ", userType);
const rizzult = ( const [rizzult] = await surreal.query<[ApiUser[]]>(
await surreal.query<[ApiUser[]]>(
`select * from apiuser where disable = 0 and userType = $userType and userId notinside $ignoreList order by rand() limit 1`, `select * from apiuser where disable = 0 and userType = $userType and userId notinside $ignoreList order by rand() limit 1`,
{ userType: userType, ignoreList: ignoreList }, { userType: userType, ignoreList: ignoreList },
) );
)[0];
if (rizzult.status == "OK") {
console.log("found random user"); console.log("found random user");
return rizzult.result[0]; return rizzult[0];
}
}; };
const doesExist = async (userId?: string) => { const doesExist = async (userId?: string) => {
@@ -175,9 +154,7 @@ const doesExist = async (userId?: string) => {
"select count() from apiuser where userId = $userId group all", "select count() from apiuser where userId = $userId group all",
{ userId: userId }, { userId: userId },
); );
if (rizzult.status == "OK") { return rizzult?.count > 0;
return rizzult.result?.count > 0;
}
} }
return false; return false;
}; };
@@ -200,7 +177,9 @@ const insertMany = async (data: LooseApiUser[], postUsers: ApiPostUser[]) => {
async function upsertMany( async function upsertMany(
data: LooseApiUser[], data: LooseApiUser[],
wipeTable: boolean, wipeTable: boolean,
deleteUserType: typeof ApiUserTypes.DISTRIBUTOR | typeof ApiUserTypes.DEALER, deleteUserType:
| typeof ApiUserTypes.DISTRIBUTOR
| typeof ApiUserTypes.DEALER,
) { ) {
const postUsers = await getAllPostUsers(); const postUsers = await getAllPostUsers();
console.log(postUsers); console.log(postUsers);
@@ -217,14 +196,17 @@ async function upsertMany(
if (apiUser.disable === 1) { if (apiUser.disable === 1) {
return; return;
} }
const [u] = await surreal.select<ApiUser>(`apiuser:${apiUser.id}`); const uid = parseToRID(`apiuser:${apiUser.id}`);
const u = await surreal.select<ApiUser>(uid);
if (!u || !u.id) { if (!u || !u.id) {
toCreate.push(apiUser); toCreate.push(apiUser);
return; return;
} }
let postData = let postData =
u.postData ?? !!postUsers.find((pu) => pu.userId === u.userId) ?? false; u.postData ??
const qId = u.id; !!postUsers.find((pu) => pu.userId === u.userId) ??
false;
const qId = parseToRID(u.id);
await surreal.update<LooseApiUser>(qId, { await surreal.update<LooseApiUser>(qId, {
id: u.id, id: u.id,
userId: apiUser.userId, userId: apiUser.userId,
@@ -267,7 +249,6 @@ export const dbApiUser = {
allUsersOfType, allUsersOfType,
allUsersOfTypeLimitedInfo, allUsersOfTypeLimitedInfo,
getUserById, getUserById,
getAllDistributorsWithTheirChildren,
getUserTypeCount, getUserTypeCount,
getAllIdsByUserType, getAllIdsByUserType,
getAllPostUsers, getAllPostUsers,

View File

@@ -1,4 +0,0 @@
export const dbBooking = {
};

View File

@@ -1,5 +1,5 @@
import type { FinalSheetData } from "$lib/utils/data.types"; import type { FinalSheetData } from "$lib/utils/data.types";
import { surreal } from "../connectors/surreal.db"; import { parseToRID, surreal } from "../connectors/surreal.db";
const getTableName = (date: string) => { const getTableName = (date: string) => {
return `finalsheet${date.replaceAll("-", "")}`; return `finalsheet${date.replaceAll("-", "")}`;
@@ -9,22 +9,23 @@ const upsertData = async (data: FinalSheetData, date: string) => {
const tableName = getTableName(date); const tableName = getTableName(date);
const [present] = await surreal.query<[FinalSheetData[]]>( const [present] = await surreal.query<[FinalSheetData[]]>(
`select id from type::table($tableName) where date = $date and drawId = $drawId`, `select id from type::table($tableName) where date = $date and drawId = $drawId`,
{ tableName, date: `${date}`, drawId: data.drawId } { tableName, date: `${date}`, drawId: data.drawId },
); );
const id = parseToRID(`${tableName}:${data.id}`);
if (present) { if (present) {
// @ts-ignore // @ts-ignore
await surreal.update<FinalSheetData>(`${tableName}:${data.id}`, { await surreal.update<FinalSheetData>(id, {
date: data.date, date: data.date,
drawId: data.drawId, drawId: data.drawId,
data: data.data, data: data.data,
totals: data.totals, totals: data.totals,
// @ts-ignore // @ts-ignore
createdAt: present?.result[0]?.createdAt ?? new Date().toISOString(), createdAt: present[0]?.createdAt ?? new Date().toISOString(),
updatedAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
}); });
} else { } else {
// @ts-ignore // @ts-ignore
await surreal.create<FinalSheetData>(`${tableName}:${data.id}`, { await surreal.create<FinalSheetData>(id, {
date: data.date, date: data.date,
drawId: data.drawId, drawId: data.drawId,
data: data.data, data: data.data,
@@ -35,6 +36,4 @@ const upsertData = async (data: FinalSheetData, date: string) => {
} }
}; };
export const dbFinalSheet = { export const dbFinalSheet = { upsertData };
upsertData,
};

View File

@@ -1,8 +1,4 @@
import type { import type { PresetDataEntry } from "$lib/utils/data.types";
ApiPostUser,
PostDataEntry,
PresetDataEntry,
} from "$lib/utils/data.types";
import { surreal } from "../connectors/surreal.db"; import { surreal } from "../connectors/surreal.db";
const getTableName = (date: string) => { const getTableName = (date: string) => {
@@ -12,6 +8,7 @@ const getTableName = (date: string) => {
const insertData = async (data: PresetDataEntry[]) => { const insertData = async (data: PresetDataEntry[]) => {
if (data.length < 1) return; if (data.length < 1) return;
const tableName = getTableName(data[0].bookDate); const tableName = getTableName(data[0].bookDate);
console.log(`Inserting ${data.length} rows in ${tableName}`);
const out = await surreal.insert<PresetDataEntry>(tableName, data); const out = await surreal.insert<PresetDataEntry>(tableName, data);
console.log( console.log(
`[+] Inserted post data in ${tableName} for ${data[0].bookDate} - ${data[0].drawId}`, `[+] Inserted post data in ${tableName} for ${data[0].bookDate} - ${data[0].drawId}`,
@@ -25,7 +22,7 @@ const getDataByDraw = async (date: string, drawId: number) => {
`select * from type::table($tableName) where bookDate = $date and drawId = $drawId`, `select * from type::table($tableName) where bookDate = $date and drawId = $drawId`,
{ tableName, date, drawId }, { tableName, date, drawId },
); );
return data.result || ([] as PresetDataEntry[]); return data || ([] as PresetDataEntry[]);
}; };
const getDataGroupedBySheetByDraw = async (date: string, drawId: number) => { const getDataGroupedBySheetByDraw = async (date: string, drawId: number) => {
@@ -37,9 +34,9 @@ const getDataGroupedBySheetByDraw = async (date: string, drawId: number) => {
const out = { const out = {
abData: [] as PresetDataEntry[], abData: [] as PresetDataEntry[],
abcData: [] as PresetDataEntry[], abcData: [] as PresetDataEntry[],
all: data.result || ([] as PresetDataEntry[]), all: data || ([] as PresetDataEntry[]),
}; };
for (const row of data.result ?? []) { for (const row of data ?? []) {
if (row.number.length === 2) { if (row.number.length === 2) {
out.abData.push(row); out.abData.push(row);
} else if (row.number.length === 3) { } else if (row.number.length === 3) {

View File

@@ -1,16 +1,14 @@
import type { User } from "$lib/utils/data.types"; import type { User } from "$lib/utils/data.types";
import { surreal, type QueryResult } from "../connectors/surreal.db"; import { surreal } from "../connectors/surreal.db";
export const dbUser = { export const dbUser = {
doesExist: async (username?: string) => { doesExist: async (username?: string) => {
if (username) { if (username) {
const [rizzult] = await surreal.query<{ count: number }[]>( const [rizzult] = await surreal.query<{ count: number }[]>(
"select count() from user where username = $username group all", "select count() from user where username = $username group all",
{ username: username } { username },
); );
if (rizzult.status == "OK") { return rizzult?.count > 0;
return rizzult.result?.count > 0;
}
} }
return false; return false;
}, },
@@ -26,7 +24,7 @@ export const dbUser = {
return [{ message: "User already exists." }]; return [{ message: "User already exists." }];
} }
const { username, password, association, userType } = data; const { username, password, association, userType } = data;
const out = await surreal.create<any>(`user:ulid()`, { const out = await surreal.create<any>(`user`, {
createdAt: Date.now().toString(), createdAt: Date.now().toString(),
updatedAt: Date.now().toString(), updatedAt: Date.now().toString(),
username, username,
@@ -50,21 +48,19 @@ export const dbUser = {
const rizzult = ( const rizzult = (
await surreal.query<[User[]]>( await surreal.query<[User[]]>(
`select * from user where username = $username`, `select * from user where username = $username`,
{ username: d.username } { username: d.username },
) )
)[0]; )[0];
if (rizzult.status == "OK") { return rizzult[0];
return rizzult.result[0];
}
} }
return undefined; return undefined;
}, },
getChildren: async (username?: string) => { getChildren: async (username?: string) => {
const rizzult = await surreal.query<User[]>( const [rizzult] = await surreal.query<User[]>(
`select * from user where association = $username`, `select * from user where association = $username`,
{ username: username } { username: username },
); );
return getParsedUsers(rizzult); return rizzult;
}, },
update: async (id: string, data: { association: string }) => { update: async (id: string, data: { association: string }) => {
const [rizzult] = await surreal.update<User>(`user:${id}`, { const [rizzult] = await surreal.update<User>(`user:${id}`, {
@@ -78,13 +74,3 @@ export const dbUser = {
return out[0].id; return out[0].id;
}, },
}; };
const getParsedUsers = (data: QueryResult<User>[]) => {
const users = [] as User[];
for (const each of data) {
if (each.status == "OK") {
users.push(each.result);
}
}
return users;
};

View File

@@ -72,7 +72,9 @@ export async function postDataToApi(payload: {
return { return {
ok: false, ok: false,
detail: "User not found to post data with", detail: "User not found to post data with",
errors: [{ message: "User not found for request" }] as ServerError, errors: [
{ message: "User not found for request" },
] as ServerError,
}; };
} }
if (!dataByUser[userId]) { if (!dataByUser[userId]) {
@@ -87,7 +89,9 @@ export async function postDataToApi(payload: {
const session = payload.sessions[userId]; const session = payload.sessions[userId];
const usr = payload.users.find((u) => u.userId === userId); const usr = payload.users.find((u) => u.userId === userId);
if (!usr) { if (!usr) {
throw new Error(`User ${userId} not found for posting to api`); throw new Error(
`User ${userId} not found for posting to api`,
);
} }
const distId = usr.parentDistributor ?? 0; const distId = usr.parentDistributor ?? 0;
@@ -96,7 +100,10 @@ export async function postDataToApi(payload: {
const date = new Date().toISOString().split("T")[0]; const date = new Date().toISOString().split("T")[0];
let ptr = 0; let ptr = 0;
const userResponseIds = [] as { requestId: number; bookId: string }[]; const userResponseIds = [] as {
requestId: number;
bookId: string;
}[];
while (ptr < userData.length) { while (ptr < userData.length) {
let tries = 0; let tries = 0;
@@ -109,7 +116,7 @@ export async function postDataToApi(payload: {
drawId, drawId,
date, date,
); );
const res = await sendBatchRequest( const res = await mockSendBatchRequest(
session, session,
dealerId, dealerId,
payload.draw, payload.draw,
@@ -118,13 +125,18 @@ export async function postDataToApi(payload: {
); );
let rj: let rj:
| APIRespnose<{ | APIRespnose<{
bookDtos: { bookId: string; requestId: number }[]; bookDtos: {
bookId: string;
requestId: number;
}[];
}> }>
| undefined = undefined; | undefined = undefined;
try { try {
rj = (await res.json()) as any; rj = (await res.json()) as any;
} catch (err) { } catch (err) {
console.log("Encountered error while parsing post response"); console.log(
"Encountered error while parsing post response",
);
console.log(res.status, err); console.log(res.status, err);
} }
if (rj && rj.code === 200 && res.status === 200) { if (rj && rj.code === 200 && res.status === 200) {
@@ -155,7 +167,9 @@ export async function postDataToApi(payload: {
}); });
console.log(await out.text()); console.log(await out.text());
} }
throw new Error(`Failed to send data to api for user ${userId}`); throw new Error(
`Failed to send data to api for user ${userId}`,
);
} }
} }
return userResponseIds; return userResponseIds;

View File

@@ -44,9 +44,12 @@ function filterMatching(
export async function updateBalanceOfPostUsers(users: ApiPostUserWithParent[]) { export async function updateBalanceOfPostUsers(users: ApiPostUserWithParent[]) {
const sessions = await getAllSessions(); const sessions = await getAllSessions();
console.log(sessions);
console.log(`Fetching balances`);
const balances = [] as { id: string; balance: number }[]; const balances = [] as { id: string; balance: number }[];
for (const user of users) { for (const user of users) {
const session = sessions.find((e) => e.value.userId === user.id); console.log(`Finding ${user.id} in sessions`);
const session = sessions.find((e) => e.value.userId.includes(user.id));
const jwt = session?.value.sessionToken; const jwt = session?.value.sessionToken;
if (!jwt) { if (!jwt) {
return { return {
@@ -54,15 +57,19 @@ export async function updateBalanceOfPostUsers(users: ApiPostUserWithParent[]) {
detail: `Session not found for user ${user.userId}`, detail: `Session not found for user ${user.userId}`,
}; };
} }
const out = await getUsersBalance(+user.id.split(":")[1], jwt); const uid = session.value.userId.toString();
console.log(`Found ${uid} in session`);
const out = await getUsersBalance(+uid.split(":")[1], jwt);
if (!out) { if (!out) {
return { return {
ok: false, ok: false,
detail: `Error fetching balance for user ${user.userName}`, detail: `Error fetching balance for user ${user.userName}`,
}; };
} }
balances.push({ id: user.id, balance: out }); balances.push({ id: uid, balance: out });
} }
console.log(`Updating balances`);
console.log(balances);
await dbApiUser.updatePostUsersBalances(balances); await dbApiUser.updatePostUsersBalances(balances);
return { return {
ok: true, ok: true,
@@ -131,7 +138,9 @@ export async function fetchDataForPosting(
if (draw.filterDuplicatesWhilePosting === true) { if (draw.filterDuplicatesWhilePosting === true) {
console.log(`[+] Removing numbers that have repeating digits`); console.log(`[+] Removing numbers that have repeating digits`);
console.log(`[=] Original : AB: ${abNums.length}, ABC: ${abcNums.length}`); console.log(
`[=] Original : AB: ${abNums.length}, ABC: ${abcNums.length}`,
);
abNums = removeNumbersWithRepeatingDigits(abNums); abNums = removeNumbersWithRepeatingDigits(abNums);
abcNums = removeNumbersWithRepeatingDigits(abcNums); abcNums = removeNumbersWithRepeatingDigits(abcNums);
} }
@@ -243,7 +252,8 @@ export async function generatePostDataArrayFromBaseInfo(
) { ) {
continue; continue;
} }
const randomUserId = users[Math.floor(Math.random() * users.length)].userId; const randomUserId =
users[Math.floor(Math.random() * users.length)].userId;
if (balanceCounts[randomUserId] === undefined) { if (balanceCounts[randomUserId] === undefined) {
balanceCounts[randomUserId] = 0; balanceCounts[randomUserId] = 0;
} }
@@ -274,7 +284,8 @@ export async function generatePostDataArrayFromBaseInfo(
let out = false; let out = false;
for (const key in balanceCounts) { for (const key in balanceCounts) {
if ( if (
balanceCounts[key] > (users.find((u) => u.userId === key)?.balance ?? 0) balanceCounts[key] >
(users.find((u) => u.userId === key)?.balance ?? 0)
) { ) {
out = true; out = true;
break; break;
@@ -375,7 +386,9 @@ export function rebalancePostDataListByBalanceOfUsers(
} }
}) })
.map((r) => r.id); .map((r) => r.id);
console.log(`Have to move ${entriesToMove.length} entries to other users`); console.log(
`Have to move ${entriesToMove.length} entries to other users`,
);
// find a user who has enough balance // find a user who has enough balance
@@ -406,7 +419,8 @@ export function rebalancePostDataListByBalanceOfUsers(
continue; continue;
} }
entry.userId = userWithEnoughBalance.userId; entry.userId = userWithEnoughBalance.userId;
balanceCounts[userWithEnoughBalance.userId] += entry.first + entry.second; balanceCounts[userWithEnoughBalance.userId] +=
entry.first + entry.second;
balanceCounts[user.userId] -= entry.first + entry.second; balanceCounts[user.userId] -= entry.first + entry.second;
amountMoved += entry.first + entry.second; amountMoved += entry.first + entry.second;
if (amountMoved >= difference) { if (amountMoved >= difference) {
@@ -422,7 +436,9 @@ export function rebalancePostDataListByBalanceOfUsers(
export async function fetchPostDataHistory(input: PostDataHistoryFilters) { export async function fetchPostDataHistory(input: PostDataHistoryFilters) {
const { draw, date } = input; const { draw, date } = input;
console.log(`Fetching post data from HISTORY for draw: ${date} - ${draw.id}`); console.log(
`Fetching post data from HISTORY for draw: ${date} - ${draw.id}`,
);
const found = await dbApiPostData.getPostDataByDraw(date, draw.id); const found = await dbApiPostData.getPostDataByDraw(date, draw.id);
if (!found) { if (!found) {
return { data: [], users: [], ok: false, detail: "Data not found" }; return { data: [], users: [], ok: false, detail: "Data not found" };

View File

@@ -21,8 +21,7 @@ export const apiAuthRouter = createTRPCRouter({
{ {
headers: { headers: {
...constants.SCRAP_API_BASE_HEADERS, ...constants.SCRAP_API_BASE_HEADERS,
Accept: Accept: "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
"image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
}, },
}, },
); );
@@ -75,7 +74,10 @@ export const apiAuthRouter = createTRPCRouter({
userType: userType, userType: userType,
password: password, password: password,
}); });
console.log("[=] Token Response :: ", JSON.stringify(token, null, 2)); console.log(
"[=] Token Response :: ",
JSON.stringify(token, null, 2),
);
if (!token.ok) { if (!token.ok) {
return { return {
success: false, success: false,

View File

@@ -70,6 +70,9 @@ export const apiDataRouter = createTRPCRouter({
] as ServerError, ] as ServerError,
}; };
} }
console.log(
`Fetching data for ${userIds.length} users for draw ${drawId}`,
);
const userIdsInt = userIds.map((x) => parseInt(x.split(":")[1])); const userIdsInt = userIds.map((x) => parseInt(x.split(":")[1]));
const out = await getData( const out = await getData(
sess.sessionToken, sess.sessionToken,
@@ -95,7 +98,11 @@ export const apiDataRouter = createTRPCRouter({
getDataByFilters: protectedProcedure getDataByFilters: protectedProcedure
.input( .input(
z.object({ date: z.string(), drawId: z.string(), userId: z.string() }), z.object({
date: z.string(),
drawId: z.string(),
userId: z.string(),
}),
) )
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
const { date, drawId, userId } = input; const { date, drawId, userId } = input;
@@ -151,7 +158,11 @@ export const apiDataRouter = createTRPCRouter({
getFinalSheetRow: protectedProcedure getFinalSheetRow: protectedProcedure
.input( .input(
z.object({ date: z.string(), drawId: z.string(), number: z.string() }), z.object({
date: z.string(),
drawId: z.string(),
number: z.string(),
}),
) )
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
return { return {

View File

@@ -16,10 +16,6 @@ export const apiUserRouter = createTRPCRouter({
getAllDealersCount: protectedProcedure.query(async () => { getAllDealersCount: protectedProcedure.query(async () => {
return await dbApiUser.getUserTypeCount(ApiUserTypes.DEALER); return await dbApiUser.getUserTypeCount(ApiUserTypes.DEALER);
}), }),
getDistributorsWithTheirChildren: protectedProcedure.query(async () => {
const users = await dbApiUser.getAllDistributorsWithTheirChildren();
return { users };
}),
getAllDealersPostUserFormat: protectedProcedure.query(async () => { getAllDealersPostUserFormat: protectedProcedure.query(async () => {
return await dbApiUser.allUsersOfTypeLimitedInfo(ApiUserTypes.DEALER); return await dbApiUser.allUsersOfTypeLimitedInfo(ApiUserTypes.DEALER);

View File

@@ -33,9 +33,9 @@ export const bookingRouter = createTRPCRouter({
const did = parseInt(drawId.split(":")[1]); const did = parseInt(drawId.split(":")[1]);
const [out] = await surreal.query<[BookingEntry[]]>( const [out] = await surreal.query<[BookingEntry[]]>(
`select * from type::table($table) where drawId = $drawId and bookDate = $bookDate order by requestId desc`, `select * from type::table($table) where drawId = $drawId and bookDate = $bookDate order by requestId desc`,
{ table: tn, drawId: did, bookDate: date } { table: tn, drawId: did, bookDate: date },
); );
return { data: out.result ?? [], errors: [] as ServerError }; return { data: out ?? [], errors: [] as ServerError };
}), }),
syncBooking: protectedProcedure syncBooking: protectedProcedure
@@ -53,7 +53,7 @@ export const bookingRouter = createTRPCRouter({
createdAt: new Date().toISOString(), createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
}; };
}) }),
); );
} }
return { detail: "Add Booking api donezo", syncedEntriesIds }; return { detail: "Add Booking api donezo", syncedEntriesIds };
@@ -65,7 +65,7 @@ export const bookingRouter = createTRPCRouter({
await Promise.all( await Promise.all(
input.bookingIds.map(async (id) => { input.bookingIds.map(async (id) => {
await surreal.delete(id); await surreal.delete(id);
}) }),
); );
return { detail: `Deleted ${input.bookingIds.length} Entries` }; return { detail: `Deleted ${input.bookingIds.length} Entries` };
}), }),

View File

@@ -21,10 +21,11 @@ export const drawRouter = createTRPCRouter({
savePresetInfoForDraws: protectedProcedure savePresetInfoForDraws: protectedProcedure
.input(z.object({ draws: z.array(zDraw) })) .input(z.object({ draws: z.array(zDraw) }))
.mutation(async ({ input }) => { .mutation(async ({ input }) => {
// console.log("savePresetInfoForDraws", input); console.log("Saving preset info for draws");
for (const draw of input.draws) { for (const draw of input.draws) {
await dbDraw.updateDrawPresetInfo(draw); await dbDraw.updateDrawPresetInfo(draw);
} }
console.log("Done saving preset info for draws");
return { success: true }; return { success: true };
}), }),
}); });

View File

@@ -28,6 +28,7 @@ export const zUser = z.object({
userType: z.string().min(4).max(5), userType: z.string().min(4).max(5),
association: z.string(), association: z.string(),
}); });
export type User = z.infer<typeof zUser>;
export const zLooseUser = z.object({ export const zLooseUser = z.object({
id: z.string().length(16).optional(), id: z.string().length(16).optional(),
@@ -38,6 +39,7 @@ export const zLooseUser = z.object({
userType: z.string().min(4).max(5), userType: z.string().min(4).max(5),
association: z.string(), association: z.string(),
}); });
export type LooseUser = z.infer<typeof zLooseUser>;
export const zApiUser = z.object({ export const zApiUser = z.object({
id: z.string().length(16), id: z.string().length(16),
@@ -70,6 +72,7 @@ export const zApiUser = z.object({
createdAt: z.string().nullable(), createdAt: z.string().nullable(),
updatedAt: z.string().nullable(), updatedAt: z.string().nullable(),
}); });
export type ApiUser = z.infer<typeof zApiUser>;
export const zApiPostUser = z.object({ export const zApiPostUser = z.object({
id: z.string(), id: z.string(),
@@ -78,6 +81,7 @@ export const zApiPostUser = z.object({
postData: z.boolean(), postData: z.boolean(),
balance: z.number().optional(), balance: z.number().optional(),
}); });
export type ApiPostUser = z.infer<typeof zApiPostUser>;
export const zLooseApiUser = z.object({ export const zLooseApiUser = z.object({
id: z.string().length(16).optional(), id: z.string().length(16).optional(),
@@ -110,6 +114,7 @@ export const zLooseApiUser = z.object({
createdAt: z.string().nullable().optional(), createdAt: z.string().nullable().optional(),
updatedAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(),
}); });
export type LooseApiUser = z.infer<typeof zLooseApiUser>;
export const zDraw = z.object({ export const zDraw = z.object({
id: z.string(), id: z.string(),
@@ -125,7 +130,6 @@ export const zDraw = z.object({
createdAt: z.string().nullable().optional(), createdAt: z.string().nullable().optional(),
updatedAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(),
}); });
export type Draw = z.infer<typeof zDraw>; export type Draw = z.infer<typeof zDraw>;
export const zBookingEntry = z.object({ export const zBookingEntry = z.object({
@@ -245,21 +249,11 @@ export const UserTypes = { ADMIN: "ADMIN", USER: "USER" };
export const ApiUserTypes = { ADMIN: 1, DISTRIBUTOR: 2, DEALER: 3 }; export const ApiUserTypes = { ADMIN: 1, DISTRIBUTOR: 2, DEALER: 3 };
export type User = z.infer<typeof zUser>;
export type LooseUser = z.infer<typeof zLooseUser>;
export type ApiUser = z.infer<typeof zApiUser>;
export type ApiPostUser = z.infer<typeof zApiPostUser>;
export type ApiPostUserWithParent = ApiPostUser & { export type ApiPostUserWithParent = ApiPostUser & {
parentAdmin: number; parentAdmin: number;
parentDistributor: number; parentDistributor: number;
}; };
export type LooseApiUser = z.infer<typeof zLooseApiUser>;
export type LexiCodeCacheObject = z.infer<typeof zLexiCodeCacheObject>; export type LexiCodeCacheObject = z.infer<typeof zLexiCodeCacheObject>;
export type SimpleLexiCodeObject = { number: string; lexiCode: string }; export type SimpleLexiCodeObject = { number: string; lexiCode: string };

View File

@@ -7,7 +7,6 @@ import {
} from "$lib/server/external/api.scraping.helpers"; } from "$lib/server/external/api.scraping.helpers";
import { dbApiUser } from "$lib/server/db/apiuser.db"; import { dbApiUser } from "$lib/server/db/apiuser.db";
import { constants } from "$lib/utils/constants"; import { constants } from "$lib/utils/constants";
import fs from "fs";
import { getSessionFromStore } from "$lib/server/utils/session.service"; import { getSessionFromStore } from "$lib/server/utils/session.service";
export const actions = { export const actions = {
@@ -43,6 +42,7 @@ export const actions = {
const distributor_ids = await dbApiUser.getAllIdsByUserType( const distributor_ids = await dbApiUser.getAllIdsByUserType(
ApiUserTypes.DISTRIBUTOR, ApiUserTypes.DISTRIBUTOR,
); );
console.log(distributor_ids);
const done = await getDealers(sess.sessionToken, distributor_ids); const done = await getDealers(sess.sessionToken, distributor_ids);
console.log(`[+] ${done.dealers.length} dealers found`); console.log(`[+] ${done.dealers.length} dealers found`);
// fs.writeFileSync("dealers.json", JSON.stringify(done.dealers, null, 2)); // fs.writeFileSync("dealers.json", JSON.stringify(done.dealers, null, 2));

View File

@@ -40,6 +40,7 @@
let refetchDataM = api.apiData.refetchData.createMutation({ let refetchDataM = api.apiData.refetchData.createMutation({
onSuccess: (o) => { onSuccess: (o) => {
console.log(o);
if (!o.success) { if (!o.success) {
for (const each of o.errors) { for (const each of o.errors) {
toast.error(each.message); toast.error(each.message);
@@ -49,6 +50,7 @@
} }
}, },
onError: (e) => { onError: (e) => {
console.log(e);
toast.error(e.message); toast.error(e.message);
}, },
}); });

View File

@@ -19,12 +19,12 @@
let updateDrawFilterM = api.draw.savePresetInfoForDraws.createMutation({ let updateDrawFilterM = api.draw.savePresetInfoForDraws.createMutation({
onSuccess: (d) => { onSuccess: (d) => {
console.log(d); console.log(d);
toast("Saved successfully."); toast("Saved successfully");
}, },
onError: (e) => { onError: (e) => {
console.error(e); console.error(e);
toast( toast(
"An error occurred while fetching data. Try again after a page refresh.", "An error occurred while fetching data. Try again after a page refresh",
); );
}, },
}); });