Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions get-sticker-fulfilment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Cryptr from 'cryptr';

const cryptr = new Cryptr(process.env.APP_SECRET_KEY ?? '');

export function encrypt(plaintext: string) {
return cryptr.encrypt(plaintext);
}

export function decrypt(ciphertext: string) {
return cryptr.decrypt(ciphertext);
}

const tokens = [''];

export async function getUserData(token: string) {
const userDataURL = new URL(`https://auth.hackclub.com/api/v1/me`);
const userDataRes = await fetch(userDataURL, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`
}
});

if (!userDataRes.ok) {
throw new Error('Failed to fetch user data');
}

const userDataJSON = await userDataRes.json();

return userDataJSON.identity!;
}

const countries: Record<string, number> = {};

for (let i = 0; i < tokens.length; i++) {
try {
const token = decrypt(tokens[i]);
const userData = await getUserData(token);
const { addresses } = userData;
const address = addresses?.find((address: { primary: boolean }) => address.primary);
console.log('Address', i + '/' + tokens.length + ':', address.country);

countries[address.country as string] = (countries[address.country as string] ?? 0) + 1;
} catch {
console.warn('Failed: user ' + i);
}
}

console.log('\n');
console.log(countries);
44 changes: 42 additions & 2 deletions src/routes/dashboard/admin/admin/stickers/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { db } from '$lib/server/db/index.js';
import { user, project } from '$lib/server/db/schema.js';
import { error } from '@sveltejs/kit';
import { sql, and, eq, asc } from 'drizzle-orm';
import { sql, and, eq, asc, ne } from 'drizzle-orm';
import type { Actions } from './$types';

export async function load({ locals }) {
if (!locals.user) {
Expand All @@ -28,7 +29,10 @@ export async function load({ locals }) {
.innerJoin(user, eq(project.userId, user.id))
.where(
and(
eq(project.status, 'finalized'),
ne(project.status, 'building'),
ne(project.status, 'submitted'),
ne(project.status, 'rejected'),
ne(project.status, 'rejected_locked'),
eq(project.deleted, false),
eq(user.stickersShipped, false)
)
Expand All @@ -40,3 +44,39 @@ export async function load({ locals }) {
users
};
}

export const actions = {
fulfil: async ({ locals, request }) => {
if (!locals.user) {
throw error(500);
}
if (!locals.user.hasAdmin) {
throw error(403, { message: 'oi get out' });
}

const data = await request.formData();
const id: number = parseInt(data.get('id')?.toString() ?? '');

const [queriedUser] = await db.select().from(user).where(eq(user.id, id));

if (!queriedUser) {
return {
userNotFound: true
};
}

if (queriedUser.stickersShipped) {
return {
alreadyFulfilled: true,
id
};
}

await db.update(user).set({ stickersShipped: true }).where(eq(user.id, id));

return {
fulfilled: true,
id
}
}
} satisfies Actions;
50 changes: 47 additions & 3 deletions src/routes/dashboard/admin/admin/stickers/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<script lang="ts">
import { enhance } from '$app/forms';
import Head from '$lib/components/Head.svelte';
import relativeDate from 'tiny-relative-date';

let { data } = $props();
let { data, form } = $props();

let userSearch = $state('');

let filteredUsers = $derived(
data.users.filter((user) => user.name?.toLowerCase().includes(userSearch.toLowerCase()))
);

let formPending = $state(false);
</script>

<Head title="Stickers/keyrings" />
Expand All @@ -18,7 +21,48 @@
<h1 class="mb-3 grow font-hero text-3xl font-medium">Stickers/keyrings</h1>
</div>

<p class="mb-3 text-lg">Showing {filteredUsers.length} users</p>
<form
action="?/fulfil"
method="POST"
class="mb-3"
use:enhance={() => {
formPending = true;
return async ({ update }) => {
await update();
formPending = false;
document.getElementById("id-input")!.focus();
};
}}
>
<div class="flex flex-row gap-3">
<label class="flex flex-col grow">
<input
type="number"
name="id"
id="id-input"
class="themed-input"
placeholder="User ID"
required
/>
</label>
<div class="flex flex-col justify-center">
<button type="submit" class="button md primary" disabled={formPending}
>Mark fulfilled</button
>
</div>
</div>
{#if form?.alreadyFulfilled}
<p class="text-sm mt-1.5">Already fulfilled, go to <a href={`stickers/${form.id}`} class="underline">fulfillment page</a></p>
{/if}
{#if form?.userNotFound}
<p class="text-sm mt-1.5">User not found</p>
{/if}
{#if form?.fulfilled}
<p class="text-sm mt-1.5">Fulfilled <a href={`stickers/${form.id}`} class="underline">user</a></p>
{/if}
</form>

<p class="mb-2 text-lg">Showing {filteredUsers.length} users</p>

<input class="themed-box mb-3 w-full p-2" placeholder="Search users..." bind:value={userSearch} />

Expand Down Expand Up @@ -48,7 +92,7 @@
{user.slackId}
</code>
<div class="flex flex-row gap-1">
<p>Project created </p>
<p>Project created</p>
<abbr title={`${user.projectCreatedAt.toUTCString()}`} class="relative z-2">
{relativeDate(user.projectCreatedAt)}
</abbr>
Expand Down
Loading