diff --git a/apps/web/src/components/dynamic-breadcrumb.tsx b/apps/web/src/components/dynamic-breadcrumb.tsx index c95cbe3..a116cfc 100644 --- a/apps/web/src/components/dynamic-breadcrumb.tsx +++ b/apps/web/src/components/dynamic-breadcrumb.tsx @@ -20,6 +20,7 @@ const SEGMENT_LABELS = { customers: "Clientes", branches: "Sucursales", contacts: "Contactos", + suppliers: "Proveedores", } satisfies Record; type KnownSegment = keyof typeof SEGMENT_LABELS; diff --git a/apps/web/src/components/nav-items.ts b/apps/web/src/components/nav-items.ts index 810f2ff..7e1a231 100644 --- a/apps/web/src/components/nav-items.ts +++ b/apps/web/src/components/nav-items.ts @@ -1,11 +1,13 @@ import type { LinkProps } from "@tanstack/react-router"; import type { LucideIcon } from "lucide-react"; import { + BriefcaseBusiness, Building2, Contact, ContactRound, DollarSign, Home, + Truck, Users, } from "lucide-react"; @@ -42,6 +44,11 @@ export const NAV_ENTRIES: NavEntry[] = [ { to: "/contacts", label: "Contactos", icon: Contact }, ], }, + { + label: "Administración", + icon: BriefcaseBusiness, + items: [{ to: "/suppliers", label: "Proveedores", icon: Truck }], + }, ]; export function isSection(entry: NavEntry): entry is NavSection { diff --git a/apps/web/src/components/suppliers/supplier-card.tsx b/apps/web/src/components/suppliers/supplier-card.tsx new file mode 100644 index 0000000..bc65beb --- /dev/null +++ b/apps/web/src/components/suppliers/supplier-card.tsx @@ -0,0 +1,154 @@ +import type { AppRouterOutputs } from "@suni/api/routers/index"; +import { Button } from "@suni/ui/components/button"; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuGroup, + DropdownMenuItem, + DropdownMenuLabel, + DropdownMenuTrigger, +} from "@suni/ui/components/dropdown-menu"; +import { cn } from "@suni/ui/lib/utils"; +import { Link } from "@tanstack/react-router"; +import { + ChevronRight, + MapPin, + MoreVertical, + Pencil, + Phone, +} from "lucide-react"; + +import { ArchiveMenuItem } from "@/components/customers/shared/archive-menu-item"; +import { ArchivedFooter } from "@/components/customers/shared/archived-footer"; + +interface SupplierCardProps { + supplier: AppRouterOutputs["suppliers"]["list"][number]; + onChangeStatus?: () => void; +} + +export function SupplierCard({ supplier, onChangeStatus }: SupplierCardProps) { + const isArchived = !!supplier.archived; + const params = { supplierId: supplier.id }; + + return ( +
+
+
+ +

+ {supplier.name} + {isArchived ? , Archivado : null} +

+ + + {onChangeStatus ? ( + + + } + > + Acciones de {supplier.name} + + + + + Acciones + {isArchived ? null : ( + + } + > + + Editar + + )} + + + + + + ) : null} +
+
+ +
+
+ + {supplier.rfc} + +
+ +
+ +
+ + {supplier.suburb}, {supplier.city} + + + {supplier.state} · CP {supplier.postalCode} + +
+
+ +
+ + + {supplier.phone} + {supplier.phoneExtension && ` ext. ${supplier.phoneExtension}`} + +
+
+ + {isArchived ? ( + + ) : ( + +
+ + Ver detalles + + +
+ + )} +
+ ); +} diff --git a/apps/web/src/components/suppliers/supplier-data.tsx b/apps/web/src/components/suppliers/supplier-data.tsx new file mode 100644 index 0000000..deae341 --- /dev/null +++ b/apps/web/src/components/suppliers/supplier-data.tsx @@ -0,0 +1,40 @@ +import type { AppRouterOutputs } from "@suni/api/routers/index"; + +interface SupplierDataProps { + supplier: AppRouterOutputs["suppliers"]["get"]; +} + +export function SupplierData({ supplier }: SupplierDataProps) { + return ( +
+
+
+
+ Dirección fiscal +
+
+
+ {supplier.street}, No. Ext. {supplier.extNumber} + {supplier.intNumber && `, No. Int. ${supplier.intNumber}`}, + Colonia {supplier.suburb} +
+ {supplier.city}, {supplier.state}, CP {supplier.postalCode} +
+
+
+
+
+ Teléfono +
+
+ {supplier.phone} + {supplier.phoneExtension && ` ext. ${supplier.phoneExtension}`} +
+
+
+
+ ); +} diff --git a/apps/web/src/components/suppliers/supplier-filters.tsx b/apps/web/src/components/suppliers/supplier-filters.tsx new file mode 100644 index 0000000..837f4e5 --- /dev/null +++ b/apps/web/src/components/suppliers/supplier-filters.tsx @@ -0,0 +1,23 @@ +import { getRouteApi } from "@tanstack/react-router"; + +import { FilterBar } from "@/components/customers/shared/filter-bar"; + +const routeApi = getRouteApi("/_auth/suppliers/"); + +export function SupplierFilters() { + const { name } = routeApi.useSearch(); + const navigate = routeApi.useNavigate(); + + return ( + + navigate({ + search: (prev) => ({ ...prev, name: next }), + replace: true, + }) + } + search={name} + searchLabel="Buscar por razón social" + /> + ); +} diff --git a/apps/web/src/components/suppliers/supplier-form.tsx b/apps/web/src/components/suppliers/supplier-form.tsx new file mode 100644 index 0000000..1421ec0 --- /dev/null +++ b/apps/web/src/components/suppliers/supplier-form.tsx @@ -0,0 +1,249 @@ +import type { + AppRouterInputs, + AppRouterOutputs, +} from "@suni/api/routers/index"; +import { supplierSchema } from "@suni/db/validation/suppliers"; +import { Button } from "@suni/ui/components/button"; +import { revalidateLogic, useSelector } from "@tanstack/react-form"; +import { Link, useNavigate } from "@tanstack/react-router"; +import { Loader2 } from "lucide-react"; + +import { useAppForm } from "@/components/form/use-app-form"; +import { POSTAL_CODE_LENGTH, useAddressLookup } from "@/hooks/use-postal-code"; +import { useCreateSupplier, useUpdateSupplier } from "@/hooks/use-suppliers"; + +const RFC_MAX_LENGTH = 13; + +const PHONE_LENGTH = 10; + +const FILLED_BY_POSTAL_CODE = "Se llena con el código postal"; + +const EMPTY_VALUES: AppRouterInputs["suppliers"]["create"] = { + name: "", + rfc: "", + phone: "", + phoneExtension: "", + postalCode: "", + suburb: "", + city: "", + state: "", + street: "", + extNumber: "", + intNumber: "", +}; + +interface SupplierFormProps { + supplier?: AppRouterOutputs["suppliers"]["get"]; +} + +export function SupplierForm({ supplier }: SupplierFormProps) { + const navigate = useNavigate(); + const createSupplier = useCreateSupplier(); + const updateSupplier = useUpdateSupplier(); + const submitting = createSupplier.isPending || updateSupplier.isPending; + + const form = useAppForm({ + defaultValues: supplier + ? { + name: supplier.name, + rfc: supplier.rfc, + phone: supplier.phone, + phoneExtension: supplier.phoneExtension, + postalCode: supplier.postalCode, + suburb: supplier.suburb, + city: supplier.city, + state: supplier.state, + street: supplier.street, + extNumber: supplier.extNumber, + intNumber: supplier.intNumber, + } + : EMPTY_VALUES, + validationLogic: revalidateLogic(), + validators: { onDynamic: supplierSchema }, + onSubmit: ({ value }) => { + const fields = supplierSchema.parse(value); + + if (supplier) { + updateSupplier.mutate( + { ...fields, id: supplier.id }, + { + onSuccess: () => + navigate({ + to: "/suppliers/$supplierId", + params: { supplierId: supplier.id }, + }), + } + ); + + return; + } + + createSupplier.mutate(fields, { + onSuccess: () => navigate({ to: "/suppliers" }), + }); + }, + }); + + const zip = useSelector(form.store, (state) => state.values.postalCode); + const address = useAddressLookup(zip); + + return ( +
{ + event.preventDefault(); + form.handleSubmit(); + }} + > +
+ + Datos generales + +
+ + {(field) => ( + + )} + + + {(field) => ( + value.toUpperCase()} + /> + )} + + + {(field) => ( + + )} + + + {(field) => } + +
+
+ +
+ + Dirección + +
+ + address.fill(value, { + getPostalCode: () => form.getFieldValue("postalCode"), + set: (field, fieldValue) => + form.setFieldValue(field, fieldValue), + }), + }} + name="postalCode" + > + {(field) => ( + + )} + + + {(field) => ( + + )} + + + {(field) => ( + + )} + + + {(field) => ( + + )} + + + {(field) => ( + + )} + + + {(field) => } + + + {(field) => } + +
+
+ +
+
+ + +
+
+
+ ); +} diff --git a/apps/web/src/hooks/use-suppliers.ts b/apps/web/src/hooks/use-suppliers.ts new file mode 100644 index 0000000..6e4edea --- /dev/null +++ b/apps/web/src/hooks/use-suppliers.ts @@ -0,0 +1,47 @@ +import type { AppRouterInputs } from "@suni/api/routers/index"; +import { useMutation, useQuery, useSuspenseQuery } from "@tanstack/react-query"; +import { toast } from "sonner"; + +import { orpc } from "@/utils/orpc"; + +export function useListSuppliers(params: AppRouterInputs["suppliers"]["list"]) { + return useQuery(orpc.suppliers.list.queryOptions({ input: params })); +} + +export function useCreateSupplier() { + return useMutation( + orpc.suppliers.create.mutationOptions({ + onSuccess: () => toast.success("Proveedor creado"), + onError: (error) => toast.error(error.message), + }) + ); +} + +export function supplierQueryOptions(id: string) { + return orpc.suppliers.get.queryOptions({ input: { id } }); +} + +export function useGetSupplier(id: string) { + return useSuspenseQuery(supplierQueryOptions(id)); +} + +export function useUpdateSupplier() { + return useMutation( + orpc.suppliers.update.mutationOptions({ + onSuccess: () => toast.success("Proveedor actualizado"), + onError: (error) => toast.error(error.message), + }) + ); +} + +export function useSetSupplierArchived() { + return useMutation( + orpc.suppliers.setArchived.mutationOptions({ + onSuccess: (_data, { archived }) => + toast.success( + archived ? "Proveedor archivado" : "Proveedor desarchivado" + ), + onError: (error) => toast.error(error.message), + }) + ); +} diff --git a/apps/web/src/routes/_auth/suppliers/$supplierId/edit.tsx b/apps/web/src/routes/_auth/suppliers/$supplierId/edit.tsx new file mode 100644 index 0000000..d58aec0 --- /dev/null +++ b/apps/web/src/routes/_auth/suppliers/$supplierId/edit.tsx @@ -0,0 +1,43 @@ +import { createFileRoute, redirect } from "@tanstack/react-router"; +import { Truck } from "lucide-react"; + +import { PageHeader } from "@/components/page-header"; +import { SupplierForm } from "@/components/suppliers/supplier-form"; +import { supplierQueryOptions, useGetSupplier } from "@/hooks/use-suppliers"; + +export const Route = createFileRoute("/_auth/suppliers/$supplierId/edit")({ + beforeLoad: ({ context, params: { supplierId } }) => { + if (context.session.user.role !== "admin") { + throw redirect({ to: "/suppliers/$supplierId", params: { supplierId } }); + } + }, + loader: async ({ context: { queryClient }, params: { supplierId } }) => { + const supplier = await queryClient.query(supplierQueryOptions(supplierId)); + + if (supplier.archived) { + throw redirect({ to: "/suppliers/$supplierId", params: { supplierId } }); + } + }, + component: RouteComponent, + errorComponent: () => ( +

+ No se pudo cargar el proveedor. +

+ ), +}); + +function RouteComponent() { + const { supplierId } = Route.useParams(); + const { data: supplier } = useGetSupplier(supplierId); + + return ( +
+ + +
+ ); +} diff --git a/apps/web/src/routes/_auth/suppliers/$supplierId/index.tsx b/apps/web/src/routes/_auth/suppliers/$supplierId/index.tsx new file mode 100644 index 0000000..d464e1d --- /dev/null +++ b/apps/web/src/routes/_auth/suppliers/$supplierId/index.tsx @@ -0,0 +1,126 @@ +import { Badge } from "@suni/ui/components/badge"; +import { Button } from "@suni/ui/components/button"; +import { + Tabs, + TabsContent, + TabsList, + TabsTrigger, +} from "@suni/ui/components/tabs"; +import { createFileRoute, Link } from "@tanstack/react-router"; +import { Archive, ArchiveRestore, Pencil, Truck } from "lucide-react"; +import { useRef, useState } from "react"; + +import { ChangeStatusDialog } from "@/components/customers/shared/change-status-dialog"; +import { PageHeader } from "@/components/page-header"; +import { SupplierData } from "@/components/suppliers/supplier-data"; +import { useIsAdmin } from "@/hooks/use-is-admin"; +import { + supplierQueryOptions, + useGetSupplier, + useSetSupplierArchived, +} from "@/hooks/use-suppliers"; + +export const Route = createFileRoute("/_auth/suppliers/$supplierId/")({ + loader: ({ context: { queryClient }, params: { supplierId } }) => + queryClient.query(supplierQueryOptions(supplierId)), + component: RouteComponent, + errorComponent: () => ( +

+ No se pudo cargar el proveedor. +

+ ), +}); + +const UPCOMING_TABS = [ + { value: "catalog", label: "Catálogo de productos" }, + { value: "quotes", label: "Cotizaciones" }, + { value: "files", label: "Documentos Fiscales" }, + { value: "orders", label: "Órdenes de Compra" }, + { value: "billing-portal", label: "Portal de facturación" }, +] as const; + +function RouteComponent() { + const { supplierId } = Route.useParams(); + const { data: supplier } = useGetSupplier(supplierId); + const isAdmin = useIsAdmin(); + const [dialogOpen, setDialogOpen] = useState(false); + const statusButton = useRef(null); + const archive = useSetSupplierArchived(); + const isArchived = !!supplier.archived; + + return ( +
+ + {isArchived ? null : ( + + )} + + + ) : undefined + } + description={ +
+ RFC {supplier.rfc} + {isArchived ? Archivado : null} +
+ } + icon={Truck} + title={supplier.name} + /> + +
+ + Datos generales + {UPCOMING_TABS.map(({ value, label }) => ( + + {label} + Pronto + + ))} + +
+ + + +
+ {isAdmin ? ( + + ) : null} +
+ ); +} diff --git a/apps/web/src/routes/_auth/suppliers/index.tsx b/apps/web/src/routes/_auth/suppliers/index.tsx new file mode 100644 index 0000000..264e157 --- /dev/null +++ b/apps/web/src/routes/_auth/suppliers/index.tsx @@ -0,0 +1,131 @@ +import type { AppRouterOutputs } from "@suni/api/routers/index"; +import { supplierListSchema } from "@suni/db/validation/suppliers"; +import { Button } from "@suni/ui/components/button"; +import { + Link, + createFileRoute, + stripSearchParams, +} from "@tanstack/react-router"; +import { Plus, Truck } from "lucide-react"; +import { useRef, useState } from "react"; +import { z } from "zod"; + +import { ChangeStatusDialog } from "@/components/customers/shared/change-status-dialog"; +import { EntityGrid } from "@/components/customers/shared/entity-grid"; +import { + byStatus, + countByStatus, + matchingName, +} from "@/components/customers/shared/filters"; +import { StatusTabs } from "@/components/customers/shared/status-tabs"; +import { PageHeader } from "@/components/page-header"; +import { SupplierCard } from "@/components/suppliers/supplier-card"; +import { SupplierFilters } from "@/components/suppliers/supplier-filters"; +import { useIsAdmin } from "@/hooks/use-is-admin"; +import { + useListSuppliers, + useSetSupplierArchived, +} from "@/hooks/use-suppliers"; + +/* oxlint-disable promise/prefer-await-to-then, github/no-then -- zod `.catch()` is not Promise.catch */ +const searchSchema = supplierListSchema.extend({ + name: z.string().default("").catch(""), + status: supplierListSchema.shape.status.catch("active"), +}); +/* oxlint-enable promise/prefer-await-to-then, github/no-then */ + +const defaultFilters = searchSchema.parse({}); + +const LIST_COPY = { + loading: "Cargando proveedores", + error: "No se pudieron cargar los proveedores", + notFound: "No se encontraron proveedores", + empty: "No hay proveedores para mostrar", + emptyHint: "Los proveedores aparecerán aquí", +}; + +export const Route = createFileRoute("/_auth/suppliers/")({ + validateSearch: searchSchema, + search: { middlewares: [stripSearchParams(defaultFilters)] }, + component: RouteComponent, +}); + +function RouteComponent() { + const { name, status } = Route.useSearch(); + const navigate = Route.useNavigate(); + const isAdmin = useIsAdmin(); + const query = useListSuppliers({ status: "all" }); + const rowsByStatus = byStatus(matchingName(query.data ?? [], name)); + const suppliers = rowsByStatus[status]; + const archive = useSetSupplierArchived(); + const filtersRef = useRef(null); + + const [target, setTarget] = + useState(); + + const [dialogOpen, setDialogOpen] = useState(false); + + return ( +
+ }> + + Nuevo proveedor + + ) : undefined + } + description="Consulta el directorio de proveedores" + icon={Truck} + title="Proveedores" + /> + + navigate({ + search: (prev) => ({ ...prev, status: next }), + replace: true, + }) + } + value={status} + > +
+ +
+ + {suppliers.map((supplier) => ( + { + setTarget(supplier); + setDialogOpen(true); + } + : undefined + } + supplier={supplier} + /> + ))} + +
+ {isAdmin && target ? ( + + ) : null} +
+ ); +} diff --git a/apps/web/src/routes/_auth/suppliers/new.tsx b/apps/web/src/routes/_auth/suppliers/new.tsx new file mode 100644 index 0000000..295b385 --- /dev/null +++ b/apps/web/src/routes/_auth/suppliers/new.tsx @@ -0,0 +1,27 @@ +import { createFileRoute, redirect } from "@tanstack/react-router"; +import { Truck } from "lucide-react"; + +import { PageHeader } from "@/components/page-header"; +import { SupplierForm } from "@/components/suppliers/supplier-form"; + +export const Route = createFileRoute("/_auth/suppliers/new")({ + beforeLoad: ({ context }) => { + if (context.session.user.role !== "admin") { + throw redirect({ to: "/suppliers" }); + } + }, + component: RouteComponent, +}); + +function RouteComponent() { + return ( +
+ + +
+ ); +} diff --git a/packages/api/src/routers/index.ts b/packages/api/src/routers/index.ts index 7804daa..93ad52a 100644 --- a/packages/api/src/routers/index.ts +++ b/packages/api/src/routers/index.ts @@ -8,6 +8,7 @@ import { protectedProcedure, publicProcedure } from "../index"; import { branchesRouter } from "./branches"; import { contactsRouter } from "./contacts"; import { customersRouter } from "./customers"; +import { suppliersRouter } from "./suppliers"; import { usersRouter } from "./users"; export const appRouter = { @@ -18,6 +19,7 @@ export const appRouter = { })), users: usersRouter, customers: customersRouter, + suppliers: suppliersRouter, branches: branchesRouter, contacts: contactsRouter, }; diff --git a/packages/api/src/routers/suppliers/create.ts b/packages/api/src/routers/suppliers/create.ts new file mode 100644 index 0000000..b47f119 --- /dev/null +++ b/packages/api/src/routers/suppliers/create.ts @@ -0,0 +1,18 @@ +import { supplier } from "@suni/db/schema/suppliers"; +import { supplierSchema } from "@suni/db/validation/suppliers"; + +import { adminProcedure } from "../../index"; + +export const create = adminProcedure + .route({ method: "POST", path: "/" }) + .input(supplierSchema) + .handler(async ({ input, context }) => { + const { userId } = context; + + const [created] = await context.db + .insert(supplier) + .values({ ...input, modifiedBy: userId }) + .returning({ id: supplier.id }); + + return created; + }); diff --git a/packages/api/src/routers/suppliers/get.ts b/packages/api/src/routers/suppliers/get.ts new file mode 100644 index 0000000..8bb7ce3 --- /dev/null +++ b/packages/api/src/routers/suppliers/get.ts @@ -0,0 +1,18 @@ +import { supplierIdSchema } from "@suni/db/validation/suppliers"; + +import { protectedProcedure } from "../../index"; + +export const get = protectedProcedure + .route({ method: "GET", path: "/{id}" }) + .input(supplierIdSchema) + .handler(async ({ input, context, errors }) => { + const found = await context.db.query.supplier.findFirst({ + where: { id: input.id }, + }); + + if (!found) { + throw errors.NOT_FOUND({ message: "El proveedor no existe" }); + } + + return found; + }); diff --git a/packages/api/src/routers/suppliers/index.ts b/packages/api/src/routers/suppliers/index.ts new file mode 100644 index 0000000..fdbf48a --- /dev/null +++ b/packages/api/src/routers/suppliers/index.ts @@ -0,0 +1,11 @@ +import { o } from "../../index"; +import { create } from "./create"; +import { get } from "./get"; +import { list } from "./list"; +import { setArchived } from "./set-archived"; +import { update } from "./update"; + +export const suppliersRouter = o + .prefix("/suppliers") + .tag("Suppliers") + .router({ list, get, create, update, setArchived }); diff --git a/packages/api/src/routers/suppliers/list.ts b/packages/api/src/routers/suppliers/list.ts new file mode 100644 index 0000000..2fb6761 --- /dev/null +++ b/packages/api/src/routers/suppliers/list.ts @@ -0,0 +1,13 @@ +import { supplierListSchema } from "@suni/db/validation/suppliers"; + +import { protectedProcedure } from "../../index"; +import { statusWhere } from "../../util/status"; + +export const list = protectedProcedure + .route({ method: "GET", path: "/" }) + .input(supplierListSchema) + .handler(({ input, context }) => + context.db.query.supplier.findMany({ + where: statusWhere("supplier", input.status), + }) + ); diff --git a/packages/api/src/routers/suppliers/set-archived.ts b/packages/api/src/routers/suppliers/set-archived.ts new file mode 100644 index 0000000..d12cd30 --- /dev/null +++ b/packages/api/src/routers/suppliers/set-archived.ts @@ -0,0 +1,26 @@ +import { supplier } from "@suni/db/schema/suppliers"; +import { supplierArchiveSchema } from "@suni/db/validation/suppliers"; +import { eq } from "drizzle-orm"; + +import { adminProcedure } from "../../index"; +import { archivedValue } from "../../util/status"; + +export const setArchived = adminProcedure + .route({ method: "POST", path: "/{id}/archived" }) + .input(supplierArchiveSchema) + .handler(async ({ input, context, errors }) => { + const [updated] = await context.db + .update(supplier) + .set({ + archived: archivedValue(supplier.archived, input.archived), + modifiedBy: context.userId, + }) + .where(eq(supplier.id, input.id)) + .returning({ id: supplier.id }); + + if (!updated) { + throw errors.NOT_FOUND({ message: "El proveedor no existe" }); + } + + return updated; + }); diff --git a/packages/api/src/routers/suppliers/update.ts b/packages/api/src/routers/suppliers/update.ts new file mode 100644 index 0000000..7bce143 --- /dev/null +++ b/packages/api/src/routers/suppliers/update.ts @@ -0,0 +1,32 @@ +import { supplier } from "@suni/db/schema/suppliers"; +import { + supplierIdSchema, + supplierSchema, +} from "@suni/db/validation/suppliers"; +import { and, eq, isNull } from "drizzle-orm"; + +import { adminProcedure } from "../../index"; + +export const update = adminProcedure + .route({ method: "PATCH", path: "/{id}" }) + .input(supplierSchema.extend(supplierIdSchema.shape)) + .handler(async ({ input, context, errors }) => { + const { id, ...fields } = input; + + const { userId } = context; + + const [updated] = await context.db + .update(supplier) + .set({ ...fields, modifiedBy: userId }) + // Archived suppliers are read-only until restored. + .where(and(eq(supplier.id, id), isNull(supplier.archived))) + .returning({ id: supplier.id }); + + if (!updated) { + throw errors.NOT_FOUND({ + message: "El proveedor no existe o está archivado", + }); + } + + return updated; + }); diff --git a/packages/api/src/util/status.ts b/packages/api/src/util/status.ts index e4d5843..365975c 100644 --- a/packages/api/src/util/status.ts +++ b/packages/api/src/util/status.ts @@ -11,6 +11,7 @@ const isNotNull = { isNotNull: true } as const; const visible = { customer: { archived: isNull }, + supplier: { archived: isNull }, branch: { archived: isNull, customer: { archived: isNull } }, contact: { archived: isNull, @@ -21,6 +22,7 @@ const visible = { const archived = { customer: { archived: isNotNull }, + supplier: { archived: isNotNull }, branch: { OR: [{ archived: isNotNull }, { customer: { archived: isNotNull } }], }, diff --git a/packages/db/src/migrations/20261120182944_puzzling_cassandra_nova/migration.sql b/packages/db/src/migrations/20261120182944_puzzling_cassandra_nova/migration.sql new file mode 100644 index 0000000..813089e --- /dev/null +++ b/packages/db/src/migrations/20261120182944_puzzling_cassandra_nova/migration.sql @@ -0,0 +1,20 @@ +CREATE TABLE "supplier" ( + "id" text PRIMARY KEY, + "name" text NOT NULL, + "rfc" text NOT NULL, + "street" text NOT NULL, + "int_number" text DEFAULT '' NOT NULL, + "ext_number" text NOT NULL, + "city" text NOT NULL, + "suburb" text NOT NULL, + "state" text NOT NULL, + "postal_code" text NOT NULL, + "phone" text NOT NULL, + "phone_extension" text DEFAULT '' NOT NULL, + "modified_by" text NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL, + "updated_at" timestamp DEFAULT now() NOT NULL, + "archived" timestamp +); +--> statement-breakpoint +ALTER TABLE "supplier" ADD CONSTRAINT "supplier_modified_by_user_id_fkey" FOREIGN KEY ("modified_by") REFERENCES "user"("id"); \ No newline at end of file diff --git a/packages/db/src/migrations/20261120182944_puzzling_cassandra_nova/snapshot.json b/packages/db/src/migrations/20261120182944_puzzling_cassandra_nova/snapshot.json new file mode 100644 index 0000000..941bbe3 --- /dev/null +++ b/packages/db/src/migrations/20261120182944_puzzling_cassandra_nova/snapshot.json @@ -0,0 +1,1751 @@ +{ + "version": "8", + "dialect": "postgres", + "id": "27ebb40b-8e3f-45d8-a710-2f5ac12ed091", + "prevIds": [ + "fe2a732b-fb16-4519-abc5-629e424b62ed" + ], + "ddl": [ + { + "values": [ + "Redes sociales", + "Recomendación", + "Prospección", + "Teléfono", + "Correo", + "Página web", + "Díptico" + ], + "name": "acquisition", + "entityType": "enums", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "account", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "session", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "user", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "verification", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "customer_branches", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "contact", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "customer", + "entityType": "tables", + "schema": "public" + }, + { + "isRlsEnabled": false, + "name": "supplier", + "entityType": "tables", + "schema": "public" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "account_id", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "provider_id", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "user_id", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "access_token", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "refresh_token", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id_token", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "access_token_expires_at", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "refresh_token_expires_at", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "scope", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "password", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "account" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id", + "entityType": "columns", + "schema": "public", + "table": "session" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "expires_at", + "entityType": "columns", + "schema": "public", + "table": "session" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "token", + "entityType": "columns", + "schema": "public", + "table": "session" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "session" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "session" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "ip_address", + "entityType": "columns", + "schema": "public", + "table": "session" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "user_agent", + "entityType": "columns", + "schema": "public", + "table": "session" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "user_id", + "entityType": "columns", + "schema": "public", + "table": "session" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "impersonated_by", + "entityType": "columns", + "schema": "public", + "table": "session" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "name", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "email", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "boolean", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "false", + "generated": null, + "identity": null, + "name": "email_verified", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "image", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "role", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "boolean", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": "false", + "generated": null, + "identity": null, + "name": "banned", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "text", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "ban_reason", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "ban_expires", + "entityType": "columns", + "schema": "public", + "table": "user" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id", + "entityType": "columns", + "schema": "public", + "table": "verification" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "identifier", + "entityType": "columns", + "schema": "public", + "table": "verification" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "value", + "entityType": "columns", + "schema": "public", + "table": "verification" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "expires_at", + "entityType": "columns", + "schema": "public", + "table": "verification" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "verification" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "verification" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "name", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "street", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "''", + "generated": null, + "identity": null, + "name": "int_number", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "ext_number", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "city", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "suburb", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "state", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "postal_code", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "''", + "generated": null, + "identity": null, + "name": "phone", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "''", + "generated": null, + "identity": null, + "name": "front_image_url", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "customer_id", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "seller_id", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "modified_by", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "archived", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "customer_branches" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "name", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "email", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "phone", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "''", + "generated": null, + "identity": null, + "name": "phone_extension", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "''", + "generated": null, + "identity": null, + "name": "position", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "customer_id", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "branch_id", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "archived", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "modified_by", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "contact" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "name", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "rfc", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "street", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "''", + "generated": null, + "identity": null, + "name": "int_number", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "ext_number", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "city", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "suburb", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "state", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "postal_code", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "phone", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "''", + "generated": null, + "identity": null, + "name": "phone_extension", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "acquisition", + "typeSchema": "public", + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "acquisition", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "seller_id", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "modified_by", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "archived", + "entityType": "columns", + "schema": "public", + "table": "customer" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "id", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "name", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "rfc", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "street", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "''", + "generated": null, + "identity": null, + "name": "int_number", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "ext_number", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "city", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "suburb", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "state", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "postal_code", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "phone", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "''", + "generated": null, + "identity": null, + "name": "phone_extension", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "text", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "modified_by", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "created_at", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": true, + "dimensions": 0, + "default": "now()", + "generated": null, + "identity": null, + "name": "updated_at", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "type": "timestamp", + "typeSchema": null, + "notNull": false, + "dimensions": 0, + "default": null, + "generated": null, + "identity": null, + "name": "archived", + "entityType": "columns", + "schema": "public", + "table": "supplier" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "user_id", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": false, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "account_userId_idx", + "entityType": "indexes", + "schema": "public", + "table": "account" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "user_id", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": false, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "session_userId_idx", + "entityType": "indexes", + "schema": "public", + "table": "session" + }, + { + "nameExplicit": true, + "columns": [ + { + "value": "identifier", + "isExpression": false, + "asc": true, + "nullsFirst": false, + "opclass": null + } + ], + "isUnique": false, + "where": null, + "with": "", + "method": "btree", + "concurrently": false, + "name": "verification_identifier_idx", + "entityType": "indexes", + "schema": "public", + "table": "verification" + }, + { + "nameExplicit": false, + "columns": [ + "user_id" + ], + "schemaTo": "public", + "tableTo": "user", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "CASCADE", + "name": "account_user_id_user_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "account" + }, + { + "nameExplicit": false, + "columns": [ + "user_id" + ], + "schemaTo": "public", + "tableTo": "user", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "CASCADE", + "name": "session_user_id_user_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "session" + }, + { + "nameExplicit": false, + "columns": [ + "customer_id" + ], + "schemaTo": "public", + "tableTo": "customer", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "NO ACTION", + "name": "customer_branches_customer_id_customer_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "customer_branches" + }, + { + "nameExplicit": false, + "columns": [ + "seller_id" + ], + "schemaTo": "public", + "tableTo": "user", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "NO ACTION", + "name": "customer_branches_seller_id_user_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "customer_branches" + }, + { + "nameExplicit": false, + "columns": [ + "modified_by" + ], + "schemaTo": "public", + "tableTo": "user", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "NO ACTION", + "name": "customer_branches_modified_by_user_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "customer_branches" + }, + { + "nameExplicit": false, + "columns": [ + "customer_id" + ], + "schemaTo": "public", + "tableTo": "customer", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "NO ACTION", + "name": "contact_customer_id_customer_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "contact" + }, + { + "nameExplicit": false, + "columns": [ + "branch_id" + ], + "schemaTo": "public", + "tableTo": "customer_branches", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "NO ACTION", + "name": "contact_branch_id_customer_branches_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "contact" + }, + { + "nameExplicit": false, + "columns": [ + "modified_by" + ], + "schemaTo": "public", + "tableTo": "user", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "NO ACTION", + "name": "contact_modified_by_user_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "contact" + }, + { + "nameExplicit": false, + "columns": [ + "seller_id" + ], + "schemaTo": "public", + "tableTo": "user", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "NO ACTION", + "name": "customer_seller_id_user_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "customer" + }, + { + "nameExplicit": false, + "columns": [ + "modified_by" + ], + "schemaTo": "public", + "tableTo": "user", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "NO ACTION", + "name": "customer_modified_by_user_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "customer" + }, + { + "nameExplicit": false, + "columns": [ + "modified_by" + ], + "schemaTo": "public", + "tableTo": "user", + "columnsTo": [ + "id" + ], + "onUpdate": "NO ACTION", + "onDelete": "NO ACTION", + "name": "supplier_modified_by_user_id_fkey", + "entityType": "fks", + "schema": "public", + "table": "supplier" + }, + { + "columns": [ + "id" + ], + "nameExplicit": false, + "name": "account_pkey", + "schema": "public", + "table": "account", + "entityType": "pks" + }, + { + "columns": [ + "id" + ], + "nameExplicit": false, + "name": "session_pkey", + "schema": "public", + "table": "session", + "entityType": "pks" + }, + { + "columns": [ + "id" + ], + "nameExplicit": false, + "name": "user_pkey", + "schema": "public", + "table": "user", + "entityType": "pks" + }, + { + "columns": [ + "id" + ], + "nameExplicit": false, + "name": "verification_pkey", + "schema": "public", + "table": "verification", + "entityType": "pks" + }, + { + "columns": [ + "id" + ], + "nameExplicit": false, + "name": "customer_branches_pkey", + "schema": "public", + "table": "customer_branches", + "entityType": "pks" + }, + { + "columns": [ + "id" + ], + "nameExplicit": false, + "name": "contact_pkey", + "schema": "public", + "table": "contact", + "entityType": "pks" + }, + { + "columns": [ + "id" + ], + "nameExplicit": false, + "name": "customer_pkey", + "schema": "public", + "table": "customer", + "entityType": "pks" + }, + { + "columns": [ + "id" + ], + "nameExplicit": false, + "name": "supplier_pkey", + "schema": "public", + "table": "supplier", + "entityType": "pks" + }, + { + "nameExplicit": false, + "columns": [ + "token" + ], + "nullsNotDistinct": false, + "name": "session_token_key", + "schema": "public", + "table": "session", + "entityType": "uniques" + }, + { + "nameExplicit": false, + "columns": [ + "email" + ], + "nullsNotDistinct": false, + "name": "user_email_key", + "schema": "public", + "table": "user", + "entityType": "uniques" + } + ], + "renames": [] +} \ No newline at end of file diff --git a/packages/db/src/relations.ts b/packages/db/src/relations.ts index ee5736b..6d8fed2 100644 --- a/packages/db/src/relations.ts +++ b/packages/db/src/relations.ts @@ -10,6 +10,7 @@ import { import { branch } from "./schema/branches"; import { contact } from "./schema/contacts"; import { customer } from "./schema/customers"; +import { supplier } from "./schema/suppliers"; const customerRelations = defineRelationsPart({ customer, user }, (r) => ({ customer: { @@ -69,6 +70,7 @@ export const relations = { customer, branch, contact, + supplier, }), ...authRelations, ...customerRelations, diff --git a/packages/db/src/schema/index.ts b/packages/db/src/schema/index.ts index 4cbb2bd..7306d4f 100644 --- a/packages/db/src/schema/index.ts +++ b/packages/db/src/schema/index.ts @@ -5,3 +5,5 @@ export * from "./branches"; export * from "./contacts"; export * from "./customers"; + +export * from "./suppliers"; diff --git a/packages/db/src/schema/suppliers.ts b/packages/db/src/schema/suppliers.ts new file mode 100644 index 0000000..8470c63 --- /dev/null +++ b/packages/db/src/schema/suppliers.ts @@ -0,0 +1,30 @@ +import { createId } from "@paralleldrive/cuid2"; +import { pgTable, text, timestamp } from "drizzle-orm/pg-core"; + +import { user } from "./auth"; + +export const supplier = pgTable("supplier", { + id: text("id") + .primaryKey() + .$defaultFn(() => createId()), + name: text("name").notNull(), + rfc: text("rfc").notNull(), + street: text("street").notNull(), + intNumber: text("int_number").notNull().default(""), + extNumber: text("ext_number").notNull(), + city: text("city").notNull(), + suburb: text("suburb").notNull(), + state: text("state").notNull(), + postalCode: text("postal_code").notNull(), + phone: text("phone").notNull(), + phoneExtension: text("phone_extension").notNull().default(""), + modifiedBy: text("modified_by") + .notNull() + .references(() => user.id), + createdAt: timestamp("created_at").defaultNow().notNull(), + updatedAt: timestamp("updated_at") + .defaultNow() + .$onUpdate(() => new Date()) + .notNull(), + archived: timestamp("archived"), +}); diff --git a/packages/db/src/validation/suppliers.ts b/packages/db/src/validation/suppliers.ts new file mode 100644 index 0000000..a5b28b0 --- /dev/null +++ b/packages/db/src/validation/suppliers.ts @@ -0,0 +1,53 @@ +import { z } from "zod"; + +import { ID_LENGTH } from "./ids"; + +export const supplierSchema = z.object({ + name: z + .string() + .trim() + .min(1, "La razón social es requerida") + .max(254, "Máximo 254 caracteres"), + rfc: z.string().regex(/^[A-ZÑ&]{3,4}\d{6}[A-Z0-9]{3}$/u, "RFC inválido"), + street: z + .string() + .trim() + .min(1, "La calle es requerida") + .max(150, "Máximo 150 caracteres"), + intNumber: z.string().trim().max(20, "Máximo 20 caracteres"), + extNumber: z + .string() + .trim() + .min(1, "El número exterior es requerido") + .max(20, "Máximo 20 caracteres"), + city: z + .string() + .trim() + .min(1, "El municipio es requerido") + .max(100, "Máximo 100 caracteres"), + suburb: z + .string() + .trim() + .min(1, "La colonia es requerida") + .max(100, "Máximo 100 caracteres"), + state: z + .string() + .trim() + .min(1, "El estado es requerido") + .max(50, "Máximo 50 caracteres"), + postalCode: z.string().regex(/^\d{5}$/u, "Debe tener 5 dígitos"), + phone: z.string().regex(/^\d{10}$/u, "Debe tener 10 dígitos"), + phoneExtension: z.string().regex(/^\d{0,10}$/u, "Solo dígitos"), +}); + +export const supplierIdSchema = z.object({ + id: z.string().length(ID_LENGTH, "Identificador de proveedor inválido"), +}); + +export const supplierListSchema = z.object({ + status: z.enum(["active", "archived", "all"]).default("active"), +}); + +export const supplierArchiveSchema = supplierIdSchema.extend({ + archived: z.boolean(), +});