diff --git a/scripts/wichtelbot/database/database.ts b/scripts/wichtelbot/database/database.ts index 8217ce7aebfc2228c165b6a885a0caf00a36de6e..0bfa9f870db9f516266a469ea4449424d2c1099d 100644 --- a/scripts/wichtelbot/database/database.ts +++ b/scripts/wichtelbot/database/database.ts @@ -9,7 +9,6 @@ import { GiftTypeStatistics } from './giftTypeStatistics'; import { InformationData } from '../classes/information'; import Member from '../classes/member'; import { ParcelStatistics } from './parcelStatistics'; -import { RelationshipWithMembers } from './relationshipWithMembers'; import { State } from '../endpoint/definitions'; import Utils from '../../utility/utils'; import Sqlite = require('better-sqlite3'); @@ -570,51 +569,6 @@ export default class Database return relationships; } - public getRelationshipsWithMembers (): RelationshipWithMembers[] - { - const statement = this.mainDatabase.prepare( - `SELECT - giverContact.*, giverInformation.*, takerContact.*, takerInformation.* - FROM - relationship - LEFT JOIN - contact AS giverContact - ON giverContact.id = relationship.giverId - LEFT JOIN - information AS giverInformation - ON giverInformation.contactId = giverContact.id - LEFT JOIN - contact AS takerContact - ON takerContact.id = relationship.takerId - LEFT JOIN - information AS takerInformation - ON takerInformation.contactId = takerContact.id` - ); - - statement.expand(true); - - const resultData = statement.all() as { - giverContact: ContactData, - giverInformation: InformationData, - takerContact: ContactData, - takerInformation: InformationData - }[]; - - const relationshipsWithMembers: RelationshipWithMembers[] = []; - - for (const result of resultData) - { - const relationshipWithMembers: RelationshipWithMembers = { - giver: new Member(result.giverContact, result.giverInformation), - taker: new Member(result.takerContact, result.takerInformation), - }; - - relationshipsWithMembers.push(relationshipWithMembers); - } - - return relationshipsWithMembers; - } - public saveRelationships (relationships: RelationshipData[]): void { const statement = this.mainDatabase.prepare( diff --git a/scripts/wichtelbot/database/relationshipWithMembers.ts b/scripts/wichtelbot/database/relationshipWithMembers.ts deleted file mode 100644 index e924a1366aab50ec86c58ba598de2a2c072fcec3..0000000000000000000000000000000000000000 --- a/scripts/wichtelbot/database/relationshipWithMembers.ts +++ /dev/null @@ -1,7 +0,0 @@ -import Member from "../classes/member"; - -export interface RelationshipWithMembers -{ - giver: Member; - taker: Member; -} diff --git a/scripts/wichtelbot/message/modules/moderationModule.ts b/scripts/wichtelbot/message/modules/moderationModule.ts index 0f0bbf4cc3bd6b94bc662d615004c75bd57f874d..ef3d32f54c0144902b8ec6dd906586b0737ed116 100644 --- a/scripts/wichtelbot/message/modules/moderationModule.ts +++ b/scripts/wichtelbot/message/modules/moderationModule.ts @@ -116,18 +116,23 @@ export class ModerationModule public async distributeWichtelProfiles (message: Message): Promise<void> { - const relationships = this.database.getRelationshipsWithMembers(); + // TODO: This is relatively slow. Could it be sped up? Getting the contacts/members could be cached or maybe the profile is slow? + + const relationships = this.database.getRelationships(); for (const relationship of relationships) { - const profileOverviewText = Localisation.texts.wichtelProfileDistribution.process(relationship.giver); - const profileVisualisations = HandlingUtils.getProfileVisualisations(relationship.taker); + const giver = this.database.getContact(relationship.giverId); + const taker = this.database.getMember(relationship.takerId); + + const profileOverviewText = Localisation.texts.wichtelProfileDistribution.process(giver); + const profileVisualisations = HandlingUtils.getProfileVisualisations(taker); - await message.reply(profileOverviewText, profileVisualisations); + const giverUser = await message.client.fetchUser(giver.id); + await giverUser.send(profileOverviewText, profileVisualisations); - relationship.giver.state = State.Wichteling; - // NOTE: We can use "updateContacts" instead of "updateMembers" because we changed the state, which is only part of the contact: - this.database.updateContact(relationship.giver); + giver.state = State.Wichteling; + this.database.updateContact(giver); } const parameters = new KeyValuePairList('profileCount', `${relationships.length}`);