fix bonus completeions
This commit is contained in:
@@ -1,10 +1,31 @@
|
||||
import { todayInTz, resolveTz } from '@shared/timezone';
|
||||
import { famMeta } from './fam';
|
||||
import { pbAdmin } from '$lib/server/pocketbase';
|
||||
|
||||
function resolveServerTz(tz?: string): string {
|
||||
return resolveTz(tz || 'auto');
|
||||
}
|
||||
|
||||
// A `once` goal is finished the moment its reward is approved — complete the
|
||||
// config here (not just in evaluateFam, which only runs on chore toggles) so
|
||||
// the child dashboard stops offering it even before the next toggle.
|
||||
// Config write via superuser: bonus_configs updates are parent-only.
|
||||
// Outstanding (unclaimed/requested) rewards are untouched — they stay visible
|
||||
// and collectible; only the approval moment completes the goal.
|
||||
async function completeOnceConfig(famId: string, reward: any) {
|
||||
if (!reward?.bonusConfigId) return;
|
||||
try {
|
||||
const cfg: any = await pbAdmin.getOne('bonus_configs', reward.bonusConfigId);
|
||||
if (!cfg || cfg.famId !== famId) return;
|
||||
if (cfg.occurrence !== 'once' || cfg.status === 'completed') return;
|
||||
await pbAdmin.update('bonus_configs', cfg.id, {
|
||||
status: 'completed',
|
||||
completedAt: new Date().toISOString().slice(0, 10),
|
||||
completedBy: reward.memberId || 'admin'
|
||||
});
|
||||
} catch {}
|
||||
}
|
||||
|
||||
export function assertPaydayUnlocked(reward: any, tz?: string) {
|
||||
if (!reward || reward.claimable !== 'payday' || !reward.settleDate) return;
|
||||
const today = todayInTz(resolveServerTz(tz));
|
||||
@@ -28,10 +49,12 @@ export async function claim(pb: any, famId: string, id: string) {
|
||||
|
||||
// Admin approval → status 'claimed'.
|
||||
export async function approve(pb: any, famId: string, id: string) {
|
||||
return pb.collection('rewards').update(id, {
|
||||
const record = await pb.collection('rewards').update(id, {
|
||||
status: 'claimed',
|
||||
claimedAt: new Date().toISOString()
|
||||
});
|
||||
await completeOnceConfig(famId, record);
|
||||
return record;
|
||||
}
|
||||
|
||||
export async function requestAll(pb: any, famId: string, memberId: string) {
|
||||
@@ -61,7 +84,8 @@ export async function issueAll(pb: any, famId: string, memberId: string) {
|
||||
});
|
||||
let count = 0;
|
||||
for (const r of rewards) {
|
||||
await pb.collection('rewards').update(r.id, { status: 'claimed', claimedAt: now });
|
||||
const record = await pb.collection('rewards').update(r.id, { status: 'claimed', claimedAt: now });
|
||||
await completeOnceConfig(famId, record);
|
||||
count++;
|
||||
}
|
||||
return { count };
|
||||
|
||||
Reference in New Issue
Block a user