Skip to content

Instantly share code, notes, and snippets.

@coneforapine
Created January 26, 2021 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coneforapine/85e77c0a1183153adc4c2061e5129087 to your computer and use it in GitHub Desktop.
Save coneforapine/85e77c0a1183153adc4c2061e5129087 to your computer and use it in GitHub Desktop.
import { Entity, Column, OneToMany, BeforeInsert, OneToOne, JoinColumn } from "typeorm";
import { IsString, IsEmail, IsMobilePhone, IsJWT } from "class-validator";
import * as bc from "bcrypt";
import { BaseEntity } from "../base/BaseEntity"
import { UserLogin } from "./login.model";
import { Image } from "../common/image.model";
import { Wallet } from "../payment/wallet.model";
import { Restaurant } from "../restaurant/restaurant.model";
import { PaymentMethod } from "../payment/method.model";
import { Token } from "./token.model";
import { Coupon } from "../coupon/coupon.model";
import { UserConnection } from "./connection.model";
import { Order } from "../order/order.model";
import { Review } from "../restaurant/review.model";
import { Notification } from "./notification.model";
import { Address } from "../common/address.model";
import { Transaction } from "../payment/transaction.model"
export enum RegisteredFrom {
api = "API",
site = "SITE",
mobile = "MOBILE_APP",
customer = "CUSTOMER_APP"
}
export enum Roles {
admin = "ROLE_ADMIN",
restaurant = "ROLE_RESTAURANT",
user = "ROLE_USER",
driver = "ROLE_DRIVER"
}
const genSalt = () => bc.genSalt(parseInt(process.env.BCRYPT_SALT_ROUNDS as string));
@Entity()
export class User extends BaseEntity {
@Column()
@IsString()
public name!: string;
@Column()
@IsString()
public surname!: string;
@Column({unique: true})
@IsEmail()
public email!: string;
@Column()
public password!: string;
@OneToMany(() => Address, address => address.user, { nullable: true })
public addresses!: Address[];
@Column({unique: true})
@IsMobilePhone()
public phoneNumber!: string;
@OneToMany(() => Token, token => token.user, { cascade: true, onDelete: "CASCADE" })
public tokens!: Token[];
@OneToMany(() => UserLogin, ul => ul.id, { nullable: true })
public previousLogins!: UserLogin[];
@OneToOne(() => Image, { nullable: true })
public profileImage!: Image;
@Column({
type: "enum",
enum: RegisteredFrom,
default: RegisteredFrom.site
})
public registeredFrom!: RegisteredFrom;
@OneToOne(() => Wallet, wallet => wallet.id, { nullable: true, onUpdate: 'CASCADE' })
@JoinColumn()
public wallet!: Wallet;
@OneToMany(() => Restaurant, res => res.id, { nullable: true })
public favorites!: Restaurant[];
@Column({
default: 0
})
public trustLevel!: number;
@Column({
type: 'enum',
enum: Roles,
default: Roles.user
})
public roles!: Roles;
@OneToMany(() => Notification, nt => nt.id)
public notifications!: Notification[];
@OneToMany(() => PaymentMethod, pm => pm.user, { nullable: true, cascade: true })
public paymentMethods!: PaymentMethod[];
@OneToMany(() => Coupon, c => c.id)
public usedCoupons!: Coupon[];
@OneToMany(() => UserConnection, connection => connection.user)
public userConnections!: UserConnection[];
@OneToMany(() => Order, order => order.user, { onDelete: "CASCADE" })
public orders!: Order[];
@OneToMany(() => Review, r => r.user, { onDelete: "CASCADE" })
public reviews!: Review[];
@OneToMany(() => Transaction, t => t.user, { cascade: true, nullable: true })
public transactions!: Transaction[];
@BeforeInsert()
public async hashPassword() {
this.password = await bc.hash(this.password, await genSalt())
}
public get flat() {
return {
id: this.id,
name: this.name,
surname: this.surname,
email: this.email,
phoneNumber: this.phoneNumber,
roles: this.roles,
profileImage: this.profileImage ? this.profileImage : null,
walletBalance: this.wallet?.balance
}
}
}
import {EventSubscriber, EntitySubscriberInterface, InsertEvent} from "typeorm";
import { Wallet } from "../entity/payment/wallet.model";
import { User } from "../entity/user/user.model";
@EventSubscriber()
export class UserSubscriber implements EntitySubscriberInterface<any> {
listenTo() {
return User;
}
afterInsert(e: InsertEvent<User>) {
const wallet = new Wallet();
wallet.user = e.entity;
e.entity.wallet = wallet;
try {
e.manager.save(e.entity.wallet);
} catch(err) {
console.log(err);
}
}
}
import { Entity, Column, OneToMany, OneToOne } from "typeorm"
import { BaseEntity } from "../base/BaseEntity"
import { User } from "../user/user.model";
import { Transaction } from "./transaction.model";
@Entity()
export class Wallet extends BaseEntity {
@Column({
default: 0
})
public balance!: number;
@OneToMany(() => Transaction, tr => tr.wallet, { nullable: true })
public transactions!: Transaction[];
@OneToOne(() => User, user => user.id)
public user!: User;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment