@adventurelabs/scout-core 1.4.85 → 1.4.87

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,52 @@
1
+ "use server";
2
+ import { newServerClient } from "../supabase/server";
3
+ import { EnumWebResponse, IWebResponse, } from "../types/requests";
4
+ export async function server_get_manufacturers(options) {
5
+ const supabase = await newServerClient();
6
+ let query = supabase.from("manufacturers").select("*").order("name");
7
+ if (!options?.include_inactive) {
8
+ query = query.eq("lifecycle", "active");
9
+ }
10
+ const { data, error } = await query;
11
+ if (error) {
12
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: [] };
13
+ }
14
+ return IWebResponse.success(data ?? []).to_compatible();
15
+ }
16
+ export async function server_get_manufacturer_by_id(manufacturer_id) {
17
+ const supabase = await newServerClient();
18
+ const { data, error } = await supabase
19
+ .from("manufacturers")
20
+ .select("*")
21
+ .eq("id", manufacturer_id)
22
+ .maybeSingle();
23
+ if (error) {
24
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
25
+ }
26
+ return IWebResponse.success(data).to_compatible();
27
+ }
28
+ export async function server_create_manufacturer(row) {
29
+ const supabase = await newServerClient();
30
+ const { data, error } = await supabase
31
+ .from("manufacturers")
32
+ .insert(row)
33
+ .select("*")
34
+ .single();
35
+ if (error) {
36
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
37
+ }
38
+ return IWebResponse.success(data).to_compatible();
39
+ }
40
+ export async function server_update_manufacturer(manufacturer_id, updates) {
41
+ const supabase = await newServerClient();
42
+ const { data, error } = await supabase
43
+ .from("manufacturers")
44
+ .update(updates)
45
+ .eq("id", manufacturer_id)
46
+ .select("*")
47
+ .single();
48
+ if (error) {
49
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
50
+ }
51
+ return IWebResponse.success(data).to_compatible();
52
+ }
@@ -1,5 +1,5 @@
1
1
  import { Database } from "../types/supabase";
2
- import { EntityLifecycle, IPart, PartInsert } from "../types/db";
2
+ import { EntityLifecycle, IPart, IPartWithProduct, PartInsert } from "../types/db";
3
3
  import { IWebResponseCompatible } from "../types/requests";
4
4
  import { SupabaseClient } from "@supabase/supabase-js";
5
5
  import { LifecycleUpdateOptions } from "./lifecycle";
@@ -70,7 +70,7 @@ export declare function update_part_status(client: SupabaseClient<Database>, par
70
70
  */
71
71
  export declare function get_parts_by_certificate_id(client: SupabaseClient<Database>, certificate_id: number): Promise<IWebResponseCompatible<IPart[]>>;
72
72
  /**
73
- * Retrieves all active parts for devices in a specific herd
73
+ * Retrieves all active parts for a herd
74
74
  * @param client - Supabase client instance
75
75
  * @param herd_id - ID of the herd to get parts for
76
76
  */
@@ -81,7 +81,7 @@ export declare function get_parts_by_herd_id(client: SupabaseClient<Database>, h
81
81
  * @param client - Supabase client instance
82
82
  * @param herd_ids - IDs of the herds to get parts for
83
83
  */
84
- export declare function get_parts_by_herd_ids(client: SupabaseClient<Database>, herd_ids: number[]): Promise<IWebResponseCompatible<Record<number, IPart[]>>>;
84
+ export declare function get_parts_by_herd_ids(client: SupabaseClient<Database>, herd_ids: number[]): Promise<IWebResponseCompatible<Record<number, IPartWithProduct[]>>>;
85
85
  /**
86
86
  * Restores a deleted part to active lifecycle
87
87
  * @param client - Supabase client instance
@@ -216,27 +216,18 @@ export async function get_parts_by_certificate_id(client, certificate_id) {
216
216
  return IWebResponse.success(data).to_compatible();
217
217
  }
218
218
  /**
219
- * Retrieves all active parts for devices in a specific herd
219
+ * Retrieves all active parts for a herd
220
220
  * @param client - Supabase client instance
221
221
  * @param herd_id - ID of the herd to get parts for
222
222
  */
223
223
  export async function get_parts_by_herd_id(client, herd_id) {
224
- const { data, error } = await client
225
- .from("parts")
226
- .select(`
227
- *,
228
- devices!parts_device_id_fkey(herd_id)
229
- `)
230
- .eq("devices.herd_id", herd_id)
231
- .eq("lifecycle", "active")
232
- .order("created_at", { ascending: false });
224
+ const { data, error } = await client.rpc("get_parts_for_herd", {
225
+ herd_id_caller: herd_id,
226
+ });
233
227
  if (error) {
234
228
  return IWebResponse.error(error.message).to_compatible();
235
229
  }
236
- if (!data) {
237
- return IWebResponse.error(`No parts found for herd: ${herd_id}`).to_compatible();
238
- }
239
- return IWebResponse.success(data).to_compatible();
230
+ return IWebResponse.success(data ?? []).to_compatible();
240
231
  }
241
232
  /**
242
233
  * Retrieves active parts for all devices across many herds in one query,
@@ -253,7 +244,8 @@ export async function get_parts_by_herd_ids(client, herd_ids) {
253
244
  .from("parts")
254
245
  .select(`
255
246
  *,
256
- devices!parts_device_id_fkey(herd_id)
247
+ devices!parts_device_id_fkey(herd_id),
248
+ product_numbers!parts_product_number_fkey(products(*))
257
249
  `)
258
250
  .in("devices.herd_id", herd_ids)
259
251
  .eq("lifecycle", "active")
@@ -266,7 +258,12 @@ export async function get_parts_by_herd_ids(client, herd_ids) {
266
258
  for (const row of data ?? []) {
267
259
  if (row.device_id == null)
268
260
  continue;
269
- (grouped[_a = row.device_id] ?? (grouped[_a] = [])).push(row);
261
+ const { product_numbers, devices: _devices, ...partRow } = row;
262
+ const part = {
263
+ ...partRow,
264
+ product: product_numbers?.products ?? null,
265
+ };
266
+ (grouped[_a = row.device_id] ?? (grouped[_a] = [])).push(part);
270
267
  }
271
268
  return IWebResponse.success(grouped).to_compatible();
272
269
  }
@@ -0,0 +1,7 @@
1
+ import { IProduct, IProductNumber, ProductNumberInsert, ProductNumberUpdate } from "../types/db";
2
+ import { IWebResponseCompatible } from "../types/requests";
3
+ export declare function server_get_product_numbers_by_product(product_id: number): Promise<IWebResponseCompatible<IProductNumber[]>>;
4
+ export declare function server_get_product_number(product_number: string): Promise<IWebResponseCompatible<IProductNumber | null>>;
5
+ export declare function server_get_product_for_product_number(product_number: string): Promise<IWebResponseCompatible<IProduct | null>>;
6
+ export declare function server_create_product_number(row: ProductNumberInsert): Promise<IWebResponseCompatible<IProductNumber | null>>;
7
+ export declare function server_update_product_number(product_number: string, updates: ProductNumberUpdate): Promise<IWebResponseCompatible<IProductNumber | null>>;
@@ -0,0 +1,66 @@
1
+ "use server";
2
+ import { newServerClient } from "../supabase/server";
3
+ import { EnumWebResponse, IWebResponse, } from "../types/requests";
4
+ export async function server_get_product_numbers_by_product(product_id) {
5
+ const supabase = await newServerClient();
6
+ const { data, error } = await supabase
7
+ .from("product_numbers")
8
+ .select("*")
9
+ .eq("product_id", product_id)
10
+ .order("product_number");
11
+ if (error) {
12
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: [] };
13
+ }
14
+ return IWebResponse.success(data ?? []).to_compatible();
15
+ }
16
+ export async function server_get_product_number(product_number) {
17
+ const supabase = await newServerClient();
18
+ const { data, error } = await supabase
19
+ .from("product_numbers")
20
+ .select("*")
21
+ .eq("product_number", product_number)
22
+ .maybeSingle();
23
+ if (error) {
24
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
25
+ }
26
+ return IWebResponse.success(data).to_compatible();
27
+ }
28
+ // Resolves the product line for a given product_number via the FK chain.
29
+ export async function server_get_product_for_product_number(product_number) {
30
+ const supabase = await newServerClient();
31
+ const { data, error } = await supabase
32
+ .from("product_numbers")
33
+ .select("products(*)")
34
+ .eq("product_number", product_number)
35
+ .maybeSingle();
36
+ if (error) {
37
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
38
+ }
39
+ const product = data?.products ?? null;
40
+ return IWebResponse.success(product).to_compatible();
41
+ }
42
+ export async function server_create_product_number(row) {
43
+ const supabase = await newServerClient();
44
+ const { data, error } = await supabase
45
+ .from("product_numbers")
46
+ .insert(row)
47
+ .select("*")
48
+ .single();
49
+ if (error) {
50
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
51
+ }
52
+ return IWebResponse.success(data).to_compatible();
53
+ }
54
+ export async function server_update_product_number(product_number, updates) {
55
+ const supabase = await newServerClient();
56
+ const { data, error } = await supabase
57
+ .from("product_numbers")
58
+ .update(updates)
59
+ .eq("product_number", product_number)
60
+ .select("*")
61
+ .single();
62
+ if (error) {
63
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
64
+ }
65
+ return IWebResponse.success(data).to_compatible();
66
+ }
@@ -0,0 +1,9 @@
1
+ import { IProduct, ProductInsert, ProductUpdate } from "../types/db";
2
+ import { IWebResponseCompatible } from "../types/requests";
3
+ export declare function server_get_products(options?: {
4
+ include_inactive?: boolean;
5
+ }): Promise<IWebResponseCompatible<IProduct[]>>;
6
+ export declare function server_get_product_by_id(product_id: number): Promise<IWebResponseCompatible<IProduct | null>>;
7
+ export declare function server_get_products_by_manufacturer(manufacturer_id: number): Promise<IWebResponseCompatible<IProduct[]>>;
8
+ export declare function server_create_product(row: ProductInsert): Promise<IWebResponseCompatible<IProduct | null>>;
9
+ export declare function server_update_product(product_id: number, updates: ProductUpdate): Promise<IWebResponseCompatible<IProduct | null>>;
@@ -0,0 +1,65 @@
1
+ "use server";
2
+ import { newServerClient } from "../supabase/server";
3
+ import { EnumWebResponse, IWebResponse, } from "../types/requests";
4
+ export async function server_get_products(options) {
5
+ const supabase = await newServerClient();
6
+ let query = supabase.from("products").select("*").order("name");
7
+ if (!options?.include_inactive) {
8
+ query = query.eq("lifecycle", "active");
9
+ }
10
+ const { data, error } = await query;
11
+ if (error) {
12
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: [] };
13
+ }
14
+ return IWebResponse.success(data ?? []).to_compatible();
15
+ }
16
+ export async function server_get_product_by_id(product_id) {
17
+ const supabase = await newServerClient();
18
+ const { data, error } = await supabase
19
+ .from("products")
20
+ .select("*")
21
+ .eq("id", product_id)
22
+ .maybeSingle();
23
+ if (error) {
24
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
25
+ }
26
+ return IWebResponse.success(data).to_compatible();
27
+ }
28
+ export async function server_get_products_by_manufacturer(manufacturer_id) {
29
+ const supabase = await newServerClient();
30
+ const { data, error } = await supabase
31
+ .from("products")
32
+ .select("*")
33
+ .eq("manufacturer_id", manufacturer_id)
34
+ .eq("lifecycle", "active")
35
+ .order("name");
36
+ if (error) {
37
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: [] };
38
+ }
39
+ return IWebResponse.success(data ?? []).to_compatible();
40
+ }
41
+ export async function server_create_product(row) {
42
+ const supabase = await newServerClient();
43
+ const { data, error } = await supabase
44
+ .from("products")
45
+ .insert(row)
46
+ .select("*")
47
+ .single();
48
+ if (error) {
49
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
50
+ }
51
+ return IWebResponse.success(data).to_compatible();
52
+ }
53
+ export async function server_update_product(product_id, updates) {
54
+ const supabase = await newServerClient();
55
+ const { data, error } = await supabase
56
+ .from("products")
57
+ .update(updates)
58
+ .eq("id", product_id)
59
+ .select("*")
60
+ .single();
61
+ if (error) {
62
+ return { status: EnumWebResponse.ERROR, msg: error.message, data: null };
63
+ }
64
+ return IWebResponse.success(data).to_compatible();
65
+ }
package/dist/index.d.ts CHANGED
@@ -61,6 +61,15 @@ export * from "./helpers/operators";
61
61
  export * from "./helpers/versions_software";
62
62
  export * from "./helpers/versions_software_server";
63
63
  export * from "./helpers/parts";
64
+ export * from "./helpers/manufacturers";
65
+ export * from "./helpers/products";
66
+ export * from "./helpers/product_numbers";
67
+ export * from "./helpers/localizations";
68
+ export * from "./helpers/issuers";
69
+ export * from "./helpers/document_templates";
70
+ export * from "./helpers/documents";
71
+ export * from "./helpers/maintenance_requests";
72
+ export * from "./helpers/contacts";
64
73
  export * from "./helpers/lifecycle";
65
74
  export * from "./helpers/storagePath";
66
75
  export * from "./hooks/useScoutRealtimeConnectivity";
@@ -84,5 +93,5 @@ export * from "./supabase/middleware";
84
93
  export * from "./supabase/server";
85
94
  export * from "./api_keys/actions";
86
95
  export type { IHerdModule } from "./types/herd_module";
87
- export type { IDevice, IEvent, IUser, IHerd, IHerdPrettyLocation, IEventWithTags, IZoneWithActions, IPointsForUser, PointsForUserInsert, PointsForUserUpdate, ICertificate, CertificateInsert, CertificateUpdate, IUserAndRole, IUserProfileRow, IHerdInvitation, IHerdInvitationWithHerdSlug, IHerdAllowedDomain, IUserRolePerHerd, IAbility, IHerdRole, IHerdRoleWithAbilities, HerdRoleSystemName, HerdInvitationStatus, IApiKeyScout, ILayer, IHeartbeat, IProvider, IConnectivity, ISession, ISessionWithCoordinates, IConnectivityWithCoordinates, IObservation, ObservationInsert, ObservationUpdate, IAnalysisJob, IAnalysisTask, AnalysisWorkStatus, } from "./types/db";
96
+ export type { IDevice, IEvent, IUser, IHerd, IHerdPrettyLocation, IEventWithTags, IZoneWithActions, IPointsForUser, PointsForUserInsert, PointsForUserUpdate, ICertificate, CertificateInsert, CertificateUpdate, ILocalization, LocalizationInsert, LocalizationUpdate, IIssuer, IssuerInsert, IssuerUpdate, IDocumentTemplate, DocumentTemplateInsert, DocumentTemplateUpdate, IDocument, DocumentInsert, DocumentUpdate, IMaintenanceRequest, MaintenanceRequestInsert, MaintenanceRequestUpdate, IContact, ContactInsert, ContactUpdate, IUserAndRole, IUserProfileRow, IHerdInvitation, IHerdInvitationWithHerdSlug, IHerdAllowedDomain, IUserRolePerHerd, IAbility, IHerdRole, IHerdRoleWithAbilities, HerdRoleSystemName, HerdInvitationStatus, IApiKeyScout, ILayer, IHeartbeat, IProvider, IConnectivity, ISession, ISessionWithCoordinates, IConnectivityWithCoordinates, IObservation, ObservationInsert, ObservationUpdate, IAnalysisJob, IAnalysisTask, AnalysisWorkStatus, } from "./types/db";
88
97
  export { EnumSessionsVisibility } from "./types/events";
package/dist/index.js CHANGED
@@ -64,6 +64,15 @@ export * from "./helpers/operators";
64
64
  export * from "./helpers/versions_software";
65
65
  export * from "./helpers/versions_software_server";
66
66
  export * from "./helpers/parts";
67
+ export * from "./helpers/manufacturers";
68
+ export * from "./helpers/products";
69
+ export * from "./helpers/product_numbers";
70
+ export * from "./helpers/localizations";
71
+ export * from "./helpers/issuers";
72
+ export * from "./helpers/document_templates";
73
+ export * from "./helpers/documents";
74
+ export * from "./helpers/maintenance_requests";
75
+ export * from "./helpers/contacts";
67
76
  export * from "./helpers/lifecycle";
68
77
  export * from "./helpers/storagePath";
69
78
  // Hooks
@@ -13,7 +13,7 @@ export type IUserProfileRow = Database["public"]["Tables"]["users"]["Row"];
13
13
  export type IDeviceRow = Database["public"]["Tables"]["devices"]["Row"];
14
14
  export type IDevice = Database["public"]["CompositeTypes"]["device_pretty_location"] & {
15
15
  api_keys_scout?: IApiKeyScout[];
16
- parts?: IPart[];
16
+ parts?: IPartWithProduct[];
17
17
  };
18
18
  export type IPin = Database["public"]["CompositeTypes"]["pins_pretty_location"];
19
19
  export type IEvent = Database["public"]["Tables"]["events"]["Row"];
@@ -58,6 +58,21 @@ export type IOperator = Database["public"]["Tables"]["operators"]["Row"];
58
58
  export type IObservation = Database["public"]["Tables"]["observations"]["Row"];
59
59
  export type IProvider = Database["public"]["Tables"]["providers"]["Row"];
60
60
  export type IPart = Database["public"]["Tables"]["parts"]["Row"];
61
+ export type IManufacturer = Database["public"]["Tables"]["manufacturers"]["Row"];
62
+ export type IProduct = Database["public"]["Tables"]["products"]["Row"];
63
+ export type IProductNumber = Database["public"]["Tables"]["product_numbers"]["Row"];
64
+ export type ISensorPose = Database["public"]["Tables"]["sensor_poses"]["Row"];
65
+ export type ICalibration = Database["public"]["Tables"]["calibrations"]["Row"];
66
+ export type ILocalization = Database["public"]["Tables"]["localizations"]["Row"];
67
+ export type IIssuer = Database["public"]["Tables"]["issuers"]["Row"];
68
+ export type IDocumentTemplate = Database["public"]["Tables"]["document_templates"]["Row"];
69
+ export type IDocument = Database["public"]["Tables"]["documents"]["Row"];
70
+ export type IMaintenanceRequest = Database["public"]["Tables"]["maintenance_requests"]["Row"];
71
+ export type IContact = Database["public"]["Tables"]["contacts"]["Row"];
72
+ /** A part with its resolved product (parts.product_number -> product_numbers -> products). */
73
+ export type IPartWithProduct = IPart & {
74
+ product?: IProduct | null;
75
+ };
61
76
  export type IUserCredential = Database["public"]["Tables"]["user_credentials"]["Row"];
62
77
  export type IHerdCredential = Database["public"]["Tables"]["herd_credentials"]["Row"];
63
78
  export type IPointsForUser = Database["public"]["Tables"]["points_for_user"]["Row"];
@@ -101,10 +116,32 @@ export interface ISessionSummary {
101
116
  }
102
117
  export type ISessionUsageOverTime = Database["public"]["Functions"]["get_session_usage_over_time"]["Returns"];
103
118
  export type PartInsert = Database["public"]["Tables"]["parts"]["Insert"];
119
+ export type ManufacturerInsert = Database["public"]["Tables"]["manufacturers"]["Insert"];
120
+ export type ManufacturerUpdate = Database["public"]["Tables"]["manufacturers"]["Update"];
121
+ export type ProductInsert = Database["public"]["Tables"]["products"]["Insert"];
122
+ export type ProductUpdate = Database["public"]["Tables"]["products"]["Update"];
123
+ export type ProductNumberInsert = Database["public"]["Tables"]["product_numbers"]["Insert"];
124
+ export type ProductNumberUpdate = Database["public"]["Tables"]["product_numbers"]["Update"];
125
+ export type SensorPoseInsert = Database["public"]["Tables"]["sensor_poses"]["Insert"];
126
+ export type SensorPoseUpdate = Database["public"]["Tables"]["sensor_poses"]["Update"];
127
+ export type CalibrationInsert = Database["public"]["Tables"]["calibrations"]["Insert"];
128
+ export type CalibrationUpdate = Database["public"]["Tables"]["calibrations"]["Update"];
104
129
  export type UserCredentialInsert = Database["public"]["Tables"]["user_credentials"]["Insert"];
105
130
  export type UserCredentialUpdate = Database["public"]["Tables"]["user_credentials"]["Update"];
106
131
  export type HerdCredentialInsert = Database["public"]["Tables"]["herd_credentials"]["Insert"];
107
132
  export type HerdCredentialUpdate = Database["public"]["Tables"]["herd_credentials"]["Update"];
133
+ export type LocalizationInsert = Database["public"]["Tables"]["localizations"]["Insert"];
134
+ export type LocalizationUpdate = Database["public"]["Tables"]["localizations"]["Update"];
135
+ export type IssuerInsert = Database["public"]["Tables"]["issuers"]["Insert"];
136
+ export type IssuerUpdate = Database["public"]["Tables"]["issuers"]["Update"];
137
+ export type DocumentTemplateInsert = Database["public"]["Tables"]["document_templates"]["Insert"];
138
+ export type DocumentTemplateUpdate = Database["public"]["Tables"]["document_templates"]["Update"];
139
+ export type DocumentInsert = Database["public"]["Tables"]["documents"]["Insert"];
140
+ export type DocumentUpdate = Database["public"]["Tables"]["documents"]["Update"];
141
+ export type MaintenanceRequestInsert = Database["public"]["Tables"]["maintenance_requests"]["Insert"];
142
+ export type MaintenanceRequestUpdate = Database["public"]["Tables"]["maintenance_requests"]["Update"];
143
+ export type ContactInsert = Database["public"]["Tables"]["contacts"]["Insert"];
144
+ export type ContactUpdate = Database["public"]["Tables"]["contacts"]["Update"];
108
145
  export interface CredentialQueryArgs {
109
146
  type?: string;
110
147
  }