task-managment / src / database / schema / project.ts
project.ts
Raw
import { relations } from "drizzle-orm";
import {
  pgTable,
  pgEnum,
  index,
  timestamp,
  varchar,
  bigint,
  uuid,
  text,
} from "drizzle-orm/pg-core";
import { floor } from "./floor";
import { task } from "./task";
import { projectMaterials } from "./projectMaterials";
import { z } from "zod";
import { projectStatusEnum as projectStatusEnumValues,typeCotizationEnum as typeCotizationEnumValues,typeEnum as typeEnumValues } from "@/server/project/domain/models";

export const type = pgEnum("type", typeEnumValues);
export const typeEnum = z.enum(type.enumValues);

export const projectStatus=pgEnum("status", projectStatusEnumValues) 

export const projectStatusEnum = z.enum(projectStatus.enumValues, {
	message: "Estatus invalido.",
});

export const project = pgTable(
  "project",
  {
    id: uuid("id").primaryKey().notNull(),
    name: varchar("name", { length: 250 }).notNull(),
    residence: text("residence").notNull(),
    costPerMeter: bigint("costPerMeter", { mode: "number" })
      .notNull()
      .default(0),
    totalCostPerMeter: bigint("totalCostPerMeter", { mode: "number" })
      .default(0),
    totalCostMaterials: bigint("totalCostMaterials", { mode: "number" })
      .default(0),
    laborCost: bigint("laborCost", { mode: "number" }).notNull().default(0),
    startDate: timestamp("startDate", { mode: "date" }).notNull().defaultNow(),
    typeCotization: text('type_cotization').notNull(),
    estimadedEndDate: timestamp("estimadedEndDate", { mode: "date" })
      .notNull()
      .defaultNow(),
    status: projectStatus("status").default("no iniciada").notNull(),
    createdAT: timestamp("created_at").defaultNow().notNull(),
    updatedAt:timestamp('updated_at').defaultNow().$onUpdate(()=>new Date())
  },
  (t) => ({
    project_name: index("project_name").on(t.name),
    project_startDate: index("startDate").on(t.startDate),
    project_status: index("project_status").on(t.status),
  })
);

export const projectRelations = relations(project, ({ many }) => ({
  floor: many(floor),
  task: many(task),
  projectMaterials: many(projectMaterials),
}));