Compare commits

..

3 commits

Author SHA1 Message Date
b08561c9bb Squashed commit of the following:
Some checks failed
CI / build-test (push) Has been cancelled
commit 594a14009096f7321ff92638e05cfa7c5ae6ca07
Author: jableader <jacobdunk@gmail.com>
Date:   Sat Nov 1 12:20:33 2025 +1100

    API update - avoid optional arrays

commit 85787384cfc24dc28953ebb4630fe7614cb0ead1
Author: jableader <jacobdunk@gmail.com>
Date:   Sat Nov 1 10:14:51 2025 +1100

    refactor(types): add NonNullableArrays and apply to Meal arrays; test: add shopping mapper boundary tests; all checks green

commit 1c50b7e23585cd4a34b5a691ffbf87d6d123f474
Author: jableader <jacobdunk@gmail.com>
Date:   Sun Oct 26 15:30:45 2025 +1100

    Use discriminated unions and overloads
2025-11-01 12:22:52 +11:00
f2dee9ca30 Stricter api changes 2025-10-26 15:13:33 +11:00
211489ca55 pruning (#3)
Some checks failed
CI / build-test (push) Has been cancelled
Reviewed-on: #3
Co-authored-by: jableader <jacobdunk@gmail.com>
Co-committed-by: jableader <jacobdunk@gmail.com>
2025-10-25 02:18:30 +00:00
9 changed files with 402 additions and 133 deletions

45
.github/workflows/ci.yml vendored Normal file
View file

@ -0,0 +1,45 @@
name: CI
on:
push:
branches: [ main, master ]
pull_request:
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Codegen check
run: npm run codegen:check || true
- name: Generate OpenAPI types
run: |
if [ -f "../munch-ease-backend/openapi.json" ]; then
npm run codegen
else
echo "Backend openapi.json not found. Skipping codegen."
fi
- name: Typecheck
run: npm run -s typecheck
- name: Typecheck Vue SFC templates
run: npm run -s typecheck:vue
- name: Lint
run: npm run -s lint
- name: Test
run: npm run -s test

View file

@ -98,3 +98,15 @@ Environment
Testing Testing
- Unit tests use MSW; the client defaults to a localhost base in tests for easy mocking - Unit tests use MSW; the client defaults to a localhost base in tests for easy mocking
### CurrentShoppingList item kinds
The OpenAPI spec models current shopping list items as distinct kinds:
- outstandingItems: ListIngredientItem[]
- requestedMeals: RequestedMealItem[]
- purchasedItems: ListIngredientItem[]
The SDK maps these to a domain DTO (`CurrentShoppingListDTO`) and may attach refs (`ingredient`, `recipe`, `meal`, `list`) for convenience. UI code should:
- Prefer stable IDs (`ingredientId`, `mealId`, `recipeId`, `listId`) for actions and lookups
- Treat attached refs as optional view helpers (never required)
- Keep all normalization at the boundary (decoders); avoid casts and runtime type checks in app code

View file

@ -1,18 +1,32 @@
import { api } from '@/api/client' import { api } from '@/api/client'
import type { components } from '@/api/types' import type { components } from '@/api/types'
import { toDate, decodeMeal, decodeRecipe, decodeIngredients, decodeShoppingList, decodeShoppingListItems, decodeIngredient, decodeLookup } from '@/domain/decoders' import {
toDate,
decodeMeal,
decodeRecipe,
decodeIngredients,
decodeShoppingList,
decodeShoppingListItems,
decodeListIngredientItems,
decodeRequestedMealItems,
decodeIngredient,
decodeLookup,
} from '@/domain/decoders'
import type { import type {
Recipe, Recipe,
Meal, Meal,
Ingredient, Ingredient,
ShoppingList, ShoppingList,
ShoppingListItemWithRefs, ShoppingListItemWithRefs,
ListIngredientItemWithRefs,
RequestedMealItemWithRefs,
CurrentShoppingListDTO, CurrentShoppingListDTO,
PurchasedShoppingListDTO, PurchasedShoppingListDTO,
ShoppingLookups, ShoppingLookups,
} from '@/domain/types' } from '@/domain/types'
import { fromOpenApiPage, type Page } from '@/domain/pagination' import { fromOpenApiPage, type Page } from '@/domain/pagination'
import type { PurchaseRequest } from '@/domain/commands'
function httpError(response: Response, error: unknown): Error { function httpError(response: Response, error: unknown): Error {
if (error instanceof Error) return error if (error instanceof Error) return error
if (typeof error === 'string') return new Error(error) if (typeof error === 'string') return new Error(error)
@ -24,34 +38,32 @@ function httpError(response: Response, error: unknown): Error {
// Shopping list mapped view types now come from domain/types // Shopping list mapped view types now come from domain/types
function attachItemRefs( function attachItemRefs(
items: Array<ShoppingListItemWithRefs> | null | undefined, items: Array<ShoppingListItemWithRefs | ListIngredientItemWithRefs | RequestedMealItemWithRefs> | null | undefined,
lookups: ShoppingLookups lookups: ShoppingLookups
): void { ): void {
if (!Array.isArray(items)) return if (!Array.isArray(items)) return
for (const item of items) { for (const item of items) {
const ingredientId = item.ingredientId ?? undefined // ingredient ref
const mealId = item.mealId ?? undefined if ('ingredientId' in item && item.ingredientId !== undefined && lookups.ingredientsLookup) {
const recipeId = item.recipeId ?? undefined const v = lookups.ingredientsLookup[String(item.ingredientId)]
const listId = item.listId ?? undefined
const created = item.createdDate
if (ingredientId !== undefined && lookups.ingredientsLookup) {
const v = lookups.ingredientsLookup[String(ingredientId)]
if (v !== undefined) item.ingredient = v if (v !== undefined) item.ingredient = v
} }
if (mealId !== undefined && lookups.mealsLookup) { // meal ref (present on all item types)
const v = lookups.mealsLookup[String(mealId)] if ('mealId' in item && item.mealId !== undefined && lookups.mealsLookup) {
const v = lookups.mealsLookup[String(item.mealId)]
if (v !== undefined) item.meal = v if (v !== undefined) item.meal = v
} }
if (recipeId !== undefined && lookups.recipesLookup) { // recipe ref
const v = lookups.recipesLookup[String(recipeId)] if ('recipeId' in item && item.recipeId !== undefined && lookups.recipesLookup) {
const v = lookups.recipesLookup[String(item.recipeId)]
if (v !== undefined) item.recipe = v if (v !== undefined) item.recipe = v
} }
if (listId !== undefined && lookups.shoppingListLookup) { // list ref
const v = lookups.shoppingListLookup[String(listId)] if ('listId' in item && item.listId !== undefined && lookups.shoppingListLookup) {
const v = lookups.shoppingListLookup[String(item.listId)]
if (v !== undefined) item.list = v if (v !== undefined) item.list = v
} }
if (created !== undefined) item.createdDate = toDate(created) if ('createdDate' in item) item.createdDate = toDate(item.createdDate)
} }
} }
@ -115,9 +127,9 @@ export function mapCurrentShoppingList(dto: components['schemas']['CurrentShoppi
} }
const dtoOut: CurrentShoppingListDTO = { const dtoOut: CurrentShoppingListDTO = {
outstandingItems: decodeShoppingListItems(outstandingRaw ?? []), outstandingItems: decodeListIngredientItems(outstandingRaw ?? []),
requestedMeals: decodeShoppingListItems(requestedRaw ?? []), requestedMeals: decodeRequestedMealItems(requestedRaw ?? []),
purchasedItems: decodeShoppingListItems(purchasedRaw ?? []), purchasedItems: decodeListIngredientItems(purchasedRaw ?? []),
...(ingredientsLookup && { ingredientsLookup }), ...(ingredientsLookup && { ingredientsLookup }),
...(mealsLookup && { mealsLookup }), ...(mealsLookup && { mealsLookup }),
...(recipesLookup && { recipesLookup }), ...(recipesLookup && { recipesLookup }),
@ -299,33 +311,26 @@ export async function getCurrentShoppingList(): Promise<CurrentShoppingListDTO>
return mapped return mapped
} }
type PurchaseExisting = { type: 'existing'; id: number; personId: number; ingredientId?: number | null } // PurchaseRequest comes from domain/commands
type PurchaseRefs = { type: 'refs'; personId: number; ingredientId?: number | null; recipeId?: number | null; mealId?: number | null }
export type PurchaseRequest = PurchaseExisting | PurchaseRefs
export async function purchaseShoppingList( export async function purchaseShoppingList(
completedRequests: PurchaseRequest[] completedRequests: PurchaseRequest[]
): Promise<import('@/domain/types').ShoppingListWithRefs | null> { ): Promise<import('@/domain/types').ShoppingListWithRefs | null> {
if (!Array.isArray(completedRequests) || completedRequests.length === 0) return null if (!Array.isArray(completedRequests) || completedRequests.length === 0) return null
// Map incoming requests: if id provided and >= 0, use it; otherwise send identifiers for ingredient/recipe/meal // Map incoming requests: if id provided and >= 0, use it; otherwise send identifiers for ingredient/recipe/meal
const items: components['schemas']['ShoppingListItem'][] = completedRequests.map((i) => const items: components['schemas']['IngredientPurchaseItemIn'][] = completedRequests.map((i) => ({
i.type === 'existing' personId: i.personId,
? { id: i.id, personId: i.personId, ingredientId: i.ingredientId ?? null } ingredientId: i.ingredientId ?? -1,
: { recipeId: i.type === 'refs' ? i.recipeId ?? null : null,
id: -1, mealId: i.type === 'refs' ? i.mealId ?? null : null,
personId: i.personId, createdDate: null,
ingredientId: i.ingredientId ?? null, }))
recipeId: i.recipeId ?? null,
mealId: i.mealId ?? null,
}
)
if (items.length === 0) return null if (items.length === 0) return null
const body: components['schemas']['ShoppingList'] = { const body: components['schemas']['PurchaseListIn'] = {
id: -1, // Default to a valid StoreNameOut per updated OpenAPI ("home" | "coles" | "woolworths")
storeName: '', storeName: 'home',
purchasedById: -1,
items, items,
} }
const { data, error, response } = await api.POST('/api/v1/shopping', { const { data, error, response } = await api.POST('/api/v1/shopping', {
@ -351,3 +356,6 @@ export async function unrequestMeal(mealId: number | string): Promise<void> {
}) })
if (!response.ok) throw httpError(response, error) if (!response.ok) throw httpError(response, error)
} }
// Re-export domain command types for convenience at SDK surface
export type { PurchaseRequest } from '@/domain/commands'

View file

@ -340,25 +340,25 @@ export interface components {
/** CurrentShoppingList */ /** CurrentShoppingList */
CurrentShoppingList: { CurrentShoppingList: {
/** Outstandingitems */ /** Outstandingitems */
outstandingItems: components["schemas"]["ShoppingListItem"][]; outstandingItems: components["schemas"]["ListIngredientItem"][];
/** Requestedmeals */ /** Requestedmeals */
requestedMeals: components["schemas"]["ShoppingListItem"][]; requestedMeals: components["schemas"]["RequestedMealItem"][];
/** Purchaseditems */ /** Purchaseditems */
purchasedItems?: components["schemas"]["ShoppingListItem"][]; purchasedItems: components["schemas"]["ListIngredientItem"][];
/** Ingredientslookup */ /** Ingredientslookup */
ingredientsLookup?: { ingredientsLookup: {
[key: string]: components["schemas"]["Ingredient"]; [key: string]: components["schemas"]["Ingredient"];
}; };
/** Mealslookup */ /** Mealslookup */
mealsLookup?: { mealsLookup: {
[key: string]: components["schemas"]["Meal-Output"]; [key: string]: components["schemas"]["Meal-Output"];
}; };
/** Shoppinglistlookup */ /** Shoppinglistlookup */
shoppingListLookup?: { shoppingListLookup: {
[key: string]: components["schemas"]["ShoppingList"]; [key: string]: components["schemas"]["ShoppingListOut"];
}; };
/** Recipeslookup */ /** Recipeslookup */
recipesLookup?: { recipesLookup: {
[key: string]: components["schemas"]["Recipe-Output"]; [key: string]: components["schemas"]["Recipe-Output"];
}; };
}; };
@ -404,6 +404,49 @@ export interface components {
mealId?: number | null; mealId?: number | null;
product?: components["schemas"]["Product"] | null; product?: components["schemas"]["Product"] | null;
}; };
/** IngredientPurchaseItemIn */
IngredientPurchaseItemIn: {
/** Ingredientid */
ingredientId: number;
/** Personid */
personId: number;
/** Createddate */
createdDate?: string | null;
/** Mealid */
mealId?: number | null;
/** Recipeid */
recipeId?: number | null;
};
/** ListIngredientItem */
ListIngredientItem: {
/**
* Kind
* @default ingredient
* @constant
* @enum {string}
*/
kind: "ingredient";
/**
* Id
* @default -1
*/
id: number;
/** Ingredientid */
ingredientId: number;
/** Personid */
personId: number;
/**
* Createddate
* Format: date-time
*/
createdDate: string;
/** Listid */
listId?: number | null;
/** Mealid */
mealId?: number | null;
/** Recipeid */
recipeId?: number | null;
};
/** LoginBody */ /** LoginBody */
LoginBody: { LoginBody: {
/** Username */ /** Username */
@ -468,6 +511,33 @@ export interface components {
/** Mealid */ /** Mealid */
mealId: number; mealId: number;
}; };
/** MealOut */
MealOut: {
/**
* Id
* @default -1
*/
id: number;
/**
* Suggesteddate
* Format: date-time
*/
suggestedDate: string;
/** Consumeddate */
consumedDate?: string | null;
/** Chefs */
chefs: components["schemas"]["Person"][];
/** Cleanup */
cleanup: components["schemas"]["Person"][];
/** Consumers */
consumers: components["schemas"]["Person"][];
/** Recipes */
recipes: components["schemas"]["MealRecipe-Output"][];
/** Extraingredients */
extraIngredients: components["schemas"]["Ingredient"][];
/** Purchasedate */
purchaseDate?: string | null;
};
/** MealRecipe */ /** MealRecipe */
"MealRecipe-Input": { "MealRecipe-Input": {
/** Mealid */ /** Mealid */
@ -511,10 +581,10 @@ export interface components {
*/ */
total: number; total: number;
}; };
/** Page[Recipe] */ /** Page[RecipeOut] */
Page_Recipe_: { Page_RecipeOut_: {
/** Items */ /** Items */
items: components["schemas"]["Recipe-Output"][]; items: components["schemas"]["RecipeOut"][];
/** Nextcursor */ /** Nextcursor */
nextCursor?: string | null; nextCursor?: string | null;
/** Prevcursor */ /** Prevcursor */
@ -585,19 +655,25 @@ export interface components {
/** Tags */ /** Tags */
tags?: string[]; tags?: string[];
}; };
/** PurchaseListIn */
PurchaseListIn: {
storeName: components["schemas"]["StoreNameOut"];
/** Items */
items: components["schemas"]["IngredientPurchaseItemIn"][];
};
/** PurchasedShoppingList */ /** PurchasedShoppingList */
PurchasedShoppingList: { PurchasedShoppingList: {
list: components["schemas"]["ShoppingList"]; list: components["schemas"]["ShoppingListOut"];
/** Mealslookup */ /** Mealslookup */
mealsLookup?: { mealsLookup: {
[key: string]: components["schemas"]["Meal-Output"]; [key: string]: components["schemas"]["Meal-Output"];
}; };
/** Ingredientslookup */ /** Ingredientslookup */
ingredientsLookup?: { ingredientsLookup: {
[key: string]: components["schemas"]["Ingredient"]; [key: string]: components["schemas"]["Ingredient"];
}; };
/** Recipeslookup */ /** Recipeslookup */
recipesLookup?: { recipesLookup: {
[key: string]: components["schemas"]["Recipe-Output"]; [key: string]: components["schemas"]["Recipe-Output"];
}; };
}; };
@ -667,54 +743,82 @@ export interface components {
hiddenById?: number | null; hiddenById?: number | null;
hiddenBy?: components["schemas"]["Person"] | null; hiddenBy?: components["schemas"]["Person"] | null;
}; };
/** ShoppingList */ /** RecipeOut */
ShoppingList: { RecipeOut: {
/** /**
* Id * Id
* @default -1 * @default -1
*/ */
id: number; id: number;
/** Name */
name: string;
/** Link */
link: string;
/** Serves */
serves: number;
/** Imageurls */
imageUrls: string[];
/** Ingredients */
ingredients: components["schemas"]["Ingredient"][];
/** Basedonrecipe */
basedOnRecipe?: number | null;
/**
* Datecreated
* Format: date-time
*/
dateCreated: string;
/** Createdbyid */
createdById: number;
createdBy?: components["schemas"]["Person"] | null;
/** Datehidden */
dateHidden?: string | null;
/** Hiddenbyid */
hiddenById?: number | null;
hiddenBy?: components["schemas"]["Person"] | null;
};
/** RequestedMealItem */
RequestedMealItem: {
/**
* Kind
* @default requestedMeal
* @constant
* @enum {string}
*/
kind: "requestedMeal";
/**
* Id
* @default -1
*/
id: number;
/** Personid */
personId: number;
/** Mealid */
mealId: number;
/** /**
* Createddate * Createddate
* Format: date-time * Format: date-time
*/ */
createdDate?: string; createdDate: string;
/** @default */ };
storeName: components["schemas"]["StoreEnum"]; /** ShoppingListOut */
ShoppingListOut: {
/** Id */
id: number;
/** /**
* Purchasedbyid * Createddate
* @default -1 * Format: date-time
*/ */
createdDate: string;
/**
* Storename
* @enum {string}
*/
storeName: "woolworths" | "coles" | "home";
/** Purchasedbyid */
purchasedById: number; purchasedById: number;
purchasedBy?: components["schemas"]["Person"] | null; purchasedBy?: components["schemas"]["Person"] | null;
/** Items */ /** Items */
items?: components["schemas"]["ShoppingListItem"][]; items: components["schemas"]["ListIngredientItem"][];
};
/** ShoppingListItem */
ShoppingListItem: {
/**
* Id
* @default -1
*/
id: number;
/** Listid */
listId?: number | null;
/**
* Personid
* @default -1
*/
personId: number;
/** Ingredientid */
ingredientId?: number | null;
/** Recipeid */
recipeId?: number | null;
/** Mealid */
mealId?: number | null;
/**
* Createddate
* Format: date-time
*/
createdDate?: string;
}; };
/** /**
* StoreEnum * StoreEnum
@ -730,6 +834,11 @@ export interface components {
/** Error Type */ /** Error Type */
type: string; type: string;
}; };
/**
* StoreNameOut
* @enum {string}
*/
StoreNameOut: "woolworths" | "coles" | "home";
}; };
responses: { responses: {
/** @description Bad Request */ /** @description Bad Request */
@ -822,7 +931,7 @@ export interface operations {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["Recipe-Output"]; "application/json": components["schemas"]["RecipeOut"];
}; };
}; };
400: components["responses"]["Problem400"]; 400: components["responses"]["Problem400"];
@ -907,7 +1016,7 @@ export interface operations {
* "total": 1 * "total": 1
* } * }
*/ */
"application/json": components["schemas"]["Page_Recipe_"]; "application/json": components["schemas"]["Page_RecipeOut_"];
}; };
}; };
/** @description Validation Error */ /** @description Validation Error */
@ -942,7 +1051,7 @@ export interface operations {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["Recipe-Output"]; "application/json": components["schemas"]["RecipeOut"];
}; };
}; };
400: components["responses"]["Problem400"]; 400: components["responses"]["Problem400"];
@ -974,7 +1083,7 @@ export interface operations {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["Recipe-Output"]; "application/json": components["schemas"]["RecipeOut"];
}; };
}; };
404: components["responses"]["Problem404"]; 404: components["responses"]["Problem404"];
@ -1072,7 +1181,7 @@ export interface operations {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["Meal-Output"]; "application/json": components["schemas"]["MealOut"];
}; };
}; };
404: components["responses"]["Problem404"]; 404: components["responses"]["Problem404"];
@ -1108,7 +1217,7 @@ export interface operations {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["Meal-Output"]; "application/json": components["schemas"]["MealOut"];
}; };
}; };
400: components["responses"]["Problem400"]; 400: components["responses"]["Problem400"];
@ -1143,7 +1252,7 @@ export interface operations {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["Meal-Output"]; "application/json": components["schemas"]["MealOut"];
}; };
}; };
404: components["responses"]["Problem404"]; 404: components["responses"]["Problem404"];
@ -1177,7 +1286,7 @@ export interface operations {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["Meal-Output"]; "application/json": components["schemas"]["MealOut"];
}; };
}; };
400: components["responses"]["Problem400"]; 400: components["responses"]["Problem400"];
@ -1213,7 +1322,7 @@ export interface operations {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["Meal-Output"]; "application/json": components["schemas"]["MealOut"];
}; };
}; };
400: components["responses"]["Problem400"]; 400: components["responses"]["Problem400"];
@ -1293,7 +1402,7 @@ export interface operations {
}; };
requestBody: { requestBody: {
content: { content: {
"application/json": components["schemas"]["ShoppingList"]; "application/json": components["schemas"]["PurchaseListIn"];
}; };
}; };
responses: { responses: {
@ -1415,7 +1524,7 @@ export interface operations {
[name: string]: unknown; [name: string]: unknown;
}; };
content: { content: {
"application/json": components["schemas"]["ShoppingListItem"]; "application/json": components["schemas"]["RequestedMealItem"];
}; };
}; };
404: components["responses"]["Problem404"]; 404: components["responses"]["Problem404"];

View file

@ -1,15 +1,15 @@
import * as sdk from '@/api/sdk' import * as sdk from '@/api/sdk'
import type { ShoppingListItemWithRefs, Product, Meal } from '@/domain/types' import type { ListIngredientItemWithRefs, RequestedMealItemWithRefs, Product, Meal } from '@/domain/types'
export type GroupByProduct = { type: 'product'; product: Product; shoppingListItems: ShoppingListItemWithRefs[] } export type GroupByProduct = { type: 'product'; product: Product; shoppingListItems: ListIngredientItemWithRefs[] }
export type GroupByName = { type: 'name'; name: string; shoppingListItems: ShoppingListItemWithRefs[] } export type GroupByName = { type: 'name'; name: string; shoppingListItems: ListIngredientItemWithRefs[] }
export type Group = GroupByProduct | GroupByName export type Group = GroupByProduct | GroupByName
export function groupsToItems(groups: Group[]): ShoppingListItemWithRefs[] { export function groupsToItems(groups: Group[]): ListIngredientItemWithRefs[] {
return groups.map((g) => g.shoppingListItems).flat() return groups.map((g) => g.shoppingListItems).flat()
} }
export function uniqueMeals(shoppingListItems: ShoppingListItemWithRefs[]): Meal[] { export function uniqueMeals(shoppingListItems: Array<ListIngredientItemWithRefs | RequestedMealItemWithRefs>): Meal[] {
const mealsWithDuplicates = shoppingListItems.map((item) => item.meal).filter((m): m is Meal => !!m) const mealsWithDuplicates = shoppingListItems.map((item) => item.meal).filter((m): m is Meal => !!m)
const mealsLookup: Record<string | number, Meal> = mealsWithDuplicates.reduce<Record<string | number, Meal>>( const mealsLookup: Record<string | number, Meal> = mealsWithDuplicates.reduce<Record<string | number, Meal>>(
(acc, meal) => { (acc, meal) => {
@ -21,7 +21,7 @@ export function uniqueMeals(shoppingListItems: ShoppingListItemWithRefs[]): Meal
return Object.values(mealsLookup) return Object.values(mealsLookup)
} }
export function itemsToGroups(shoppingListItems: ShoppingListItemWithRefs[]): Group[] { export function itemsToGroups(shoppingListItems: ListIngredientItemWithRefs[]): Group[] {
const ingredients_by_product_id: Record<string | number, GroupByProduct> = {} const ingredients_by_product_id: Record<string | number, GroupByProduct> = {}
const ingredients_by_name: Record<string, GroupByName> = {} const ingredients_by_name: Record<string, GroupByName> = {}
for (const item of shoppingListItems) { for (const item of shoppingListItems) {
@ -52,8 +52,8 @@ export function itemsToGroups(shoppingListItems: ShoppingListItemWithRefs[]): Gr
} }
export function useShopping() { export function useShopping() {
const groupsFrom = (items?: ShoppingListItemWithRefs[]): Group[] => itemsToGroups(items ?? []) const groupsFrom = (items?: ListIngredientItemWithRefs[]): Group[] => itemsToGroups(items ?? [])
const mealsFrom = (items?: ShoppingListItemWithRefs[]): Meal[] => uniqueMeals(items ?? []) const mealsFrom = (items?: Array<ListIngredientItemWithRefs | RequestedMealItemWithRefs>): Meal[] => uniqueMeals(items ?? [])
return { return {
getCurrentShoppingList: sdk.getCurrentShoppingList, getCurrentShoppingList: sdk.getCurrentShoppingList,
getShoppingList: sdk.getShoppingList, getShoppingList: sdk.getShoppingList,
@ -68,14 +68,14 @@ export function useShopping() {
async purchaseFromGroups(groups: Group[]) { async purchaseFromGroups(groups: Group[]) {
const items = groupsToItems(groups).map((i): sdk.PurchaseRequest => { const items = groupsToItems(groups).map((i): sdk.PurchaseRequest => {
if (typeof i.id === 'number' && i.id >= 0) { if (typeof i.id === 'number' && i.id >= 0) {
return { type: 'existing', id: i.id, personId: i.personId, ingredientId: i.ingredient?.id ?? null } return { type: 'existing', id: i.id, personId: i.personId, ingredientId: i.ingredientId ?? null }
} }
return { return {
type: 'refs', type: 'refs',
personId: i.personId, personId: i.personId,
ingredientId: i.ingredient?.id ?? null, ingredientId: i.ingredientId ?? null,
recipeId: i.recipe?.id ?? null, recipeId: i.recipeId ?? null,
mealId: i.meal?.id ?? null, mealId: i.mealId ?? null,
} }
}) })
if (!items?.length) return null if (!items?.length) return null

5
src/domain/commands.ts Normal file
View file

@ -0,0 +1,5 @@
// Domain command types: stable UI intents, translated to OpenAPI at the SDK boundary
export type PurchaseExisting = { type: 'existing'; id: number; personId: number; ingredientId?: number | null }
export type PurchaseRefs = { type: 'refs'; personId: number; ingredientId?: number | null; recipeId?: number | null; mealId?: number | null }
export type PurchaseRequest = PurchaseExisting | PurchaseRefs

View file

@ -1,4 +1,4 @@
import type { RecipeOut, Recipe, MealOut, Meal, MealRecipe, Ingredient as DomainIngredient, ShoppingList, ShoppingListItem, ShoppingListItemWithRefs, MealInput } from './types' import type { Recipe, Meal, MealRecipe, Ingredient as DomainIngredient, ShoppingList, ShoppingListItem, ShoppingListItemWithRefs, MealInput, ListIngredientItem, RequestedMealItem, ListIngredientItemWithRefs, RequestedMealItemWithRefs } from './types'
import type { components } from '@/api/types' import type { components } from '@/api/types'
export function toDate(value: string | Date | null | undefined): Date | null { export function toDate(value: string | Date | null | undefined): Date | null {
@ -23,16 +23,25 @@ export function decodeLookup<TIn, TOut>(
return out return out
} }
export function decodeRecipe(r: RecipeOut | null | undefined): Recipe { export function decodeRecipe(
r: components['schemas']['RecipeOut'] | components['schemas']['Recipe-Output'] | null | undefined
): Recipe {
if (!r) throw new Error('Invalid recipe payload') if (!r) throw new Error('Invalid recipe payload')
// Normalize arrays that may be optional in legacy Recipe-Output
const imageUrls = r.imageUrls ?? []
const ingredients = r.ingredients ?? []
return { return {
...r, ...r,
imageUrls,
ingredients,
dateCreated: toDate(r.dateCreated), dateCreated: toDate(r.dateCreated),
dateHidden: toDate(r.dateHidden), dateHidden: toDate(r.dateHidden),
} }
} }
export function decodeMeal(m: MealOut | null | undefined): Meal { export function decodeMeal(
m: components['schemas']['MealOut'] | components['schemas']['Meal-Output'] | null | undefined
): Meal {
if (!m) throw new Error('Invalid meal payload') if (!m) throw new Error('Invalid meal payload')
const recipes = Array.isArray(m.recipes) const recipes = Array.isArray(m.recipes)
? m.recipes.map((mr) => decodeMealRecipe(mr)) ? m.recipes.map((mr) => decodeMealRecipe(mr))
@ -70,7 +79,7 @@ export function decodeIngredients(list: components['schemas']['Ingredient'][] |
return list.map((i) => decodeIngredient(i)) return list.map((i) => decodeIngredient(i))
} }
function decodeShoppingListItem(i: components['schemas']['ShoppingListItem'] | null | undefined): ShoppingListItem { function decodeShoppingListItem(i: components['schemas']['ListIngredientItem'] | null | undefined): ShoppingListItem {
if (!i) throw new Error('Invalid shopping list item payload') if (!i) throw new Error('Invalid shopping list item payload')
return { return {
...i, ...i,
@ -78,13 +87,13 @@ function decodeShoppingListItem(i: components['schemas']['ShoppingListItem'] | n
} }
} }
export function decodeShoppingListItems(list: components['schemas']['ShoppingListItem'][] | null | undefined): ShoppingListItemWithRefs[] { export function decodeShoppingListItems(list: components['schemas']['ListIngredientItem'][] | null | undefined): ShoppingListItemWithRefs[] {
if (!Array.isArray(list)) return [] if (!Array.isArray(list)) return []
// Build a new array with item clones to allow optional refs to be attached later // Build a new array with item clones to allow optional refs to be attached later
return list.map((raw) => ({ ...decodeShoppingListItem(raw) })) return list.map((raw) => ({ ...decodeShoppingListItem(raw) }))
} }
export function decodeShoppingList(v: components['schemas']['ShoppingList'] | null | undefined): ShoppingList { export function decodeShoppingList(v: components['schemas']['ShoppingListOut'] | null | undefined): ShoppingList {
if (!v) throw new Error('Invalid shopping list payload') if (!v) throw new Error('Invalid shopping list payload')
const { items: rawItems, ...rest } = v const { items: rawItems, ...rest } = v
const items = Array.isArray(rawItems) ? decodeShoppingListItems(rawItems) : undefined const items = Array.isArray(rawItems) ? decodeShoppingListItems(rawItems) : undefined
@ -95,6 +104,33 @@ export function decodeShoppingList(v: components['schemas']['ShoppingList'] | nu
} }
} }
// New item decoders for tightened CurrentShoppingList
function decodeListIngredientItem(i: components['schemas']['ListIngredientItem'] | null | undefined): ListIngredientItem {
if (!i) throw new Error('Invalid list ingredient item payload')
return {
...i,
createdDate: toDate(i.createdDate),
}
}
export function decodeListIngredientItems(list: components['schemas']['ListIngredientItem'][] | null | undefined): ListIngredientItemWithRefs[] {
if (!Array.isArray(list)) return []
return list.map((raw) => ({ ...decodeListIngredientItem(raw) }))
}
function decodeRequestedMealItem(i: components['schemas']['RequestedMealItem'] | null | undefined): RequestedMealItem {
if (!i) throw new Error('Invalid requested meal item payload')
return {
...i,
createdDate: toDate(i.createdDate),
}
}
export function decodeRequestedMealItems(list: components['schemas']['RequestedMealItem'][] | null | undefined): RequestedMealItemWithRefs[] {
if (!Array.isArray(list)) return []
return list.map((raw) => ({ ...decodeRequestedMealItem(raw) }))
}
// Helper to convert domain Meal to MealInput, keeping Date→string conversion in boundary // Helper to convert domain Meal to MealInput, keeping Date→string conversion in boundary
export function toMealInput(meal: Meal): MealInput { export function toMealInput(meal: Meal): MealInput {
return { return {

View file

@ -3,6 +3,10 @@ import type { components } from '@/api/types'
// Utility mapped types // Utility mapped types
export type Replace<T, M> = Omit<T, keyof M> & M export type Replace<T, M> = Omit<T, keyof M> & M
export type WithDates<T, K extends keyof T> = Replace<T, { [P in K]: Date | null }> export type WithDates<T, K extends keyof T> = Replace<T, { [P in K]: Date | null }>
// Convert selected array keys to their non-nullable array counterparts
export type NonNullableArrays<T, K extends keyof T> = Replace<T, { [P in K]: NonNullable<T[P]> extends Array<infer U> ? U[] : T[P] }>
// Make selected keys required (non-optional) on a type
export type RequiredKeys<T, K extends keyof T> = Replace<T, { [P in K]-?: NonNullable<T[P]> }>
// Common helpers (intentionally minimal to avoid unused exports) // Common helpers (intentionally minimal to avoid unused exports)
export type Lookup<T> = Record<string, T> export type Lookup<T> = Record<string, T>
@ -10,8 +14,8 @@ export type Lookup<T> = Record<string, T>
export type WithRefs<T, Refs extends object> = T & { [K in keyof Refs]?: Refs[K] | undefined } export type WithRefs<T, Refs extends object> = T & { [K in keyof Refs]?: Refs[K] | undefined }
// Domain type aliases // Domain type aliases
export type RecipeOut = components['schemas']['Recipe-Output'] export type RecipeOut = components['schemas']['RecipeOut']
export type MealOut = components['schemas']['Meal-Output'] export type MealOut = components['schemas']['MealOut']
export type Ingredient = components['schemas']['Ingredient'] export type Ingredient = components['schemas']['Ingredient']
export type Product = components['schemas']['Product'] export type Product = components['schemas']['Product']
export type Person = components['schemas']['Person'] export type Person = components['schemas']['Person']
@ -27,24 +31,26 @@ export type MealRecipe = Replace<MealRecipeOut, { recipe: Recipe | null }>
// Meal with decoded dates and nested MealRecipe with decoded recipe dates // Meal with decoded dates and nested MealRecipe with decoded recipe dates
// Arrays (chefs, consumers, cleanup, recipes, extraIngredients) are non-nullable per OpenAPI spec // Arrays (chefs, consumers, cleanup, recipes, extraIngredients) are non-nullable per OpenAPI spec
export type Meal = Replace< type MealBase = Replace<WithDates<MealOut, 'suggestedDate' | 'consumedDate' | 'purchaseDate'>, { recipes: MealRecipe[] }>
WithDates<MealOut, 'suggestedDate' | 'consumedDate' | 'purchaseDate'>, export type Meal = RequiredKeys<
{ NonNullableArrays<MealBase, 'chefs' | 'consumers' | 'cleanup' | 'extraIngredients'>,
recipes: MealRecipe[] 'recipes' | 'chefs' | 'consumers' | 'cleanup' | 'extraIngredients'
chefs: Person[]
consumers: Person[]
cleanup: Person[]
extraIngredients: Ingredient[]
}
> >
// Shopping domain shapes with dates normalized // Shopping domain shapes with dates normalized
export type ShoppingListItem = WithDates<components['schemas']['ShoppingListItem'], 'createdDate'> // Items inside purchased lists and current lists share the ListIngredientItem shape
type ShoppingListBase = WithDates<components['schemas']['ShoppingList'], 'createdDate'> export type ShoppingListItem = WithDates<components['schemas']['ListIngredientItem'], 'createdDate'>
type ShoppingListBase = WithDates<components['schemas']['ShoppingListOut'], 'createdDate'>
export type ShoppingList = Replace<ShoppingListBase, { items?: ShoppingListItem[] | undefined }> export type ShoppingList = Replace<ShoppingListBase, { items?: ShoppingListItem[] | undefined }>
// Refs attached to shopping list items // Current shopping list item types (tightened OpenAPI)
export type ListIngredientItem = WithDates<components['schemas']['ListIngredientItem'], 'createdDate'>
export type RequestedMealItem = WithDates<components['schemas']['RequestedMealItem'], 'createdDate'>
// Refs attached to items
export type ShoppingListItemWithRefs = WithRefs<ShoppingListItem, { ingredient: Ingredient; recipe: Recipe; meal: Meal; list: ShoppingList }> export type ShoppingListItemWithRefs = WithRefs<ShoppingListItem, { ingredient: Ingredient; recipe: Recipe; meal: Meal; list: ShoppingList }>
export type ListIngredientItemWithRefs = WithRefs<ListIngredientItem, { ingredient: Ingredient; recipe: Recipe; meal: Meal; list: ShoppingList }>
export type RequestedMealItemWithRefs = WithRefs<RequestedMealItem, { meal: Meal }>
export type ShoppingListWithRefs = Replace<ShoppingList, { items?: ShoppingListItemWithRefs[] }> export type ShoppingListWithRefs = Replace<ShoppingList, { items?: ShoppingListItemWithRefs[] }>
@ -58,9 +64,9 @@ export type ShoppingLookups = {
// DTO shapes returned by SDK for shopping pages // DTO shapes returned by SDK for shopping pages
export type CurrentShoppingListDTO = { export type CurrentShoppingListDTO = {
outstandingItems: ShoppingListItemWithRefs[] outstandingItems: ListIngredientItemWithRefs[]
requestedMeals: ShoppingListItemWithRefs[] requestedMeals: RequestedMealItemWithRefs[]
purchasedItems: ShoppingListItemWithRefs[] purchasedItems: ListIngredientItemWithRefs[]
} & ShoppingLookups } & ShoppingLookups
export type PurchasedShoppingListDTO = ShoppingLookups & { export type PurchasedShoppingListDTO = ShoppingLookups & {

View file

@ -0,0 +1,48 @@
import { describe, it, expect } from 'vitest'
import { mapCurrentShoppingList, mapPurchasedShoppingList } from '@/api/sdk'
// These tests lock boundary behavior for partial/missing lookups and date normalization
describe('shopping mappers boundary', () => {
it('handles missing lookups gracefully (no refs attached)', () => {
const dto = {
outstandingItems: [
{ ingredientId: 10, mealId: 1, recipeId: 5, listId: 7, createdDate: '2025-01-01T00:00:00Z' },
],
requestedMeals: [
{ mealId: 2, createdDate: '2025-01-02T00:00:00Z' },
],
purchasedItems: [],
}
const mapped = mapCurrentShoppingList(dto as any)
expect(mapped.outstandingItems[0].ingredient).toBeUndefined()
expect(mapped.outstandingItems[0].meal).toBeUndefined()
expect(mapped.outstandingItems[0].recipe).toBeUndefined()
expect(mapped.outstandingItems[0].list).toBeUndefined()
expect(mapped.requestedMeals[0].meal).toBeUndefined()
// Dates normalized
expect(mapped.outstandingItems[0].createdDate).toBeInstanceOf(Date)
expect(mapped.requestedMeals[0].createdDate).toBeInstanceOf(Date)
})
it('normalizes dates on purchased list and items even with partial lookups', () => {
const dto = {
ingredientsLookup: { 10: { id: 10, name: 'Milk' } },
list: {
id: 11,
createdDate: '2025-03-03T00:00:00Z',
items: [
{ ingredientId: 10, listId: 11, createdDate: '2025-03-03T00:00:00Z' },
{ ingredientId: 99, listId: 11, createdDate: '2025-03-03T00:00:00Z' },
],
},
}
const mapped = mapPurchasedShoppingList(dto as any)
expect(mapped.list.createdDate).toBeInstanceOf(Date)
expect(mapped.list.items[0].createdDate).toBeInstanceOf(Date)
expect(mapped.list.items[1].createdDate).toBeInstanceOf(Date)
// First item gets ingredient ref, second does not
expect(mapped.list.items[0].ingredient?.name).toBe('Milk')
expect(mapped.list.items[1].ingredient).toBeUndefined()
})
})