Skip to content

Commit

Permalink
Merge pull request #127 from GenieWizards/feat/settlement-model
Browse files Browse the repository at this point in the history
feat(settlement.model.ts): create settlement model
  • Loading branch information
shivamvijaywargi authored Dec 25, 2024
2 parents 9058d8d + e024db6 commit 67cdffc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
67 changes: 67 additions & 0 deletions src/db/schemas/settlement.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
pgEnum,
pgTable,
real,
timestamp,
varchar,
} from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import type { z } from "zod";

import { splitTypeArr } from "@/common/enums";

export const splitTypeEnum = pgEnum("splitType", splitTypeArr);

const settlementModel = pgTable("settlement", {
id: varchar({ length: 60 })
.$defaultFn(() => Bun.randomUUIDv7())
.primaryKey(),
senderId: varchar({ length: 60 })
.notNull(),
receiverId: varchar({ length: 60 })
.notNull(),
groupId: varchar({ length: 60 }),
amount: real().notNull(),
createdAt: timestamp().notNull().defaultNow(),
updatedAt: timestamp().notNull().defaultNow(),
});

// Schema for selecting/inserting a settlement
export const selectSettlementSchema = createSelectSchema(settlementModel, {
id: schema => schema.id.describe("Unique identifier for the settlement"),
senderId: schema =>
schema.senderId.describe("Reference to the user who is payer"),
receiverId: schema =>
schema.receiverId.describe("Reference to the user who is ower "),
groupId: schema =>
schema.groupId.describe(
"Reference to the group the settlement belongs to",
),
amount: schema => schema.amount.describe("Amount of the settlement"),
createdAt: schema =>
schema.createdAt.describe("Timestamp when the settlement was created"),
updatedAt: schema =>
schema.updatedAt.describe("Timestamp when the settlement was last updated"),
});

export const insertSettlementSchema = createInsertSchema(settlementModel, {
id: schema => schema.id.describe("Unique identifier for the settlement"),
senderId: schema =>
schema.senderId.describe("Reference to the user who is payer"),
receiverId: schema =>
schema.receiverId.describe("Reference to the user who is ower "),
groupId: schema =>
schema.groupId.describe(
"Reference to the group the settlement belongs to",
),
amount: schema => schema.amount.describe("Amount of the settlement"),
createdAt: schema =>
schema.createdAt.describe("Timestamp when the settlement was created"),
updatedAt: schema =>
schema.updatedAt.describe("Timestamp when the settlement was last updated"),
});

export type TSelectSettlementSchema = z.infer<typeof selectSettlementSchema>;
export type TInsertSettlementSchema = z.infer<typeof insertSettlementSchema>;

export default settlementModel;
10 changes: 0 additions & 10 deletions src/db/schemas/split.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
boolean,
pgTable,
real,
timestamp,
Expand Down Expand Up @@ -31,7 +30,6 @@ const splitModel = pgTable("split", {
}),

amount: real().notNull(),
isSettled: boolean().default(false),

createdAt: timestamp().notNull().defaultNow(),
updatedAt: timestamp().notNull().defaultNow(),
Expand All @@ -46,10 +44,6 @@ export const selectSplitSchema = createSelectSchema(splitModel, {
schema.expenseId.describe("Reference to the expense being split"),
amount: schema =>
schema.amount.describe("Amount to be paid by this user in the split"),
isSettled: schema =>
schema.isSettled.describe(
"Whether this split amount has been settled/paid",
),
createdAt: schema =>
schema.createdAt.describe("Timestamp when the split was created"),
updatedAt: schema =>
Expand All @@ -63,10 +57,6 @@ export const insertSplitSchema = createInsertSchema(splitModel, {
schema.expenseId.describe("Reference to the expense being split"),
amount: schema =>
schema.amount.describe("Amount to be paid by this user in the split"),
isSettled: schema =>
schema.isSettled.describe(
"Whether this split amount has been settled/paid",
),
createdAt: schema =>
schema.createdAt.describe("Timestamp when the split was created"),
updatedAt: schema =>
Expand Down

0 comments on commit 67cdffc

Please sign in to comment.