@internetderdinge/api 1.229.28 → 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,54 @@
1
+ import httpStatus from "http-status";
2
+ import { ApiError } from "../utils/ApiError.js";
3
+ import catchAsync from "../utils/catchAsync.js";
4
+ import { getAdminDevices, getAdminIotDevices, getAdminStats, searchAdminCollections, } from "./adminSearch.service.js";
5
+ const ADMIN_ROLE_CLAIM = "https://memo.wirewire.de/roles";
6
+ const hasAdminRole = (auth) => {
7
+ const roles = auth?.[ADMIN_ROLE_CLAIM];
8
+ return Array.isArray(roles) && roles.includes("admin");
9
+ };
10
+ const readListQuery = (req) => {
11
+ const rawPage = Number(req.query.page);
12
+ const rawPerPage = Number(req.query.perPage);
13
+ const updatedSince = String(req.query.updatedSince ?? "").trim() || null;
14
+ return {
15
+ page: Number.isFinite(rawPage) ? Math.max(1, Math.floor(rawPage)) : 1,
16
+ perPage: Number.isFinite(rawPerPage)
17
+ ? Math.max(1, Math.min(500, Math.floor(rawPerPage)))
18
+ : 100,
19
+ updatedSince,
20
+ };
21
+ };
22
+ export const searchAdmin = catchAsync(async (req, res) => {
23
+ if (!hasAdminRole(req.auth)) {
24
+ throw new ApiError(httpStatus.FORBIDDEN, "User is not part of the admin group");
25
+ }
26
+ const search = String(req.query.search || "").trim();
27
+ const rawLimit = Number(req.query.limit);
28
+ const limit = Number.isFinite(rawLimit)
29
+ ? Math.max(1, Math.min(50, Math.floor(rawLimit)))
30
+ : 12;
31
+ const result = await searchAdminCollections({ search, limit });
32
+ res.send(result);
33
+ });
34
+ export const getStats = catchAsync(async (req, res) => {
35
+ if (!hasAdminRole(req.auth)) {
36
+ throw new ApiError(httpStatus.FORBIDDEN, "User is not part of the admin group");
37
+ }
38
+ const result = await getAdminStats();
39
+ res.send(result);
40
+ });
41
+ export const getIotDevices = catchAsync(async (req, res) => {
42
+ if (!hasAdminRole(req.auth)) {
43
+ throw new ApiError(httpStatus.FORBIDDEN, "User is not part of the admin group");
44
+ }
45
+ const result = await getAdminIotDevices(readListQuery(req));
46
+ res.send(result);
47
+ });
48
+ export const getDevices = catchAsync(async (req, res) => {
49
+ if (!hasAdminRole(req.auth)) {
50
+ throw new ApiError(httpStatus.FORBIDDEN, "User is not part of the admin group");
51
+ }
52
+ const result = await getAdminDevices(readListQuery(req));
53
+ res.send(result);
54
+ });
@@ -0,0 +1,52 @@
1
+ import { Router } from "express";
2
+ import buildRouterAndDocs from "../utils/buildRouterAndDocs.js";
3
+ import auth from "../middlewares/auth.js";
4
+ import { validateAdmin } from "../middlewares/validateAdmin.js";
5
+ import { getDevices, getIotDevices, getStats, searchAdmin, } from "./adminSearch.controller.js";
6
+ import { adminDevicesSchema, adminIotDevicesSchema, adminSearchSchema, adminStatsSchema, } from "./adminSearch.validation.js";
7
+ import { adminDevicesResponseSchema, adminIotDevicesResponseSchema, adminSearchResponseSchema, adminStatsResponseSchema, } from "./adminSearch.schemas.js";
8
+ export const adminSearchRouteSpecs = [
9
+ {
10
+ method: "get",
11
+ path: "/stats",
12
+ validate: [auth("getUsers"), validateAdmin],
13
+ requestSchema: adminStatsSchema,
14
+ responseSchema: adminStatsResponseSchema,
15
+ handler: getStats,
16
+ summary: "Get admin stats",
17
+ description: "Returns total counts for organizations, users, and devices.",
18
+ },
19
+ {
20
+ method: "get",
21
+ path: "/search",
22
+ validate: [auth("getUsers"), validateAdmin],
23
+ requestSchema: adminSearchSchema,
24
+ responseSchema: adminSearchResponseSchema,
25
+ handler: searchAdmin,
26
+ summary: "Search organizations, users, and devices",
27
+ description: "Performs an admin-only global search over organizations, users, and devices.",
28
+ },
29
+ {
30
+ method: "get",
31
+ path: "/iotDevices",
32
+ validate: [auth("getUsers"), validateAdmin],
33
+ requestSchema: adminIotDevicesSchema,
34
+ responseSchema: adminIotDevicesResponseSchema,
35
+ handler: getIotDevices,
36
+ summary: "List IoT devices",
37
+ description: "Returns the IoT device status list used for admin device/order sync workflows.",
38
+ },
39
+ {
40
+ method: "get",
41
+ path: "/devices",
42
+ validate: [auth("getUsers"), validateAdmin],
43
+ requestSchema: adminDevicesSchema,
44
+ responseSchema: adminDevicesResponseSchema,
45
+ handler: getDevices,
46
+ summary: "List MongoDB devices",
47
+ description: "Returns all device documents from MongoDB for admin sync workflows.",
48
+ },
49
+ ];
50
+ const router = Router();
51
+ buildRouterAndDocs(router, adminSearchRouteSpecs, "/admin", ["Admin"]);
52
+ export default router;
@@ -0,0 +1,68 @@
1
+ import { z } from "zod";
2
+ const organizationSearchEntrySchema = z.object({
3
+ id: z.string(),
4
+ name: z.string().optional(),
5
+ kind: z.string().optional(),
6
+ });
7
+ const userSearchEntrySchema = z.object({
8
+ id: z.string(),
9
+ name: z.string().optional(),
10
+ email: z.string().optional(),
11
+ role: z.string().optional(),
12
+ organization: organizationSearchEntrySchema.optional(),
13
+ });
14
+ const deviceSearchEntrySchema = z.object({
15
+ id: z.string(),
16
+ name: z.string().optional(),
17
+ deviceId: z.string().optional(),
18
+ kind: z.string().optional(),
19
+ timezone: z.string().optional(),
20
+ eventDate: z.string().optional(),
21
+ createdAt: z.string().optional(),
22
+ updatedAt: z.string().optional(),
23
+ serialNumber: z.string().optional(),
24
+ paymentId: z.string().optional(),
25
+ batteryStatus: z.string().optional(),
26
+ batteryLevel: z.number().optional(),
27
+ signalStrength: z.number().optional(),
28
+ lastReachableAgo: z.string().optional(),
29
+ organization: organizationSearchEntrySchema.optional(),
30
+ patient: z
31
+ .object({
32
+ id: z.string(),
33
+ name: z.string().optional(),
34
+ })
35
+ .optional(),
36
+ });
37
+ export const adminSearchResponseSchema = z.object({
38
+ query: z.string(),
39
+ organizations: z.array(organizationSearchEntrySchema),
40
+ users: z.array(userSearchEntrySchema),
41
+ devices: z.array(deviceSearchEntrySchema),
42
+ total: z.number(),
43
+ tookMs: z.number(),
44
+ });
45
+ export const adminStatsResponseSchema = z.object({
46
+ users: z.number(),
47
+ auth0Users: z.number(),
48
+ devices: z.number(),
49
+ organizations: z.number(),
50
+ total: z.number(),
51
+ tookMs: z.number(),
52
+ });
53
+ export const adminIotDevicesResponseSchema = z.object({
54
+ results: z.array(z.record(z.string(), z.unknown())),
55
+ page: z.number(),
56
+ perPage: z.number(),
57
+ totalPages: z.number(),
58
+ total: z.number(),
59
+ tookMs: z.number(),
60
+ });
61
+ export const adminDevicesResponseSchema = z.object({
62
+ results: z.array(z.record(z.string(), z.unknown())),
63
+ page: z.number(),
64
+ perPage: z.number(),
65
+ totalPages: z.number(),
66
+ total: z.number(),
67
+ tookMs: z.number(),
68
+ });