@adventurelabs/scout-core 1.5.4 → 2.0.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.
Files changed (219) hide show
  1. package/README.md +47 -0
  2. package/dist/api_keys/actions.d.ts +0 -1
  3. package/dist/api_keys/actions.js +0 -14
  4. package/dist/client/index.d.ts +75 -0
  5. package/dist/client/index.js +82 -0
  6. package/dist/constants/index.d.ts +1 -0
  7. package/dist/constants/index.js +1 -0
  8. package/dist/helpers/analysis_usage/jobs_server.d.ts +25 -0
  9. package/dist/helpers/analysis_usage/jobs_server.js +10 -0
  10. package/dist/helpers/analysis_usage/tasks_server.d.ts +63 -0
  11. package/dist/helpers/analysis_usage/tasks_server.js +14 -0
  12. package/dist/helpers/artifacts.d.ts +0 -1
  13. package/dist/helpers/artifacts.js +10 -147
  14. package/dist/helpers/artifacts.queries.d.ts +26 -0
  15. package/dist/helpers/{artifacts.internal.js → artifacts.queries.js} +24 -41
  16. package/dist/helpers/auth.d.ts +1 -1
  17. package/dist/helpers/auth.js +2 -3
  18. package/dist/helpers/auth_server.d.ts +2 -0
  19. package/dist/helpers/auth_server.js +10 -0
  20. package/dist/helpers/certificates_server.d.ts +101 -0
  21. package/dist/helpers/certificates_server.js +42 -0
  22. package/dist/helpers/chat.d.ts +1 -2
  23. package/dist/helpers/chat.js +3 -38
  24. package/dist/helpers/chat.queries.d.ts +13 -0
  25. package/dist/helpers/chat.queries.js +39 -0
  26. package/dist/helpers/client_abilities_jwt_keys.js +0 -1
  27. package/dist/helpers/client_abilities_token_server.d.ts +1 -0
  28. package/dist/helpers/client_abilities_token_server.js +5 -2
  29. package/dist/helpers/connectivity.d.ts +1 -5
  30. package/dist/helpers/connectivity.js +7 -140
  31. package/dist/helpers/connectivity.queries.d.ts +18 -0
  32. package/dist/helpers/connectivity.queries.js +121 -0
  33. package/dist/helpers/contacts_server.d.ts +63 -0
  34. package/dist/helpers/contacts_server.js +18 -0
  35. package/dist/helpers/deviceTransfer.js +2 -11
  36. package/dist/helpers/deviceTransfer.queries.d.ts +4 -0
  37. package/dist/helpers/deviceTransfer.queries.js +11 -0
  38. package/dist/helpers/devices.js +4 -61
  39. package/dist/helpers/devices.queries.d.ts +4 -1
  40. package/dist/helpers/devices.queries.js +52 -0
  41. package/dist/helpers/document_templates.js +6 -52
  42. package/dist/helpers/document_templates.queries.d.ts +9 -0
  43. package/dist/helpers/document_templates.queries.js +56 -0
  44. package/dist/helpers/documents_server.d.ts +226 -0
  45. package/dist/helpers/documents_server.js +38 -0
  46. package/dist/helpers/email.d.ts +0 -6
  47. package/dist/helpers/email.js +1 -19
  48. package/dist/helpers/embeddings.d.ts +0 -5
  49. package/dist/helpers/embeddings.js +2 -19
  50. package/dist/helpers/embeddings.queries.d.ts +7 -0
  51. package/dist/helpers/{embeddings.internal.js → embeddings.queries.js} +2 -2
  52. package/dist/helpers/eventUtils.d.ts +3 -12
  53. package/dist/helpers/eventUtils.js +0 -10
  54. package/dist/helpers/events.d.ts +0 -1
  55. package/dist/helpers/events.js +7 -107
  56. package/dist/helpers/events.queries.d.ts +17 -0
  57. package/dist/helpers/{events.internal.js → events.queries.js} +24 -15
  58. package/dist/helpers/health_metrics.js +3 -38
  59. package/dist/helpers/health_metrics.queries.d.ts +12 -0
  60. package/dist/helpers/health_metrics.queries.js +39 -0
  61. package/dist/helpers/heartbeats.js +4 -83
  62. package/dist/helpers/heartbeats.queries.d.ts +11 -0
  63. package/dist/helpers/heartbeats.queries.js +76 -0
  64. package/dist/helpers/herd_allowed_domains.js +2 -12
  65. package/dist/helpers/{herd_allowed_domains_internal.d.ts → herd_allowed_domains.queries.d.ts} +1 -1
  66. package/dist/helpers/{herd_allowed_domains_internal.js → herd_allowed_domains.queries.js} +2 -2
  67. package/dist/helpers/herd_credentials_server.d.ts +76 -0
  68. package/dist/helpers/herd_credentials_server.js +22 -0
  69. package/dist/helpers/herd_modules.queries.js +10 -7
  70. package/dist/helpers/herd_roles.js +13 -148
  71. package/dist/helpers/herd_roles.queries.d.ts +18 -0
  72. package/dist/helpers/{herd_roles_internal.js → herd_roles.queries.js} +28 -28
  73. package/dist/helpers/herds.js +5 -47
  74. package/dist/helpers/herds.queries.d.ts +8 -1
  75. package/dist/helpers/herds.queries.js +48 -0
  76. package/dist/helpers/invitations.js +7 -58
  77. package/dist/helpers/invitations.queries.d.ts +10 -0
  78. package/dist/helpers/{invitations_internal.js → invitations.queries.js} +12 -12
  79. package/dist/helpers/issuers.js +6 -52
  80. package/dist/helpers/issuers.queries.d.ts +9 -0
  81. package/dist/helpers/issuers.queries.js +53 -0
  82. package/dist/helpers/layers.js +2 -20
  83. package/dist/helpers/layers.queries.d.ts +2 -1
  84. package/dist/helpers/layers.queries.js +9 -13
  85. package/dist/helpers/lifecycle_server.d.ts +175 -0
  86. package/dist/helpers/lifecycle_server.js +38 -0
  87. package/dist/helpers/localizations.js +5 -42
  88. package/dist/helpers/localizations.queries.d.ts +8 -0
  89. package/dist/helpers/localizations.queries.js +45 -0
  90. package/dist/helpers/maintenance_requests_server.d.ts +124 -0
  91. package/dist/helpers/maintenance_requests_server.js +34 -0
  92. package/dist/helpers/manufacturers.js +5 -43
  93. package/dist/helpers/manufacturers.queries.d.ts +10 -0
  94. package/dist/helpers/manufacturers.queries.js +46 -0
  95. package/dist/helpers/media_urls.d.ts +13 -0
  96. package/dist/helpers/media_urls.js +21 -0
  97. package/dist/helpers/models.js +8 -80
  98. package/dist/helpers/models.queries.d.ts +13 -0
  99. package/dist/helpers/models.queries.js +79 -0
  100. package/dist/helpers/observations_server.d.ts +51 -0
  101. package/dist/helpers/observations_server.js +22 -0
  102. package/dist/helpers/operating_contexts.js +39 -154
  103. package/dist/helpers/operating_contexts.queries.d.ts +48 -0
  104. package/dist/helpers/operating_contexts.queries.js +223 -0
  105. package/dist/helpers/operators.js +4 -57
  106. package/dist/helpers/operators.queries.d.ts +7 -0
  107. package/dist/helpers/operators.queries.js +55 -0
  108. package/dist/helpers/parts.d.ts +0 -87
  109. package/dist/helpers/parts.js +4 -90
  110. package/dist/helpers/parts_server.d.ts +260 -0
  111. package/dist/helpers/parts_server.js +71 -0
  112. package/dist/helpers/pins_server.d.ts +118 -0
  113. package/dist/helpers/pins_server.js +38 -0
  114. package/dist/helpers/plans.js +4 -56
  115. package/dist/helpers/plans.queries.d.ts +4 -1
  116. package/dist/helpers/plans.queries.js +39 -12
  117. package/dist/helpers/points_for_user_server.d.ts +9 -0
  118. package/dist/helpers/points_for_user_server.js +14 -0
  119. package/dist/helpers/product_compatibilities.js +6 -78
  120. package/dist/helpers/product_compatibilities.queries.d.ts +9 -0
  121. package/dist/helpers/product_compatibilities.queries.js +83 -0
  122. package/dist/helpers/product_numbers.js +6 -55
  123. package/dist/helpers/product_numbers.queries.d.ts +9 -0
  124. package/dist/helpers/product_numbers.queries.js +58 -0
  125. package/dist/helpers/products.js +6 -54
  126. package/dist/helpers/products.queries.d.ts +11 -0
  127. package/dist/helpers/products.queries.js +58 -0
  128. package/dist/helpers/providers.js +5 -71
  129. package/dist/helpers/providers.queries.d.ts +5 -1
  130. package/dist/helpers/providers.queries.js +52 -11
  131. package/dist/helpers/pubsub_token_server.d.ts +1 -0
  132. package/dist/helpers/pubsub_token_server.js +5 -2
  133. package/dist/helpers/segmentations_sam3.js +5 -78
  134. package/dist/helpers/segmentations_sam3.queries.d.ts +16 -0
  135. package/dist/helpers/segmentations_sam3.queries.js +81 -0
  136. package/dist/helpers/sensor_poses_server.d.ts +98 -0
  137. package/dist/helpers/sensor_poses_server.js +26 -0
  138. package/dist/helpers/session_incidents_server.d.ts +106 -0
  139. package/dist/helpers/session_incidents_server.js +22 -0
  140. package/dist/helpers/session_summaries.d.ts +2 -0
  141. package/dist/helpers/session_summaries.js +7 -23
  142. package/dist/helpers/session_summaries.queries.d.ts +6 -3
  143. package/dist/helpers/session_summaries.queries.js +27 -0
  144. package/dist/helpers/sessions.js +8 -122
  145. package/dist/helpers/sessions.queries.d.ts +14 -2
  146. package/dist/helpers/sessions.queries.js +88 -1
  147. package/dist/helpers/storage.js +0 -1
  148. package/dist/helpers/supabase_auth.d.ts +6 -0
  149. package/dist/helpers/supabase_auth.js +37 -0
  150. package/dist/helpers/tags.d.ts +0 -1
  151. package/dist/helpers/tags.js +13 -559
  152. package/dist/helpers/tags.queries.d.ts +33 -0
  153. package/dist/helpers/tags.queries.js +454 -0
  154. package/dist/helpers/user_credentials_server.d.ts +76 -0
  155. package/dist/helpers/user_credentials_server.js +22 -0
  156. package/dist/helpers/users.d.ts +2 -2
  157. package/dist/helpers/users.js +8 -107
  158. package/dist/helpers/users.queries.d.ts +9 -1
  159. package/dist/helpers/users.queries.js +107 -0
  160. package/dist/helpers/versions_software.d.ts +3 -1
  161. package/dist/helpers/versions_software.js +24 -0
  162. package/dist/helpers/versions_software_server.d.ts +83 -4
  163. package/dist/helpers/versions_software_server.js +23 -50
  164. package/dist/helpers/zones.d.ts +1 -16
  165. package/dist/helpers/zones.js +4 -104
  166. package/dist/helpers/zones.queries.d.ts +6 -0
  167. package/dist/helpers/zones.queries.js +78 -0
  168. package/dist/hooks/useAuthorizedRealtimeTopics.js +8 -2
  169. package/dist/hooks/useInfiniteQuery.js +18 -14
  170. package/dist/hooks/useScoutRealtimeBroadcast.js +8 -6
  171. package/dist/hooks/useScoutRealtimeDocuments.js +3 -2
  172. package/dist/hooks/useScoutRefresh.d.ts +9 -25
  173. package/dist/hooks/useScoutRefresh.js +33 -213
  174. package/dist/providers/ScoutRefreshProvider.d.ts +1 -1
  175. package/dist/providers/ScoutRefreshProvider.js +56 -13
  176. package/dist/server/index.d.ts +77 -0
  177. package/dist/server/index.js +79 -0
  178. package/dist/server/middleware.d.ts +2 -0
  179. package/dist/server/middleware.js +2 -0
  180. package/dist/server/supabase.d.ts +2 -0
  181. package/dist/server/supabase.js +2 -0
  182. package/dist/store/api.d.ts +40 -40
  183. package/dist/store/api.js +38 -109
  184. package/dist/store/configureStore.d.ts +0 -55
  185. package/dist/store/configureStore.js +1 -8
  186. package/dist/store/hooks.js +1 -0
  187. package/dist/supabase/client.d.ts +3 -0
  188. package/dist/supabase/client.js +10 -0
  189. package/dist/supabase/middleware.d.ts +1 -0
  190. package/dist/supabase/middleware.js +6 -20
  191. package/dist/supabase/server.d.ts +7 -2
  192. package/dist/supabase/server.js +13 -1
  193. package/dist/types/db.d.ts +2 -2
  194. package/dist/types/index.d.ts +5 -0
  195. package/dist/types/index.js +5 -0
  196. package/package.json +53 -4
  197. package/dist/api_keys/index.d.ts +0 -1
  198. package/dist/api_keys/index.js +0 -1
  199. package/dist/helpers/artifacts.internal.d.ts +0 -22
  200. package/dist/helpers/client_abilities_jwt_keys_internal.d.ts +0 -9
  201. package/dist/helpers/client_abilities_jwt_keys_internal.js +0 -20
  202. package/dist/helpers/connectivity.internal.d.ts +0 -10
  203. package/dist/helpers/connectivity.internal.js +0 -47
  204. package/dist/helpers/credentials.d.ts +0 -9
  205. package/dist/helpers/credentials.js +0 -92
  206. package/dist/helpers/embeddings.internal.d.ts +0 -5
  207. package/dist/helpers/events.internal.d.ts +0 -13
  208. package/dist/helpers/herd_roles_internal.d.ts +0 -18
  209. package/dist/helpers/index.d.ts +0 -56
  210. package/dist/helpers/index.js +0 -56
  211. package/dist/helpers/invitations_internal.d.ts +0 -10
  212. package/dist/helpers/versions_software_internal.d.ts +0 -6
  213. package/dist/helpers/versions_software_internal.js +0 -53
  214. package/dist/hooks/useDeviceDisplayName.d.ts +0 -8
  215. package/dist/hooks/useDeviceDisplayName.js +0 -49
  216. package/dist/index.d.ts +0 -108
  217. package/dist/index.js +0 -114
  218. package/dist/supabase/index.d.ts +0 -2
  219. package/dist/supabase/index.js +0 -2
@@ -1,43 +1,8 @@
1
- "use server";
2
1
  import { newServerClient } from "../supabase/server";
3
- import { EnumWebResponse, IWebResponse, } from "../types/requests";
2
+ import { get_health_metrics_query, get_health_metrics_summary_query, } from "./health_metrics.queries";
4
3
  export async function server_get_health_metrics(device_id, options) {
5
- const supabase = await newServerClient();
6
- const lookbackMs = options.lookbackSeconds * 1000;
7
- const anchorMs = options.timestampAnchor != null && options.timestampAnchor !== ""
8
- ? new Date(options.timestampAnchor).getTime()
9
- : Date.now();
10
- const startMs = anchorMs - lookbackMs;
11
- const startISO = new Date(startMs).toISOString();
12
- const endISO = new Date(anchorMs).toISOString();
13
- let query = supabase
14
- .from("health_metrics")
15
- .select("*")
16
- .eq("device_id", device_id)
17
- .gte("timestamp", startISO)
18
- .lt("timestamp", endISO)
19
- .order("timestamp", { ascending: false })
20
- .limit(options.maxCount);
21
- if (options.source != null && options.source !== "") {
22
- query = query.eq("source", options.source);
23
- }
24
- if (options.metricName != null && options.metricName !== "") {
25
- query = query.eq("metric_name", options.metricName);
26
- }
27
- const { data, error } = await query;
28
- if (error) {
29
- return { status: EnumWebResponse.ERROR, msg: error.message, data: [] };
30
- }
31
- return IWebResponse.success(data ?? []).to_compatible();
4
+ return get_health_metrics_query(await newServerClient(), device_id, options);
32
5
  }
33
6
  export async function server_get_health_metrics_summary(device_id, lookbackMinutes = 60) {
34
- const supabase = await newServerClient();
35
- const { data, error } = await supabase.rpc("get_health_metrics_summary", {
36
- p_device_id: device_id,
37
- p_lookback_minutes: lookbackMinutes,
38
- });
39
- if (error) {
40
- return { status: EnumWebResponse.ERROR, msg: error.message, data: [] };
41
- }
42
- return IWebResponse.success(data ?? []).to_compatible();
7
+ return get_health_metrics_summary_query(await newServerClient(), device_id, lookbackMinutes);
43
8
  }
@@ -0,0 +1,12 @@
1
+ import { SupabaseClient } from "@supabase/supabase-js";
2
+ import { IHealthMetric, IHealthMetricSummaryRow } from "../types/db";
3
+ import { IWebResponseCompatible } from "../types/requests";
4
+ import { Database } from "../types/supabase";
5
+ export declare function get_health_metrics_query(client: SupabaseClient<Database>, device_id: number, options: {
6
+ lookbackSeconds: number;
7
+ maxCount: number;
8
+ timestampAnchor?: string | null;
9
+ source?: string | null;
10
+ metricName?: string | null;
11
+ }): Promise<IWebResponseCompatible<IHealthMetric[]>>;
12
+ export declare function get_health_metrics_summary_query(client: SupabaseClient<Database>, device_id: number, lookbackMinutes?: number): Promise<IWebResponseCompatible<IHealthMetricSummaryRow[]>>;
@@ -0,0 +1,39 @@
1
+ import { EnumWebResponse, IWebResponse, } from "../types/requests";
2
+ export async function get_health_metrics_query(client, device_id, options) {
3
+ const lookbackMs = options.lookbackSeconds * 1000;
4
+ const anchorMs = options.timestampAnchor != null && options.timestampAnchor !== ""
5
+ ? new Date(options.timestampAnchor).getTime()
6
+ : Date.now();
7
+ const startMs = anchorMs - lookbackMs;
8
+ const startISO = new Date(startMs).toISOString();
9
+ const endISO = new Date(anchorMs).toISOString();
10
+ let query = client
11
+ .from("health_metrics")
12
+ .select("*")
13
+ .eq("device_id", device_id)
14
+ .gte("timestamp", startISO)
15
+ .lt("timestamp", endISO)
16
+ .order("timestamp", { ascending: false })
17
+ .limit(options.maxCount);
18
+ if (options.source != null && options.source !== "") {
19
+ query = query.eq("source", options.source);
20
+ }
21
+ if (options.metricName != null && options.metricName !== "") {
22
+ query = query.eq("metric_name", options.metricName);
23
+ }
24
+ const { data, error } = await query;
25
+ if (error) {
26
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: [] };
27
+ }
28
+ return IWebResponse.success(data ?? []).to_compatible();
29
+ }
30
+ export async function get_health_metrics_summary_query(client, device_id, lookbackMinutes = 60) {
31
+ const { data, error } = await client.rpc("get_health_metrics_summary", {
32
+ p_device_id: device_id,
33
+ p_lookback_minutes: lookbackMinutes,
34
+ });
35
+ if (error) {
36
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: [] };
37
+ }
38
+ return IWebResponse.success(data ?? []).to_compatible();
39
+ }
@@ -1,90 +1,11 @@
1
- "use server";
2
1
  import { newServerClient } from "../supabase/server";
3
- import { EnumWebResponse, IWebResponse, } from "../types/requests";
4
- // Function to get the last heartbeat for a device
2
+ import { check_device_online_status_query, get_heartbeats_by_device_query, get_last_heartbeat_by_device_query, } from "./heartbeats.queries";
5
3
  export async function server_get_last_heartbeat_by_device(device_id) {
6
- const supabase = await newServerClient();
7
- const { data, error } = await supabase
8
- .from("heartbeats")
9
- .select("*")
10
- .eq("device_id", device_id)
11
- .order("timestamp", { ascending: false })
12
- .limit(1)
13
- .single();
14
- if (error) {
15
- // If no heartbeats found, return null data instead of error
16
- if (error.code === "PGRST116") {
17
- return {
18
- status: EnumWebResponse.SUCCESS,
19
- msg: null,
20
- data: null,
21
- };
22
- }
23
- return {
24
- status: EnumWebResponse.ERROR,
25
- msg: error.message,
26
- data: null,
27
- };
28
- }
29
- else {
30
- return IWebResponse.success(data).to_compatible();
31
- }
4
+ return get_last_heartbeat_by_device_query(await newServerClient(), device_id);
32
5
  }
33
- // Function to get all heartbeats for a device
34
6
  export async function server_get_heartbeats_by_device(device_id, limit) {
35
- const supabase = await newServerClient();
36
- let query = supabase
37
- .from("heartbeats")
38
- .select("*")
39
- .eq("device_id", device_id)
40
- .order("timestamp", { ascending: false });
41
- if (limit) {
42
- query = query.limit(limit);
43
- }
44
- const { data, error } = await query;
45
- if (error) {
46
- return {
47
- status: EnumWebResponse.ERROR,
48
- msg: error.message,
49
- data: [],
50
- };
51
- }
52
- else {
53
- return IWebResponse.success(data || []).to_compatible();
54
- }
7
+ return get_heartbeats_by_device_query(await newServerClient(), device_id, limit);
55
8
  }
56
- // Function to check if a device is online based on heartbeat recency
57
9
  export async function server_check_device_online_status(device_id, offline_threshold_minutes = 5) {
58
- const supabase = await newServerClient();
59
- const { data, error } = await supabase
60
- .from("heartbeats")
61
- .select("*")
62
- .eq("device_id", device_id)
63
- .order("timestamp", { ascending: false })
64
- .limit(1)
65
- .single();
66
- if (error) {
67
- // If no heartbeats found
68
- if (error.code === "PGRST116") {
69
- return IWebResponse.success({
70
- is_online: false,
71
- last_heartbeat: null,
72
- minutes_since_last_heartbeat: null,
73
- }).to_compatible();
74
- }
75
- return {
76
- status: EnumWebResponse.ERROR,
77
- msg: error.message,
78
- data: null,
79
- };
80
- }
81
- const now = new Date();
82
- const heartbeatTime = new Date(data.timestamp);
83
- const minutesSinceLastHeartbeat = Math.floor((now.getTime() - heartbeatTime.getTime()) / (1000 * 60));
84
- const isOnline = minutesSinceLastHeartbeat <= offline_threshold_minutes;
85
- return IWebResponse.success({
86
- is_online: isOnline,
87
- last_heartbeat: data,
88
- minutes_since_last_heartbeat: minutesSinceLastHeartbeat,
89
- }).to_compatible();
10
+ return check_device_online_status_query(await newServerClient(), device_id, offline_threshold_minutes);
90
11
  }
@@ -0,0 +1,11 @@
1
+ import { SupabaseClient } from "@supabase/supabase-js";
2
+ import { IHeartbeat } from "../types/db";
3
+ import { IWebResponseCompatible } from "../types/requests";
4
+ import { Database } from "../types/supabase";
5
+ export declare function get_last_heartbeat_by_device_query(client: SupabaseClient<Database>, device_id: number): Promise<IWebResponseCompatible<IHeartbeat | null>>;
6
+ export declare function get_heartbeats_by_device_query(client: SupabaseClient<Database>, device_id: number, limit?: number): Promise<IWebResponseCompatible<IHeartbeat[]>>;
7
+ export declare function check_device_online_status_query(client: SupabaseClient<Database>, device_id: number, offline_threshold_minutes?: number): Promise<IWebResponseCompatible<{
8
+ is_online: boolean;
9
+ last_heartbeat: IHeartbeat | null;
10
+ minutes_since_last_heartbeat: number | null;
11
+ }>>;
@@ -0,0 +1,76 @@
1
+ import { EnumWebResponse, IWebResponse, } from "../types/requests";
2
+ export async function get_last_heartbeat_by_device_query(client, device_id) {
3
+ const { data, error } = await client
4
+ .from("heartbeats")
5
+ .select("*")
6
+ .eq("device_id", device_id)
7
+ .order("timestamp", { ascending: false })
8
+ .limit(1)
9
+ .single();
10
+ if (error) {
11
+ if (error.code === "PGRST116") {
12
+ return {
13
+ status: EnumWebResponse.SUCCESS,
14
+ msg: null,
15
+ data: null,
16
+ };
17
+ }
18
+ return {
19
+ status: EnumWebResponse.ERROR,
20
+ msg: error.message,
21
+ data: null,
22
+ };
23
+ }
24
+ return IWebResponse.success(data).to_compatible();
25
+ }
26
+ export async function get_heartbeats_by_device_query(client, device_id, limit) {
27
+ let query = client
28
+ .from("heartbeats")
29
+ .select("*")
30
+ .eq("device_id", device_id)
31
+ .order("timestamp", { ascending: false });
32
+ if (limit) {
33
+ query = query.limit(limit);
34
+ }
35
+ const { data, error } = await query;
36
+ if (error) {
37
+ return {
38
+ status: EnumWebResponse.ERROR,
39
+ msg: error.message,
40
+ data: [],
41
+ };
42
+ }
43
+ return IWebResponse.success(data || []).to_compatible();
44
+ }
45
+ export async function check_device_online_status_query(client, device_id, offline_threshold_minutes = 5) {
46
+ const { data, error } = await client
47
+ .from("heartbeats")
48
+ .select("*")
49
+ .eq("device_id", device_id)
50
+ .order("timestamp", { ascending: false })
51
+ .limit(1)
52
+ .single();
53
+ if (error) {
54
+ if (error.code === "PGRST116") {
55
+ return IWebResponse.success({
56
+ is_online: false,
57
+ last_heartbeat: null,
58
+ minutes_since_last_heartbeat: null,
59
+ }).to_compatible();
60
+ }
61
+ return {
62
+ status: EnumWebResponse.ERROR,
63
+ msg: error.message,
64
+ data: null,
65
+ };
66
+ }
67
+ const now = new Date();
68
+ const heartbeatTime = new Date(data.timestamp);
69
+ const minutesSinceLastHeartbeat = Math.floor((now.getTime() - heartbeatTime.getTime()) / (1000 * 60));
70
+ const isOnline = minutesSinceLastHeartbeat <= offline_threshold_minutes;
71
+ return IWebResponse.success({
72
+ is_online: isOnline,
73
+ last_heartbeat: data,
74
+ minutes_since_last_heartbeat: minutesSinceLastHeartbeat,
75
+ }).to_compatible();
76
+ }
@@ -1,15 +1,5 @@
1
- "use server";
2
1
  import { newServerClient } from "../supabase/server";
3
- import { IWebResponse } from "../types/requests";
2
+ import { get_herd_allowed_domains_query } from "./herd_allowed_domains.queries";
4
3
  export async function server_get_herd_allowed_domains(herd_id) {
5
- const supabase = await newServerClient();
6
- const { data, error } = await supabase
7
- .from("herd_allowed_domains")
8
- .select("*")
9
- .eq("herd_id", herd_id)
10
- .order("domain", { ascending: true });
11
- if (error) {
12
- return IWebResponse.error(error.message).to_compatible();
13
- }
14
- return IWebResponse.success(data ?? []).to_compatible();
4
+ return get_herd_allowed_domains_query(await newServerClient(), herd_id);
15
5
  }
@@ -2,4 +2,4 @@ import { SupabaseClient } from "@supabase/supabase-js";
2
2
  import { IHerdAllowedDomain } from "../types/db";
3
3
  import { IWebResponseCompatible } from "../types/requests";
4
4
  import { Database } from "../types/supabase";
5
- export declare function getHerdAllowedDomainsWithClient(supabase: SupabaseClient<Database>, herd_id: number): Promise<IWebResponseCompatible<IHerdAllowedDomain[]>>;
5
+ export declare function get_herd_allowed_domains_query(client: SupabaseClient<Database>, herd_id: number): Promise<IWebResponseCompatible<IHerdAllowedDomain[]>>;
@@ -1,6 +1,6 @@
1
1
  import { IWebResponse } from "../types/requests";
2
- export async function getHerdAllowedDomainsWithClient(supabase, herd_id) {
3
- const { data, error } = await supabase
2
+ export async function get_herd_allowed_domains_query(client, herd_id) {
3
+ const { data, error } = await client
4
4
  .from("herd_allowed_domains")
5
5
  .select("*")
6
6
  .eq("herd_id", herd_id)
@@ -0,0 +1,76 @@
1
+ import { CredentialQueryArgs, HerdCredentialInsert, HerdCredentialUpdate } from "../types/db";
2
+ export declare function server_get_herd_credentials_by_herd_id(herd_id: number, args?: CredentialQueryArgs): Promise<import("../types").IWebResponseCompatible<{
3
+ created_at: string;
4
+ expiration: string | null;
5
+ file_uri: string | null;
6
+ herd_id: number;
7
+ id: number;
8
+ issuer: string;
9
+ lifecycle: import("../types").Database["public"]["Enums"]["entity_lifecycle"];
10
+ lifecycle_changed_at: string;
11
+ lifecycle_changed_by: string | null;
12
+ lifecycle_reason: string | null;
13
+ tracking_number: string | null;
14
+ type: string;
15
+ updated_at: string;
16
+ }[]>>;
17
+ export declare function server_get_herd_credential_by_id(credential_id: number): Promise<import("../types").IWebResponseCompatible<{
18
+ created_at: string;
19
+ expiration: string | null;
20
+ file_uri: string | null;
21
+ herd_id: number;
22
+ id: number;
23
+ issuer: string;
24
+ lifecycle: import("../types").Database["public"]["Enums"]["entity_lifecycle"];
25
+ lifecycle_changed_at: string;
26
+ lifecycle_changed_by: string | null;
27
+ lifecycle_reason: string | null;
28
+ tracking_number: string | null;
29
+ type: string;
30
+ updated_at: string;
31
+ } | null>>;
32
+ export declare function server_create_herd_credential(row: HerdCredentialInsert): Promise<import("../types").IWebResponseCompatible<{
33
+ created_at: string;
34
+ expiration: string | null;
35
+ file_uri: string | null;
36
+ herd_id: number;
37
+ id: number;
38
+ issuer: string;
39
+ lifecycle: import("../types").Database["public"]["Enums"]["entity_lifecycle"];
40
+ lifecycle_changed_at: string;
41
+ lifecycle_changed_by: string | null;
42
+ lifecycle_reason: string | null;
43
+ tracking_number: string | null;
44
+ type: string;
45
+ updated_at: string;
46
+ } | null>>;
47
+ export declare function server_update_herd_credential(credential_id: number, patch: HerdCredentialUpdate): Promise<import("../types").IWebResponseCompatible<{
48
+ created_at: string;
49
+ expiration: string | null;
50
+ file_uri: string | null;
51
+ herd_id: number;
52
+ id: number;
53
+ issuer: string;
54
+ lifecycle: import("../types").Database["public"]["Enums"]["entity_lifecycle"];
55
+ lifecycle_changed_at: string;
56
+ lifecycle_changed_by: string | null;
57
+ lifecycle_reason: string | null;
58
+ tracking_number: string | null;
59
+ type: string;
60
+ updated_at: string;
61
+ } | null>>;
62
+ export declare function server_delete_herd_credential(credential_id: number): Promise<import("../types").IWebResponseCompatible<{
63
+ created_at: string;
64
+ expiration: string | null;
65
+ file_uri: string | null;
66
+ herd_id: number;
67
+ id: number;
68
+ issuer: string;
69
+ lifecycle: import("../types").Database["public"]["Enums"]["entity_lifecycle"];
70
+ lifecycle_changed_at: string;
71
+ lifecycle_changed_by: string | null;
72
+ lifecycle_reason: string | null;
73
+ tracking_number: string | null;
74
+ type: string;
75
+ updated_at: string;
76
+ } | null>>;
@@ -0,0 +1,22 @@
1
+ import { newServerClient } from "../supabase/server";
2
+ import { create_herd_credential, delete_herd_credential, get_herd_credential_by_id, get_herd_credentials_by_herd_id, update_herd_credential, } from "./herd_credentials";
3
+ export async function server_get_herd_credentials_by_herd_id(herd_id, args) {
4
+ const client = await newServerClient();
5
+ return get_herd_credentials_by_herd_id(client, herd_id, args);
6
+ }
7
+ export async function server_get_herd_credential_by_id(credential_id) {
8
+ const client = await newServerClient();
9
+ return get_herd_credential_by_id(client, credential_id);
10
+ }
11
+ export async function server_create_herd_credential(row) {
12
+ const client = await newServerClient();
13
+ return create_herd_credential(client, row);
14
+ }
15
+ export async function server_update_herd_credential(credential_id, patch) {
16
+ const client = await newServerClient();
17
+ return update_herd_credential(client, credential_id, patch);
18
+ }
19
+ export async function server_delete_herd_credential(credential_id) {
20
+ const client = await newServerClient();
21
+ return delete_herd_credential(client, credential_id);
22
+ }
@@ -1,4 +1,3 @@
1
- import { createHash } from "crypto";
2
1
  import { list_api_keys_batch_query } from "../api_keys/queries";
3
2
  import { LABELS } from "../constants/annotator";
4
3
  import { EnumWebResponse, } from "../types/requests";
@@ -9,10 +8,14 @@ import { get_parts_by_herd_ids } from "./parts";
9
8
  import { get_session_summaries_query } from "./session_summaries.queries";
10
9
  import { get_session_usage_over_time_by_herd_query } from "./sessions.queries";
11
10
  import { get_users_with_herd_access_batch_query } from "./users.queries";
12
- import { server_get_more_zones_and_actions_for_herd } from "./zones";
13
- function hashHerdModules(modules) {
11
+ import { get_more_zones_and_actions_for_herd_query } from "./zones.queries";
12
+ async function hashHerdModules(modules) {
14
13
  const stable = modules.map(({ timestamp_last_refreshed: _ts, ...rest }) => rest);
15
- return createHash("sha1").update(JSON.stringify(stable)).digest("hex");
14
+ const bytes = new TextEncoder().encode(JSON.stringify(stable));
15
+ const digest = await crypto.subtle.digest("SHA-1", bytes);
16
+ return Array.from(new Uint8Array(digest))
17
+ .map((byte) => byte.toString(16).padStart(2, "0"))
18
+ .join("");
16
19
  }
17
20
  export async function load_herd_modules_query(client) {
18
21
  const startTime = Date.now();
@@ -39,7 +42,7 @@ export async function load_herd_modules_query(client) {
39
42
  time_finished: endTime,
40
43
  time_sent: endTime,
41
44
  server_processing_time_ms: endTime - startTime,
42
- content_hash: hashHerdModules([]),
45
+ content_hash: await hashHerdModules([]),
43
46
  };
44
47
  }
45
48
  const herdIds = herds.map((herd) => herd.id);
@@ -59,7 +62,7 @@ export async function load_herd_modules_query(client) {
59
62
  .catch(() => null),
60
63
  grouped(get_parts_by_herd_ids(client, herdIds)),
61
64
  perHerd((id) => get_devices_by_herd_query(client, id)),
62
- perHerd((id) => server_get_more_zones_and_actions_for_herd(id, 0, 10, client)),
65
+ perHerd((id) => get_more_zones_and_actions_for_herd_query(client, id, 0, 10)),
63
66
  perHerd((id) => get_session_summaries_query(client, { herd_id: id })),
64
67
  perHerd((id) => get_session_usage_over_time_by_herd_query(client, id)),
65
68
  ]);
@@ -101,6 +104,6 @@ export async function load_herd_modules_query(client) {
101
104
  time_finished: endTime,
102
105
  time_sent: endTime,
103
106
  server_processing_time_ms: endTime - startTime,
104
- content_hash: hashHerdModules(serialized_herd_modules),
107
+ content_hash: await hashHerdModules(serialized_herd_modules),
105
108
  };
106
109
  }
@@ -1,173 +1,38 @@
1
- "use server";
2
- import { IWebResponse } from "../types/requests";
3
1
  import { newServerClient } from "../supabase/server";
2
+ import { create_herd_role_query, delete_herd_role_query, get_abilities_by_ids_query, get_herd_role_by_system_name_query, get_herd_roles_query, get_herd_roles_with_abilities_query, get_platform_default_role_abilities_query, list_abilities_query, reset_herd_default_role_abilities_query, set_herd_role_abilities_query, set_platform_default_role_abilities_query, update_herd_role_query, } from "./herd_roles.queries";
4
3
  export async function server_list_abilities() {
5
- const supabase = await newServerClient();
6
- const { data, error } = await supabase
7
- .from("abilities")
8
- .select("*")
9
- .order("key", { ascending: true });
10
- if (error) {
11
- return IWebResponse.error(error.message).to_compatible();
12
- }
13
- return IWebResponse.success(data ?? []).to_compatible();
4
+ return list_abilities_query(await newServerClient());
14
5
  }
15
6
  export async function server_get_abilities_by_ids(ids) {
16
- if (ids.length === 0) {
17
- return IWebResponse.success([]).to_compatible();
18
- }
19
- const supabase = await newServerClient();
20
- const { data, error } = await supabase
21
- .from("abilities")
22
- .select("*")
23
- .in("id", ids)
24
- .order("key", { ascending: true });
25
- if (error) {
26
- return IWebResponse.error(error.message).to_compatible();
27
- }
28
- return IWebResponse.success(data ?? []).to_compatible();
7
+ return get_abilities_by_ids_query(await newServerClient(), ids);
29
8
  }
30
9
  export async function server_get_herd_roles(herd_id) {
31
- const supabase = await newServerClient();
32
- const { data, error } = await supabase
33
- .from("herd_roles")
34
- .select("*")
35
- .eq("herd_id", herd_id)
36
- .order("system_name", { ascending: true });
37
- if (error) {
38
- return IWebResponse.error(error.message).to_compatible();
39
- }
40
- return IWebResponse.success(data ?? []).to_compatible();
10
+ return get_herd_roles_query(await newServerClient(), herd_id);
41
11
  }
42
12
  export async function server_get_herd_roles_with_abilities(herd_id) {
43
- const supabase = await newServerClient();
44
- const { data, error } = await supabase
45
- .from("herd_roles")
46
- .select(`
47
- *,
48
- herd_role_abilities (
49
- id,
50
- ability_id,
51
- abilities (*)
52
- )
53
- `)
54
- .eq("herd_id", herd_id)
55
- .order("system_name", { ascending: true });
56
- if (error) {
57
- return IWebResponse.error(error.message).to_compatible();
58
- }
59
- const rows = (data ?? []).map((row) => {
60
- const { herd_role_abilities: matrix, ...role } = row;
61
- const links = matrix ?? [];
62
- return {
63
- ...role,
64
- abilities: links
65
- .map((m) => m.abilities)
66
- .filter((a) => a != null),
67
- ability_ids: links.map((m) => m.ability_id),
68
- };
69
- });
70
- return IWebResponse.success(rows).to_compatible();
13
+ return get_herd_roles_with_abilities_query(await newServerClient(), herd_id);
71
14
  }
72
15
  export async function server_get_herd_role_by_system_name(herd_id, system_name) {
73
- const supabase = await newServerClient();
74
- const { data, error } = await supabase
75
- .from("herd_roles")
76
- .select("*")
77
- .eq("herd_id", herd_id)
78
- .eq("system_name", system_name)
79
- .maybeSingle();
80
- if (error) {
81
- return IWebResponse.error(error.message).to_compatible();
82
- }
83
- return IWebResponse.success(data).to_compatible();
16
+ return get_herd_role_by_system_name_query(await newServerClient(), herd_id, system_name);
84
17
  }
85
18
  export async function server_create_herd_role(herd_id, system_name, name) {
86
- const supabase = await newServerClient();
87
- const { data, error } = await supabase
88
- .from("herd_roles")
89
- .insert({ herd_id, system_name, name, is_system: false })
90
- .select("*")
91
- .single();
92
- if (error) {
93
- return IWebResponse.error(error.message).to_compatible();
94
- }
95
- return IWebResponse.success(data).to_compatible();
19
+ return create_herd_role_query(await newServerClient(), herd_id, system_name, name);
96
20
  }
97
21
  export async function server_update_herd_role(herd_role_id, patch) {
98
- const supabase = await newServerClient();
99
- const { data, error } = await supabase
100
- .from("herd_roles")
101
- .update(patch)
102
- .eq("id", herd_role_id)
103
- .select("*")
104
- .single();
105
- if (error) {
106
- return IWebResponse.error(error.message).to_compatible();
107
- }
108
- return IWebResponse.success(data).to_compatible();
22
+ return update_herd_role_query(await newServerClient(), herd_role_id, patch);
109
23
  }
110
24
  export async function server_delete_herd_role(herd_role_id) {
111
- const supabase = await newServerClient();
112
- const { error } = await supabase
113
- .from("herd_roles")
114
- .delete()
115
- .eq("id", herd_role_id);
116
- if (error) {
117
- return IWebResponse.error(error.message).to_compatible();
118
- }
119
- return IWebResponse.success(null).to_compatible();
25
+ return delete_herd_role_query(await newServerClient(), herd_role_id);
120
26
  }
121
27
  export async function server_set_herd_role_abilities(herd_role_id, ability_ids) {
122
- const supabase = await newServerClient();
123
- const { error: deleteError } = await supabase
124
- .from("herd_role_abilities")
125
- .delete()
126
- .eq("herd_role_id", herd_role_id);
127
- if (deleteError) {
128
- return IWebResponse.error(deleteError.message).to_compatible();
129
- }
130
- if (ability_ids.length === 0) {
131
- return IWebResponse.success([]).to_compatible();
132
- }
133
- const { data, error } = await supabase
134
- .from("herd_role_abilities")
135
- .insert(ability_ids.map((ability_id) => ({ herd_role_id, ability_id })))
136
- .select("*");
137
- if (error) {
138
- return IWebResponse.error(error.message).to_compatible();
139
- }
140
- return IWebResponse.success(data ?? []).to_compatible();
28
+ return set_herd_role_abilities_query(await newServerClient(), herd_role_id, ability_ids);
141
29
  }
142
30
  export async function server_get_platform_default_role_abilities() {
143
- const supabase = await newServerClient();
144
- const { data, error } = await supabase.rpc("get_platform_default_role_abilities");
145
- if (error) {
146
- return IWebResponse.error(error.message).to_compatible();
147
- }
148
- const groups = Array.isArray(data)
149
- ? data
150
- : [];
151
- return IWebResponse.success(groups).to_compatible();
31
+ return get_platform_default_role_abilities_query(await newServerClient());
152
32
  }
153
33
  export async function server_set_platform_default_role_abilities(system_name, ability_keys) {
154
- const supabase = await newServerClient();
155
- const { error } = await supabase.rpc("set_platform_default_role_abilities", {
156
- p_system_name: system_name,
157
- p_ability_keys: ability_keys,
158
- });
159
- if (error) {
160
- return IWebResponse.error(error.message).to_compatible();
161
- }
162
- return IWebResponse.success(null).to_compatible();
34
+ return set_platform_default_role_abilities_query(await newServerClient(), system_name, ability_keys);
163
35
  }
164
36
  export async function server_reset_herd_default_role_abilities(herd_id) {
165
- const supabase = await newServerClient();
166
- const { error } = await supabase.rpc("reset_herd_default_role_abilities", {
167
- p_herd_id: herd_id,
168
- });
169
- if (error) {
170
- return IWebResponse.error(error.message).to_compatible();
171
- }
172
- return IWebResponse.success(null).to_compatible();
37
+ return reset_herd_default_role_abilities_query(await newServerClient(), herd_id);
173
38
  }