@internetderdinge/api 1.229.27 → 1.229.31

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.
@@ -0,0 +1,24 @@
1
+ import { z } from "zod";
2
+ export const adminSearchSchema = {
3
+ query: z.object({
4
+ search: z.string().max(120).optional(),
5
+ limit: z.coerce.number().min(1).max(50).optional(),
6
+ }),
7
+ };
8
+ export const adminStatsSchema = {
9
+ query: z.object({}),
10
+ };
11
+ export const adminIotDevicesSchema = {
12
+ query: z.object({
13
+ page: z.coerce.number().int().min(1).optional(),
14
+ perPage: z.coerce.number().int().min(1).max(500).optional(),
15
+ updatedSince: z.string().datetime().optional(),
16
+ }),
17
+ };
18
+ export const adminDevicesSchema = {
19
+ query: z.object({
20
+ page: z.coerce.number().int().min(1).optional(),
21
+ perPage: z.coerce.number().int().min(1).max(500).optional(),
22
+ updatedSince: z.string().datetime().optional(),
23
+ }),
24
+ };
package/dist/src/index.js CHANGED
@@ -10,6 +10,7 @@ export { default as i18n } from "../src/i18n/i18n";
10
10
  export { default as usersRoute } from "../src/users/users.route";
11
11
  export { default as usersService } from "../src/users/users.service";
12
12
  export { default as accountsRoute } from "../src/accounts/accounts.route";
13
+ export { default as adminSearchRoute } from "../src/admin/adminSearch.route";
13
14
  export { default as accountsService } from "../src/accounts/accounts.service";
14
15
  export { auth0 } from "../src/accounts/auth0.service";
15
16
  export { default as organizationsRoute } from "../src/organizations/organizations.route";
@@ -7,7 +7,7 @@ import { getEventsSchema, getDeviceSchema, getEntrySchema, updateEntrySchema, pi
7
7
  // add other input schemas as needed
8
8
  } from "./iotdevice.validation";
9
9
  import { iotDeviceResponseSchema, eventResponseSchema, deviceResponseSchema, shadowAlarmSchema, pingResponseSchema, deviceStatusSchema, apiStatusSchema, entryResponseSchema, } from "./iotdevice.schemas";
10
- import { getIotDevices, getEvents, getDevice, shadowAlarmGet, shadowAlarmUpdate, shadowAdmin, pingDevice, getDeviceStatus, getApiStatus, getEntry, updateEntry, } from "./iotdevice.controller";
10
+ import { getIotDevices, getEvents, getDevice, shadowAlarmGet, shadowAlarmUpdate, shadowAdmin, pingDevice, getDeviceStatus, getApiStatus, getEntry, updateEntry, ledLightHint, } from "./iotdevice.controller";
11
11
  export const iotdeviceRouteSpecs = [
12
12
  {
13
13
  method: "get",
@@ -74,7 +74,7 @@ export const iotdeviceRouteSpecs = [
74
74
  path: "/ledlight/:deviceId",
75
75
  validate: [auth("getUsers"), validateAdmin],
76
76
  responseSchema: pingResponseSchema,
77
- handler: pingDevice,
77
+ handler: ledLightHint,
78
78
  summary: "Ping device LED light",
79
79
  description: "Sends a ping to the device’s LED light to test its connectivity or response.",
80
80
  },
@@ -10,22 +10,19 @@ function hasRoleValidation(validators = []) {
10
10
  }
11
11
  export default function buildAiRouterAndDocs(router, routeSpecs, basePath = "/", tags = []) {
12
12
  routeSpecs.forEach((spec) => {
13
- // mount Express
14
- if (!spec.validate) {
15
- spec.validate = [];
13
+ const validate = spec.validate || [];
14
+ const routeMiddleware = spec.validateWithRequestSchema ||
15
+ (spec.requestSchema
16
+ ? [validateZod(spec.requestSchema), ...validate]
17
+ : validate);
18
+ router[spec.method](spec.path, ...routeMiddleware, spec.handler);
19
+ const { body, ...rest } = spec.requestSchema || {};
20
+ const request = { ...rest };
21
+ if (spec.requestBody) {
22
+ request.body = spec.requestBody;
16
23
  }
17
- if (spec.requestSchema) {
18
- spec.validateWithRequestSchema = [
19
- validateZod(spec.requestSchema),
20
- ...spec.validate,
21
- ];
22
- }
23
- if (spec.validateWithRequestSchema) {
24
- router[spec.method](spec.path, ...spec.validateWithRequestSchema, spec.handler);
25
- }
26
- var { body, ...rest } = spec.requestSchema || {};
27
- if (body) {
28
- rest.body = {
24
+ else if (body) {
25
+ request.body = {
29
26
  content: {
30
27
  "application/json": {
31
28
  schema: body,
@@ -34,17 +31,17 @@ export default function buildAiRouterAndDocs(router, routeSpecs, basePath = "/",
34
31
  };
35
32
  }
36
33
  if (spec.responseSchema &&
37
- !hasRoleValidation(spec.validate) &&
34
+ !hasRoleValidation(spec.validateWithRequestSchema || validate) &&
38
35
  spec.privateDocs !== true &&
39
36
  spec.memoOnly !== true) {
40
37
  // collect all middleware fn names (falls back to '<anonymous>' if unnamed)
41
- const middlewareNames = (spec.validate || []).map((fn) => `\`${fn.name}\`` || "<anonymous>");
38
+ const middlewareNames = (spec.validateWithRequestSchema || validate).map((fn) => `\`${fn.name}\`` || "<anonymous>");
42
39
  const openApiPath = (basePath + spec.path).replace(/:([A-Za-z0-9_]+)/g, "{$1}");
43
40
  registry.registerPath({
44
41
  method: spec.method,
45
42
  path: openApiPath,
46
43
  summary: spec.summary,
47
- request: rest,
44
+ request,
48
45
  // append middleware names to the description
49
46
  description: [
50
47
  spec.description,
@@ -3,6 +3,12 @@ import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
3
3
  import { z } from "zod";
4
4
  extendZodWithOpenApi(z);
5
5
  export const registry = new OpenAPIRegistry();
6
+ export const xApiKey = registry.registerComponent("securitySchemes", "x-api-key", {
7
+ type: "apiKey",
8
+ in: "header",
9
+ name: "x-api-key",
10
+ description: "API key for authentication",
11
+ });
6
12
  // add Bearer JWT auth
7
13
  export const bearerAuth = registry.registerComponent("securitySchemes", "bearerAuth", {
8
14
  type: "http",
@@ -10,12 +16,6 @@ export const bearerAuth = registry.registerComponent("securitySchemes", "bearerA
10
16
  bearerFormat: "JWT",
11
17
  description: "JWT Bearer authentication",
12
18
  });
13
- export const xApiKey = registry.registerComponent("securitySchemes", "x-api-key", {
14
- type: "apiKey",
15
- in: "header",
16
- name: "x-api-key",
17
- description: "API key for authentication",
18
- });
19
19
  const UserSchema = z
20
20
  .object({
21
21
  id: z.string().openapi({ example: "1212121" }),
@@ -23,45 +23,29 @@ const UserSchema = z
23
23
  age: z.number().openapi({ example: 42 }),
24
24
  })
25
25
  .openapi("User");
26
+ /*
26
27
  registry.registerPath({
27
- method: "get",
28
- path: "/usersnnn/{id}",
29
- summary: "Get a single user",
30
- request: {
31
- params: z.object({ id: z.string() }),
32
- },
33
- responses: {
34
- 200: {
35
- description: "Object with user data.",
36
- content: {
37
- "application/json": {
38
- schema: UserSchema,
39
- },
40
- },
28
+ method: "get",
29
+ path: "/users/{id}",
30
+ description: "Get user data by its id",
31
+ summary: "Get a single user",
32
+ request: {
33
+ params: z.object({
34
+ id: z.string().openapi({ example: "1212121" }),
35
+ }),
36
+ },
37
+ responses: {
38
+ 200: {
39
+ description: "Object with user data.",
40
+ content: {
41
+ "application/json": {
42
+ schema: UserSchema,
41
43
  },
44
+ },
42
45
  },
43
- });
44
- registry.registerPath({
45
- method: "get",
46
- path: "/users/{id}",
47
- description: "Get user data by its id",
48
- summary: "Get a single user",
49
- request: {
50
- params: z.object({
51
- id: z.string().openapi({ example: "1212121" }),
52
- }),
53
- },
54
- responses: {
55
- 200: {
56
- description: "Object with user data.",
57
- content: {
58
- "application/json": {
59
- schema: UserSchema,
60
- },
61
- },
62
- },
63
- 204: {
64
- description: "No content - successful operation",
65
- },
46
+ 204: {
47
+ description: "No content - successful operation",
66
48
  },
49
+ },
67
50
  });
51
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetderdinge/api",
3
- "version": "1.229.27",
3
+ "version": "1.229.31",
4
4
  "description": "Shared OpenIoT API modules",
5
5
  "main": "dist/src/index.js",
6
6
  "type": "module",
@@ -0,0 +1,101 @@
1
+ import type { Request, Response } from "express";
2
+ import httpStatus from "http-status";
3
+ import { ApiError } from "../utils/ApiError.js";
4
+ import catchAsync from "../utils/catchAsync.js";
5
+ import {
6
+ getAdminDevices,
7
+ getAdminIotDevices,
8
+ getAdminStats,
9
+ searchAdminCollections,
10
+ } from "./adminSearch.service.js";
11
+
12
+ const ADMIN_ROLE_CLAIM = "https://memo.wirewire.de/roles";
13
+
14
+ type AuthRequest = Request & {
15
+ auth?: Record<string, unknown>;
16
+ };
17
+
18
+ const hasAdminRole = (auth: Record<string, unknown> | undefined): boolean => {
19
+ const roles = auth?.[ADMIN_ROLE_CLAIM];
20
+ return Array.isArray(roles) && roles.includes("admin");
21
+ };
22
+
23
+ const readListQuery = (
24
+ req: Request,
25
+ ): {
26
+ page: number;
27
+ perPage: number;
28
+ updatedSince: string | null;
29
+ } => {
30
+ const rawPage = Number(req.query.page);
31
+ const rawPerPage = Number(req.query.perPage);
32
+ const updatedSince = String(req.query.updatedSince ?? "").trim() || null;
33
+
34
+ return {
35
+ page: Number.isFinite(rawPage) ? Math.max(1, Math.floor(rawPage)) : 1,
36
+ perPage: Number.isFinite(rawPerPage)
37
+ ? Math.max(1, Math.min(500, Math.floor(rawPerPage)))
38
+ : 100,
39
+ updatedSince,
40
+ };
41
+ };
42
+
43
+ export const searchAdmin = catchAsync(
44
+ async (req: AuthRequest, res: Response) => {
45
+ if (!hasAdminRole(req.auth)) {
46
+ throw new ApiError(
47
+ httpStatus.FORBIDDEN,
48
+ "User is not part of the admin group",
49
+ );
50
+ }
51
+
52
+ const search = String(req.query.search || "").trim();
53
+ const rawLimit = Number(req.query.limit);
54
+ const limit = Number.isFinite(rawLimit)
55
+ ? Math.max(1, Math.min(50, Math.floor(rawLimit)))
56
+ : 12;
57
+
58
+ const result = await searchAdminCollections({ search, limit });
59
+ res.send(result);
60
+ },
61
+ );
62
+
63
+ export const getStats = catchAsync(async (req: AuthRequest, res: Response) => {
64
+ if (!hasAdminRole(req.auth)) {
65
+ throw new ApiError(
66
+ httpStatus.FORBIDDEN,
67
+ "User is not part of the admin group",
68
+ );
69
+ }
70
+
71
+ const result = await getAdminStats();
72
+ res.send(result);
73
+ });
74
+
75
+ export const getIotDevices = catchAsync(
76
+ async (req: AuthRequest, res: Response) => {
77
+ if (!hasAdminRole(req.auth)) {
78
+ throw new ApiError(
79
+ httpStatus.FORBIDDEN,
80
+ "User is not part of the admin group",
81
+ );
82
+ }
83
+
84
+ const result = await getAdminIotDevices(readListQuery(req));
85
+ res.send(result);
86
+ },
87
+ );
88
+
89
+ export const getDevices = catchAsync(
90
+ async (req: AuthRequest, res: Response) => {
91
+ if (!hasAdminRole(req.auth)) {
92
+ throw new ApiError(
93
+ httpStatus.FORBIDDEN,
94
+ "User is not part of the admin group",
95
+ );
96
+ }
97
+
98
+ const result = await getAdminDevices(readListQuery(req));
99
+ res.send(result);
100
+ },
101
+ );
@@ -0,0 +1,73 @@
1
+ import { Router } from "express";
2
+ import buildRouterAndDocs, { type RouteSpec } from "../utils/buildRouterAndDocs.js";
3
+ import auth from "../middlewares/auth.js";
4
+ import { validateAdmin } from "../middlewares/validateAdmin.js";
5
+ import {
6
+ getDevices,
7
+ getIotDevices,
8
+ getStats,
9
+ searchAdmin,
10
+ } from "./adminSearch.controller.js";
11
+ import {
12
+ adminDevicesSchema,
13
+ adminIotDevicesSchema,
14
+ adminSearchSchema,
15
+ adminStatsSchema,
16
+ } from "./adminSearch.validation.js";
17
+ import {
18
+ adminDevicesResponseSchema,
19
+ adminIotDevicesResponseSchema,
20
+ adminSearchResponseSchema,
21
+ adminStatsResponseSchema,
22
+ } from "./adminSearch.schemas.js";
23
+
24
+ export const adminSearchRouteSpecs: RouteSpec[] = [
25
+ {
26
+ method: "get",
27
+ path: "/stats",
28
+ validate: [auth("getUsers"), validateAdmin],
29
+ requestSchema: adminStatsSchema,
30
+ responseSchema: adminStatsResponseSchema,
31
+ handler: getStats,
32
+ summary: "Get admin stats",
33
+ description: "Returns total counts for organizations, users, and devices.",
34
+ },
35
+ {
36
+ method: "get",
37
+ path: "/search",
38
+ validate: [auth("getUsers"), validateAdmin],
39
+ requestSchema: adminSearchSchema,
40
+ responseSchema: adminSearchResponseSchema,
41
+ handler: searchAdmin,
42
+ summary: "Search organizations, users, and devices",
43
+ description:
44
+ "Performs an admin-only global search over organizations, users, and devices.",
45
+ },
46
+ {
47
+ method: "get",
48
+ path: "/iotDevices",
49
+ validate: [auth("getUsers"), validateAdmin],
50
+ requestSchema: adminIotDevicesSchema,
51
+ responseSchema: adminIotDevicesResponseSchema,
52
+ handler: getIotDevices,
53
+ summary: "List IoT devices",
54
+ description:
55
+ "Returns the IoT device status list used for admin device/order sync workflows.",
56
+ },
57
+ {
58
+ method: "get",
59
+ path: "/devices",
60
+ validate: [auth("getUsers"), validateAdmin],
61
+ requestSchema: adminDevicesSchema,
62
+ responseSchema: adminDevicesResponseSchema,
63
+ handler: getDevices,
64
+ summary: "List MongoDB devices",
65
+ description:
66
+ "Returns all device documents from MongoDB for admin sync workflows.",
67
+ },
68
+ ];
69
+
70
+ const router: Router = Router();
71
+ buildRouterAndDocs(router, adminSearchRouteSpecs, "/admin", ["Admin"]);
72
+
73
+ export default router;
@@ -0,0 +1,75 @@
1
+ import { z } from "zod";
2
+
3
+ const organizationSearchEntrySchema = z.object({
4
+ id: z.string(),
5
+ name: z.string().optional(),
6
+ kind: z.string().optional(),
7
+ });
8
+
9
+ const userSearchEntrySchema = z.object({
10
+ id: z.string(),
11
+ name: z.string().optional(),
12
+ email: z.string().optional(),
13
+ role: z.string().optional(),
14
+ organization: organizationSearchEntrySchema.optional(),
15
+ });
16
+
17
+ const deviceSearchEntrySchema = z.object({
18
+ id: z.string(),
19
+ name: z.string().optional(),
20
+ deviceId: z.string().optional(),
21
+ kind: z.string().optional(),
22
+ timezone: z.string().optional(),
23
+ eventDate: z.string().optional(),
24
+ createdAt: z.string().optional(),
25
+ updatedAt: z.string().optional(),
26
+ serialNumber: z.string().optional(),
27
+ paymentId: z.string().optional(),
28
+ batteryStatus: z.string().optional(),
29
+ batteryLevel: z.number().optional(),
30
+ signalStrength: z.number().optional(),
31
+ lastReachableAgo: z.string().optional(),
32
+ organization: organizationSearchEntrySchema.optional(),
33
+ patient: z
34
+ .object({
35
+ id: z.string(),
36
+ name: z.string().optional(),
37
+ })
38
+ .optional(),
39
+ });
40
+
41
+ export const adminSearchResponseSchema = z.object({
42
+ query: z.string(),
43
+ organizations: z.array(organizationSearchEntrySchema),
44
+ users: z.array(userSearchEntrySchema),
45
+ devices: z.array(deviceSearchEntrySchema),
46
+ total: z.number(),
47
+ tookMs: z.number(),
48
+ });
49
+
50
+ export const adminStatsResponseSchema = z.object({
51
+ users: z.number(),
52
+ auth0Users: z.number(),
53
+ devices: z.number(),
54
+ organizations: z.number(),
55
+ total: z.number(),
56
+ tookMs: z.number(),
57
+ });
58
+
59
+ export const adminIotDevicesResponseSchema = z.object({
60
+ results: z.array(z.record(z.string(), z.unknown())),
61
+ page: z.number(),
62
+ perPage: z.number(),
63
+ totalPages: z.number(),
64
+ total: z.number(),
65
+ tookMs: z.number(),
66
+ });
67
+
68
+ export const adminDevicesResponseSchema = z.object({
69
+ results: z.array(z.record(z.string(), z.unknown())),
70
+ page: z.number(),
71
+ perPage: z.number(),
72
+ totalPages: z.number(),
73
+ total: z.number(),
74
+ tookMs: z.number(),
75
+ });