Hermetiska integrationstester + auto-hushall i onboarding (size, i18n, locations)

This commit is contained in:
Sven (AAMOS AI)
2026-08-06 22:06:11 +07:00
parent dd66ebedbf
commit 3696aadf98
12 changed files with 9079 additions and 44 deletions
+35
View File
@@ -47,6 +47,41 @@ export async function requireMembership(
return { role: member.role, portionFactor: member.portionFactor };
}
/** Skapa hushåll med standard-förvaringsplatser (spec §78). Återanvänds av
* både POST /v1/households och onboarding auto-create. */
export async function createHouseholdWithDefaults(
db: Database,
{
userId,
name,
size,
}: {
userId: string;
name: string;
size?: number;
},
): Promise<{ id: string; name: string }> {
const [household] = await db
.insert(schema.households)
.values({
name,
inviteCode: generateInviteCode(),
size: size ?? null,
})
.returning();
await db.insert(schema.householdMembers).values({
householdId: household!.id,
userId,
role: "owner",
});
await db.insert(schema.storageLocations).values([
{ householdId: household!.id, type: "fridge", name: "Kylen", sortOrder: 0 },
{ householdId: household!.id, type: "freezer", name: "Frysen", sortOrder: 1 },
{ householdId: household!.id, type: "pantry", name: "Skafferiet", sortOrder: 2 },
]);
return { id: household!.id, name: household!.name };
}
/** Hämta användarens aktiva hushåll (första medlemskapet) eller null. */
export async function getActiveHouseholdId(db: Database, userId: string): Promise<string | null> {
const [member] = await db
+31
View File
@@ -0,0 +1,31 @@
import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const cache: Record<string, Record<string, string>> = {};
/**
* Minimal server-side i18n lookup into the mobile app's common.json files.
* Falls back to the requested key if the translation is missing.
*/
export function t(key: string, languageTag: string): string {
const lang = (languageTag.split("-")[0] ?? languageTag).toLowerCase();
let dict = cache[lang];
if (!dict) {
try {
const file = path.resolve(
__dirname,
"../../../../apps/mobile/src/locales",
lang,
"common.json",
);
dict = JSON.parse(readFileSync(file, "utf8")) as Record<string, string>;
} catch {
dict = {};
}
cache[lang] = dict;
}
return dict[key] ?? key;
}