|
|
@ -1,4 +1,4 @@ |
|
|
|
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, OneToMany, OneToOne, JoinColumn, CreateDateColumn } from "typeorm"; |
|
|
|
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, OneToMany, OneToOne, JoinColumn, CreateDateColumn, MoreThanOrEqual } from "typeorm"; |
|
|
|
import bcrypt from "bcrypt"; |
|
|
|
import jwt from "jsonwebtoken"; |
|
|
|
import { MovieTicket } from "./MovieTicket"; |
|
|
@ -28,7 +28,7 @@ export class User extends BaseEntity |
|
|
|
|
|
|
|
@OneToOne(() => MovieQuota, { nullable: true }) |
|
|
|
@JoinColumn() |
|
|
|
quota!: MovieQuota; |
|
|
|
quota!: MovieQuota | null; |
|
|
|
|
|
|
|
@OneToMany(() => User, user => user.movieTickets) |
|
|
|
movieTickets!: MovieTicket[]; |
|
|
@ -69,4 +69,32 @@ export class User extends BaseEntity |
|
|
|
} |
|
|
|
return await user.save(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Determine the user's available quota |
|
|
|
*/ |
|
|
|
public async availableQuota() { |
|
|
|
let quota = await this.fetchQuota(); |
|
|
|
if (quota === null) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
let oneWeekAgo = new Date(Date.now() - 1000*60*60*24*7); |
|
|
|
let numTicketsThisWeek = await MovieTicket.count({ |
|
|
|
user: this, |
|
|
|
createdAt: MoreThanOrEqual(oneWeekAgo), |
|
|
|
isCanceled: false |
|
|
|
}); |
|
|
|
return quota.moviesPerWeek - numTicketsThisWeek; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the user's quota, fetching it if undefined |
|
|
|
*/ |
|
|
|
public async fetchQuota() { |
|
|
|
if (this.quota !== undefined) { |
|
|
|
return this.quota; |
|
|
|
} |
|
|
|
let user = <User>await User.findOne(this.id, { relations: ["quota"] }); |
|
|
|
return user.quota; |
|
|
|
} |
|
|
|
} |