-
Notifications
You must be signed in to change notification settings - Fork 1
use messages from db instead of hardcoded ones #682
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
Draft
KaiWissel
wants to merge
1
commit into
main
Choose a base branch
from
message-templates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { Sequelize, Model, DataTypes } from "sequelize"; | ||
| import { Models } from "../../types/Models"; | ||
|
|
||
| export const MessageTypes = { | ||
| EMAIL: "email", | ||
| OTHER: "other", | ||
| } as const; | ||
|
|
||
| export type MessageTypesType = typeof MessageTypes[keyof typeof MessageTypes]; | ||
|
|
||
| export const EmailMessagePosition = { | ||
| TITLE: "title", | ||
| TEXT: "text", | ||
| } as const; | ||
|
|
||
| export type EmailMessagePositionType = | ||
| typeof EmailMessagePosition[keyof typeof EmailMessagePosition]; | ||
|
|
||
| interface MessageAttributes { | ||
| name: string; | ||
| content: string; | ||
| typeOfMessage: MessageTypesType; | ||
| position: EmailMessagePositionType; | ||
| } | ||
|
|
||
| export interface MessageInstance | ||
| extends Model<MessageAttributes>, | ||
| MessageAttributes { | ||
| createdAt?: Date; | ||
| updatedAt?: Date; | ||
| } | ||
|
|
||
| export function initMessage(sequelize: Sequelize): Models["Message"] { | ||
| const MessageTemplate = sequelize.define<MessageInstance>("Message", { | ||
| name: { | ||
| type: DataTypes.STRING, | ||
| allowNull: false, | ||
| }, | ||
| content: { | ||
| type: DataTypes.STRING(512), | ||
| allowNull: false, | ||
| }, | ||
| typeOfMessage: { | ||
| type: DataTypes.STRING(64), | ||
| defaultValue: "other", | ||
| }, | ||
| position: { | ||
| type: DataTypes.STRING(64), | ||
| }, | ||
| }); | ||
|
|
||
| return MessageTemplate; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import logger from "../config/logger"; | ||
| import { cache } from "../controller/CacheManager"; | ||
| import db from "../db"; | ||
| import { | ||
| EmailMessagePosition, | ||
| EmailMessagePositionType, | ||
| MessageTypes, | ||
| MessageTypesType, | ||
| } from "../db/models/Message"; | ||
|
|
||
| const PLACEHOLDER_PREFIX = "$"; | ||
| const CACHE_PREFIX = "msg_template_"; | ||
|
|
||
| interface PlaceholderReplacements { | ||
| [name: string]: string | undefined; | ||
| } | ||
|
|
||
| interface EmailMessage { | ||
| title: string; | ||
| text: string; | ||
| } | ||
|
|
||
| export async function getMessageByNameForEmail( | ||
| name: string, | ||
| placeholderReplacementsText?: PlaceholderReplacements, | ||
| placeholderReplacementsTitle?: PlaceholderReplacements | ||
| ) { | ||
| const CACHE_KEY = CACHE_PREFIX + name; | ||
|
|
||
| let message = cache.get(CACHE_KEY) as EmailMessage | null; | ||
|
|
||
| if (!message) { | ||
| try { | ||
| message = await retrieveEmailParts(name); | ||
| } catch (error) { | ||
| logger.error("MS: " + error); | ||
| return; | ||
| } | ||
|
|
||
| cache.set(CACHE_KEY, message, 0); | ||
| } | ||
|
|
||
| console.log("JSON: ", JSON.stringify(message, null, 2)); | ||
|
|
||
| try { | ||
| message.text = replacePlaceholder( | ||
| message.text, | ||
| placeholderReplacementsText | ||
| ); | ||
| message.title = replacePlaceholder( | ||
| message.title, | ||
| placeholderReplacementsTitle | ||
| ); | ||
| } catch (error) { | ||
| logger.error("MS: " + error); | ||
| return; | ||
| } | ||
|
|
||
| return message; | ||
| } | ||
|
|
||
| async function retrieveEmailParts(name: string) { | ||
| const [title, text] = await Promise.all([ | ||
| retrieveMessageFromDb(name, MessageTypes.EMAIL, EmailMessagePosition.TITLE), | ||
| retrieveMessageFromDb(name, MessageTypes.EMAIL, EmailMessagePosition.TEXT), | ||
| ]); | ||
|
|
||
| if (!title) { | ||
| throw `No title for message with name ${name} found`; | ||
| } | ||
|
|
||
| if (!text) { | ||
| throw `No text for message with name ${name} found`; | ||
| } | ||
|
|
||
| return { title, text }; | ||
| } | ||
|
|
||
| async function retrieveMessageFromDb( | ||
| name: string, | ||
| typeOfMessage?: MessageTypesType, | ||
| position?: EmailMessagePositionType | ||
| ) { | ||
| const dbEntry = await db.Message.findOne({ | ||
| where: { name, typeOfMessage, position }, | ||
| raw: true, | ||
| attributes: ["content"], | ||
| }); | ||
|
|
||
| return dbEntry?.content; | ||
| } | ||
|
|
||
| /** | ||
| * Replaces the placeholder in the message template with a specified value. | ||
| * The name of the placeholder has to match the name of the property in the parameters object. | ||
| */ | ||
| function replacePlaceholder( | ||
| messageWithPlaceholders: string, | ||
| placeholderReplacements?: PlaceholderReplacements | ||
| ) { | ||
| if (!placeholderReplacements) { | ||
| logger.debug("MS: No replacements specified"); | ||
| return messageWithPlaceholders; | ||
| } | ||
|
|
||
| for (const [key, value] of Object.entries(placeholderReplacements)) { | ||
| if (!value) throw `Value of ${key} is undefined`; | ||
| console.log("K: " + key); | ||
| console.log("V: " + value); | ||
| messageWithPlaceholders = messageWithPlaceholders.replaceAll( | ||
| PLACEHOLDER_PREFIX + key, | ||
| value | ||
| ); | ||
| console.log("MWP: " + messageWithPlaceholders); | ||
| } | ||
| return messageWithPlaceholders; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [ | ||
| { | ||
| "name": "REGISTRATION", | ||
| "content": "Deine Anmeldung bei XCCup.net", | ||
| "typeOfMessage": "email", | ||
| "position": "title" | ||
| }, | ||
| { | ||
| "name": "REGISTRATION", | ||
| "content": "Hallo $firstName!\nWillkommen beim XCCup.\n\nUm dein Konto zu aktivieren klicke bitte auf den folgenden Link:\n$activationLink\n\nWir wünschen Dir allzeit gute Flüge und viel Spaß.\n\nDein XCCup Team", | ||
| "typeOfMessage": "email", | ||
| "position": "text" | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Der Error wurde nie gecatched und hat deshalb den Server heruntergerissen.
Habe deshalb aus dem Error ein Log gemacht.