mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
feat(labs): let a person order the lab categories (#1349)
The labs list layout gains categoryOrder, a bounded allow-list of category keys (200 entries, 64 characters each, deduplicated, malformed values dropped on read). GET /api/labs/overview reads the caller's own layout and answers categories already in that order, so every client renders them as given. Listed categories come first; keys the data does not have are ignored; a category the order does not name follows in the default order (catalog panels, then the person's own by label, then other). Other left at the end of the order stays last, behind any category added later; moved anywhere else, it stays there. Settings > Arrangement > Labs shows a category order card while categories are on. It lists the overview's categories under their localised labels, sends the whole order after every move, moves the cached overview at once and refetches it. The card says how the default order works. The OpenAPI document carries the new field.
This commit is contained in:
1 parent
157809c24f
commit
928ade5109
28 files changed
+660
-34
No files matched your search
@@ -401,6 +401,53 @@ describe("GET /api/labs/overview categories", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("answers the categories in the caller's saved order", async () => {
|
||||
await seedCategories();
|
||||
cookieJar.set("healthlog-locale", "en");
|
||||
const { PUT } = await import("@/app/api/settings/module-list-layout/route");
|
||||
const saved = await PUT(
|
||||
new Request("http://localhost/api/settings/module-list-layout", {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
// Lipids is left out (it follows the listed ones), "iron" has no
|
||||
// values here (ignored), and "other" sits first.
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
list: "labs",
|
||||
categoryOrder: ["other", "iron", "thyroid"],
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(saved.status).toBe(200);
|
||||
const { categories, grouped } = (
|
||||
(await (await getOverview()).json()) as CategoryBody
|
||||
).data;
|
||||
expect(grouped).toBe(true);
|
||||
expect(categories.map((c) => c.label)).toEqual([
|
||||
"Other",
|
||||
"Thyroid",
|
||||
"Lipids",
|
||||
"trace ELEMENTS",
|
||||
]);
|
||||
|
||||
// Left at the end, "other" stays last behind the categories not named.
|
||||
await PUT(
|
||||
new Request("http://localhost/api/settings/module-list-layout", {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
version: 1,
|
||||
list: "labs",
|
||||
categoryOrder: ["thyroid", "other"],
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const again = (
|
||||
(await (await getOverview()).json()) as CategoryBody
|
||||
).data.categories.map((c) => c.label);
|
||||
expect(again).toEqual(["Thyroid", "Lipids", "trace ELEMENTS", "Other"]);
|
||||
});
|
||||
|
||||
it("stays flat when grouping adds nothing", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
await prisma.labResult.create({
|
||||
|
||||
@@ -126,6 +126,7 @@ describe("/api/settings/module-list-layout — integration", () => {
|
||||
sortDir: "manual",
|
||||
groupBy: "none",
|
||||
groupsOpen: "expanded",
|
||||
categoryOrder: [],
|
||||
saved: true,
|
||||
});
|
||||
expect(body.data.lists.illness).toMatchObject({
|
||||
@@ -190,6 +191,61 @@ describe("/api/settings/module-list-layout — integration", () => {
|
||||
expect(refused.status).toBe(422);
|
||||
});
|
||||
|
||||
it("stores the category order deduplicated and keeps it through other writes", async () => {
|
||||
const { userId } = await seedUser();
|
||||
const { GET, PUT } =
|
||||
await import("@/app/api/settings/module-list-layout/route");
|
||||
const put = await (PUT as Handler)(
|
||||
json("PUT", URL, {
|
||||
version: 1,
|
||||
list: "labs",
|
||||
categoryOrder: ["thyroid", "other", "thyroid", "lipids"],
|
||||
}),
|
||||
);
|
||||
expect(put.status).toBe(200);
|
||||
await (PUT as Handler)(
|
||||
json("PUT", URL, { version: 1, list: "labs", groupBy: "category" }),
|
||||
);
|
||||
const res = await (GET as Handler)(new Request(URL));
|
||||
const body = (await res.json()) as {
|
||||
data: { lists: Record<string, Record<string, unknown>> };
|
||||
};
|
||||
expect(body.data.lists.labs).toMatchObject({
|
||||
categoryOrder: ["thyroid", "other", "lipids"],
|
||||
groupBy: "category",
|
||||
saved: true,
|
||||
});
|
||||
const row = await getPrismaClient().user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { moduleListLayoutJson: true },
|
||||
});
|
||||
expect(row?.moduleListLayoutJson).toEqual({
|
||||
version: 1,
|
||||
labs: {
|
||||
categoryOrder: ["thyroid", "other", "lipids"],
|
||||
groupBy: "category",
|
||||
},
|
||||
});
|
||||
|
||||
// An empty order is a choice too: back to the default order.
|
||||
await (PUT as Handler)(
|
||||
json("PUT", URL, { version: 1, list: "labs", categoryOrder: [] }),
|
||||
);
|
||||
const reset = (await (await (GET as Handler)(new Request(URL))).json()) as {
|
||||
data: { lists: { labs: { categoryOrder: string[] } } };
|
||||
};
|
||||
expect(reset.data.lists.labs.categoryOrder).toEqual([]);
|
||||
|
||||
const refused = await (PUT as Handler)(
|
||||
json("PUT", URL, {
|
||||
version: 1,
|
||||
list: "labs",
|
||||
categoryOrder: ["x".repeat(65), ""],
|
||||
}),
|
||||
);
|
||||
expect(refused.status).toBe(422);
|
||||
});
|
||||
|
||||
it("refuses unknown lists, values and keys with every issue named", async () => {
|
||||
await seedUser();
|
||||
const { PUT } = await import("@/app/api/settings/module-list-layout/route");
|
||||
|
||||
Reference in new issue
Block a user