-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor seed + add transactions tables
- Loading branch information
1 parent
47a558c
commit 5235e6a
Showing
9 changed files
with
244 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { char, decimal, mysqlTable, timestamp, varchar } from "drizzle-orm/mysql-core"; | ||
import { randomUUID } from "node:crypto"; | ||
import { usersTable } from "./users"; | ||
import { sql } from "drizzle-orm"; | ||
|
||
export const transactionsTable = mysqlTable("transactions", { | ||
id: char("id", { length: 36 }).primaryKey().$default(() => randomUUID()), | ||
status: varchar("status", { length: 255 }).notNull(), | ||
totalAmount: decimal("total_amount", { precision: 10, scale: 2 }).notNull(), | ||
paidAmount: decimal("paid_amount", { precision: 10, scale: 2 }).notNull().default(sql`0.00`), | ||
pendingAmount: decimal("pending_amount", { precision: 10, scale: 2 }).notNull().default(sql`0.00`), | ||
tax: decimal("tax", { precision: 10, scale: 2 }).default(sql`0.00`), | ||
fee: decimal("fee", { precision: 10, scale: 2 }).default(sql`0.00`), | ||
location: varchar("location", { length: 255 }).notNull(), | ||
description: varchar("description", { length: 255 }).notNull(), | ||
employeeId: char("employee_id", { length: 36 }).references(() => usersTable.id).notNull(), | ||
categoryId: char("category_id", { length: 36 }).references(() => transactionCategoriesTable.id).notNull(), | ||
createdAt: timestamp("created_at").notNull().defaultNow(), | ||
updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(), | ||
}); | ||
|
||
export const transactionPaymentsTable = mysqlTable("transaction_payments", { | ||
id: char("id", { length: 36 }).primaryKey().$default(() => randomUUID()), | ||
amount: decimal("amount", { precision: 10, scale: 2 }).notNull(), | ||
description: varchar("description", { length: 255 }).notNull(), | ||
currency: varchar("currency", { length: 255 }).notNull(), | ||
method: char("method", { length: 36 }).references(() => paymentMethodsTable.id).notNull(), | ||
transactionId: char("transaction_id", { length: 36 }).references(() => transactionsTable.id).notNull(), | ||
createdAt: timestamp("created_at").notNull().defaultNow(), | ||
updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(), | ||
}); | ||
|
||
export const transactionCategoriesTable = mysqlTable("transaction_categories", { | ||
id: char("id", { length: 36 }).primaryKey().$default(() => randomUUID()), | ||
name: varchar("name", { length: 255 }).notNull().unique(), | ||
createdAt: timestamp("created_at").notNull().defaultNow(), | ||
updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(), | ||
}); | ||
|
||
export const paymentMethodsTable = mysqlTable("payment_methods", { | ||
id: char("id", { length: 36 }).primaryKey().$default(() => randomUUID()), | ||
name: varchar("name", { length: 255 }).notNull().unique(), | ||
createdAt: timestamp("created_at").notNull().defaultNow(), | ||
updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { db } from '@/db/database'; | ||
import { rolesTable } from '@/db/schema/roles'; | ||
import { usersTable } from '@/db/schema/users'; | ||
import { genSalt, hash } from 'bcrypt'; | ||
import { eq, or } from 'drizzle-orm'; | ||
import { paymentMethodsTable, transactionCategoriesTable } from './schema/transactions'; | ||
|
||
async function seedUsers() { | ||
await db.transaction(async (tx) => { | ||
const existentAdminRole = await tx.select().from(rolesTable).where(eq(rolesTable.name, "admin")); | ||
const existentUserRole = await tx.select().from(rolesTable).where(eq(rolesTable.name, "user")); | ||
|
||
const roles = []; | ||
if (existentAdminRole.length === 0) { | ||
roles.push({ | ||
name: "admin", | ||
description: "Administrator", | ||
}); | ||
} | ||
|
||
if (existentUserRole.length === 0) { | ||
roles.push({ | ||
name: "user", | ||
description: "User", | ||
}); | ||
} | ||
|
||
if (roles.length === 0) return; | ||
|
||
const savedIds = await tx.insert(rolesTable).values(roles).$returningId(); | ||
const savedRoles = await tx.select().from(rolesTable).where(or(...savedIds.map((savedId) => eq(rolesTable.id, savedId.id)))); | ||
console.log(savedRoles); | ||
}); | ||
|
||
await db.transaction(async (tx) => { | ||
const exists = await tx.select().from(usersTable).where(eq(usersTable.email, 'admin@admin.com')); | ||
if (exists.length > 0) return; | ||
|
||
const roles = await tx.select({ id: rolesTable.id }).from(rolesTable).where(eq(rolesTable.name, 'admin')); | ||
|
||
if (roles.length === 0) { | ||
console.error('Admin role not found!'); | ||
return; | ||
} | ||
|
||
const savedId = await tx.insert(usersTable).values({ | ||
name: 'Admin', | ||
email: 'admin@admin.com', | ||
roleId: roles[0].id, | ||
password: await hash(process.env.ADMIN_PASSWORD!, await genSalt()), | ||
}).$returningId(); | ||
|
||
const savedUser = await tx.select().from(usersTable).where(eq(usersTable.id, savedId[0].id)); | ||
if (savedUser.length === 0) { | ||
console.error('Admin user not found!'); | ||
return; | ||
} | ||
console.log(savedUser[0]); | ||
}); | ||
} | ||
|
||
async function seedPaymentMethods() { | ||
// Cash | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(paymentMethodsTable).where(eq(paymentMethodsTable.name, "Cash")); | ||
if (existent.length > 0) return; | ||
await tx.insert(paymentMethodsTable).values({ | ||
name: "Cash", | ||
}); | ||
}); | ||
|
||
// Credit Card | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(paymentMethodsTable).where(eq(paymentMethodsTable.name, "Credit Card")); | ||
if (existent.length > 0) return; | ||
await tx.insert(paymentMethodsTable).values({ | ||
name: "Credit Card", | ||
}); | ||
}); | ||
|
||
// Debit Card | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(paymentMethodsTable).where(eq(paymentMethodsTable.name, "Debit Card")); | ||
if (existent.length > 0) return; | ||
await tx.insert(paymentMethodsTable).values({ | ||
name: "Debit Card", | ||
}); | ||
}); | ||
} | ||
|
||
async function seedTransactionCategories() { | ||
// Food | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(transactionCategoriesTable).where(eq(transactionCategoriesTable.name, "Food")); | ||
if (existent.length > 0) return; | ||
await tx.insert(transactionCategoriesTable).values({ | ||
name: "Food", | ||
}); | ||
}); | ||
|
||
//Transportation | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(transactionCategoriesTable).where(eq(transactionCategoriesTable.name, "Transportation")); | ||
if (existent.length > 0) return; | ||
await tx.insert(transactionCategoriesTable).values({ | ||
name: "Transportation", | ||
}); | ||
}); | ||
|
||
//Entertainment | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(transactionCategoriesTable).where(eq(transactionCategoriesTable.name, "Entertainment")); | ||
if (existent.length > 0) return; | ||
await tx.insert(transactionCategoriesTable).values({ | ||
name: "Entertainment", | ||
}); | ||
}); | ||
|
||
// Health | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(transactionCategoriesTable).where(eq(transactionCategoriesTable.name, "Health")); | ||
if (existent.length > 0) return; | ||
await tx.insert(transactionCategoriesTable).values({ | ||
name: "Health", | ||
}); | ||
}); | ||
|
||
// Cleaning | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(transactionCategoriesTable).where(eq(transactionCategoriesTable.name, "Cleaning")); | ||
if (existent.length > 0) return; | ||
await tx.insert(transactionCategoriesTable).values({ | ||
name: "Cleaning", | ||
}); | ||
}); | ||
|
||
// Technology | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(transactionCategoriesTable).where(eq(transactionCategoriesTable.name, "Technology")); | ||
if (existent.length > 0) return; | ||
await tx.insert(transactionCategoriesTable).values({ | ||
name: "Technology", | ||
}); | ||
}); | ||
|
||
// Stationery | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(transactionCategoriesTable).where(eq(transactionCategoriesTable.name, "Stationery")); | ||
if (existent.length > 0) return; | ||
await tx.insert(transactionCategoriesTable).values({ | ||
name: "Stationery", | ||
}); | ||
}); | ||
|
||
// Other | ||
await db.transaction(async (tx) => { | ||
const existent = await tx.select().from(transactionCategoriesTable).where(eq(transactionCategoriesTable.name, "Other")); | ||
if (existent.length > 0) return; | ||
await tx.insert(transactionCategoriesTable).values({ | ||
name: "Other", | ||
}); | ||
}); | ||
} | ||
|
||
async function main() { | ||
|
||
await seedUsers(); | ||
console.log('Users and roles seeded!'); | ||
await seedPaymentMethods(); | ||
console.log('Payment methods seeded!'); | ||
await seedTransactionCategories(); | ||
console.log('Transaction categories seeded!'); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
import { Request, Response } from "express"; | ||
import { verify } from "jsonwebtoken"; | ||
|
||
export const getSession = async (req: Request, res: Response) => { | ||
export const getUserFromToken = async (req: Request, res: Response) => { | ||
const accessToken = req.cookies.access_token; | ||
|
||
if (!accessToken) { | ||
res.status(401).json({ errors: ["Unauthorized"] }); | ||
return; | ||
} | ||
|
||
const payload = verify(accessToken, process.env.ACCESS_TOKEN_SECRET as string); | ||
res.json(payload); | ||
try { | ||
const payload = verify(accessToken, process.env.ACCESS_TOKEN_SECRET as string); | ||
res.json(payload); | ||
} catch (error) { | ||
res.clearCookie("access_token"); | ||
res.status(500).json({ errors: ["Token is invalid"] }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { getSession } from "@/handlers/session"; | ||
import { getUserFromToken } from "@/handlers/session"; | ||
import { Router } from "express"; | ||
|
||
const router = Router(); | ||
|
||
router.get( | ||
"/", | ||
getSession | ||
getUserFromToken | ||
); | ||
|
||
export default router; |
Oops, something went wrong.