@revenexx/cover 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app/app.config.ts CHANGED
@@ -23,6 +23,7 @@ export default defineAppConfig({
23
23
  priceService: "mock",
24
24
  orderService: "mock",
25
25
  inventoryService: "mock",
26
+ orderListService: "mock",
26
27
  themeService: "mock",
27
28
  // Search UX defaults the layer's components read — apps may override,
28
29
  // but the layer must ship working values (a missing `search` object
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revenexx/cover",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Cover — revenexx design system for Nuxt. Distributed as a Nuxt layer: generic UI components, theming tokens and stores shared by the demo shop, custom storefronts and the Blokkli theme.",
5
5
  "type": "module",
6
6
  "main": "./nuxt.config.ts",
@@ -2,12 +2,18 @@ import type { OrderList } from "../../../../app/interfaces/account/order-lists";
2
2
 
3
3
  /** Deletes an order list (owner only). */
4
4
  export default defineEventHandler(async (event) => {
5
+ const id = getRouterParam(event, "id") ?? "";
6
+
7
+ if (resolveOrderListServiceKey(event) === "api") {
8
+ await deleteOrderList(event, id);
9
+ return { ok: true };
10
+ }
11
+
5
12
  const user = await getAuthService(event).me(event);
6
13
  if (!user) {
7
14
  throw createError({ statusCode: 401, message: "Unauthorized" });
8
15
  }
9
16
 
10
- const id = getRouterParam(event, "id") ?? "";
11
17
  const lists = await readCoverMutableJson<OrderList[]>("account/order-lists.json");
12
18
  const record = lists.find(list => list.id === id);
13
19
  if (!record) {
@@ -2,12 +2,17 @@ import type { OrderList } from "../../../../app/interfaces/account/order-lists";
2
2
 
3
3
  /** Single order list (own or organization-public). */
4
4
  export default defineEventHandler(async (event) => {
5
+ const id = getRouterParam(event, "id") ?? "";
6
+
7
+ if (resolveOrderListServiceKey(event) === "api") {
8
+ return await getOrderList(event, id);
9
+ }
10
+
5
11
  const user = await getAuthService(event).me(event);
6
12
  if (!user) {
7
13
  throw createError({ statusCode: 401, message: "Unauthorized" });
8
14
  }
9
15
 
10
- const id = getRouterParam(event, "id") ?? "";
11
16
  const lists = await readCoverMutableJson<OrderList[]>("account/order-lists.json");
12
17
  const list = lists.find(l => l.id === id && (l.owner.id === user.$id || l.public));
13
18
  if (!list) {
@@ -5,11 +5,6 @@ import type { OrderList, OrderListPosition } from "../../../../app/interfaces/ac
5
5
  * (quantity changes, removals, appends). Only the owner may modify.
6
6
  */
7
7
  export default defineEventHandler(async (event) => {
8
- const user = await getAuthService(event).me(event);
9
- if (!user) {
10
- throw createError({ statusCode: 401, message: "Unauthorized" });
11
- }
12
-
13
8
  const id = getRouterParam(event, "id") ?? "";
14
9
  const body = await readBody<{
15
10
  name?: string;
@@ -18,6 +13,15 @@ export default defineEventHandler(async (event) => {
18
13
  appendPositions?: OrderListPosition[];
19
14
  }>(event);
20
15
 
16
+ if (resolveOrderListServiceKey(event) === "api") {
17
+ return await updateOrderList(event, id, body);
18
+ }
19
+
20
+ const user = await getAuthService(event).me(event);
21
+ if (!user) {
22
+ throw createError({ statusCode: 401, message: "Unauthorized" });
23
+ }
24
+
21
25
  const lists = await readCoverMutableJson<OrderList[]>("account/order-lists.json");
22
26
  const record = lists.find(list => list.id === id);
23
27
  if (!record) {
@@ -2,6 +2,10 @@ import type { OrderList } from "../../../../app/interfaces/account/order-lists";
2
2
 
3
3
  /** Order lists visible to the user: own lists plus public organization lists. */
4
4
  export default defineEventHandler(async (event) => {
5
+ if (resolveOrderListServiceKey(event) === "api") {
6
+ return await listOrderLists(event);
7
+ }
8
+
5
9
  const user = await getAuthService(event).me(event);
6
10
  if (!user) {
7
11
  throw createError({ statusCode: 401, message: "Unauthorized" });
@@ -2,11 +2,6 @@ import type { OrderList, OrderListPosition } from "../../../../app/interfaces/ac
2
2
 
3
3
  /** Creates an order list, optionally pre-filled with positions. */
4
4
  export default defineEventHandler(async (event) => {
5
- const user = await getAuthService(event).me(event);
6
- if (!user) {
7
- throw createError({ statusCode: 401, message: "Unauthorized" });
8
- }
9
-
10
5
  const body = await readBody<{
11
6
  name?: string;
12
7
  kind?: string;
@@ -20,6 +15,15 @@ export default defineEventHandler(async (event) => {
20
15
  throw createError({ statusCode: 400, message: "name required" });
21
16
  }
22
17
 
18
+ if (resolveOrderListServiceKey(event) === "api") {
19
+ return await createOrderList(event, { name, kind, public: Boolean(body?.public), positions: body?.positions });
20
+ }
21
+
22
+ const user = await getAuthService(event).me(event);
23
+ if (!user) {
24
+ throw createError({ statusCode: 401, message: "Unauthorized" });
25
+ }
26
+
23
27
  const now = new Date().toISOString();
24
28
  const record: OrderList = {
25
29
  id: `ol-${Date.now().toString(36)}`,
@@ -111,10 +111,20 @@ export class ApiAuthService implements IAuthService {
111
111
  };
112
112
  }
113
113
  catch (err) {
114
- if (!isApiUserError(err)) {
115
- getLogService().error("API request failed: auth/me", apiErrorContext(err));
114
+ // Only a genuine auth failure (401 = revoked/expired session) proves
115
+ // the session is gone — clear the cookie so the user logs out cleanly.
116
+ // Any other error (5xx like the gateway's "capability dispatch failed",
117
+ // or a network blip) is transient infrastructure trouble, NOT proof the
118
+ // session is invalid. Deleting the cookie here turned a flaky gateway
119
+ // into a SILENT LOGOUT + empty order history (the account history is
120
+ // filtered strictly by contact_id and falls back to me()). So keep the
121
+ // cookie and return null: callers still degrade to the guest path for
122
+ // this one request, and the session self-heals on the next.
123
+ if (isApiUserError(err) && err.code === 401) {
124
+ deleteCookie(event, SESSION_COOKIE_NAME, sessionCookieOptions());
125
+ return null;
116
126
  }
117
- deleteCookie(event, SESSION_COOKIE_NAME, sessionCookieOptions());
127
+ getLogService().error("API request failed: auth/me", apiErrorContext(err));
118
128
  return null;
119
129
  }
120
130
  }
@@ -0,0 +1,168 @@
1
+ import type { H3Event } from "h3";
2
+
3
+ import type { OrderList, OrderListPosition } from "../../app/interfaces/account/order-lists";
4
+
5
+ /**
6
+ * Live order lists through the `orderlists` gateway app
7
+ * (revenexx-apps/orderlists). The app is not in the pinned @revenexx/sdk
8
+ * contract yet, so every call uses the documented `sdk.call()` escape hatch;
9
+ * it moves to a typed method once the SDK regenerates.
10
+ *
11
+ * Identity: the app stores `owner_id`/`organization_id` and filters
12
+ * `GET /orderlists` to the owner's own lists ∪ the organization's public
13
+ * lists. We supply both from the authenticated contact (auth/me), mapping
14
+ * the cover OrderList (camelCase, `owner {id,name}`, `positions[]`) to the
15
+ * app shape (snake_case, `items[]`).
16
+ */
17
+
18
+ interface AppItem {
19
+ id: string;
20
+ sku: string | null;
21
+ product_id: string | null;
22
+ name: string;
23
+ image: string | null;
24
+ quantity: number | string;
25
+ unit: string | null;
26
+ price: number | string | null;
27
+ tax_rate: number | string | null;
28
+ cost_center_id: string | null;
29
+ position_texts: string[] | null;
30
+ custom_sku: string | null;
31
+ category_slug: string | null;
32
+ subcategory_slug: string | null;
33
+ }
34
+
35
+ interface AppList {
36
+ id: string;
37
+ name: string;
38
+ kind: string;
39
+ public: boolean;
40
+ owner_id: string;
41
+ owner_name: string;
42
+ organization_id: string | null;
43
+ created_at: string;
44
+ updated_at: string;
45
+ items?: AppItem[];
46
+ }
47
+
48
+ async function actor(event: H3Event): Promise<{ contactId: string; name: string; organizationId?: string }> {
49
+ const user = await getAuthService(event).me(event);
50
+ if (!user?.contactId) {
51
+ throw createError({ statusCode: 401, message: "Unauthorized" });
52
+ }
53
+ return { contactId: user.contactId, name: user.name, organizationId: user.organizationId };
54
+ }
55
+
56
+ function toCoverPosition(item: AppItem): OrderListPosition {
57
+ return {
58
+ id: String(item.id),
59
+ sku: item.sku ?? "",
60
+ name: item.name,
61
+ ...(item.image ? { image: item.image } : {}),
62
+ quantity: Number(item.quantity) || 0,
63
+ ...(item.unit ? { unit: item.unit } : {}),
64
+ price: item.price != null ? Number(item.price) : 0,
65
+ ...(item.tax_rate != null ? { taxRate: Number(item.tax_rate) } : {}),
66
+ ...(item.cost_center_id ? { costCenterId: item.cost_center_id } : {}),
67
+ ...(Array.isArray(item.position_texts) ? { positionTexts: item.position_texts } : {}),
68
+ ...(item.custom_sku ? { customSku: item.custom_sku } : {}),
69
+ ...(item.category_slug ? { categorySlug: item.category_slug } : {}),
70
+ ...(item.subcategory_slug ? { subcategorySlug: item.subcategory_slug } : {}),
71
+ };
72
+ }
73
+
74
+ function toAppItem(position: OrderListPosition): Record<string, unknown> {
75
+ return {
76
+ sku: position.sku || null,
77
+ name: position.name,
78
+ quantity: Number(position.quantity) || 1,
79
+ image: position.image ?? null,
80
+ unit: position.unit ?? null,
81
+ price: position.price ?? null,
82
+ tax_rate: position.taxRate ?? null,
83
+ cost_center_id: position.costCenterId ?? null,
84
+ position_texts: position.positionTexts ?? null,
85
+ custom_sku: position.customSku ?? null,
86
+ category_slug: position.categorySlug ?? null,
87
+ subcategory_slug: position.subcategorySlug ?? null,
88
+ };
89
+ }
90
+
91
+ function toCoverList(row: AppList): OrderList {
92
+ return {
93
+ id: row.id,
94
+ name: row.name,
95
+ kind: row.kind === "label" ? "label" : "shopping",
96
+ public: Boolean(row.public),
97
+ owner: { id: row.owner_id, name: row.owner_name },
98
+ positions: (row.items ?? []).map(toCoverPosition),
99
+ createdAt: row.created_at,
100
+ updatedAt: row.updated_at,
101
+ };
102
+ }
103
+
104
+ /** Own lists ∪ org-public, each with its positions (bounded fan-out). */
105
+ export async function listOrderLists(event: H3Event): Promise<OrderList[]> {
106
+ const me = await actor(event);
107
+ const sdk = useRevenexxSdk();
108
+ const { items } = await sdk.call<{ items: AppList[] }>("GET", "/v1/orderlists", {
109
+ query: { owner_id: me.contactId, ...(me.organizationId ? { organization_id: me.organizationId } : {}) },
110
+ });
111
+ const full = await Promise.all(items.map(async row => sdk.call<AppList>("GET", `/v1/orderlists/${row.id}`)));
112
+ return full.map(toCoverList);
113
+ }
114
+
115
+ export async function getOrderList(event: H3Event, id: string): Promise<OrderList> {
116
+ await actor(event);
117
+ const row = await useRevenexxSdk().call<AppList>("GET", `/v1/orderlists/${id}`);
118
+ return toCoverList(row);
119
+ }
120
+
121
+ export async function createOrderList(
122
+ event: H3Event,
123
+ body: { name: string; kind?: string; public?: boolean; positions?: OrderListPosition[] },
124
+ ): Promise<OrderList> {
125
+ const me = await actor(event);
126
+ const row = await useRevenexxSdk().call<AppList>("POST", "/v1/orderlists", {
127
+ body: {
128
+ name: body.name,
129
+ kind: body.kind === "label" ? "label" : "shopping",
130
+ public: Boolean(body.public),
131
+ owner_id: me.contactId,
132
+ owner_name: me.name,
133
+ organization_id: me.organizationId ?? null,
134
+ items: Array.isArray(body.positions) ? body.positions.map(toAppItem) : [],
135
+ },
136
+ });
137
+ return toCoverList(row);
138
+ }
139
+
140
+ export async function updateOrderList(
141
+ event: H3Event,
142
+ id: string,
143
+ body: { name?: string; public?: boolean; positions?: OrderListPosition[]; appendPositions?: OrderListPosition[] },
144
+ ): Promise<OrderList> {
145
+ await actor(event);
146
+ const sdk = useRevenexxSdk();
147
+
148
+ const meta: Record<string, unknown> = {};
149
+ if (typeof body.name === "string" && body.name.trim()) meta.name = body.name.trim();
150
+ if (typeof body.public === "boolean") meta.public = body.public;
151
+ if (Object.keys(meta).length > 0) {
152
+ await sdk.call<AppList>("PUT", `/v1/orderlists/${id}`, { body: meta });
153
+ }
154
+
155
+ if (Array.isArray(body.positions)) {
156
+ await sdk.call("PUT", `/v1/orderlists/${id}/items`, { body: { items: body.positions.map(toAppItem) } });
157
+ }
158
+ for (const position of body.appendPositions ?? []) {
159
+ await sdk.call("POST", `/v1/orderlists/${id}/items`, { body: toAppItem(position) });
160
+ }
161
+
162
+ return toCoverList(await sdk.call<AppList>("GET", `/v1/orderlists/${id}`));
163
+ }
164
+
165
+ export async function deleteOrderList(event: H3Event, id: string): Promise<void> {
166
+ await actor(event);
167
+ await useRevenexxSdk().call("DELETE", `/v1/orderlists/${id}`);
168
+ }
@@ -0,0 +1,14 @@
1
+ import type { H3Event } from "h3";
2
+
3
+ /**
4
+ * The order-lists domain has no service class — endpoints branch on the
5
+ * resolved registry key ("mock" = bundled mutable JSON, "api" = the
6
+ * `orderlists` gateway app via the public revenexx API, see liveOrderLists).
7
+ */
8
+ export function resolveOrderListServiceKey(event?: H3Event): string {
9
+ return resolveServiceKey(event, {
10
+ domain: "orderListService",
11
+ mockKey: "mock",
12
+ liveKey: "api",
13
+ });
14
+ }