@unifiedcommerce/core 0.3.3 → 0.4.0
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/dist/auth/setup.d.ts +25 -66
- package/dist/auth/setup.d.ts.map +1 -1
- package/dist/auth/setup.js +7 -20
- package/dist/interfaces/mcp/tools/webhooks.d.ts +1 -1
- package/dist/interfaces/rest/index.d.ts.map +1 -1
- package/dist/interfaces/rest/index.js +14 -3
- package/dist/interfaces/rest/routes/customers.d.ts +5 -0
- package/dist/interfaces/rest/routes/customers.d.ts.map +1 -0
- package/dist/interfaces/rest/routes/customers.js +74 -0
- package/dist/interfaces/rest/routes/inventory.d.ts.map +1 -1
- package/dist/interfaces/rest/routes/inventory.js +14 -1
- package/dist/interfaces/rest/schemas/customers.d.ts +422 -0
- package/dist/interfaces/rest/schemas/customers.d.ts.map +1 -0
- package/dist/interfaces/rest/schemas/customers.js +150 -0
- package/dist/interfaces/rest/schemas/inventory.d.ts +92 -0
- package/dist/interfaces/rest/schemas/inventory.d.ts.map +1 -1
- package/dist/interfaces/rest/schemas/inventory.js +20 -0
- package/dist/modules/customers/service.d.ts +2 -0
- package/dist/modules/customers/service.d.ts.map +1 -1
- package/dist/modules/customers/service.js +15 -0
- package/dist/modules/inventory/service.d.ts +4 -0
- package/dist/modules/inventory/service.d.ts.map +1 -1
- package/dist/modules/inventory/service.js +12 -0
- package/dist/runtime/server.d.ts.map +1 -1
- package/dist/runtime/server.js +29 -0
- package/package.json +2 -2
- package/src/auth/setup.ts +30 -79
- package/src/interfaces/rest/index.ts +17 -3
- package/src/interfaces/rest/routes/customers.ts +94 -0
- package/src/interfaces/rest/routes/inventory.ts +17 -0
- package/src/interfaces/rest/schemas/customers.ts +155 -0
- package/src/interfaces/rest/schemas/inventory.ts +21 -0
- package/src/modules/customers/service.ts +25 -0
- package/src/modules/inventory/service.ts +17 -0
- package/src/runtime/server.ts +30 -0
package/dist/auth/setup.d.ts
CHANGED
|
@@ -1,70 +1,29 @@
|
|
|
1
|
+
import { betterAuth } from "better-auth";
|
|
2
|
+
import { apiKey } from "@better-auth/api-key";
|
|
3
|
+
import { organization, twoFactor, phoneNumber, jwt, bearer } from "better-auth/plugins";
|
|
1
4
|
import type { CommerceConfig } from "../config/types.js";
|
|
2
5
|
import type { DatabaseAdapter } from "../kernel/database/adapter.js";
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
organizationId: string;
|
|
25
|
-
};
|
|
26
|
-
headers: Headers;
|
|
27
|
-
}) => Promise<{
|
|
28
|
-
id: string;
|
|
29
|
-
name: string;
|
|
30
|
-
members: OrgMember[];
|
|
31
|
-
} | null>;
|
|
32
|
-
listMembers?: (input: {
|
|
33
|
-
query: {
|
|
34
|
-
organizationId: string;
|
|
35
|
-
};
|
|
36
|
-
}) => Promise<OrgMember[]>;
|
|
37
|
-
verifyApiKey?: (input: {
|
|
38
|
-
body: {
|
|
39
|
-
key: string;
|
|
40
|
-
permissions?: Record<string, string[]>;
|
|
41
|
-
};
|
|
42
|
-
}) => Promise<{
|
|
43
|
-
valid: boolean;
|
|
44
|
-
error: {
|
|
45
|
-
message: string;
|
|
46
|
-
code: string;
|
|
47
|
-
} | null;
|
|
48
|
-
key: Record<string, unknown> | null;
|
|
49
|
-
}>;
|
|
50
|
-
createApiKey?: (input: {
|
|
51
|
-
body: {
|
|
52
|
-
configId?: string;
|
|
53
|
-
name?: string;
|
|
54
|
-
permissions?: Record<string, string[]>;
|
|
55
|
-
userId?: string;
|
|
56
|
-
organizationId?: string;
|
|
57
|
-
};
|
|
58
|
-
headers?: Headers;
|
|
59
|
-
}) => Promise<{
|
|
60
|
-
key: string;
|
|
61
|
-
id: string;
|
|
62
|
-
}>;
|
|
63
|
-
/** Allow access to other Better Auth API methods added by plugins */
|
|
64
|
-
[key: string]: unknown;
|
|
65
|
-
};
|
|
66
|
-
options?: Record<string, unknown>;
|
|
67
|
-
$context?: Promise<unknown>;
|
|
68
|
-
}
|
|
6
|
+
/**
|
|
7
|
+
* The auth type is inferred from `typeof betterAuth(...)` with all plugins enabled.
|
|
8
|
+
* This gives us the full API surface without manual interface maintenance.
|
|
9
|
+
*
|
|
10
|
+
* We use a type-level-only reference (never executed) to capture the complete type.
|
|
11
|
+
*/
|
|
12
|
+
type FullBetterAuth = ReturnType<typeof betterAuth<{
|
|
13
|
+
plugins: [
|
|
14
|
+
ReturnType<typeof organization>,
|
|
15
|
+
ReturnType<typeof bearer>,
|
|
16
|
+
ReturnType<typeof jwt>,
|
|
17
|
+
ReturnType<typeof twoFactor>,
|
|
18
|
+
ReturnType<typeof apiKey>,
|
|
19
|
+
ReturnType<typeof phoneNumber>
|
|
20
|
+
];
|
|
21
|
+
}>>;
|
|
22
|
+
/**
|
|
23
|
+
* AuthInstance — inferred from Better Auth with all plugins.
|
|
24
|
+
* No manual type maintenance needed.
|
|
25
|
+
*/
|
|
26
|
+
export type AuthInstance = FullBetterAuth;
|
|
69
27
|
export declare function createAuth(db: DatabaseAdapter, config: CommerceConfig): AuthInstance;
|
|
28
|
+
export {};
|
|
70
29
|
//# sourceMappingURL=setup.d.ts.map
|
package/dist/auth/setup.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/auth/setup.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/auth/setup.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AACxF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAiBrE;;;;;GAKG;AACH,KAAK,cAAc,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC;IACjD,OAAO,EAAE;QACP,UAAU,CAAC,OAAO,YAAY,CAAC;QAC/B,UAAU,CAAC,OAAO,MAAM,CAAC;QACzB,UAAU,CAAC,OAAO,GAAG,CAAC;QACtB,UAAU,CAAC,OAAO,SAAS,CAAC;QAC5B,UAAU,CAAC,OAAO,MAAM,CAAC;QACzB,UAAU,CAAC,OAAO,WAAW,CAAC;KAC/B,CAAC;CACH,CAAC,CAAC,CAAC;AAEJ;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,cAAc,CAAC;AAE1C,wBAAgB,UAAU,CACxB,EAAE,EAAE,eAAe,EACnB,MAAM,EAAE,cAAc,GACrB,YAAY,CA+Gd"}
|
package/dist/auth/setup.js
CHANGED
|
@@ -4,24 +4,17 @@ import { apiKey } from "@better-auth/api-key";
|
|
|
4
4
|
import { organization, twoFactor, phoneNumber, jwt, bearer } from "better-auth/plugins";
|
|
5
5
|
import * as authSchema from "./auth-schema.js";
|
|
6
6
|
function resolveAuthDbProvider(provider) {
|
|
7
|
-
if (provider === "postgres" ||
|
|
8
|
-
provider === "postgresql" ||
|
|
9
|
-
provider === "pg") {
|
|
7
|
+
if (provider === "postgres" || provider === "postgresql" || provider === "pg")
|
|
10
8
|
return "pg";
|
|
11
|
-
|
|
12
|
-
if (provider === "mysql") {
|
|
9
|
+
if (provider === "mysql")
|
|
13
10
|
return "mysql";
|
|
14
|
-
|
|
15
|
-
if (provider === "sqlite") {
|
|
11
|
+
if (provider === "sqlite")
|
|
16
12
|
return "sqlite";
|
|
17
|
-
}
|
|
18
|
-
throw new Error(`Unsupported auth database provider "${provider}". Expected one of: postgres, mysql, sqlite.`);
|
|
13
|
+
throw new Error(`Unsupported auth database provider "${provider}".`);
|
|
19
14
|
}
|
|
20
15
|
export function createAuth(db, config) {
|
|
21
16
|
const plugins = [
|
|
22
17
|
organization({
|
|
23
|
-
// Better Auth's Role includes `authorize` and `statements` fields that
|
|
24
|
-
// our RoleDefinition doesn't have. Double-cast is required — upstream type gap.
|
|
25
18
|
roles: (config.auth?.roles ?? {}),
|
|
26
19
|
}),
|
|
27
20
|
bearer(),
|
|
@@ -30,7 +23,6 @@ export function createAuth(db, config) {
|
|
|
30
23
|
if (config.auth?.twoFactor?.enabled) {
|
|
31
24
|
plugins.push(twoFactor({ issuer: config.storeName ?? "UnifiedCommerce" }));
|
|
32
25
|
}
|
|
33
|
-
// Configure API key plugin — one config per defined scope, or a single default config.
|
|
34
26
|
const scopes = config.auth?.apiKeyScopes;
|
|
35
27
|
if (scopes && Object.keys(scopes).length > 0) {
|
|
36
28
|
const apiKeyConfigs = Object.entries(scopes).map(([scopeId, scope]) => ({
|
|
@@ -62,11 +54,8 @@ export function createAuth(db, config) {
|
|
|
62
54
|
},
|
|
63
55
|
}));
|
|
64
56
|
}
|
|
65
|
-
// API key support can be attached via external plugin package in newer better-auth versions.
|
|
66
57
|
try {
|
|
67
58
|
const auth = betterAuth({
|
|
68
|
-
// Better Auth's drizzle adapter expects a plain object, not PgDatabase.
|
|
69
|
-
// Double-cast required — PgDatabase has no index signature.
|
|
70
59
|
database: drizzleAdapter(db.db, {
|
|
71
60
|
provider: resolveAuthDbProvider(db.provider),
|
|
72
61
|
schema: authSchema,
|
|
@@ -100,7 +89,7 @@ export function createAuth(db, config) {
|
|
|
100
89
|
updateAge: 60 * 60 * 24,
|
|
101
90
|
cookieCache: {
|
|
102
91
|
enabled: true,
|
|
103
|
-
maxAge: 60 * 5,
|
|
92
|
+
maxAge: 60 * 5,
|
|
104
93
|
},
|
|
105
94
|
},
|
|
106
95
|
advanced: {
|
|
@@ -115,10 +104,8 @@ export function createAuth(db, config) {
|
|
|
115
104
|
},
|
|
116
105
|
},
|
|
117
106
|
});
|
|
118
|
-
//
|
|
119
|
-
//
|
|
120
|
-
// Double-cast is the accepted pattern — same approach used by PayloadCMS.
|
|
121
|
-
// @see https://github.com/better-auth/better-auth/discussions
|
|
107
|
+
// The runtime return matches FullBetterAuth structurally — plugins may differ
|
|
108
|
+
// at runtime (conditional) but the API surface is a superset.
|
|
122
109
|
return auth;
|
|
123
110
|
}
|
|
124
111
|
catch (error) {
|
|
@@ -2,8 +2,8 @@ import { z } from "zod";
|
|
|
2
2
|
import type { ToolDefinition } from "./registry.js";
|
|
3
3
|
export declare const webhooksManage: ToolDefinition<z.ZodObject<{
|
|
4
4
|
action: z.ZodEnum<{
|
|
5
|
-
delete: "delete";
|
|
6
5
|
create: "create";
|
|
6
|
+
delete: "delete";
|
|
7
7
|
list: "list";
|
|
8
8
|
}>;
|
|
9
9
|
url: z.ZodOptional<z.ZodString>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/interfaces/rest/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/interfaces/rest/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAgBzC,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,gCAmE9C"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
-
import {
|
|
2
|
+
import { Scalar } from "@scalar/hono-api-reference";
|
|
3
3
|
import { sql } from "drizzle-orm";
|
|
4
4
|
import { catalogRoutes } from "./routes/catalog.js";
|
|
5
5
|
import { inventoryRoutes } from "./routes/inventory.js";
|
|
@@ -14,6 +14,7 @@ import { promotionRoutes } from "./routes/promotions.js";
|
|
|
14
14
|
import { searchRoutes } from "./routes/search.js";
|
|
15
15
|
import { auditRoutes } from "./routes/audit.js";
|
|
16
16
|
import { adminJobRoutes } from "./routes/admin-jobs.js";
|
|
17
|
+
import { customerRoutes } from "./routes/customers.js";
|
|
17
18
|
export function createRestRoutes(kernel) {
|
|
18
19
|
const router = new OpenAPIHono({
|
|
19
20
|
// Standardize Zod validation error responses across all routes
|
|
@@ -57,12 +58,22 @@ export function createRestRoutes(kernel) {
|
|
|
57
58
|
router.route("/pricing", pricingRoutes(kernel));
|
|
58
59
|
router.route("/promotions", promotionRoutes(kernel));
|
|
59
60
|
router.route("/search", searchRoutes(kernel));
|
|
61
|
+
router.route("/customers", customerRoutes(kernel));
|
|
60
62
|
router.route("/audit", auditRoutes(kernel));
|
|
61
63
|
router.route("/admin", adminJobRoutes(kernel));
|
|
62
|
-
//
|
|
64
|
+
// API Reference (Scalar) — disabled in production unless config.exposeOpenApiSpec is true
|
|
63
65
|
const exposeSpec = kernel.config.exposeOpenApiSpec ?? (process.env.NODE_ENV !== "production");
|
|
64
66
|
if (exposeSpec) {
|
|
65
|
-
router.get("/reference",
|
|
67
|
+
router.get("/reference", Scalar({
|
|
68
|
+
url: "/api/doc-ext",
|
|
69
|
+
theme: "kepler",
|
|
70
|
+
layout: "modern",
|
|
71
|
+
darkMode: true,
|
|
72
|
+
hideModels: true,
|
|
73
|
+
tagsSorter: "alpha",
|
|
74
|
+
defaultHttpClient: { targetKey: "js", clientKey: "fetch" },
|
|
75
|
+
metaData: { title: "UnifiedCommerce API Reference" },
|
|
76
|
+
}));
|
|
66
77
|
}
|
|
67
78
|
return router;
|
|
68
79
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
+
import type { Kernel } from "../../../runtime/kernel.js";
|
|
3
|
+
import { type AppEnv } from "../utils.js";
|
|
4
|
+
export declare function customerRoutes(kernel: Kernel): OpenAPIHono<AppEnv, {}, "/">;
|
|
5
|
+
//# sourceMappingURL=customers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customers.d.ts","sourceRoot":"","sources":["../../../../src/interfaces/rest/routes/customers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAQzD,OAAO,EAAE,KAAK,MAAM,EAAyD,MAAM,aAAa,CAAC;AAEjG,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,gCAkF5C"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
+
import { listCustomersRoute, getCustomerRoute, updateCustomerRoute, getCustomerOrdersRoute, getCustomerAddressesRoute, } from "../schemas/customers.js";
|
|
3
|
+
import { mapErrorToResponse, mapErrorToStatus, parsePagination } from "../utils.js";
|
|
4
|
+
export function customerRoutes(kernel) {
|
|
5
|
+
const router = new OpenAPIHono();
|
|
6
|
+
// @ts-expect-error -- openapi handler union return type
|
|
7
|
+
router.openapi(listCustomersRoute, async (c) => {
|
|
8
|
+
const actor = c.get("actor");
|
|
9
|
+
const { page, limit } = parsePagination(c.req.query());
|
|
10
|
+
const result = await kernel.services.customers.list(actor);
|
|
11
|
+
if (!result.ok)
|
|
12
|
+
return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
|
|
13
|
+
const all = result.value;
|
|
14
|
+
const total = all.length;
|
|
15
|
+
const start = (page - 1) * limit;
|
|
16
|
+
const paged = all.slice(start, start + limit);
|
|
17
|
+
return c.json({
|
|
18
|
+
data: paged,
|
|
19
|
+
meta: {
|
|
20
|
+
pagination: { page, limit, total, totalPages: Math.ceil(total / limit) },
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
// @ts-expect-error -- openapi handler union return type
|
|
25
|
+
router.openapi(getCustomerRoute, async (c) => {
|
|
26
|
+
const { id } = c.req.valid("param");
|
|
27
|
+
const actor = c.get("actor");
|
|
28
|
+
const result = await kernel.services.customers.getById(id, actor);
|
|
29
|
+
if (!result.ok)
|
|
30
|
+
return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
|
|
31
|
+
return c.json({ data: result.value });
|
|
32
|
+
});
|
|
33
|
+
// @ts-expect-error -- openapi handler union return type
|
|
34
|
+
router.openapi(updateCustomerRoute, async (c) => {
|
|
35
|
+
const { id } = c.req.valid("param");
|
|
36
|
+
const body = c.req.valid("json");
|
|
37
|
+
const actor = c.get("actor");
|
|
38
|
+
// Strip undefined values — exactOptionalPropertyTypes requires omission, not undefined
|
|
39
|
+
const updates = {};
|
|
40
|
+
for (const [k, v] of Object.entries(body)) {
|
|
41
|
+
if (v !== undefined)
|
|
42
|
+
updates[k] = v;
|
|
43
|
+
}
|
|
44
|
+
const result = await kernel.services.customers.update(id, updates, actor);
|
|
45
|
+
if (!result.ok)
|
|
46
|
+
return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
|
|
47
|
+
return c.json({ data: result.value });
|
|
48
|
+
});
|
|
49
|
+
// @ts-expect-error -- openapi handler union return type
|
|
50
|
+
router.openapi(getCustomerOrdersRoute, async (c) => {
|
|
51
|
+
const { id } = c.req.valid("param");
|
|
52
|
+
const actor = c.get("actor");
|
|
53
|
+
const { page, limit } = parsePagination(c.req.query());
|
|
54
|
+
const status = c.req.query("status") || undefined;
|
|
55
|
+
const result = await kernel.services.orders.listByCustomer(id, { page, limit, ...(status ? { status } : {}) }, actor);
|
|
56
|
+
if (!result.ok)
|
|
57
|
+
return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
|
|
58
|
+
return c.json({ data: result.value.items, meta: { pagination: result.value.pagination } });
|
|
59
|
+
});
|
|
60
|
+
// @ts-expect-error -- openapi handler union return type
|
|
61
|
+
router.openapi(getCustomerAddressesRoute, async (c) => {
|
|
62
|
+
const { id } = c.req.valid("param");
|
|
63
|
+
const actor = c.get("actor");
|
|
64
|
+
// Get customer first, then use their userId for address lookup
|
|
65
|
+
const customerResult = await kernel.services.customers.getById(id, actor);
|
|
66
|
+
if (!customerResult.ok)
|
|
67
|
+
return c.json(mapErrorToResponse(customerResult.error), mapErrorToStatus(customerResult.error));
|
|
68
|
+
const addressResult = await kernel.services.customers.getAddresses(customerResult.value.userId, actor);
|
|
69
|
+
if (!addressResult.ok)
|
|
70
|
+
return c.json(mapErrorToResponse(addressResult.error), mapErrorToStatus(addressResult.error));
|
|
71
|
+
return c.json({ data: addressResult.value });
|
|
72
|
+
});
|
|
73
|
+
return router;
|
|
74
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inventory.d.ts","sourceRoot":"","sources":["../../../../src/interfaces/rest/routes/inventory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"inventory.d.ts","sourceRoot":"","sources":["../../../../src/interfaces/rest/routes/inventory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAUzD,OAAO,EAAE,KAAK,MAAM,EAAwC,MAAM,aAAa,CAAC;AAEhF,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,gCAyE7C"}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
-
import { inventoryAdjustRoute, inventoryReserveRoute, inventoryReleaseRoute, createWarehouseRoute, inventoryCheckRoute, listWarehousesRoute, } from "../schemas/inventory.js";
|
|
2
|
+
import { inventoryAdjustRoute, inventoryReserveRoute, inventoryReleaseRoute, createWarehouseRoute, inventoryCheckRoute, listWarehousesRoute, listInventoryLevelsRoute, } from "../schemas/inventory.js";
|
|
3
3
|
import { mapErrorToResponse, mapErrorToStatus } from "../utils.js";
|
|
4
4
|
export function inventoryRoutes(kernel) {
|
|
5
5
|
const router = new OpenAPIHono();
|
|
6
6
|
// @ts-expect-error -- openapi handler union return type
|
|
7
|
+
router.openapi(listInventoryLevelsRoute, async (c) => {
|
|
8
|
+
const actor = c.get("actor");
|
|
9
|
+
const warehouseId = c.req.query("warehouseId");
|
|
10
|
+
const entityId = c.req.query("entityId");
|
|
11
|
+
const result = await kernel.services.inventory.listLevels({
|
|
12
|
+
...(warehouseId ? { warehouseId } : {}),
|
|
13
|
+
...(entityId ? { entityId } : {}),
|
|
14
|
+
}, actor);
|
|
15
|
+
if (!result.ok)
|
|
16
|
+
return c.json(mapErrorToResponse(result.error), mapErrorToStatus(result.error));
|
|
17
|
+
return c.json({ data: result.value });
|
|
18
|
+
});
|
|
19
|
+
// @ts-expect-error -- openapi handler union return type
|
|
7
20
|
router.openapi(inventoryCheckRoute, async (c) => {
|
|
8
21
|
const entityIds = (c.req.query("entityIds") ?? "")
|
|
9
22
|
.split(",")
|