refactor: more legacy code replaced more on the model and a bit on the admin side

This commit is contained in:
user
2025-10-20 22:08:45 +03:00
parent 2fdb934ec9
commit 2ed74c267d
33 changed files with 1160 additions and 11574 deletions

View File

@@ -34,25 +34,21 @@ export const order = pgTable("order", {
fullfilledPrice: decimal("fullfilled_price", { precision: 12, scale: 2 })
.$type<string>()
.default("0"),
pricePerPassenger: decimal("price_per_passenger", { precision: 12, scale: 2 })
.$type<string>()
.default("0"),
status: varchar("status", { length: 24 }),
pnr: varchar("pnr", { length: 12 }).default("").notNull(),
flightTicketInfoId: integer("flight_ticket_info_id")
.references(() => flightTicketInfo.id, { onDelete: "no action" })
.notNull(),
productId: integer("product_id").references(() => product.id, {
onDelete: "cascade",
}),
customerInfoId: integer("customer_info_id").references(
() => customerInfo.id,
{ onDelete: "cascade" },
),
paymentDetailsId: integer("payment_details_id").references(
() => paymentDetails.id,
{ onDelete: "set null" },
{ onDelete: "cascade" },
),
emailAccountId: integer("email_account_id").references(
() => emailAccount.id,
{ onDelete: "set null" },
),
agentId: text("agent_id").references(() => user.id, { onDelete: "set null" }),
createdAt: timestamp("created_at").defaultNow(),
@@ -92,98 +88,19 @@ export const customerInfo = pgTable("customer_info", {
address: text("address").notNull(),
address2: text("address2"),
orderId: integer("order_id").references(() => order.id, {
onDelete: "cascade",
}),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});
export const paymentDetails = pgTable("payment_details", {
id: serial("id").primaryKey(),
cardholderName: varchar("cardholder_name", { length: 128 }).notNull(),
cardNumber: varchar("card_number", { length: 20 }).notNull(),
expiry: varchar("expiry", { length: 5 }).notNull(),
cvv: varchar("cvv", { length: 6 }).notNull(),
flightTicketInfoId: integer("flight_ticket_info_id").references(
() => flightTicketInfo.id,
{ onDelete: "set null" },
),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});
export const emailAccount = pgTable("email_account", {
id: serial("id").primaryKey(),
email: varchar("email", { length: 128 }).unique().notNull(),
password: varchar("password", { length: 128 }).notNull(),
used: boolean("used").default(false).notNull(),
agentId: text("agent_id").references(() => user.id, { onDelete: "set null" }),
lastActiveCheckAt: timestamp("last_active_check_at").defaultNow(),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});
export const inbox = pgTable("inbox", {
id: serial("id").primaryKey(),
emailId: text("email_id").default("").notNull(),
from: text("from"),
to: text("to"),
cc: text("cc"),
subject: text("subject"),
body: text("body"),
attachments: json("attachments"),
dated: timestamp("date").defaultNow(),
emailAccountId: integer("email_account_id")
.references(() => emailAccount.id, { onDelete: "cascade" })
.notNull(),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});
export const flightTicketInfo = pgTable("flight_ticket_info", {
id: serial("id").primaryKey(),
ticketId: text("ticket_id").default("").notNull(),
flightType: varchar("flight_type", { length: 24 }).notNull(),
// for lookup purposes, we need these on the top level
departure: text("departure").notNull(),
arrival: text("arrival").notNull(),
departureDate: timestamp("departure_date").notNull(),
returnDate: timestamp("return_date").notNull(),
dates: json("dates").$type<string[]>().default([]).notNull(),
flightIteneraries: json("flight_iteneraries").default([]).notNull(),
priceDetails: json("price_details").notNull(),
bagsInfo: json("bags_info").notNull(),
lastAvailable: json("last_available").notNull(),
refundable: boolean("refundable").default(false).notNull(),
passengerCounts: json("passenger_counts")
.default({ adult: 1, children: 0 })
.notNull(),
cabinClass: varchar("cabin_class", { length: 24 }).notNull(),
shareId: text("share_id").default("").notNull(),
checkoutUrl: text("checkout_url").default("").notNull(),
isCache: boolean("is_cache").default(false).notNull(),
refOIds: json("ref_oids").$type<number[]>().default([]).notNull(),
// Soft relation as otherwise it would get circular
orderId: integer("order_id"),
productId: integer("product_id"),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});
@@ -263,34 +180,25 @@ export const checkoutFlowSession = pgTable("checkout_flow_session", {
sessionOutcome: varchar("session_outcome", { length: 50 }),
isDeleted: boolean("is_deleted").default(false).notNull(),
ticketId: integer("ticket_id").references(() => flightTicketInfo.id, {
product: integer("product_id").references(() => product.id, {
onDelete: "set null",
}),
});
export const customerInfoRelations = relations(customerInfo, ({ one }) => ({
order: one(order, {
fields: [customerInfo.orderId],
references: [order.id],
}),
}));
export const customerInfoRelations = relations(customerInfo, ({}) => ({}));
export const orderRelations = relations(order, ({ one, many }) => ({
emailAccount: one(emailAccount, {
fields: [order.emailAccountId],
references: [emailAccount.id],
export const orderRelations = relations(order, ({ one }) => ({
product: one(product, {
fields: [order.productId],
references: [product.id],
}),
flightTicketInfo: one(flightTicketInfo, {
fields: [order.flightTicketInfoId],
references: [flightTicketInfo.id],
customerInfo: one(customerInfo, {
fields: [order.customerInfoId],
references: [customerInfo.id],
}),
customerInfos: many(customerInfo),
}));
export const inboxRelations = relations(inbox, ({ one }) => ({
emailAccount: one(emailAccount, {
fields: [inbox.emailAccountId],
references: [emailAccount.id],
paymentInfo: one(paymentDetails, {
fields: [order.paymentDetailsId],
references: [paymentDetails.id],
}),
}));