60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
// Dev/test helper (settings debugMode) — random completions for a range of days.
|
|
|
|
export async function generateData(pb: any, famId: string, days = 7) {
|
|
const [members, templates] = await Promise.all([
|
|
pb.collection('users').getFullList({ filter: `famId = '${famId}' && role = 'child'` }),
|
|
pb.collection('chore_templates').getFullList({ filter: `famId = '${famId}'` })
|
|
]);
|
|
|
|
if (!members.length) return { error: 'No members found' };
|
|
if (!templates.length) return { error: 'No templates found' };
|
|
|
|
let completionsCreated = 0;
|
|
const today = new Date();
|
|
|
|
for (let d = 0; d < days; d++) {
|
|
const date = new Date(today);
|
|
date.setDate(date.getDate() - d);
|
|
const dateStr = date.toISOString().slice(0, 10);
|
|
|
|
for (const m of members) {
|
|
const completionRate = 0.5 + Math.random() * 0.5;
|
|
for (const t of templates) {
|
|
if (Math.random() > completionRate) continue;
|
|
|
|
let assigned = await pb.collection('assigned_chores').getFullList({
|
|
filter: `famId = '${famId}' && memberId = '${m.id}' && templateId = '${t.id}'`
|
|
});
|
|
let assignedId;
|
|
if (assigned?.length > 0) {
|
|
assignedId = assigned[0].id;
|
|
} else {
|
|
const record = await pb.collection('assigned_chores').create({
|
|
famId,
|
|
memberId: m.id,
|
|
templateId: t.id,
|
|
frequency: t.defaultFrequency || 'daily',
|
|
type: t.defaultType || 'points',
|
|
value: t.defaultValue || 10
|
|
});
|
|
assignedId = record.id;
|
|
}
|
|
|
|
const existing = await pb.collection('completions').getFullList({
|
|
filter: `famId = '${famId}' && memberId = '${m.id}' && assignedChoreId = '${assignedId}' && date = '${dateStr}'`
|
|
});
|
|
if (existing?.length > 0) continue;
|
|
|
|
await pb.collection('completions').create({
|
|
famId,
|
|
memberId: m.id,
|
|
assignedChoreId: assignedId,
|
|
date: dateStr
|
|
});
|
|
completionsCreated++;
|
|
}
|
|
}
|
|
}
|
|
|
|
return { completionsCreated, days };
|
|
} |