Skip to content

Commit

Permalink
chore: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
AbolfazlGhaderi committed Dec 7, 2024
1 parent 3375ede commit c07ab0a
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 22 deletions.
52 changes: 51 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"pg": "^8.11.3",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"typeorm": "^0.3.20"
"typeorm": "^0.3.20",
"ua-parser-js": "^2.0.0-rc.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
5 changes: 4 additions & 1 deletion src/common/abstracts/base.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CreateDateColumn, PrimaryGeneratedColumn } from 'typeorm'
import { Column, CreateDateColumn, PrimaryGeneratedColumn } from 'typeorm'

export class BaseEntity
{
Expand All @@ -8,4 +8,7 @@ export class BaseEntity

@CreateDateColumn({ type: 'timestamp with time zone' })
created_at: Date

@Column({ type: 'boolean', nullable: true, default: false })
Deleted?: boolean
}
3 changes: 2 additions & 1 deletion src/common/enums/entity.name.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export enum EntityName
{
Users = 'users',
UserAddress = 'user_adress',
Categories = 'categories'
Categories = 'categories',
UserSessions = 'user_sessions'
}
3 changes: 2 additions & 1 deletion src/configs/database.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { TypeOrmModuleOptions } from '@nestjs/typeorm'

export function DatabaseConfigs(): TypeOrmModuleOptions
{

return {
port: +process.env.DB_PORT || 5432,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST || 'localhost',
type: 'postgres',
entities: [ './dist/entities/*.entity.js' ],
entities: [ './dist/entities/**.entity.js' ],
synchronize: true,
logging: true,
}
Expand Down
10 changes: 8 additions & 2 deletions src/entities/address.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@ import { UserEntity } from './user.entity'
import { EntityName } from '../common/enums/entity.name.enum'
import { BaseEntity } from '../common/abstracts/base.entity'

import { Column, DeleteDateColumn, Entity, JoinColumn, ManyToMany, UpdateDateColumn } from 'typeorm'
import { Column, DeleteDateColumn, Entity, JoinColumn, ManyToMany, ManyToOne, UpdateDateColumn } from 'typeorm'

@Entity({ name: EntityName.UserAddress })
export class UserAddressEntity extends BaseEntity
{
@Column()
title: string

@Column()
province: string

@Column()
city: string

@Column()
address: string

@Column({ unique: true, nullable: false })
postal_code: string

@UpdateDateColumn({ type: 'timestamptz' })
update_at: Date

@DeleteDateColumn({ type: 'timestamptz' })
delete_at: Date

@ManyToMany(() => UserEntity, (user) => user.user_address, { onDelete: 'CASCADE', nullable: false })
@ManyToOne(() => UserEntity, (user) => user.user_address, { onDelete: 'CASCADE', nullable: false })
@JoinColumn({ name: 'user_id' })
user: UserEntity
}
5 changes: 3 additions & 2 deletions src/entities/category.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class CategoryEntity extends BaseEntity

@UpdateDateColumn({ type: 'timestamptz' })
update_at: Date
@DeleteDateColumn({ type: 'timestamptz' })
delete_at: Date

@DeleteDateColumn({ type: 'timestamptz', nullable: true })
delete_at?: Date
}
30 changes: 30 additions & 0 deletions src/entities/user-session.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Column, DeleteDateColumn, Entity, JoinColumn, ManyToMany, ManyToOne, UpdateDateColumn } from 'typeorm'
import { UserEntity } from './user.entity'
import { BaseEntity } from '@/common/abstracts/base.entity'
import { EntityName } from '@/common/enums/entity.name.enum'

@Entity({ name: EntityName.UserSessions })
export class UserSessionEntity extends BaseEntity
{
@Column({ type:'text', unique: true, nullable: false })
token: string

@ManyToOne(() => UserEntity, (user) => user.user_session, { nullable: false })
@JoinColumn({ name: 'user_id' })
user: UserEntity

@Column({ type: 'timestamptz', nullable: false })
last_activity_at: Date

@Column({ type: 'enum', enum: [ 'active', 'expired' ], default: 'active' })
status: 'active' | 'expired'

@Column({ type: 'text', nullable: false })
user_agent: string

@UpdateDateColumn({ type: 'timestamptz' })
update_at: Date

@DeleteDateColumn({ type: 'timestamptz', nullable: true })
delete_at?: Date
}
13 changes: 13 additions & 0 deletions src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,41 @@ import { BaseEntity } from '../common/abstracts/base.entity'
import { EntityName } from '../common/enums/entity.name.enum'

import { Column, DeleteDateColumn, Entity, OneToMany, UpdateDateColumn } from 'typeorm'
import { UserSessionEntity } from './user-session.entity'

@Entity({ name: EntityName.Users })
export class UserEntity extends BaseEntity
{
@Column({ nullable: false })
first_name: string

@Column({ nullable: true })
last_name: string

@Column({ nullable: false, unique: true })
mobile: string

@Column({ nullable: false, unique: true })
email: string

@Column({ nullable: false, unique: true })
invite_code: string

@Column({ default: 0 })
score: number

@Column({ nullable: true })
agentId: number // maybe not needed

@UpdateDateColumn({ type: 'timestamp with time zone' })
update_at: Date

@DeleteDateColumn({ type: 'timestamp with time zone' })
delete_at: Date

@OneToMany(() => UserAddressEntity, (address) => address.user)
user_address: UserAddressEntity[]

@OneToMany(() => UserSessionEntity, (session) => session.user)
user_session: UserSessionEntity[]
}
11 changes: 0 additions & 11 deletions src/modules/category/category.repository.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { Repository } from 'typeorm'
import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { CategoryEntity } from '@/entities/category.entity'

@Injectable()
export class CategoryRepository extends Repository<CategoryEntity>
{
constructor(
@InjectRepository(CategoryEntity)
private readonly categoryRepository: Repository<CategoryEntity>,
)
{
super(categoryRepository.target, categoryRepository.manager, categoryRepository.queryRunner)
console.log('categoryRepository.target ===>', categoryRepository.target)
console.log('categoryRepository.manager ===>', categoryRepository.manager)
console.log('categoryRepository.queryRunner ===>', categoryRepository.queryRunner)
}

async findOneBySlug(slug: string)
{
Expand Down
3 changes: 1 addition & 2 deletions src/modules/category/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,9 @@ export class CategoryService
}
async deleteCategory(id: string)
{
const result = await this.categoryRepository.delete({
await this.categoryRepository.delete({
id: id,
})
console.log(result)
return {
message : PublicMessage.DeleteSuccess,
}
Expand Down

0 comments on commit c07ab0a

Please sign in to comment.