add fixes to claims and seasons
This commit is contained in:
@@ -1,23 +1,81 @@
|
||||
import { pbAdmin } from '$lib/server/pb-admin';
|
||||
import { redirect, fail } from '@sveltejs/kit';
|
||||
import { PB_EMAIL, PB_PASSWORD } from '../../../../config.ts';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
|
||||
export async function load() {
|
||||
const fams = await pbAdmin.getList('fams');
|
||||
const famsWithStats = await Promise.all(fams.map(async (fam: any) => {
|
||||
const [members, rewards] = await Promise.all([
|
||||
pbAdmin.getList('members', `famId = '${fam.id}'`),
|
||||
pbAdmin.getList('rewards', `famId = '${fam.id}'`),
|
||||
]);
|
||||
return {
|
||||
id: fam.id, name: fam.name, slug: fam.slug, inviteCode: fam.inviteCode,
|
||||
memberCount: members.length,
|
||||
claimedRewards: (rewards as any[]).filter((r: any) => r.claimed).length,
|
||||
totalRewards: rewards.length,
|
||||
};
|
||||
}));
|
||||
export const load: PageServerLoad = async ({ cookies }) => {
|
||||
const session = cookies.get('platform_session');
|
||||
if (!session) {
|
||||
return { authenticated: false, fams: [], totalFams: 0, totalMembers: 0, totalRewards: 0 };
|
||||
}
|
||||
|
||||
const totalFams = fams.length;
|
||||
const totalMembers = famsWithStats.reduce((s: number, f: any) => s + f.memberCount, 0);
|
||||
const totalRewards = famsWithStats.reduce((s: number, f: any) => s + f.totalRewards, 0);
|
||||
try {
|
||||
const fams = await pbAdmin.getList('fams');
|
||||
const famsWithStats = await Promise.all(fams.map(async (fam: any) => {
|
||||
const [members, rewards, famAdmins] = await Promise.all([
|
||||
pbAdmin.getList('members', `famId = '${fam.id}'`),
|
||||
pbAdmin.getList('rewards', `famId = '${fam.id}'`),
|
||||
pbAdmin.getList('fam_admins', `famId = '${fam.id}'`),
|
||||
]);
|
||||
return {
|
||||
id: fam.id, name: fam.name, slug: fam.slug, inviteCode: fam.inviteCode,
|
||||
memberCount: members.length,
|
||||
requestedRewards: (rewards as any[]).filter((r: any) => r.status === 'requested').length,
|
||||
totalRewards: rewards.length,
|
||||
parentEmail: (famAdmins as any[])?.[0]?.email || '',
|
||||
featureFlags: fam.featureFlags || {},
|
||||
};
|
||||
}));
|
||||
|
||||
return { fams: famsWithStats, totalFams, totalMembers, totalRewards };
|
||||
}
|
||||
const totalFams = fams.length;
|
||||
const totalMembers = famsWithStats.reduce((s: number, f: any) => s + f.memberCount, 0);
|
||||
const totalRewards = famsWithStats.reduce((s: number, f: any) => s + f.totalRewards, 0);
|
||||
|
||||
return { authenticated: true, fams: famsWithStats, totalFams, totalMembers, totalRewards };
|
||||
} catch {
|
||||
return { authenticated: false, fams: [], totalFams: 0, totalMembers: 0, totalRewards: 0 };
|
||||
}
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
login: async ({ request, cookies }) => {
|
||||
const fd = await request.formData();
|
||||
const email = fd.get('email') as string;
|
||||
const password = fd.get('password') as string;
|
||||
|
||||
if (email === PB_EMAIL && password === PB_PASSWORD) {
|
||||
cookies.set('platform_session', 'authenticated', {
|
||||
path: '/',
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
maxAge: 60 * 60 * 24, // 24 hours
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
return fail(400, { error: 'Invalid credentials' });
|
||||
},
|
||||
|
||||
logout: async ({ cookies }) => {
|
||||
cookies.delete('platform_session', { path: '/' });
|
||||
throw redirect(303, '/admin');
|
||||
},
|
||||
|
||||
toggleFeatureFlag: async ({ request, cookies }) => {
|
||||
const session = cookies.get('platform_session');
|
||||
if (!session) return fail(401, { error: 'Not authenticated' });
|
||||
|
||||
const fd = await request.formData();
|
||||
const famId = fd.get('famId') as string;
|
||||
const flag = fd.get('flag') as string;
|
||||
|
||||
try {
|
||||
const fam = await pbAdmin.getOne('fams', famId);
|
||||
const flags = fam.featureFlags || {};
|
||||
flags[flag] = !flags[flag];
|
||||
await pbAdmin.update('fams', famId, { featureFlags: flags });
|
||||
return { success: true };
|
||||
} catch (e) {
|
||||
return fail(500, { error: e instanceof Error ? e.message : 'Failed to update' });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user