import { BaseEntity, Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from "typeorm";
import { DiscordAccount } from "./DiscordAccount";
import { User } from "./User";

@Entity()
export class DiscordLinkRequest extends BaseEntity
{
	@PrimaryGeneratedColumn()
	id!: number;

	@Column()
	discordId!: string;

	@Column()
	token!: string;

	@CreateDateColumn()
	createdAt!: Date;

	/**
	 * Fulfill a link request for the given user
	 */
	public async linkUser(user: User) {
		let account = new DiscordAccount();
		account.discordId = this.discordId;
		account.user = user;
		let awaitSave = account.save();
		await DiscordLinkRequest.delete({ id: this.id });
		return await awaitSave;
	}
}
