Skip to content

Commit

Permalink
Add a command to list all entries from a user, including the ids
Browse files Browse the repository at this point in the history
  • Loading branch information
nlinnanen committed May 5, 2024
1 parent c02fc2b commit 1241320
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 20 deletions.
77 changes: 63 additions & 14 deletions src/commands/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
amountToValidate,
fileIdsForUserId,
fileIdsForUsername,
getEntriesByUserId,
getEntriesByUsername,
getEntry,
getRandomNotValidEntry,
removeEntry,
Expand Down Expand Up @@ -57,17 +59,19 @@ export const validate = async (ctx: CommandContext) => {
"Please give the id of entry to validate as an argument (eg. /validate 10)",
);

const possibleNum = Number.parseInt(args[1]);
const possibleNum = Number.parseInt(args[1]);

if (Number.isNaN(possibleNum)) return ctx.reply("Given id is not a number!");

if(Number.isNaN(possibleNum)) return ctx.reply("Given id is not a number!");

const entry = await getEntry(possibleNum);
if (!entry) return ctx.reply("No such entry");

underValidation.set(ctx.chat.id, entry.id);
await ctx.replyWithHTML(formatEntryWithUser(entry as unknown as EntryWithUser));
await ctx.replyWithHTML(
formatEntryWithUser(entry as unknown as EntryWithUser),
);
await ctx.replyWithPhoto(entry.fileId, validationKeyboard);
}
};

export const notValidated = async (ctx: ActionContext | CommandContext) => {
const notValidated = await amountToValidate();
Expand Down Expand Up @@ -135,7 +139,16 @@ export const adminLogin = async (
const userId = ctx.message.from.id;
admins.add(userId);
await ctx.reply(
"You are now an admin! \n/csv - get all entries in csv \n/pistokoe - validate entries \n/remove [entry id] - remove one entry \n/numtovalidate - number of entries not yet validated \n/allphotos [user id or username] - gets all uploaded photos by user \n/updatedistance [entry id] [distance] - sets a new distance on an entry \n/resetvalidation [entry id] - resets the validation of entry \n/validate [entry id] - starts validation from specific entry",
`You are now an admin! Possible commands:
/csv - get all entries in csv
/pistokoe - validate entries
/remove [entry id] - remove one entry
/numtovalidate - number of entries not yet validated
/allphotos [user id or username] - gets all uploaded photos by user
/allentries [user id or username] - gets all entries by user
/updatedistance [entry id] [distance] - sets a new distance on an entry
/resetvalidation [entry id] - resets the validation of entry
/validate [entry id] - starts validation from specific entry`,
);
return next();
};
Expand All @@ -154,7 +167,7 @@ export const allPhotosFromUser = async (

const possibleNum = Number.parseInt(args[1]);

let fileIds;
let fileIds: { fileId: string }[] | null;
if (isBigInteger(possibleNum)) {
fileIds = await fileIdsForUserId(possibleNum);
} else {
Expand All @@ -179,8 +192,46 @@ export const allPhotosFromUser = async (
return next();
};

export const allEntriesFromUser = async (
ctx: CommandContext,
next: () => Promise<void>,
) => {
if (!admins.has(ctx!.from!.id)) return;

const args = ctx.message.text.split(" ");

if (args.length <= 1)
return ctx.reply(
"Please give the id or username to get all entries from as an argument (eg. /allentries mediakeisari)",
);

const possibleNum = Number.parseInt(args[1]);

let entries: Entry[];
if (!Number.isNaN(possibleNum) && isBigInteger(possibleNum)) {
entries = await getEntriesByUserId(possibleNum);
} else {
entries = await getEntriesByUsername(args[1]);
}

if (!entries) return await ctx.reply("No such user 👀");

const chunks = _.chunk(
entries.map((e) => formatEntry(e, true)),
10,
);

for (const chunk of chunks) {
await ctx.reply(chunk.join("\n\n"));
}
return next();
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const confirmedRemove = async (ctx: any, next: () => Promise<void>) => {
export const confirmedRemove = async (
ctx: CommandContext,
next: () => Promise<void>,
) => {
if (!admins.has(ctx.from.id)) return;

const entryId = removeConsideration.get(ctx.chat.id);
Expand Down Expand Up @@ -264,9 +315,8 @@ export const resetValidation = async (
valid: null,
});
await ctx.reply(
`Resetted valdation for entry ${entry.id}: \n${formatEntry(
entry as unknown as Entry,
)}`,
`Resetted valdation for entry ${entry.id}:
${formatEntry(entry as unknown as Entry)}`,
);
} catch (e) {
console.log(e);
Expand Down Expand Up @@ -298,9 +348,8 @@ export const setDistance = async (
distance,
});
await ctx.reply(
`Updated distance for entry ${entry.id}: \n${formatEntry(
entry as unknown as Entry,
)}`,
`Updated distance for entry ${entry.id}:
${formatEntry(entry as unknown as Entry)}`,
);
} catch (e) {
console.log(e);
Expand Down
5 changes: 2 additions & 3 deletions src/commands/entries.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import _ from "lodash";

import { COEFFICIENTS } from "../common/constants";
import type { ActionContext, CommandContext } from "../common/types";
import { formatEntry } from "../common/utils";
import { getEntries } from "../entries";
import { getEntriesByUserId } from "../entries";
import { commandsKeyboard } from "../keyboards";

const entries = async (
ctx: CommandContext | ActionContext,
next: () => Promise<void>,
) => {
const entries = await getEntries(ctx!.from!.id);
const entries = await getEntriesByUserId(ctx!.from!.id);

if (entries.length > 0) {
const validEntries = entries.filter((e) => e.valid !== false);
Expand Down
2 changes: 1 addition & 1 deletion src/common/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const isEntry = (entry: any): entry is Entry => {
};

const isBigInteger = (number: unknown): number is bigint => {
return Number.isInteger(number);
return typeof number === "bigint" || Number.isInteger(number);
};

export {
Expand Down
18 changes: 16 additions & 2 deletions src/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const updateEntryStash = (chatId: number, update: Partial<Entry>) => {
});
};

const getEntries = async (userId: unknown): Promise<Entry[]> => {
const getEntriesByUserId = async (userId: unknown): Promise<Entry[]> => {
if (!isBigInteger(userId)) {
throw TypeError("userId should be a long");
}
Expand All @@ -29,6 +29,19 @@ const getEntries = async (userId: unknown): Promise<Entry[]> => {
})) as unknown as Entry[];
};

const getEntriesByUsername = async (username: string): Promise<Entry[]> => {
const result = await prisma.user.findFirst({
select: { telegramUserId: true },
where: { telegramUsername: username },
});

if (result) {
return await getEntriesByUserId(result.telegramUserId);
}
return [];
}


const getAllEntries = async () => {
return await prisma.entry.findMany({ include: { user: true } });
};
Expand Down Expand Up @@ -169,7 +182,8 @@ export {
updateEntryStash,
entryToDb,
getAllEntries,
getEntries,
getEntriesByUserId,
getEntriesByUsername,
getRandomNotValidEntry,
setEntryValidation,
setEntryDoublePoints,
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./commands/action/privacy";
import {
adminLogin,
allEntriesFromUser,
allPhotosFromUser,
cancelRemove,
confirmedRemove,
Expand Down Expand Up @@ -76,6 +77,7 @@ bot.command("allphotos", allPhotosFromUser);
bot.command("resetvalidation", resetValidation);
bot.command("updatedistance", setDistance);
bot.command("validate", validate);
bot.command("allentries", allEntriesFromUser);

bot.action("invalid", invalid);
bot.action("valid1x", valid1x);
Expand Down

0 comments on commit 1241320

Please sign in to comment.