This commit is contained in:
parent
9e963d90cb
commit
3d8745dc0f
4 changed files with 80 additions and 0 deletions
|
@ -0,0 +1 @@
|
|||
DROP TABLE `user_effect`;
|
|
@ -0,0 +1,10 @@
|
|||
CREATE TABLE `user_effect` (
|
||||
`Id` varchar(255) NOT NULL,
|
||||
`WhenCreated` datetime NOT NULL,
|
||||
`WhenUpdated` datetime NOT NULL,
|
||||
`Name` varchar(255) NOT NULL,
|
||||
`UserId` varchar(255) NOT NULL,
|
||||
`Unused` int NOT NULL DEFAULT 0,
|
||||
`WhenExpires` datetime NULL,
|
||||
PRIMARY KEY (`Id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
51
src/database/entities/app/UserEffect.ts
Normal file
51
src/database/entities/app/UserEffect.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import {Column, Entity} from "typeorm";
|
||||
import AppBaseEntity from "../../../contracts/AppBaseEntity";
|
||||
|
||||
@Entity()
|
||||
export default class UserEffect extends AppBaseEntity {
|
||||
constructor(name: string, userId: string, unused: number, WhenExpires?: Date) {
|
||||
super();
|
||||
|
||||
this.Name = name;
|
||||
this.UserId = userId;
|
||||
this.Unused = unused;
|
||||
this.WhenExpires = WhenExpires;
|
||||
}
|
||||
|
||||
@Column()
|
||||
Name: string;
|
||||
|
||||
@Column()
|
||||
UserId: string;
|
||||
|
||||
@Column()
|
||||
Unused: number;
|
||||
|
||||
@Column({ nullable: true })
|
||||
WhenExpires?: Date;
|
||||
|
||||
public AddUnused(amount: number) {
|
||||
this.Unused += amount;
|
||||
}
|
||||
|
||||
public UseEffect(whenExpires: Date): boolean {
|
||||
if (this.Unused == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.Unused -= 1;
|
||||
this.WhenExpires = whenExpires;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public IsEffectActive(): boolean {
|
||||
const now = new Date();
|
||||
|
||||
if (this.WhenExpires && now < this.WhenExpires) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
import MigrationHelper from "../../../../helpers/MigrationHelper";
|
||||
|
||||
export class CreateUserEffect1729962056556 implements MigrationInterface {
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
MigrationHelper.Up("1729962056556-createUserEffect", "0.9", [
|
||||
"01-table-userEffect",
|
||||
], queryRunner);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
MigrationHelper.Down("1729962056556-createUserEffect", "0.9", [
|
||||
"01-table-userEffect",
|
||||
], queryRunner);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue