@adventurelabs/scout-core
Version:
Core utilities and helpers for Adventure Labs Scout applications
156 lines (155 loc) • 5.54 kB
JavaScript
import { IWebResponse } from "../types/requests";
export async function get_components_by_device_id(client, device_id) {
const { data, error } = await client
.from("components")
.select("*")
.eq("device_id", device_id)
.order("created_at", { ascending: false });
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error("No components found for device").to_compatible();
}
return IWebResponse.success(data).to_compatible();
}
export async function get_component_by_id(client, component_id) {
const { data, error } = await client
.from("components")
.select("*")
.eq("id", component_id)
.single();
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error("Component not found").to_compatible();
}
return IWebResponse.success(data).to_compatible();
}
export async function get_components_by_serial_number(client, serial_number) {
const { data, error } = await client
.from("components")
.select("*")
.eq("serial_number", serial_number)
.order("created_at", { ascending: false });
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error(`No components found with serial number: ${serial_number}`).to_compatible();
}
return IWebResponse.success(data).to_compatible();
}
export async function get_components_by_product_number(client, product_number) {
const { data, error } = await client
.from("components")
.select("*")
.eq("product_number", product_number)
.order("created_at", { ascending: false });
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error(`No components found with product number: ${product_number}`).to_compatible();
}
return IWebResponse.success(data).to_compatible();
}
export async function get_components_by_status(client, status) {
const { data, error } = await client
.from("components")
.select("*")
.eq("status", status)
.order("created_at", { ascending: false });
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error(`No components found with status: ${status}`).to_compatible();
}
return IWebResponse.success(data).to_compatible();
}
export async function create_component(client, newComponent) {
// Validate required fields
if (!newComponent.device_id) {
return IWebResponse.error("Device ID is required").to_compatible();
}
if (!newComponent.serial_number) {
return IWebResponse.error("Serial number is required").to_compatible();
}
const { data, error } = await client
.from("components")
.insert([newComponent])
.select("*")
.single();
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error("Failed to create component").to_compatible();
}
return IWebResponse.success(data).to_compatible();
}
export async function update_component(client, component_id, updatedComponent) {
// Remove fields that shouldn't be updated
const updateData = { ...updatedComponent };
delete updateData.id;
delete updateData.created_at;
const { data, error } = await client
.from("components")
.update(updateData)
.eq("id", component_id)
.select("*")
.single();
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error("Component not found or update failed").to_compatible();
}
return IWebResponse.success(data).to_compatible();
}
export async function delete_component(client, component_id) {
const { data, error } = await client
.from("components")
.delete()
.eq("id", component_id)
.select("*")
.single();
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error("Component not found or deletion failed").to_compatible();
}
return IWebResponse.success(data).to_compatible();
}
export async function update_component_status(client, component_id, status) {
const { data, error } = await client
.from("components")
.update({ status })
.eq("id", component_id)
.select("*")
.single();
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error("Component not found or status update failed").to_compatible();
}
return IWebResponse.success(data).to_compatible();
}
export async function get_components_by_certificate_id(client, certificate_id) {
const { data, error } = await client
.from("components")
.select("*")
.eq("certificate_id", certificate_id)
.order("created_at", { ascending: false });
if (error) {
return IWebResponse.error(error.message).to_compatible();
}
if (!data) {
return IWebResponse.error(`No components found with certificate ID: ${certificate_id}`).to_compatible();
}
return IWebResponse.success(data).to_compatible();
}