Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert database entities from TypeORM Repositories to extending BaseEntity #228

Merged
7 changes: 2 additions & 5 deletions src/entities/Birthday.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Entity, PrimaryColumn, Column, getConnection } from "typeorm";
import { Entity, PrimaryColumn, Column, BaseEntity } from "typeorm";

@Entity()
export class Birthday {
export class Birthday extends BaseEntity {
@PrimaryColumn("text")
userid: string;

Expand All @@ -11,6 +11,3 @@ export class Birthday {
@Column("text", { nullable: true })
timezone: string;
}

export const BirthdayRepository = async () =>
getConnection().getRepository(Birthday);
8 changes: 2 additions & 6 deletions src/entities/Deadchat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Entity, PrimaryGeneratedColumn, Column, getConnection } from "typeorm";
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from "typeorm";

@Entity()
export class DeadchatQuestion {
export class DeadchatQuestion extends BaseEntity {
@PrimaryGeneratedColumn()
id: string;

Expand All @@ -11,7 +11,3 @@ export class DeadchatQuestion {
@Column({ default: new Date(), name: "last_used" })
lastUsed: Date;
}

export const DeadchatRepository = async () => {
return getConnection().getRepository(DeadchatQuestion);
};
8 changes: 2 additions & 6 deletions src/entities/Someone.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Entity, PrimaryColumn, Column, getConnection } from "typeorm";
import { BaseEntity, Entity, PrimaryColumn, Column } from "typeorm";

@Entity()
export class SomeoneUser {
export class SomeoneUser extends BaseEntity {
@PrimaryColumn()
id: string;

@Column("timestamp with time zone") // Adds TZ support in psql
time: Date;
}

export const SomeoneRepository = async () => {
return getConnection().getRepository(SomeoneUser);
};
13 changes: 3 additions & 10 deletions src/entities/UserGroup.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {
BaseEntity,
Entity,
PrimaryGeneratedColumn,
PrimaryColumn,
Column,
getConnection,
ManyToMany,
JoinTable,
} from "typeorm";

// Pretty temporary solution; this should be a more generic User
// model which we can use for a lot more than just this.
@Entity()
export class GroupMember {
export class GroupMember extends BaseEntity {
@PrimaryColumn()
id: string;
}

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

Expand All @@ -40,10 +40,3 @@ export class UserGroup {
@Column({ default: 60 })
cooldown: number;
}

export const UserGroupRepository = async () => {
return getConnection().getRepository(UserGroup);
};

export const UserGroupMembershipRepository = async () =>
getConnection().getRepository(GroupMember);
12 changes: 4 additions & 8 deletions src/events/MemberLeave.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { GuildMember, PartialGuildMember } from "discord.js";
import {
BirthdayRepository,
Birthday,
BuddyProjectEntryRepository,
GroupMember,
UserGroupMembershipRepository,
UserGroupRepository,
} from "../entities";
import { textLog } from "../common/moderator";

Expand Down Expand Up @@ -42,12 +40,11 @@ const RemoveFromBuddyProject = async (memberId: string) => {

const RemoveFromBirthdays = async (memberId: string) => {
try {
const birthdayEntries = await BirthdayRepository();
const foundUser = await birthdayEntries.findOne({
const foundUser = await Birthday.findOne({
userid: memberId,
});
if (foundUser) {
birthdayEntries.remove(foundUser);
await Birthday.remove(foundUser);
}
} catch (e) {
textLog(
Expand All @@ -58,8 +55,7 @@ const RemoveFromBirthdays = async (memberId: string) => {

const RemoveFromGroups = async (memberId: string) => {
try {
const groupMemberRepository = await UserGroupMembershipRepository();
await groupMemberRepository.remove({ id: memberId });
await GroupMember.delete(memberId);
} catch (e) {
textLog(
`(MemberLeave) There was an error removing member from the group DB: ${memberId}`
Expand Down
12 changes: 4 additions & 8 deletions src/programs/BirthdayManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getAllCountries, getCountry } from "countries-and-timezones";

import Tools from "../common/tools";
import { textLog, isAuthorModerator } from "../common/moderator";
import { BirthdayRepository, Birthday } from "../entities";
import { Birthday } from "../entities";
import { ENGINEER_ROLE_NAME } from "../const";
import { Logger } from "../common/Logger";

Expand Down Expand Up @@ -154,8 +154,7 @@ export async function createBirthday(
timezone: string
) {
try {
const birthdayRepository = await BirthdayRepository();
return birthdayRepository.create({
return Birthday.create({
userid: id,
birthdate: zonedTimeToUtc(birthdate, timezone),
timezone,
Expand All @@ -167,8 +166,7 @@ export async function createBirthday(

export async function saveBirthday(birthday: Birthday) {
try {
const birthdayRepository = await BirthdayRepository();
return birthdayRepository.save(birthday);
return Birthday.save(birthday);
} catch (err) {
Logger("BirthdayManager", "saveBirthday", err);
}
Expand Down Expand Up @@ -479,9 +477,7 @@ function timezonesFromRole(props: CountryWithRegion): readonly string[] {
}

export async function getUserBirthday(userId: string): Promise<Date | null> {
const birthdayRepository = await BirthdayRepository();

const userExistingBirthday = await birthdayRepository.findOne({
const userExistingBirthday = await Birthday.findOne({
where: {
userid: userId,
},
Expand Down
14 changes: 9 additions & 5 deletions src/programs/Deadchat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Message, TextChannel } from "discord.js";
import { DeadchatQuestion, DeadchatRepository } from "../entities";
import { DeadchatQuestion } from "../entities";
import Tools from "../common/tools";

export default async function Deadchat(pMessage: Message) {
Expand All @@ -17,16 +17,20 @@ export default async function Deadchat(pMessage: Message) {
return;
}

const deadchatRepo = await DeadchatRepository();
const question: DeadchatQuestion = await deadchatRepo
.createQueryBuilder()
const question: DeadchatQuestion = await DeadchatQuestion.createQueryBuilder()
.select()
.andWhere("random() < 0.5 OR id = 1") // To get a random-ish question (strongly biased towards the top few questions but good enough I guess)
.orderBy("last_used", "ASC")
.limit(1)
.getOne();

if (question === undefined) {
pMessage.channel.send(
":robot: Yikes! I could not find a question to use to revive chat. Is this the end?"
);
return;
}
pMessage.channel.send(question.question);
question.lastUsed = new Date();
deadchatRepo.save(question);
question.save();
}
Loading