Skip to content

Commit

Permalink
🗃️ refactor (finance): Refactored DB schema into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanprince committed Feb 28, 2024
1 parent 424cd10 commit a28196d
Show file tree
Hide file tree
Showing 30 changed files with 506 additions and 2,384 deletions.
2 changes: 1 addition & 1 deletion apps/finance/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Config } from "drizzle-kit";
import { env } from "~/env";

export default {
schema: "./src/server/db/schema.ts",
schema: "./src/server/db/schema/main-schema.ts",
out: "./src/server/db/migrations",
driver: "pg",
dbCredentials: {
Expand Down
2 changes: 1 addition & 1 deletion apps/finance/src/app/api/accounts/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";

import { db } from "~/server/db";
import { financeAccount } from "~/server/db/schema";
import { financeAccount } from "~/server/db/schema/main-schema";

export async function GET(
request: Request,
Expand Down
2 changes: 1 addition & 1 deletion apps/finance/src/app/api/accounts/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";

import { db } from "~/server/db";
import { financeAccount } from "~/server/db/schema";
import { financeAccount } from "~/server/db/schema/main-schema";

interface RequestBody {
studentId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";

import { db } from "~/server/db";
import { financeAccount } from "~/server/db/schema";
import { financeAccount } from "~/server/db/schema/main-schema";

export async function GET(
request: Request,
Expand All @@ -12,7 +12,7 @@ export async function GET(
const account = await db.query.financeAccount.findFirst({
where: eq(financeAccount.studentId, studentId),
columns: {
id: true,
financeAccountId: true,
studentId: true,
hasOutstandingBalance: true,
},
Expand Down
2 changes: 1 addition & 1 deletion apps/finance/src/app/api/invoices/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";

import { db } from "~/server/db";
import { invoice } from "~/server/db/schema";
import { invoice } from "~/server/db/schema/main-schema";

export async function GET(
request: Request,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";

import { db } from "~/server/db";
import { invoice } from "~/server/db/schema";
import { invoice } from "~/server/db/schema/main-schema";

export async function PUT(
request: Request,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";

import { db } from "~/server/db";
import { invoice } from "~/server/db/schema";
import { invoice } from "~/server/db/schema/main-schema";

export async function DELETE(
request: Request,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";

import { db } from "~/server/db";
import { invoice } from "~/server/db/schema";
import { invoice } from "~/server/db/schema/main-schema";

export async function PUT(
request: Request,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";

import { db } from "~/server/db";
import { invoice } from "~/server/db/schema";
import { invoice } from "~/server/db/schema/main-schema";

export async function GET(
request: Request,
Expand Down
2 changes: 1 addition & 1 deletion apps/finance/src/app/api/invoices/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { asc } from "drizzle-orm";

import { generateRandomReferenceString } from "~/lib/utils";
import { db } from "~/server/db";
import { invoice } from "~/server/db/schema";
import { invoice } from "~/server/db/schema/main-schema";

type RequestBody = typeof invoice.$inferInsert;

Expand Down
6 changes: 3 additions & 3 deletions apps/finance/src/app/pay/invoice/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { format } from "date-fns";
import { ArrowLeft } from "lucide-react";

import type { invoice } from "~/server/db/schema";
import type { invoice } from "~/server/db/schema/main-schema";
import { Badge } from "~/components/ui/badge";
import { Button } from "~/components/ui/button";
import { env } from "~/env";
Expand All @@ -27,7 +27,7 @@ export default async function Page({ params }: { params: { id: string } }) {
<h1 className="text-3xl font-semibold tracking-tight text-foreground">
Invoice Details
</h1>
<p className="text-muted-foreground">Invoice #{invoice.id}</p>
<p className="text-muted-foreground">Invoice #{invoice.invoiceId}</p>
</div>

{/* MAIN CONTENT */}
Expand Down Expand Up @@ -91,7 +91,7 @@ function InvoiceCard({ invoice }: { invoice: Invoice }) {
<div className="flex flex-col gap-3 border-y py-8 text-sm">
<div className="flex items-center justify-between">
<h2 className="text-muted-foreground">Invoice ID</h2>
<p>{invoice.id}</p>
<p>{invoice.invoiceId}</p>
</div>
<div className="flex items-center justify-between">
<h2 className="text-muted-foreground">Student ID</h2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useRouter } from "next/navigation";
import { CreditCard } from "lucide-react";
import { toast } from "sonner";

import type { invoice } from "~/server/db/schema";
import type { invoice } from "~/server/db/schema/main-schema";
import { Button } from "~/components/ui/button";
import {
Dialog,
Expand Down
2 changes: 1 addition & 1 deletion apps/finance/src/server/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

import { env } from "~/env.js";
import * as schema from "./schema";
import * as schema from "./schema/main-schema";

// Fix for "sorry, too many clients already"
declare global {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
DO $$ BEGIN
CREATE TYPE "INVOICE_STATUS" AS ENUM('PAID', 'OUTSTANDING', 'PARTIALLY_PAID', 'CANCELLED');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "INVOICE_TYPE" AS ENUM('TUITION_FEES', 'LIBRARY_FINE');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "PAYMENT_METHOD" AS ENUM('CASH', 'CHEQUE', 'BANK_TRANSFER', 'CREDIT_CARD', 'DEBIT_CARD');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "account" (
"userId" varchar(255) NOT NULL,
"type" varchar(255) NOT NULL,
Expand All @@ -14,12 +32,42 @@ CREATE TABLE IF NOT EXISTS "account" (
CONSTRAINT "account_provider_providerAccountId_pk" PRIMARY KEY("provider","providerAccountId")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "finance_account" (
"finance_account_id" serial PRIMARY KEY NOT NULL,
"student_id" varchar NOT NULL,
"has_outstanding_balance" boolean DEFAULT false,
CONSTRAINT "finance_account_student_id_unique" UNIQUE("student_id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "invoice" (
"invoice_id" serial PRIMARY KEY NOT NULL,
"student_id" varchar NOT NULL,
"reference_id" varchar NOT NULL,
"amount" numeric NOT NULL,
"due_date" date NOT NULL,
"invoice_type" "INVOICE_TYPE" NOT NULL,
"invoice_status" "INVOICE_STATUS" DEFAULT 'OUTSTANDING',
"created_at" timestamp DEFAULT now(),
"updated_at" timestamp DEFAULT now(),
CONSTRAINT "invoice_reference_id_unique" UNIQUE("reference_id")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "session" (
"sessionToken" varchar(255) PRIMARY KEY NOT NULL,
"userId" varchar(255) NOT NULL,
"expires" timestamp NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "transaction" (
"transaction_id" serial PRIMARY KEY NOT NULL,
"invoice_id" varchar NOT NULL,
"amount" numeric NOT NULL,
"transaction_date" date NOT NULL,
"payment_method" "PAYMENT_METHOD" NOT NULL,
"created_at" timestamp DEFAULT now(),
"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user" (
"id" varchar(255) PRIMARY KEY NOT NULL,
"name" varchar(255),
Expand Down

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions apps/finance/src/server/db/migrations/0003_vengeful_punisher.sql

This file was deleted.

16 changes: 0 additions & 16 deletions apps/finance/src/server/db/migrations/0004_worried_hellion.sql

This file was deleted.

3 changes: 0 additions & 3 deletions apps/finance/src/server/db/migrations/0005_late_whistler.sql

This file was deleted.

Loading

0 comments on commit a28196d

Please sign in to comment.