Dashboard work and related
This commit is contained in:
parent
7bda2b1763
commit
b230a73a6f
44 changed files with 637 additions and 272 deletions
|
@ -21,21 +21,25 @@ export class AllowedGuilds extends BaseRepository {
|
|||
async isAllowed(guildId) {
|
||||
const count = await this.allowedGuilds.count({
|
||||
where: {
|
||||
guild_id: guildId,
|
||||
id: guildId,
|
||||
},
|
||||
});
|
||||
return count !== 0;
|
||||
}
|
||||
|
||||
getForDashboardUser(userId) {
|
||||
getForApiUser(userId) {
|
||||
return this.allowedGuilds
|
||||
.createQueryBuilder("allowed_guilds")
|
||||
.innerJoin(
|
||||
"dashboard_users",
|
||||
"dashboard_users",
|
||||
"dashboard_users.guild_id = allowed_guilds.guild_id AND dashboard_users.user_id = :userId",
|
||||
"api_permissions",
|
||||
"api_permissions",
|
||||
"api_permissions.guild_id = allowed_guilds.id AND api_permissions.user_id = :userId",
|
||||
{ userId },
|
||||
)
|
||||
.getMany();
|
||||
}
|
||||
|
||||
updateInfo(id, name, icon) {
|
||||
return this.allowedGuilds.update({ id }, { name, icon });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { getRepository, Repository } from "typeorm";
|
||||
import { DashboardLogin } from "./entities/DashboardLogin";
|
||||
import { ApiLogin } from "./entities/ApiLogin";
|
||||
import { BaseRepository } from "./BaseRepository";
|
||||
import crypto from "crypto";
|
||||
import moment from "moment-timezone";
|
||||
|
@ -9,18 +9,12 @@ import uuidv4 from "uuid/v4";
|
|||
import { DBDateFormat } from "../utils";
|
||||
import { log } from "util";
|
||||
|
||||
export interface DashboardLoginUserData {
|
||||
username: string;
|
||||
discriminator: string;
|
||||
avatar: string;
|
||||
}
|
||||
|
||||
export class DashboardLogins extends BaseRepository {
|
||||
private dashboardLogins: Repository<DashboardLogin>;
|
||||
export class ApiLogins extends BaseRepository {
|
||||
private apiLogins: Repository<ApiLogin>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.dashboardLogins = getRepository(DashboardLogin);
|
||||
this.apiLogins = getRepository(ApiLogin);
|
||||
}
|
||||
|
||||
async getUserIdByApiKey(apiKey: string): Promise<string | null> {
|
||||
|
@ -29,7 +23,7 @@ export class DashboardLogins extends BaseRepository {
|
|||
return null;
|
||||
}
|
||||
|
||||
const login = await this.dashboardLogins
|
||||
const login = await this.apiLogins
|
||||
.createQueryBuilder()
|
||||
.where("id = :id", { id: loginId })
|
||||
.andWhere("expires_at > NOW()")
|
||||
|
@ -49,12 +43,12 @@ export class DashboardLogins extends BaseRepository {
|
|||
return login.user_id;
|
||||
}
|
||||
|
||||
async addLogin(userId: string, userData: DashboardLoginUserData): Promise<string> {
|
||||
async addLogin(userId: string): Promise<string> {
|
||||
// Generate random login id
|
||||
let loginId;
|
||||
while (true) {
|
||||
loginId = uuidv4();
|
||||
const existing = await this.dashboardLogins.findOne({
|
||||
const existing = await this.apiLogins.findOne({
|
||||
where: {
|
||||
id: loginId,
|
||||
},
|
||||
|
@ -69,11 +63,10 @@ export class DashboardLogins extends BaseRepository {
|
|||
const hashedToken = hash.digest("hex");
|
||||
|
||||
// Save this to the DB
|
||||
await this.dashboardLogins.insert({
|
||||
await this.apiLogins.insert({
|
||||
id: loginId,
|
||||
token: hashedToken,
|
||||
user_id: userId,
|
||||
user_data: userData,
|
||||
logged_in_at: moment().format(DBDateFormat),
|
||||
expires_at: moment()
|
||||
.add(1, "day")
|
|
@ -1,17 +1,17 @@
|
|||
import { getRepository, Repository } from "typeorm";
|
||||
import { DashboardUser } from "./entities/DashboardUser";
|
||||
import { ApiPermission } from "./entities/ApiPermission";
|
||||
import { BaseRepository } from "./BaseRepository";
|
||||
|
||||
export class DashboardUsers extends BaseRepository {
|
||||
private dashboardUsers: Repository<DashboardUser>;
|
||||
export class ApiPermissions extends BaseRepository {
|
||||
private apiPermissions: Repository<ApiPermission>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.dashboardUsers = getRepository(DashboardUser);
|
||||
this.apiPermissions = getRepository(ApiPermission);
|
||||
}
|
||||
|
||||
getByGuildAndUserId(guildId, userId) {
|
||||
return this.dashboardUsers.findOne({
|
||||
return this.apiPermissions.findOne({
|
||||
where: {
|
||||
guild_id: guildId,
|
||||
user_id: userId,
|
|
@ -1,4 +1,4 @@
|
|||
export enum DashboardRoles {
|
||||
export enum ApiRoles {
|
||||
Viewer = 1,
|
||||
Editor,
|
||||
Manager,
|
38
src/data/ApiUserInfo.ts
Normal file
38
src/data/ApiUserInfo.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { getRepository, Repository } from "typeorm";
|
||||
import { ApiUserInfo as ApiUserInfoEntity, ApiUserInfoData } from "./entities/ApiUserInfo";
|
||||
import { BaseRepository } from "./BaseRepository";
|
||||
import { connection } from "./db";
|
||||
import moment from "moment-timezone";
|
||||
import { DBDateFormat } from "../utils";
|
||||
|
||||
export class ApiUserInfo extends BaseRepository {
|
||||
private apiUserInfo: Repository<ApiUserInfoEntity>;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.apiUserInfo = getRepository(ApiUserInfoEntity);
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.apiUserInfo.findOne({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
update(id, data: ApiUserInfoData) {
|
||||
return connection.transaction(async entityManager => {
|
||||
const repo = entityManager.getRepository(ApiUserInfoEntity);
|
||||
|
||||
const existingInfo = await repo.findOne({ where: { id } });
|
||||
const updatedAt = moment().format(DBDateFormat);
|
||||
|
||||
if (existingInfo) {
|
||||
await repo.update({ id }, { data, updated_at: updatedAt });
|
||||
} else {
|
||||
await repo.insert({ id, data, updated_at: updatedAt });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -32,6 +32,18 @@ export class Configs extends BaseRepository {
|
|||
return (await this.getActiveByKey(key)) != null;
|
||||
}
|
||||
|
||||
getRevisions(key, num = 10) {
|
||||
return this.configs.find({
|
||||
relations: this.getRelations(),
|
||||
where: { key },
|
||||
select: ["id", "key", "is_active", "edited_by", "edited_at"],
|
||||
order: {
|
||||
edited_at: "DESC",
|
||||
},
|
||||
take: num,
|
||||
});
|
||||
}
|
||||
|
||||
async saveNewRevision(key, config, editedBy) {
|
||||
return connection.transaction(async entityManager => {
|
||||
const repo = entityManager.getRepository(Config);
|
||||
|
|
|
@ -4,7 +4,7 @@ import { Entity, Column, PrimaryColumn, CreateDateColumn } from "typeorm";
|
|||
export class AllowedGuild {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
guild_id: string;
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
|
25
src/data/entities/ApiLogin.ts
Normal file
25
src/data/entities/ApiLogin.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { Entity, Column, PrimaryColumn, OneToOne, ManyToOne, JoinColumn } from "typeorm";
|
||||
import { ApiUserInfo } from "./ApiUserInfo";
|
||||
|
||||
@Entity("api_logins")
|
||||
export class ApiLogin {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
token: string;
|
||||
|
||||
@Column()
|
||||
user_id: string;
|
||||
|
||||
@Column()
|
||||
logged_in_at: string;
|
||||
|
||||
@Column()
|
||||
expires_at: string;
|
||||
|
||||
@ManyToOne(type => ApiUserInfo, userInfo => userInfo.logins)
|
||||
@JoinColumn({ name: "user_id" })
|
||||
userInfo: ApiUserInfo;
|
||||
}
|
20
src/data/entities/ApiPermission.ts
Normal file
20
src/data/entities/ApiPermission.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { Entity, Column, PrimaryColumn, ManyToOne, JoinColumn } from "typeorm";
|
||||
import { ApiUserInfo } from "./ApiUserInfo";
|
||||
|
||||
@Entity("api_permissions")
|
||||
export class ApiPermission {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
guild_id: string;
|
||||
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
user_id: string;
|
||||
|
||||
@Column()
|
||||
role: string;
|
||||
|
||||
@ManyToOne(type => ApiUserInfo, userInfo => userInfo.permissions)
|
||||
@JoinColumn({ name: "user_id" })
|
||||
userInfo: ApiUserInfo;
|
||||
}
|
28
src/data/entities/ApiUserInfo.ts
Normal file
28
src/data/entities/ApiUserInfo.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { Entity, Column, PrimaryColumn, OneToMany } from "typeorm";
|
||||
import { ApiLogin } from "./ApiLogin";
|
||||
import { ApiPermission } from "./ApiPermission";
|
||||
|
||||
export interface ApiUserInfoData {
|
||||
username: string;
|
||||
discriminator: string;
|
||||
avatar: string;
|
||||
}
|
||||
|
||||
@Entity("api_user_info")
|
||||
export class ApiUserInfo {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
id: string;
|
||||
|
||||
@Column("simple-json")
|
||||
data: ApiUserInfoData;
|
||||
|
||||
@Column()
|
||||
updated_at: string;
|
||||
|
||||
@OneToMany(type => ApiLogin, login => login.userInfo)
|
||||
logins: ApiLogin[];
|
||||
|
||||
@OneToMany(type => ApiPermission, perm => perm.userInfo)
|
||||
permissions: ApiPermission[];
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
import { Entity, Column, PrimaryColumn, CreateDateColumn } from "typeorm";
|
||||
import { Entity, Column, PrimaryColumn, CreateDateColumn, ManyToOne, JoinColumn } from "typeorm";
|
||||
import { ApiUserInfo } from "./ApiUserInfo";
|
||||
|
||||
@Entity("configs")
|
||||
export class Config {
|
||||
|
@ -20,4 +21,8 @@ export class Config {
|
|||
|
||||
@Column()
|
||||
edited_at: string;
|
||||
|
||||
@ManyToOne(type => ApiUserInfo)
|
||||
@JoinColumn({ name: "edited_by" })
|
||||
userInfo: ApiUserInfo;
|
||||
}
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
import { Entity, Column, PrimaryColumn } from "typeorm";
|
||||
import { DashboardLoginUserData } from "../DashboardLogins";
|
||||
|
||||
@Entity("dashboard_logins")
|
||||
export class DashboardLogin {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
token: string;
|
||||
|
||||
@Column()
|
||||
user_id: string;
|
||||
|
||||
@Column("simple-json")
|
||||
user_data: DashboardLoginUserData;
|
||||
|
||||
@Column()
|
||||
logged_in_at: string;
|
||||
|
||||
@Column()
|
||||
expires_at: string;
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
import { Entity, Column, PrimaryColumn } from "typeorm";
|
||||
|
||||
@Entity("dashboard_users")
|
||||
export class DashboardUser {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
guild_id: string;
|
||||
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
user_id: string;
|
||||
|
||||
@Column()
|
||||
username: string;
|
||||
|
||||
@Column()
|
||||
role: string;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue