optimize post migration
This commit is contained in:
@@ -212,7 +212,7 @@
|
||||
if (secondsLeft > 0 || eowFired) return;
|
||||
if (!pbToken || !famId) return;
|
||||
eowFired = true;
|
||||
memberApi.payday(pbToken, famId).catch(() => {});
|
||||
memberApi.payday(famId).catch(() => {});
|
||||
});
|
||||
|
||||
function paydayWeekStart(): string {
|
||||
@@ -546,7 +546,7 @@
|
||||
}
|
||||
|
||||
try {
|
||||
await memberApi.toggleCompletion(pbToken, famId, chore.id, todayChild);
|
||||
await memberApi.toggleCompletion(famId, chore.id, todayChild);
|
||||
const optimistic = completions.find((c) => c.id === 'optimistic-' + chore.id);
|
||||
if (optimistic) {
|
||||
famStore.applyRecord('completions', optimistic, 'delete');
|
||||
@@ -960,14 +960,11 @@
|
||||
const old = memberName;
|
||||
memberName = nameInput;
|
||||
try {
|
||||
const res = await fetch('/api/members/me', {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${pbToken}`
|
||||
},
|
||||
body: JSON.stringify({ name: nameInput })
|
||||
});
|
||||
const res = await fetch('/api/members', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ name: nameInput })
|
||||
});
|
||||
if (!res.ok) memberName = old;
|
||||
} catch {
|
||||
memberName = old;
|
||||
@@ -1022,14 +1019,11 @@
|
||||
memberColor = color;
|
||||
showColorPicker = false;
|
||||
try {
|
||||
const res = await fetch('/api/members/me', {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${pbToken}`
|
||||
},
|
||||
body: JSON.stringify({ color })
|
||||
});
|
||||
const res = await fetch('/api/members', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ color })
|
||||
});
|
||||
if (!res.ok) memberColor = old;
|
||||
} catch {
|
||||
memberColor = old;
|
||||
@@ -1253,7 +1247,7 @@
|
||||
class="wr-cta"
|
||||
onclick={async () => {
|
||||
try {
|
||||
await memberApi.claimReward(pbToken, famId, r.id);
|
||||
await memberApi.claimReward(famId, r.id);
|
||||
} catch (e) {
|
||||
claimError = e instanceof Error ? e.message : 'Claim failed';
|
||||
}
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { actingClient } from '$lib/server/routeAuth';
|
||||
import { createPbClient } from '$lib/server/pocketbase';
|
||||
import { createServices } from '$lib/server/services';
|
||||
|
||||
export async function POST(event: RequestEvent) {
|
||||
const { pb, famId, userId, role } = actingClient(event);
|
||||
if (famId !== event.params.famId) {
|
||||
const u = event.locals.user;
|
||||
if (!u || !event.locals.pbToken) throw error(401, 'Unauthorized');
|
||||
const pb = createPbClient(event.locals.pbToken);
|
||||
if (u.famId !== event.params.famId) {
|
||||
return json({ error: 'famId mismatch' }, { status: 403 });
|
||||
}
|
||||
const body = await event.request.json().catch(() => ({}));
|
||||
try {
|
||||
const s = createServices(pb, { id: userId, role });
|
||||
const record = await s.crud.create('assigned-chores', famId, body);
|
||||
const s = createServices(pb, { id: u.id, role: u.role });
|
||||
const record = await s.crud.create('assigned-chores', u.famId, body);
|
||||
return json(record);
|
||||
} catch (e) {
|
||||
return json({ error: e instanceof Error ? e.message : 'create failed' }, { status: 400 });
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { actingClient } from '$lib/server/routeAuth';
|
||||
import { createPbClient } from '$lib/server/pocketbase';
|
||||
import { createServices } from '$lib/server/services';
|
||||
|
||||
export async function DELETE(event: RequestEvent) {
|
||||
const { pb, famId, userId, role } = actingClient(event);
|
||||
if (famId !== event.params.famId) {
|
||||
const u = event.locals.user;
|
||||
if (!u || !event.locals.pbToken) throw error(401, 'Unauthorized');
|
||||
const pb = createPbClient(event.locals.pbToken);
|
||||
if (u.famId !== event.params.famId) {
|
||||
return json({ error: 'famId mismatch' }, { status: 403 });
|
||||
}
|
||||
const id = event.params.id!;
|
||||
try {
|
||||
const s = createServices(pb, { id: userId, role });
|
||||
await s.crud.remove('assigned-chores', famId, id);
|
||||
const s = createServices(pb, { id: u.id, role: u.role });
|
||||
await s.crud.remove('assigned-chores', u.famId, id);
|
||||
return json({ ok: true });
|
||||
} catch (e) {
|
||||
return json({ error: e instanceof Error ? e.message : 'delete failed' }, { status: 400 });
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { actingClient } from '$lib/server/routeAuth';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import { createPbClient } from '$lib/server/pocketbase';
|
||||
import { createServices, type ChatActor } from '$lib/server/services';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
|
||||
@@ -15,18 +15,20 @@ export async function POST(event: RequestEvent) {
|
||||
const body = (await event.request.json().catch(() => null)) as Body | null;
|
||||
if (!body || !body.action) return json({ error: 'missing action' }, { status: 400 });
|
||||
|
||||
const { pb, famId: sessionFamId, userId, role, name, color } = actingClient(event);
|
||||
const u = event.locals.user;
|
||||
if (!u || !event.locals.pbToken) throw error(401, 'Unauthorized');
|
||||
const pb = createPbClient(event.locals.pbToken);
|
||||
const actor: ChatActor = {
|
||||
id: userId,
|
||||
type: role === 'parent' ? 'admin' : 'member',
|
||||
name,
|
||||
color
|
||||
id: u.id,
|
||||
type: u.role === 'parent' ? 'admin' : 'member',
|
||||
name: u.name,
|
||||
color: u.color || '#6366f1'
|
||||
};
|
||||
const famId = body.famId || sessionFamId || '';
|
||||
const famId = body.famId || u.famId || '';
|
||||
if (!famId) return json({ error: 'famId required' }, { status: 400 });
|
||||
|
||||
try {
|
||||
const s = createServices(pb, { id: userId, role });
|
||||
const s = createServices(pb, { id: u.id, role: u.role });
|
||||
const data =
|
||||
body.action === 'typing'
|
||||
? await s.chat.typing(famId, actor, { typing: Boolean(body.typing) })
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { actingClient } from '$lib/server/routeAuth';
|
||||
import { createPbClient } from '$lib/server/pocketbase';
|
||||
import { createServices } from '$lib/server/services';
|
||||
|
||||
export async function POST(event: RequestEvent) {
|
||||
const { pb, famId, userId, role } = actingClient(event);
|
||||
const u = event.locals.user;
|
||||
if (!u || !event.locals.pbToken) throw error(401, 'Unauthorized');
|
||||
const pb = createPbClient(event.locals.pbToken);
|
||||
const body = await event.request.json().catch(() => ({}));
|
||||
try {
|
||||
const s = createServices(pb, { id: userId, role });
|
||||
return json(await s.completions.toggle(famId, body));
|
||||
const s = createServices(pb, { id: u.id, role: u.role });
|
||||
return json(await s.completions.toggle(u.famId, body));
|
||||
} catch (e) {
|
||||
return json({ error: e instanceof Error ? e.message : 'toggle failed' }, { status: 400 });
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { actingClient } from '$lib/server/routeAuth';
|
||||
import { createPbClient } from '$lib/server/pocketbase';
|
||||
import { createServices } from '$lib/server/services';
|
||||
|
||||
export async function POST(event: RequestEvent) {
|
||||
const { pb, famId, userId, role } = actingClient(event);
|
||||
const u = event.locals.user;
|
||||
if (!u || !event.locals.pbToken) throw error(401, 'Unauthorized');
|
||||
const pb = createPbClient(event.locals.pbToken);
|
||||
try {
|
||||
const s = createServices(pb, { id: userId, role });
|
||||
return json(await s.fam.payday(famId));
|
||||
const s = createServices(pb, { id: u.id, role: u.role });
|
||||
return json(await s.fam.payday(u.famId));
|
||||
} catch (e) {
|
||||
return json({ error: e instanceof Error ? e.message : 'payday failed' }, { status: 400 });
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { createPbClient } from '$lib/server/pocketbase';
|
||||
import { createServices } from '$lib/server/services';
|
||||
|
||||
export async function PATCH(event: RequestEvent) {
|
||||
const u = event.locals.user;
|
||||
if (!u || !event.locals.pbToken) throw error(401, 'Unauthorized');
|
||||
const pb = createPbClient(event.locals.pbToken);
|
||||
const body = await event.request.json().catch(() => ({}));
|
||||
try {
|
||||
const s = createServices(pb, { id: u.id, role: u.role });
|
||||
return json(await s.fam.updateProfile(u.famId, body));
|
||||
} catch (e) {
|
||||
return json({ error: e instanceof Error ? e.message : 'update failed' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { actingClient } from '$lib/server/routeAuth';
|
||||
import { createServices } from '$lib/server/services';
|
||||
|
||||
export async function PATCH(event: RequestEvent) {
|
||||
const { pb, famId, userId, role } = actingClient(event);
|
||||
const body = await event.request.json().catch(() => ({}));
|
||||
try {
|
||||
const s = createServices(pb, { id: userId, role });
|
||||
return json(await s.fam.updateProfile(famId, body));
|
||||
} catch (e) {
|
||||
return json({ error: e instanceof Error ? e.message : 'update failed' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { actingClient } from '$lib/server/routeAuth';
|
||||
import { createPbClient } from '$lib/server/pocketbase';
|
||||
import { createServices } from '$lib/server/services';
|
||||
|
||||
export async function POST(event: RequestEvent) {
|
||||
const { pb, famId, userId, role } = actingClient(event);
|
||||
const u = event.locals.user;
|
||||
if (!u || !event.locals.pbToken) throw error(401, 'Unauthorized');
|
||||
const pb = createPbClient(event.locals.pbToken);
|
||||
const id = event.params.id!;
|
||||
try {
|
||||
const s = createServices(pb, { id: userId, role });
|
||||
return json(await s.rewards.claim(famId, id));
|
||||
const s = createServices(pb, { id: u.id, role: u.role });
|
||||
return json(await s.rewards.claim(u.famId, id));
|
||||
} catch (e) {
|
||||
return json({ error: e instanceof Error ? e.message : 'claim failed' }, { status: 400 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user