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 file type to attributes

Brad Barbinb4d937f8f7ee
diff --git a/apps/erp/app/modules/account/account.models.ts b/apps/erp/app/modules/account/account.models.ts
index a1b1dfafcc..2a1c1255ce 100644
--- a/apps/erp/app/modules/account/account.models.ts
+++ b/apps/erp/app/modules/account/account.models.ts
@@ -1,5 +1,5 @@
-import { zfd } from "zod-form-data";
import { z } from "zod/v3";
+import { zfd } from "zod-form-data";
export const onboardingUserValidator = z.object({
firstName: z.string().min(1, { message: "First name is required" }),
@@ -71,6 +71,11 @@ export const attributeSupplierValidator = z.object({
value: z.string().min(1, { message: "Supplier is required" })
});
+export const attributeFileValidator = z.object({
+ ...attributeDefaults,
+ value: z.string().min(1, { message: "File is required" })
+});
+
export const deleteUserAttributeValueValidator = z.object({
userAttributeId: z.string().min(20),
userAttributeValueId: z.string().min(20)
diff --git a/apps/erp/app/modules/account/account.service.ts b/apps/erp/app/modules/account/account.service.ts
index 7e940aecb5..7c0f590ad8 100644
--- a/apps/erp/app/modules/account/account.service.ts
+++ b/apps/erp/app/modules/account/account.service.ts
@@ -33,9 +33,9 @@ export async function getAttributes(
.select(
`id, name, emoji, companyId,
userAttribute(id, name, listOptions, canSelfManage,
- attributeDataType(id, isBoolean, isDate, isNumeric, isText, isUser),
+ attributeDataType(id, isBoolean, isDate, isNumeric, isText, isUser, isFile),
userAttributeValue(
- id, valueBoolean, valueDate, valueNumeric, valueText, valueUser
+ id, valueBoolean, valueDate, valueNumeric, valueText, valueUser, valueFile
)
)`
)
@@ -73,9 +73,9 @@ export async function getAllAttributeCategories(
.select(
`id, name, emoji, companyId,
userAttribute(id, name, listOptions, canSelfManage,
- attributeDataType(id, isBoolean, isDate, isNumeric, isText, isUser),
+ attributeDataType(id, isBoolean, isDate, isNumeric, isText, isUser, isFile),
userAttributeValue(
- id, valueBoolean, valueDate, valueNumeric, valueText, valueUser
+ id, valueBoolean, valueDate, valueNumeric, valueText, valueUser, valueFile
)
)`
)
@@ -97,9 +97,9 @@ export async function getAttributeCategoryWithValues(
.select(
`id, name, emoji, companyId, public,
userAttribute(id, name, listOptions, canSelfManage,
- attributeDataType(id, isBoolean, isDate, isNumeric, isText, isUser),
+ attributeDataType(id, isBoolean, isDate, isNumeric, isText, isUser, isFile),
userAttributeValue(
- id, valueBoolean, valueDate, valueNumeric, valueText, valueUser
+ id, valueBoolean, valueDate, valueNumeric, valueText, valueUser, valueFile
)
)`
)
@@ -193,6 +193,10 @@ export async function upsertUserAttributeValue(
valueUpdate = { valueText: value };
}
+ if (type === "file" && typeof value === "string") {
+ valueUpdate = { valueFile: value };
+ }
+
if (userAttributeValueId) {
return client
.from("userAttributeValue")
diff --git a/apps/erp/app/modules/account/ui/UserAttributes/UserAttributesForm.tsx b/apps/erp/app/modules/account/ui/UserAttributes/UserAttributesForm.tsx
index 5183957e1c..d802d88431 100644
--- a/apps/erp/app/modules/account/ui/UserAttributes/UserAttributesForm.tsx
+++ b/apps/erp/app/modules/account/ui/UserAttributes/UserAttributesForm.tsx
@@ -1,10 +1,21 @@
+import { useCarbon } from "@carbon/auth";
import { ValidatedForm } from "@carbon/form";
-import { Button, HStack, Switch, VStack, useDisclosure } from "@carbon/react";
+import {
+ Button,
+ HStack,
+ Switch,
+ toast,
+ useDisclosure,
+ VStack
+} from "@carbon/react";
import { parseDate } from "@internationalized/date";
import { useLocale } from "@react-aria/i18n";
import { useFetcher, useParams } from "@remix-run/react";
import { useState } from "react";
+import { LuFile, LuPaperclip } from "react-icons/lu";
import type { ZodSchema } from "zod/v3";
+import CustomerAvatar from "~/components/CustomerAvatar";
+import FileDropzone from "~/components/FileDropzone";
import {
Boolean as BooleanInput,
Customer,
@@ -18,14 +29,14 @@ import {
Supplier
} from "~/components/Form";
import { UserSelect } from "~/components/Selectors";
-import CustomerAvatar from "~/components/CustomerAvatar";
import SupplierAvatar from "~/components/SupplierAvatar";
import { usePermissions, useUser } from "~/hooks";
import { DataType } from "~/modules/shared";
-import { path } from "~/utils/path";
+import { getPrivateUrl, path } from "~/utils/path";
import {
attributeBooleanValidator,
attributeCustomerValidator,
+ attributeFileValidator,
attributeNumericValidator,
attributeSupplierValidator,
attributeTextValidator,
@@ -423,6 +434,19 @@ function TypedForm(
</div>
</ValidatedForm>
);
+ case DataType.File:
+ return (
+ <FileAttributeForm
+ attribute={attribute}
+ userAttributeId={userAttributeId}
+ userAttributeValueId={userAttributeValueId}
+ value={value}
+ updateFetcher={updateFetcher}
+ userId={userId}
+ onSubmit={onSubmit}
+ onClose={onClose}
+ />
+ );
default:
return (
<div className="text-destructive bg-destructive-foreground p-4 w-full">
@@ -557,6 +581,36 @@ function TypedDisplay(
<p className="self-center">{displayValue}</p>
)}
+ <UpdateRemoveButtons
+ canRemove={
+ !isAuthorized || (attribute.canSelfManage === true && !!value)
+ }
+ canUpdate={!isAuthorized || (attribute.canSelfManage ?? false)}
+ {...props}
+ onSubmit={setOptimisticUpdate}
+ />
+ </div>
+ );
+ case DataType.File:
+ return (
+ <div className="grid grid-cols-[1fr_2fr_1fr] border-t border-border gap-x-2 pt-3 w-full items-center">
+ <p className="text-muted-foreground self-center">{attribute.name}</p>
+ {value ? (
+ <a
+ href={getPrivateUrl(value.toString())}
+ target="_blank"
+ rel="noopener noreferrer"
+ className="flex items-center gap-2 text-sm text-primary hover:underline"
+ >
+ <LuPaperclip className="size-4" />
+ <span className="truncate max-w-[200px]">
+ {value.toString().split("/").pop()}
+ </span>
+ </a>
+ ) : (
+ <p className="self-center">{displayValue}</p>
+ )}
+
<UpdateRemoveButtons
canRemove={
!isAuthorized || (attribute.canSelfManage === true && !!value)
@@ -641,6 +695,11 @@ function getGenericProps(
value = userAttributeValue.valueText;
if (userAttributeValue.valueText)
displayValue = userAttributeValue.valueText;
+ break;
+ case DataType.File:
+ value = userAttributeValue.valueFile;
+ if (userAttributeValue.valueFile)
+ displayValue = userAttributeValue.valueFile;
}
}
@@ -658,6 +717,117 @@ function getGenericProps(
};
}
+function FileAttributeForm({
+ attribute,
+ userAttributeId,
+ userAttributeValueId,
+ value,
+ updateFetcher,
+ userId,
+ onSubmit,
+ onClose
+}: {
+ attribute: {
+ id: string | null;
+ name: string | null;
+ canSelfManage: boolean | null;
+ listOptions: string[] | null;
+ };
+ userAttributeId: string;
+ userAttributeValueId?: string;
+ value: Date | string | number | boolean | null;
+ updateFetcher: ReturnType<typeof useFetcher>;
+ userId: string;
+ onSubmit: (value: string | boolean | number) => void;
+ onClose: () => void;
+}) {
+ const { carbon } = useCarbon();
+ const { company } = useUser();
+ const [file, setFile] = useState<File | null>(null);
+ const [filePath, setFilePath] = useState<string | null>(
+ value?.toString() ?? null
+ );
+
+ const onDrop = async (acceptedFiles: File[]) => {
+ if (!acceptedFiles[0] || !carbon) return;
+ const fileUpload = acceptedFiles[0];
+
+ setFile(fileUpload);
+ toast.info(`Uploading ${fileUpload.name}`);
+
+ const fileName = `${company.id}/person/${userId}/${fileUpload.name}`;
+
+ const upload = await carbon?.storage
+ .from("private")
+ .upload(fileName, fileUpload, {
+ cacheControl: `${12 * 60 * 60}`,
+ upsert: true
+ });
+
+ if (upload.error) {
+ toast.error(`Failed to upload file: ${fileUpload.name}`);
+ } else if (upload.data?.path) {
+ toast.success(`Uploaded: ${fileUpload.name}`);
+ setFilePath(upload.data.path);
+ }
+ };
+
+ return (
+ <ValidatedForm
+ method="post"
+ action={path.to.userAttribute(userId)}
+ validator={attributeFileValidator}
+ defaultValues={{
+ userAttributeId,
+ userAttributeValueId,
+ value: filePath ?? ""
+ }}
+ fetcher={updateFetcher}
+ onSubmit={() => {
+ if (filePath) onSubmit(filePath);
+ }}
+ >
+ <div className="grid grid-cols-[1fr_2fr_1fr] border-t border-border gap-x-2 pt-3 w-full items-center">
+ <p className="text-muted-foreground self-center">{attribute.name}</p>
+ <div>
+ <Hidden name="type" value="file" />
+ <Hidden name="userAttributeId" />
+ <Hidden name="userAttributeValueId" />
+ <Hidden name="value" value={filePath ?? ""} />
+ {file || filePath ? (
+ <div className="flex flex-col gap-2 items-center justify-center py-4 w-full">
+ <LuFile className="size-8 text-muted-foreground" />
+ <p className="text-sm text-muted-foreground truncate max-w-[200px]">
+ {file?.name ?? filePath?.split("/").pop()}
+ </p>
+ <Button
+ variant="secondary"
+ size="sm"
+ onClick={() => {
+ setFile(null);
+ setFilePath(null);
+ }}
+ >
+ Remove
+ </Button>
+ </div>
+ ) : (
+ <FileDropzone onDrop={onDrop} />
+ )}
+ </div>
+ <HStack className="justify-end w-full self-center">
+ <Submit type="submit" isDisabled={!filePath}>
+ Save
+ </Submit>
+ <Button variant="ghost" onClick={onClose}>
+ Cancel
+ </Button>
+ </HStack>
+ </div>
+ </ValidatedForm>
+ );
+}
+
function UpdateRemoveButtons({
canRemove,
canUpdate,
diff --git a/apps/erp/app/modules/people/people.service.ts b/apps/erp/app/modules/people/people.service.ts
index 9df0deb347..42624bf20f 100644
--- a/apps/erp/app/modules/people/people.service.ts
+++ b/apps/erp/app/modules/people/people.service.ts
@@ -78,9 +78,9 @@ async function getAttributes(
.select(
`*,
userAttribute(id, name, listOptions, canSelfManage,
- attributeDataType(id, isBoolean, isDate, isNumeric, isText, isUser),
+ attributeDataType(id, isBoolean, isDate, isNumeric, isText, isUser, isFile),
userAttributeValue(
- id, userId, valueBoolean, valueDate, valueNumeric, valueText, valueUser, user!userAttributeValue_userId_fkey(id, fullName, avatarUrl)
+ id, userId, valueBoolean, valueDate, valueNumeric, valueText, valueUser, valueFile, user!userAttributeValue_userId_fkey(id, fullName, avatarUrl)
)
)`
)
@@ -124,10 +124,10 @@ export async function getAttributeCategory(
return client
.from("userAttributeCategory")
.select(
- `*,
+ `*,
userAttribute(
- id, name, sortOrder,
- attributeDataType(id, label, isBoolean, isDate, isList, isNumeric, isText, isUser ))
+ id, name, sortOrder,
+ attributeDataType(id, label, isBoolean, isDate, isList, isNumeric, isText, isUser, isFile))
`,
{
count: "exact"
@@ -316,7 +316,8 @@ export async function getPeople(
: userAttributeValue?.valueDate ||
userAttributeValue?.valueNumeric ||
userAttributeValue?.valueText ||
- userAttributeValue?.valueUser;
+ userAttributeValue?.valueUser ||
+ userAttributeValue?.valueFile;
if (value && userAttributeValue?.id) {
acc[userAttributeId] = {
diff --git a/apps/erp/app/modules/people/ui/Attributes/AttributeForm.tsx b/apps/erp/app/modules/people/ui/Attributes/AttributeForm.tsx
index 122e9ee6a5..85a6d7b2b5 100644
--- a/apps/erp/app/modules/people/ui/Attributes/AttributeForm.tsx
+++ b/apps/erp/app/modules/people/ui/Attributes/AttributeForm.tsx
@@ -37,6 +37,7 @@ type AttributeFormProps = {
isList: boolean;
isNumeric: boolean;
isText: boolean;
+ isFile: boolean;
}[];
onClose: () => void;
};
diff --git a/apps/erp/app/modules/shared/types.ts b/apps/erp/app/modules/shared/types.ts
index 69baedd9f9..94e650e120 100644
--- a/apps/erp/app/modules/shared/types.ts
+++ b/apps/erp/app/modules/shared/types.ts
@@ -38,7 +38,8 @@ export enum DataType {
Text = 5,
User = 6,
Customer = 7,
- Supplier = 8
+ Supplier = 8,
+ File = 9
}
export type MethodItemType = (typeof methodItemType)[number];
diff --git a/apps/erp/app/routes/x+/account+/$userId.attribute.tsx b/apps/erp/app/routes/x+/account+/$userId.attribute.tsx
index 8295752078..fdac9b1a69 100644
--- a/apps/erp/app/routes/x+/account+/$userId.attribute.tsx
+++ b/apps/erp/app/routes/x+/account+/$userId.attribute.tsx
@@ -8,6 +8,7 @@ import type { ZodSchema } from "zod/v3";
import {
attributeBooleanValidator,
attributeCustomerValidator,
+ attributeFileValidator,
attributeNumericValidator,
attributeSupplierValidator,
attributeTextValidator,
@@ -117,6 +118,9 @@ function getValidatorByType(type: string) {
return attributeSupplierValidator;
case "customer":
return attributeCustomerValidator;
+ case "file":
+ return attributeFileValidator;
+
default:
throw new Error("Invalid type provided");
}
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 2bda712a5a..a672071271 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -1698,6 +1698,7 @@ export type Database = {
isBoolean: boolean
isCustomer: boolean
isDate: boolean
+ isFile: boolean
isList: boolean
isNumeric: boolean
isSupplier: boolean
@@ -1710,6 +1711,7 @@ export type Database = {
isBoolean?: boolean
isCustomer?: boolean
isDate?: boolean
+ isFile?: boolean
isList?: boolean
isNumeric?: boolean
isSupplier?: boolean
@@ -1722,6 +1724,7 @@ export type Database = {
isBoolean?: boolean
isCustomer?: boolean
isDate?: boolean
+ isFile?: boolean
isList?: boolean
isNumeric?: boolean
isSupplier?: boolean
@@ -36564,6 +36567,7 @@ export type Database = {
userId: string
valueBoolean: boolean | null
valueDate: string | null
+ valueFile: string | null
valueNumeric: number | null
valueText: string | null
valueUser: string | null
@@ -36578,6 +36582,7 @@ export type Database = {
userId: string
valueBoolean?: boolean | null
valueDate?: string | null
+ valueFile?: string | null
valueNumeric?: number | null
valueText?: string | null
valueUser?: string | null
@@ -36592,6 +36597,7 @@ export type Database = {
userId?: string
valueBoolean?: boolean | null
valueDate?: string | null
+ valueFile?: string | null
valueNumeric?: number | null
valueText?: string | null
valueUser?: string | null
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 2bda712a5a..a672071271 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -1698,6 +1698,7 @@ export type Database = {
isBoolean: boolean
isCustomer: boolean
isDate: boolean
+ isFile: boolean
isList: boolean
isNumeric: boolean
isSupplier: boolean
@@ -1710,6 +1711,7 @@ export type Database = {
isBoolean?: boolean
isCustomer?: boolean
isDate?: boolean
+ isFile?: boolean
isList?: boolean
isNumeric?: boolean
isSupplier?: boolean
@@ -1722,6 +1724,7 @@ export type Database = {
isBoolean?: boolean
isCustomer?: boolean
isDate?: boolean
+ isFile?: boolean
isList?: boolean
isNumeric?: boolean
isSupplier?: boolean
@@ -36564,6 +36567,7 @@ export type Database = {
userId: string
valueBoolean: boolean | null
valueDate: string | null
+ valueFile: string | null
valueNumeric: number | null
valueText: string | null
valueUser: string | null
@@ -36578,6 +36582,7 @@ export type Database = {
userId: string
valueBoolean?: boolean | null
valueDate?: string | null
+ valueFile?: string | null
valueNumeric?: number | null
valueText?: string | null
valueUser?: string | null
@@ -36592,6 +36597,7 @@ export type Database = {
userId?: string
valueBoolean?: boolean | null
valueDate?: string | null
+ valueFile?: string | null
valueNumeric?: number | null
valueText?: string | null
valueUser?: string | null
diff --git a/packages/database/supabase/migrations/20251213001722_add-file-to-person-attributes.sql b/packages/database/supabase/migrations/20251213001722_add-file-to-person-attributes.sql
new file mode 100644
index 0000000000..c27482b536
--- /dev/null
+++ b/packages/database/supabase/migrations/20251213001722_add-file-to-person-attributes.sql
@@ -0,0 +1,170 @@
+-- Add isFile column to attributeDataType
+ALTER TABLE "attributeDataType" ADD COLUMN "isFile" boolean NOT NULL DEFAULT false;
+
+-- Add valueFile column to userAttributeValue
+ALTER TABLE "userAttributeValue" ADD COLUMN "valueFile" text;
+
+-- Drop and recreate the constraint on attributeDataType to include isFile
+ALTER TABLE "attributeDataType" DROP CONSTRAINT "userAttributeDataType_singleDataType";
+
+ALTER TABLE "attributeDataType" ADD CONSTRAINT "userAttributeDataType_singleDataType"
+ CHECK (
+ (
+ "isBoolean" = true AND
+ "isDate" = false AND
+ "isList" = false AND
+ "isNumeric" = false AND
+ "isText" = false AND
+ "isUser" = false AND
+ "isCustomer" = false AND
+ "isSupplier" = false AND
+ "isFile" = false
+ )
+ OR (
+ "isBoolean" = false AND
+ "isDate" = true AND
+ "isList" = false AND
+ "isNumeric" = false AND
+ "isText" = false AND
+ "isUser" = false AND
+ "isCustomer" = false AND
+ "isSupplier" = false AND
+ "isFile" = false
+ )
+ OR (
+ "isBoolean" = false AND
+ "isDate" = false AND
+ "isList" = true AND
+ "isNumeric" = false AND
+ "isText" = false AND
+ "isUser" = false AND
+ "isCustomer" = false AND
+ "isSupplier" = false AND
+ "isFile" = false
+ )
+ OR (
+ "isBoolean" = false AND
+ "isDate" = false AND
+ "isList" = false AND
+ "isNumeric" = true AND
+ "isText" = false AND
+ "isUser" = false AND
+ "isCustomer" = false AND
+ "isSupplier" = false AND
+ "isFile" = false
+ )
+ OR (
+ "isBoolean" = false AND
+ "isDate" = false AND
+ "isList" = false AND
+ "isNumeric" = false AND
+ "isText" = true AND
+ "isUser" = false AND
+ "isCustomer" = false AND
+ "isSupplier" = false AND
+ "isFile" = false
+ )
+ OR (
+ "isBoolean" = false AND
+ "isDate" = false AND
+ "isList" = false AND
+ "isNumeric" = false AND
+ "isText" = false AND
+ "isUser" = true AND
+ "isCustomer" = false AND
+ "isSupplier" = false AND
+ "isFile" = false
+ )
+ OR (
+ "isBoolean" = false AND
+ "isDate" = false AND
+ "isList" = false AND
+ "isNumeric" = false AND
+ "isText" = false AND
+ "isUser" = false AND
+ "isCustomer" = true AND
+ "isSupplier" = false AND
+ "isFile" = false
+ )
+ OR (
+ "isBoolean" = false AND
+ "isDate" = false AND
+ "isList" = false AND
+ "isNumeric" = false AND
+ "isText" = false AND
+ "isUser" = false AND
+ "isCustomer" = false AND
+ "isSupplier" = true AND
+ "isFile" = false
+ )
+ OR (
+ "isBoolean" = false AND
+ "isDate" = false AND
+ "isList" = false AND
+ "isNumeric" = false AND
+ "isText" = false AND
+ "isUser" = false AND
+ "isCustomer" = false AND
+ "isSupplier" = false AND
+ "isFile" = true
+ )
+ );
+
+-- Insert the File data type record
+INSERT INTO "attributeDataType" ("label", "isBoolean", "isDate", "isList", "isNumeric", "isText", "isUser", "isCustomer", "isSupplier", "isFile")
+VALUES ('File', false, false, false, false, false, false, false, false, true);
+
+-- Drop and recreate the constraint on userAttributeValue to include valueFile
+ALTER TABLE "userAttributeValue" DROP CONSTRAINT "userAttributeValue_singleValue";
+
+ALTER TABLE "userAttributeValue" ADD CONSTRAINT "userAttributeValue_singleValue"
+ CHECK (
+ (
+ "valueBoolean" IS NOT NULL AND
+ "valueDate" IS NULL AND
+ "valueNumeric" IS NULL AND
+ "valueText" IS NULL AND
+ "valueUser" IS NULL AND
+ "valueFile" IS NULL
+ )
+ OR (
+ "valueBoolean" IS NULL AND
+ "valueDate" IS NULL AND
+ "valueNumeric" IS NULL AND
+ "valueText" IS NOT NULL AND
+ "valueUser" IS NULL AND
+ "valueFile" IS NULL
+ )
+ OR (
+ "valueBoolean" IS NULL AND
+ "valueDate" IS NOT NULL AND
+ "valueNumeric" IS NULL AND
+ "valueText" IS NULL AND
+ "valueUser" IS NULL AND
+ "valueFile" IS NULL
+ )
+ OR (
+ "valueBoolean" IS NULL AND
+ "valueDate" IS NULL AND
+ "valueNumeric" IS NOT NULL AND
+ "valueText" IS NULL AND
+ "valueUser" IS NULL AND
+ "valueFile" IS NULL
+ )
+ OR (
+ "valueBoolean" IS NULL AND
+ "valueDate" IS NULL AND
+ "valueNumeric" IS NULL AND
+ "valueText" IS NULL AND
+ "valueUser" IS NOT NULL AND
+ "valueFile" IS NULL
+ )
+ OR (
+ "valueBoolean" IS NULL AND
+ "valueDate" IS NULL AND
+ "valueNumeric" IS NULL AND
+ "valueText" IS NULL AND
+ "valueUser" IS NULL AND
+ "valueFile" IS NOT NULL
+ )
+ );