make major bonus updates

This commit is contained in:
JCEEE
2026-07-31 16:13:05 +01:00
parent 8834281b4d
commit d720f4e863
12 changed files with 4174 additions and 2598 deletions
@@ -4,21 +4,95 @@ import { hono } from '$lib/server/hono';
export async function load(event) {
if (!event.locals.session) throw redirect(303, '/login');
const famId = event.locals.session.famId;
const [configs, members, progress, rewards] = await Promise.all([
const [configs, templates, members, progress, rewards] = await Promise.all([
hono.admin.bonusConfigs(event, famId),
hono.admin.list(event, 'bonus-templates', famId),
hono.admin.list(event, 'members', famId),
hono.admin.bonusConfigProgress(event, famId),
hono.admin.rewards(event, famId),
hono.admin.rewards(event, famId)
]);
return { configs, members, progress, rewards };
return { configs, templates, members, progress, rewards };
}
export const actions = {
createTemplate: async (event) => {
if (!event.locals.session) throw redirect(303, '/login');
const famId = event.locals.session.famId;
const fd = await event.request.formData();
const data: Record<string, unknown> = {
name: fd.get('name'),
description: fd.get('description') || '',
target: fd.get('target'),
type: fd.get('type'),
occurrence: fd.get('occurrence'),
rewardType: fd.get('rewardType'),
rewardValue: fd.get('rewardValue')
};
const criteriaValue = fd.get('criteriaValue');
if (criteriaValue) data.criteriaValue = parseInt(criteriaValue as string, 10) || 0;
const period = fd.get('period');
if (period !== null) data.period = period;
if (data.occurrence === 'once') data.period = '';
try {
const record = await hono.admin.create(event, 'bonus-templates', famId, data);
return { record };
} catch (e) {
return fail(400, { error: e instanceof Error ? e.message : 'Failed to create template' });
}
},
updateTemplate: async (event) => {
if (!event.locals.session) throw redirect(303, '/login');
const famId = event.locals.session.famId;
const fd = await event.request.formData();
const id = fd.get('id') as string;
const data: Record<string, unknown> = {};
const name = fd.get('name');
if (name) data.name = name;
const target = fd.get('target');
if (target) data.target = target;
const type = fd.get('type');
if (type) data.type = type;
const occurrence = fd.get('occurrence');
if (occurrence) data.occurrence = occurrence;
const rewardType = fd.get('rewardType');
if (rewardType) data.rewardType = rewardType;
const rewardValue = fd.get('rewardValue');
if (rewardValue) data.rewardValue = rewardValue;
const criteriaValue = fd.get('criteriaValue');
if (criteriaValue) data.criteriaValue = parseInt(criteriaValue as string, 10) || 0;
const period = fd.get('period');
if (period !== null) data.period = period;
if (occurrence === 'once') data.period = '';
const description = fd.get('description');
if (description) data.description = description;
try {
const record = await hono.admin.update(event, 'bonus-templates', famId, id, data);
return { record };
} catch (e) {
return fail(400, { error: e instanceof Error ? e.message : 'Failed to update template' });
}
},
deleteTemplate: async (event) => {
if (!event.locals.session) throw redirect(303, '/login');
const famId = event.locals.session.famId;
const fd = await event.request.formData();
const id = fd.get('id') as string;
try {
await hono.admin.remove(event, 'bonus-templates', famId, id);
return { deleted: true };
} catch (e) {
return fail(400, { error: e instanceof Error ? e.message : 'Failed to delete template' });
}
},
createConfig: async (event) => {
if (!event.locals.session) throw redirect(303, '/login');
const famId = event.locals.session.famId;
const fd = await event.request.formData();
const phase = fd.get('phase') as string;
const startMode = fd.get('startMode') as string;
const status = startMode === 'disabled' ? 'disabled' : 'active';
const data: Record<string, unknown> = {
name: fd.get('name'),
target: fd.get('target'),
@@ -26,12 +100,13 @@ export const actions = {
occurrence: fd.get('occurrence'),
rewardType: fd.get('rewardType'),
rewardValue: fd.get('rewardValue'),
phase: phase || 'ready',
status
};
const criteriaValue = fd.get('criteriaValue');
if (criteriaValue) data.criteriaValue = parseInt(criteriaValue as string, 10) || 0;
const period = fd.get('period');
if (period !== null) data.period = period;
if (data.occurrence === 'once') data.period = '';
const description = fd.get('description');
if (description) data.description = description;
const memberId = fd.get('memberId');
@@ -66,6 +141,7 @@ export const actions = {
if (criteriaValue) data.criteriaValue = parseInt(criteriaValue as string, 10) || 0;
const period = fd.get('period');
if (period !== null) data.period = period;
if (occurrence === 'once') data.period = '';
const description = fd.get('description');
if (description) data.description = description;
const memberId = fd.get('memberId');
@@ -87,7 +163,7 @@ export const actions = {
await hono.admin.remove(event, 'bonus-configs', famId, id);
return { deleted: true };
} catch (e) {
return fail(400, { error: e instanceof Error ? e.message : 'Failed to archive config' });
return fail(400, { error: e instanceof Error ? e.message : 'Failed to delete config' });
}
},
@@ -95,6 +171,8 @@ export const actions = {
if (!event.locals.session) throw redirect(303, '/login');
const famId = event.locals.session.famId;
const fd = await event.request.formData();
const startMode = fd.get('startMode') as string;
const status = startMode === 'disabled' ? 'disabled' : 'active';
const data: Record<string, unknown> = {
name: fd.get('name'),
description: fd.get('description') || '',
@@ -106,13 +184,16 @@ export const actions = {
criteriaValue: parseInt(fd.get('criteriaValue') as string, 10) || 0,
period: fd.get('period') || '',
memberId: fd.get('memberId') || '',
phase: 'active',
status
};
if (data.occurrence === 'once') data.period = '';
try {
const record = await hono.admin.create(event, 'bonus-configs', famId, data);
return { record };
} catch (e) {
return fail(400, { error: e instanceof Error ? e.message : 'Failed to create bonus from template' });
return fail(400, {
error: e instanceof Error ? e.message : 'Failed to create bonus from template'
});
}
},
@@ -124,7 +205,10 @@ export const actions = {
const target = fd.get('target') as string;
const memberId = fd.get('memberId') as string;
try {
const record = await hono.admin.assignBonusConfig(event, famId, id, { target, memberId: memberId || null });
const record = await hono.admin.assignBonusConfig(event, famId, id, {
target,
memberId: memberId || null
});
return { record };
} catch (e) {
return fail(400, { error: e instanceof Error ? e.message : 'Failed to assign bonus' });
@@ -136,12 +220,11 @@ export const actions = {
const famId = event.locals.session.famId;
const fd = await event.request.formData();
const id = fd.get('id') as string;
const phase = fd.get('phase') as string;
try {
const record = await hono.admin.completeBonusConfig(event, famId, id, phase || 'completed');
const record = await hono.admin.completeBonusConfig(event, famId, id);
return { record };
} catch (e) {
return fail(400, { error: e instanceof Error ? e.message : 'Failed to update bonus phase' });
return fail(400, { error: e instanceof Error ? e.message : 'Failed to complete bonus' });
}
},
@@ -158,6 +241,23 @@ export const actions = {
}
},
toggleConfig: async (event) => {
if (!event.locals.session) throw redirect(303, '/login');
const famId = event.locals.session.famId;
const fd = await event.request.formData();
const id = fd.get('id') as string;
const currentStatus = fd.get('currentStatus') as string;
const newStatus = currentStatus === 'disabled' ? 'active' : 'disabled';
try {
const record = await hono.admin.update(event, 'bonus-configs', famId, id, {
status: newStatus
});
return { record };
} catch (e) {
return fail(400, { error: e instanceof Error ? e.message : 'Failed to toggle bonus' });
}
},
claimReward: async (event) => {
if (!event.locals.session) throw redirect(303, '/login');
const famId = event.locals.session.famId;
@@ -178,5 +278,5 @@ export const actions = {
} catch (e) {
return { error: e instanceof Error ? e.message : 'Failed to evaluate' };
}
},
}
};