build table user activity & user refresh token

This commit is contained in:
2024-11-26 13:33:13 +07:00
parent df875117b0
commit 6e71588f91
4 changed files with 55 additions and 78 deletions

View File

@ -1,53 +1,54 @@
import bcrypt from 'bcryptjs'; import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, DeleteDateColumn, Index } from 'typeorm';
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, DeleteDateColumn } from 'typeorm';
import { Role } from '../types/role'; import { ActivityType } from '../types/action';
import { Status } from '../types/status';
@Entity('users') @Entity('user_activities')
export class User { export class UserActivity {
@PrimaryGeneratedColumn('uuid') @PrimaryGeneratedColumn('uuid')
id: string; id: string;
@Index()
@Column({
type: 'uuid'
})
id_user: string;
@Index()
@Column({
type: 'uuid'
})
refresh_token: string;
@Index()
@Column({ @Column({
nullable: false, nullable: false,
unique: true, length: 512
length: 64
}) })
email: string; module: string;
@Column({ @Column({
nullable: false, nullable: false,
length: 64, length: 2048
select: false
}) })
password: string; description: string;
@Column({
nullable: false,
unique: true,
length: 64
})
username: string;
@Column({ @Column({
nullable: true, nullable: true,
length: 64 length: 1
}) })
name: string; action: ActivityType;
@Column({ @Column({
nullable: false, nullable: false,
default: 'STAFF' as Role, length: 2048,
length: 30,
}) })
role: Role; url: String;
@Column({ @Column({
nullable: false, type: 'text',
length: 64 nullable: true,
}) })
status: Status; data_body: String;
@Column() @Column()
@CreateDateColumn() @CreateDateColumn()
@ -60,12 +61,4 @@ export class User {
@Column({ nullable: true }) @Column({ nullable: true })
@DeleteDateColumn() @DeleteDateColumn()
deleted_at: Date; deleted_at: Date;
hashPassword() {
this.password = bcrypt.hashSync(this.password, 8);
}
checkIfPasswordMatch(unencryptedPassword: string) {
return bcrypt.compareSync(unencryptedPassword, this.password);
}
} }

View File

@ -1,53 +1,39 @@
import bcrypt from 'bcryptjs'; import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, DeleteDateColumn, Index } from 'typeorm';
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, DeleteDateColumn } from 'typeorm';
import { Role } from '../types/role'; @Entity('user_refresh_tokens')
import { Status } from '../types/status'; export class UserRefreshToken {
@Entity('users')
export class User {
@PrimaryGeneratedColumn('uuid') @PrimaryGeneratedColumn('uuid')
id: string; id: string;
@Index()
@Column({ @Column({
nullable: false, type: 'uuid'
unique: true,
length: 64
}) })
email: string; id_user: string;
@Column({ @Column({
nullable: false, nullable: false,
length: 64, length: 4096,
select: false
}) })
password: string; user_agent: string;
@Column({
nullable: false,
unique: true,
length: 64
})
username: string;
@Column({ @Column({
nullable: true, nullable: true,
length: 64 length: 64
}) })
name: string; os: string;
@Column({ @Column({
nullable: false, nullable: true,
default: 'STAFF' as Role,
length: 30,
})
role: Role;
@Column({
nullable: false,
length: 64 length: 64
}) })
status: Status; browser: string;
@Column({
nullable: true,
length: 32
})
remote_addr: string;
@Column() @Column()
@CreateDateColumn() @CreateDateColumn()
@ -60,12 +46,4 @@ export class User {
@Column({ nullable: true }) @Column({ nullable: true })
@DeleteDateColumn() @DeleteDateColumn()
deleted_at: Date; deleted_at: Date;
hashPassword() {
this.password = bcrypt.hashSync(this.password, 8);
}
checkIfPasswordMatch(unencryptedPassword: string) {
return bcrypt.compareSync(unencryptedPassword, this.password);
}
} }

View File

@ -1,10 +1,9 @@
import config from 'config'; import config from 'config';
import { DataSource } from "typeorm"; import { DataSource } from "typeorm";
import { User } from "../entity/users"; import { User } from "../entity/users";
import { TrialBalance } from '../entity/trial_balance';
import { ILogObj, Logger } from 'tslog'; import { ILogObj, Logger } from 'tslog';
import { LNKOLEK } from '../entity/lnkolek'; import { UserActivity } from '../entity/users_activity';
import { CifAccount } from '../entity/cif_account'; import { UserRefreshToken } from '../entity/users_refresh_token';
export class OrmHelper { export class OrmHelper {
static DB: DataSource = null static DB: DataSource = null
@ -23,7 +22,7 @@ export class OrmHelper {
database: String(config.get("database.database")), database: String(config.get("database.database")),
synchronize: true, synchronize: true,
logging: true, logging: true,
entities: [User, TrialBalance, LNKOLEK, CifAccount], entities: [User, UserActivity, UserRefreshToken],
subscribers: [], subscribers: [],
migrations: [], migrations: [],
}) })

7
src/types/action.ts Normal file
View File

@ -0,0 +1,7 @@
export enum ActivityType {
"Create" = "C",
"Read" = "R",
"Update" = "U",
"Delete" = "D",
"Restore" = "T",
};