Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: add employees to realtime data provider

Brad Barbinf85c30a39da1
diff --git a/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx b/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx
index 4d9e45a63b..858b9a90aa 100644
--- a/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx
+++ b/apps/carbon/app/modules/shared/ui/Realtime/RealtimeDataProvider.tsx
@@ -1,7 +1,7 @@
import idb from "localforage";
import { useEffect } from "react";
import { useSupabase } from "~/lib/supabase";
-import { useCustomers, useParts, useSuppliers } from "~/stores";
+import { useCustomers, useParts, usePeople, useSuppliers } from "~/stores";
import type { Part } from "~/stores/parts";
import type { ListItem } from "~/types";
@@ -14,6 +14,7 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
const [, setParts] = useParts();
const [, setSuppliers] = useSuppliers();
const [, setCustomers] = useCustomers();
+ const [, setPeople] = usePeople();
const hydrate = async () => {
if (!hydratedFromIdb) {
@@ -28,11 +29,14 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
idb.getItem("suppliers").then((data) => {
if (data && !hydratedFromServer) setSuppliers(data as ListItem[], true);
});
+ idb.getItem("people").then((data) => {
+ if (data && !hydratedFromServer) setPeople(data as ListItem[], true);
+ });
}
if (!supabase || !accessToken) return;
- const [parts, suppliers, customers] = await Promise.all([
+ const [parts, suppliers, customers, people] = await Promise.all([
supabase
.from("part")
.select("id, name, replenishmentSystem")
@@ -41,9 +45,10 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
.order("name"),
supabase.from("supplier").select("id, name").order("name"),
supabase.from("customer").select("id, name").order("name"),
+ supabase.from("employees").select("id, name, avatarUrl").order("name"),
]);
- if (parts.error || suppliers.error || customers.error) {
+ if (parts.error || suppliers.error || customers.error || people.error) {
throw new Error("Failed to fetch core data");
}
@@ -52,6 +57,7 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
setParts(parts.data ?? []);
setSuppliers(suppliers.data ?? []);
setCustomers(customers.data ?? []);
+ setPeople(people.data ?? []);
};
useEffect(() => {
@@ -205,6 +211,26 @@ const RealtimeDataProvider = ({ children }: { children: React.ReactNode }) => {
}
}
)
+ .on(
+ "postgres_changes",
+ {
+ event: "*",
+ schema: "public",
+ table: "employee",
+ },
+ async (payload) => {
+ // TODO: there's a cleaner way of doing this, but since customers and suppliers
+ // are also in the users table, we can't automatically add/update/delete them
+ // from our list of employees. So for now we just refetch.
+ const { data } = await supabase
+ .from("employees")
+ .select("id, name, avatarUrl")
+ .order("name");
+ if (data) {
+ setPeople(data ?? []);
+ }
+ }
+ )
.subscribe();
return () => {
diff --git a/apps/carbon/app/stores/customers.ts b/apps/carbon/app/stores/customers.ts
index e41e5b0992..3cba9f97c3 100644
--- a/apps/carbon/app/stores/customers.ts
+++ b/apps/carbon/app/stores/customers.ts
@@ -3,4 +3,4 @@ import { useNanoStore } from "~/hooks";
import type { ListItem } from "~/types";
const $customersStore = atom<ListItem[]>([]);
-export const useCustomers = () => useNanoStore($customersStore, "customer");
+export const useCustomers = () => useNanoStore($customersStore, "customers");
diff --git a/apps/carbon/app/stores/index.ts b/apps/carbon/app/stores/index.ts
index 3e6542ec72..44e13ab31d 100644
--- a/apps/carbon/app/stores/index.ts
+++ b/apps/carbon/app/stores/index.ts
@@ -1,3 +1,4 @@
export * from "./customers";
export * from "./parts";
+export * from "./people";
export * from "./suppliers";
diff --git a/apps/carbon/app/stores/parts.ts b/apps/carbon/app/stores/parts.ts
index f0353b537c..60892113a0 100644
--- a/apps/carbon/app/stores/parts.ts
+++ b/apps/carbon/app/stores/parts.ts
@@ -17,6 +17,6 @@ const $manufacturedPartsStore = computed($partsStore, (part) =>
part.filter((i) => i.replenishmentSystem === "Make")
);
-export const useParts = () => useNanoStore<Part[]>($partsStore, "part");
+export const useParts = () => useNanoStore<Part[]>($partsStore, "parts");
export const usePurchasedParts = () => useValue($purchasedPartsStore);
export const useManufacturedParts = () => useValue($manufacturedPartsStore);
diff --git a/apps/carbon/app/stores/people.ts b/apps/carbon/app/stores/people.ts
new file mode 100644
index 0000000000..434e2af48d
--- /dev/null
+++ b/apps/carbon/app/stores/people.ts
@@ -0,0 +1,6 @@
+import { atom } from "nanostores";
+import { useNanoStore } from "~/hooks";
+import type { ListItem } from "~/types";
+
+const $peopleStore = atom<ListItem[]>([]);
+export const usePeople = () => useNanoStore($peopleStore, "people");
diff --git a/apps/carbon/app/stores/suppliers.ts b/apps/carbon/app/stores/suppliers.ts
index 9bc86a4c49..f25c1ad281 100644
--- a/apps/carbon/app/stores/suppliers.ts
+++ b/apps/carbon/app/stores/suppliers.ts
@@ -3,4 +3,4 @@ import { useNanoStore } from "~/hooks";
import type { ListItem } from "~/types";
const $suppliersStore = atom<ListItem[]>([]);
-export const useSuppliers = () => useNanoStore($suppliersStore, "supplier");
+export const useSuppliers = () => useNanoStore($suppliersStore, "suppliers");
diff --git a/packages/database/supabase/migrations/20240323161015_employees-view.sql b/packages/database/supabase/migrations/20240323161015_employees-view.sql
new file mode 100644
index 0000000000..6e4c3b65a5
--- /dev/null
+++ b/packages/database/supabase/migrations/20240323161015_employees-view.sql
@@ -0,0 +1,9 @@
+CREATE OR REPLACE VIEW "employees" WITH(SECURITY_INVOKER=true) AS
+ SELECT
+ u.id,
+ u."fullName" AS "name",
+ u."avatarUrl"
+ FROM "user" u
+ INNER JOIN "employee" e
+ ON e.id = u.id
+ WHERE u.active = TRUE;