fix(inkopslista): visa den genererade listan (inte en gammal tom)
POST aterananvander EN aktiv lista per veckoplan (staplar inte nya), GET sorterar nyaste forst, och appen navigerar med listId. Tidigare hamnade de genererade raderna i en lista skarmen inte visade -> tomt.
This commit is contained in:
@@ -101,7 +101,8 @@ export async function shoppingRoutes(app: FastifyInstance) {
|
||||
eq(schema.shoppingLists.householdId, householdId),
|
||||
eq(schema.shoppingLists.status, "active"),
|
||||
),
|
||||
);
|
||||
)
|
||||
.orderBy(desc(schema.shoppingLists.createdAt));
|
||||
return { lists };
|
||||
});
|
||||
|
||||
@@ -109,14 +110,46 @@ export async function shoppingRoutes(app: FastifyInstance) {
|
||||
const input = parse(createShoppingListInputSchema, req.body);
|
||||
const householdId = await requireActiveHousehold(app.db, req.userId);
|
||||
|
||||
const [list] = await app.db
|
||||
.insert(schema.shoppingLists)
|
||||
.values({ householdId, name: input.name, weekPlanId: input.weekPlanId ?? null })
|
||||
.returning();
|
||||
|
||||
// Generera från veckoplan: receptbehov − befintligt lager (spec §27)
|
||||
// Generera från veckoplan: återanvänd EN aktiv lista per veckoplan så att
|
||||
// "skapa inköpslista" inte staplar nya (tomma) listor. Utan detta pekade
|
||||
// appen på en annan/äldre lista och den genererade listan såg tom ut.
|
||||
let list;
|
||||
if (input.generateFromPlan && input.weekPlanId) {
|
||||
const [existing] = await app.db
|
||||
.select()
|
||||
.from(schema.shoppingLists)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.shoppingLists.householdId, householdId),
|
||||
eq(schema.shoppingLists.status, "active"),
|
||||
eq(schema.shoppingLists.weekPlanId, input.weekPlanId),
|
||||
),
|
||||
)
|
||||
.limit(1);
|
||||
if (existing) {
|
||||
list = existing;
|
||||
// Rensa tidigare plan-rader så omgenerering inte dubblerar.
|
||||
await app.db
|
||||
.delete(schema.shoppingListItems)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.shoppingListItems.shoppingListId, existing.id),
|
||||
eq(schema.shoppingListItems.origin, "plan"),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
[list] = await app.db
|
||||
.insert(schema.shoppingLists)
|
||||
.values({ householdId, name: input.name, weekPlanId: input.weekPlanId })
|
||||
.returning();
|
||||
}
|
||||
// Receptbehov − befintligt lager (spec §27)
|
||||
await generateItemsFromPlan(app, list!.id, input.weekPlanId, householdId, req.userId);
|
||||
} else {
|
||||
[list] = await app.db
|
||||
.insert(schema.shoppingLists)
|
||||
.values({ householdId, name: input.name, weekPlanId: input.weekPlanId ?? null })
|
||||
.returning();
|
||||
}
|
||||
|
||||
const items = await app.db
|
||||
|
||||
Reference in New Issue
Block a user