Skip to content
Snippets Groups Projects
Commit 944afe73 authored by nar20's avatar nar20
Browse files

Issue #7: ALTER user model and its related files

parent 204a5c8b
No related branches found
No related tags found
1 merge request!4Issue #7: ALTER user model and its related files
Showing with 46 additions and 680 deletions
CREATE TABLE IF NOT EXISTS "user" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"username" varchar NOT NULL,
"password" varchar NOT NULL,
"name" varchar NOT NULL,
CONSTRAINT "user_username_unique" UNIQUE("username")
);
CREATE TABLE IF NOT EXISTS "client" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"name" varchar(255) NOT NULL,
"cnpj" varchar(20) NOT NULL,
"industry" varchar(50),
"hq_address" varchar(255),
"phone" varchar(20),
"email" varchar(255),
"contact_person" varchar(255),
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "client_cnpj_unique" UNIQUE("cnpj"),
CONSTRAINT "client_email_unique" UNIQUE("email")
);
CREATE TABLE IF NOT EXISTS "credit_card" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"client_id" uuid NOT NULL,
"card_number" varchar(20) NOT NULL,
"cardholder_name" varchar(255) NOT NULL,
"expiration_date" date NOT NULL,
"cvv" varchar(5) NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "credit_card_card_number_unique" UNIQUE("card_number")
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "credit_card" ADD CONSTRAINT "credit_card_client_id_client_id_fk" FOREIGN KEY ("client_id") REFERENCES "public"."client"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
{
"id": "4cf89ad7-1672-440b-b964-dad35de18ec8",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"username": {
"name": "username",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"password": {
"name": "password",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"user_username_unique": {
"name": "user_username_unique",
"nullsNotDistinct": false,
"columns": [
"username"
]
}
}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
\ No newline at end of file
{
"id": "b1587c07-98a9-4039-ae74-c1f6fc5f0823",
"prevId": "4cf89ad7-1672-440b-b964-dad35de18ec8",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.client": {
"name": "client",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"cnpj": {
"name": "cnpj",
"type": "varchar(20)",
"primaryKey": false,
"notNull": true
},
"industry": {
"name": "industry",
"type": "varchar(50)",
"primaryKey": false,
"notNull": false
},
"hq_address": {
"name": "hq_address",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"phone": {
"name": "phone",
"type": "varchar(20)",
"primaryKey": false,
"notNull": false
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"contact_person": {
"name": "contact_person",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"client_cnpj_unique": {
"name": "client_cnpj_unique",
"nullsNotDistinct": false,
"columns": [
"cnpj"
]
},
"client_email_unique": {
"name": "client_email_unique",
"nullsNotDistinct": false,
"columns": [
"email"
]
}
}
},
"public.user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"username": {
"name": "username",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"password": {
"name": "password",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"user_username_unique": {
"name": "user_username_unique",
"nullsNotDistinct": false,
"columns": [
"username"
]
}
}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
\ No newline at end of file
{
"id": "3e1ec9b7-7574-46ca-aec8-0f0fa2c41943",
"prevId": "b1587c07-98a9-4039-ae74-c1f6fc5f0823",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.client": {
"name": "client",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"cnpj": {
"name": "cnpj",
"type": "varchar(20)",
"primaryKey": false,
"notNull": true
},
"industry": {
"name": "industry",
"type": "varchar(50)",
"primaryKey": false,
"notNull": false
},
"hq_address": {
"name": "hq_address",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"phone": {
"name": "phone",
"type": "varchar(20)",
"primaryKey": false,
"notNull": false
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"contact_person": {
"name": "contact_person",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"client_cnpj_unique": {
"name": "client_cnpj_unique",
"nullsNotDistinct": false,
"columns": [
"cnpj"
]
},
"client_email_unique": {
"name": "client_email_unique",
"nullsNotDistinct": false,
"columns": [
"email"
]
}
}
},
"public.credit_card": {
"name": "credit_card",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"client_id": {
"name": "client_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"card_number": {
"name": "card_number",
"type": "varchar(20)",
"primaryKey": false,
"notNull": true
},
"cardholder_name": {
"name": "cardholder_name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"expiration_date": {
"name": "expiration_date",
"type": "date",
"primaryKey": false,
"notNull": true
},
"cvv": {
"name": "cvv",
"type": "varchar(5)",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"credit_card_client_id_client_id_fk": {
"name": "credit_card_client_id_client_id_fk",
"tableFrom": "credit_card",
"tableTo": "client",
"columnsFrom": [
"client_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"credit_card_card_number_unique": {
"name": "credit_card_card_number_unique",
"nullsNotDistinct": false,
"columns": [
"card_number"
]
}
}
},
"public.user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"username": {
"name": "username",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"password": {
"name": "password",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"user_username_unique": {
"name": "user_username_unique",
"nullsNotDistinct": false,
"columns": [
"username"
]
}
}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
\ No newline at end of file
......@@ -2,26 +2,5 @@
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "7",
"when": 1719362169477,
"tag": "0000_aspiring_frightful_four",
"breakpoints": true
},
{
"idx": 1,
"version": "7",
"when": 1719970953846,
"tag": "0001_normal_marvex",
"breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1720096335732,
"tag": "0002_lying_beyonder",
"breakpoints": true
}
]
}
\ No newline at end of file
import userTable from './user.model'
import clientTable, { clientTableRelations } from './client.model'
import creditCardTable, {
creditCardTableRelations,
} from './credit-card.model'
export {
userTable,
clientTable,
clientTableRelations,
creditCardTable,
creditCardTableRelations,
userTable
}
export const tables = [userTable, clientTable, creditCardTable]
export const tables = [userTable]
import { pgTable, uuid, varchar } from 'drizzle-orm/pg-core'
import { sql } from 'drizzle-orm'
import { boolean, pgTable, serial, text, timestamp, varchar } from 'drizzle-orm/pg-core'
import { createInsertSchema, createSelectSchema } from 'drizzle-zod'
import { z } from 'zod'
const userTable = pgTable('user', {
id: uuid('id').primaryKey().defaultRandom(),
username: varchar('username').unique().notNull(),
password: varchar('password').notNull(),
name: varchar('name').notNull(),
id: serial('id').primaryKey()
.unique()
.notNull(),
name: varchar('name', { length: 255 })
.notNull(),
username: varchar('username', { length: 255 })
.unique()
.notNull(),
password: varchar('password', { length: 255 })
.notNull(),
email: varchar('email', { length: 255 })
.unique()
.notNull(),
description: text('description'),
institution: text('institution'),
birthday: timestamp('birthday', { mode: 'string' })
.notNull(),
cpf: varchar('cpf', { length: 255 })
.notNull(),
createdAt: timestamp('created_at', { mode: 'string' })
.notNull()
.defaultNow(),
updatedAt: timestamp('updated_at', { mode: 'string' })
.notNull()
.defaultNow()
.$onUpdate(() => sql`current_timestamp`),
confirmed_at: timestamp('confirmed_at', { mode: 'string' }),
confirmation_sent_at: timestamp('confirmation_sent_at', { mode: 'string' }),
deleted_at: timestamp('deleted_at', { mode: 'string' }),
reactivated_at: timestamp('reactivated_at', { mode: 'string' }),
active: boolean('active')
})
const userInputSchema = createInsertSchema(userTable)
const userModelSchema = createSelectSchema(userTable)
const userDtoSchema = createSelectSchema(userTable).omit({
password: true,
password: true
})
const userUpdateSchema = createSelectSchema(userTable)
.partial()
......
......@@ -21,7 +21,5 @@ for (const table of schema.tables) {
}
await seeds.userSeed(db)
await seeds.clientSeed(db)
await seeds.creditCardSeed(db)
await connection.end()
import type db from '..'
import type { ClientInput } from '../schema/client.model'
import clientTable from '../schema/client.model'
export default async function seed(db: db) {
await db.insert(clientTable).values(clientData)
}
const clientData: ClientInput[] = [
{
id: 'f1b9b3b4-0b3b-4b3b-8b3b-0b3b4b3b4b3b',
name: 'Acme Corp',
cnpj: '12.345.678/0001-91',
industry: 'Manufacturing',
hqAddress: '1234 Industry Rd, City',
phone: '123-456-7890',
email: 'contact@acmecorp.com',
contactPerson: 'John Doe',
},
{
id: '12345678-1234-1234-1234-123456789012',
name: 'Example Corp',
cnpj: '98.765.432/0001-21',
industry: 'Technology',
hqAddress: '5678 Tech St, City',
phone: '987-654-3210',
email: 'contact@examplecorp.com',
contactPerson: 'Jane Smith',
},
{
id: '87654321-4321-4321-4321-210987654321',
name: 'XYZ Corp',
cnpj: '11.223.344/0001-55',
industry: 'Finance',
hqAddress: '9876 Finance St, City',
phone: '555-555-5555',
email: 'contact@xyzcorp.com',
contactPerson: 'Bob Johnson',
},
{
id: '98765432-2345-2345-2345-543210987654',
name: 'ABC Corp',
cnpj: '99.888.777/0001-33',
industry: 'Retail',
hqAddress: '5432 Retail St, City',
phone: '111-222-3333',
email: 'contact@abccorp.com',
contactPerson: 'Alice Brown',
},
{
id: '23456789-3456-3456-3456-654321098765',
name: 'DEF Corp',
cnpj: '44.555.666/0001-44',
industry: 'Healthcare',
hqAddress: '8765 Healthcare St, City',
phone: '999-888-7777',
email: 'contact@defcorp.com',
contactPerson: 'David Wilson',
},
{
id: '34567890-4567-4567-4567-765432109876',
name: 'GHI Corp',
cnpj: '77.888.999/0001-66',
industry: 'Education',
hqAddress: '2345 Education St, City',
phone: '333-444-5555',
email: 'contact@ghicorp.com',
contactPerson: 'Grace Thompson',
},
{
id: '45678901-5678-5678-5678-876543210987',
name: 'JKL Corp',
cnpj: '22.333.444/0001-77',
industry: 'Hospitality',
hqAddress: '7654 Hospitality St, City',
phone: '777-666-5555',
email: 'contact@jklcorp.com',
contactPerson: 'James Davis',
},
{
id: '56789012-6789-6789-6789-987654321098',
name: 'MNO Corp',
cnpj: '66.777.888/0001-88',
industry: 'Transportation',
hqAddress: '1234 Transportation St, City',
phone: '222-111-9999',
email: 'contact@mnocorp.com',
contactPerson: 'Mary Johnson',
},
{
id: '67890123-7890-7890-7890-098765432109',
name: 'PQR Corp',
cnpj: '33.444.555/0001-99',
industry: 'Energy',
hqAddress: '9876 Energy St, City',
phone: '444-333-2222',
email: 'contact@pqrcorp.com',
contactPerson: 'Peter Wilson',
},
{
id: '78901234-8901-8901-8901-987654321098',
name: 'STU Corp',
cnpj: '55.666.777/0001-00',
industry: 'Telecommunications',
hqAddress: '5432 Telecom St, City',
phone: '888-999-0000',
email: 'contact@stucorp.com',
contactPerson: 'Sarah Brown',
},
]
import type db from '..'
import { creditCardTable } from '../schema'
import type { CreditCardInput } from '../schema/credit-card.model'
export default async function seed(db: db) {
await db.insert(creditCardTable).values(creditCardData)
}
const creditCardData: CreditCardInput[] = [
{
clientId: 'f1b9b3b4-0b3b-4b3b-8b3b-0b3b4b3b4b3b',
cardNumber: '1234 5678 9012 3456',
cardholderName: 'John Doe',
expirationDate: '2025-12-31',
cvv: '123',
},
{
clientId: 'f1b9b3b4-0b3b-4b3b-8b3b-0b3b4b3b4b3b',
cardNumber: '9876 5432 1098 7654',
cardholderName: 'Jane Smith',
expirationDate: '2024-06-30',
cvv: '456',
},
{
clientId: '12345678-1234-1234-1234-123456789012',
cardNumber: '5555 4444 3333 2222',
cardholderName: 'Alice Johnson',
expirationDate: '2023-09-15',
cvv: '789',
},
{
clientId: '12345678-1234-1234-1234-123456789012',
cardNumber: '1111 2222 3333 4444',
cardholderName: 'Bob Williams',
expirationDate: '2026-03-01',
cvv: '012',
},
{
clientId: '12345678-1234-1234-1234-123456789012',
cardNumber: '9999 8888 7777 6666',
cardholderName: 'Sarah Davis',
expirationDate: '2025-11-30',
cvv: '345',
},
{
clientId: '87654321-4321-4321-4321-210987654321',
cardNumber: '4444 5555 6666 7777',
cardholderName: 'Michael Brown',
expirationDate: '2022-07-10',
cvv: '678',
},
]
......@@ -8,13 +8,19 @@ export default async function seed(db: db) {
const usersData: UserInput[] = [
{
name: 'Admin name',
username: 'admin',
password: hashPassword('1234mudar'),
name: 'Admin name',
email: 'admin@admin.com',
birthday: '2000-06-06',
cpf: '12346579800'
},
{
name: 'Client name',
username: 'client user',
password: hashPassword('1234mudar'),
name: 'Client name',
email: 'client@client.com',
birthday: '2000-06-06',
cpf: '12346579801'
},
]
......@@ -10,9 +10,7 @@ import { uploaderRouter } from './routes/uploader.route'
import { prettyJSON } from 'hono/pretty-json'
import { cors } from 'hono/cors'
import { bodyLimit } from 'hono/body-limit'
import { clientRouter } from './routes/client.route'
import { HttpStatus, createApexError } from './services/error.service'
import { creditCardRouter } from './routes/credit-card.route'
const app = new Hono()
......@@ -52,8 +50,6 @@ app.route('/signup', signUpRouter)
app
.basePath('/api')
.route('/user', userRouter)
.route('/client', clientRouter)
.route('/credit-card', creditCardRouter)
.route('/upload', uploaderRouter)
export default app
......
......@@ -78,9 +78,9 @@ export const userRouter = honoWithJwt()
.post('/delete/:id',
async (c) => {
try {
const id = c.req.param('id')
const id: number = +c.req.param('id')
const user = userSchemas.userDtoSchema.parse(
await service.delete(id!)
await service.delete(id)
)
return c.json(user)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment