<commit_before>import { messaging as firebaseMessaging } from 'firebase-admin';
import Mustache from 'mustache';
import winston from 'winston';
import Mail from '@/interfaces/Mail';
import Template from '../interfaces/Template';
import LanguagePayload from '@/interfaces/LanguagePayload';
import UserRessource from '@/interfaces/UserRessource';
import Utils from '../utils';
import Payload from '@/interfaces/Payload';

const MAIL_MESSAGE = 'MAIL';
const PUSH_MESSAGE = 'PUSH';

export default class TemplatingService {
  // region public static methods
  // endregion

  // region private static methods
  private static initializeMessageTemplates(platformId: string, templateId: string): Template[] {
    return [MAIL_MESSAGE, PUSH_MESSAGE].map((type) => {
      const messageTemplate = Utils.loadTemplate(platformId, templateId, type);
      TemplatingService.parseMessageTemplate(messageTemplate);
      return messageTemplate;
    });
  }

  private static parseMessageTemplate(template: Template) {
    for (const key in template) {
      // && typeof template[key] === 'string'
      if (template.hasOwnProperty(key) && key !== 'type') {
        Mustache.parse(template[key]);
      }
    }
  }

  private static renderMessageTemplate(template: Template, payload: Payload): Template {
    const compiledTemplate: Template = template;
    for (const key in template) {
      if (template.hasOwnProperty(key) && key !== 'type') {
        compiledTemplate[key] = Mustache.render(template[key], payload);
      }
    }
    return compiledTemplate;
  }

  private static initializeMessagePayloads(payload: any, languagePayloads: LanguagePayload[]): Payload[] {
    return languagePayloads.map((languagePayload: LanguagePayload): Payload => {
      return {
        message: payload,
        languageId: languagePayload.language,
        language: languagePayload.payload,
        user: {},
      };
      });
    }
  // endregion

  // region public members
  // endregion

  // region private members

  private parsedMessageTemplates: Template[];
  private messagePayloads: Payload[];

  // endregion

  // region constructor

  public constructor(platformId: string, templateId: string, payload: {}, languagePayloads: LanguagePayload[]) {
    this.parsedMessageTemplates = TemplatingService.initializeMessageTemplates(platformId, templateId);
    this.messagePayloads = TemplatingService.initializeMessagePayloads(payload, languagePayloads);
  }

  // endregion

  // region public methods

  public createMailMessage(user: UserRessource): Mail {
    const template = this.getTemplateForUser(user, MAIL_MESSAGE);
    const mail = {
      from: template.from,
      to: user.mail,
      subject: template.subject,
      text: template.text,
      html: template.html,
    };
    return mail;
  }

  public createPushMessage(user: UserRessource, device: string): firebaseMessaging.Message {
    const template = this.getTemplateForUser(user, PUSH_MESSAGE);
    const push = {
      token: device,
      data: template.data,
      notification: template.notification,
      android: template.android,
      webpush: template.webpush,
      apns: template.apns,
    };
    return push;
  }

  // endregion

  // region private methods

  private compileMessageTemplates(platformId: string, templateId: string, payload: {}) {
    const messageTemplates = [];

    for (const type of [MAIL_MESSAGE, PUSH_MESSAGE]) {
      // Step 1: Load base template
      const baseTemplate = Utils.getTemplate(platformId, templateId, type);
      console.log(baseTemplate);

      // Step 2: Insert general payload into base template
      const messageTemplate = TemplatingService.insertMessagePayload(baseTemplate, payload, platformId);
      messageTemplates.push(messageTemplate);
    }

    return messageTemplates;
  }

  private compileLanguageTemplates(type: string, messageTemplate: any, payload: LanguagePayload[]): any {
    for (const languagePayload of payload) {
      const template = TemplatingService.insertLanguagePayload(messageTemplate, languagePayload.payload);
      const languageId = languagePayload.language;
      const localizedTemplate = {
        languageId,
        type,
        template,
      };
      this.localizedTemplates.push(localizedTemplate);
    }
  }

  private getTemplate(languageId: string, type: string): any {
    const currentTemplate: Template | undefined = this.localizedTemplates.find(
      (template: Template) => {
        return (template.languageId === languageId) && (template.type === type);
      },
    );

    if (currentTemplate) {
      return currentTemplate.template;
    } else {
      return new Error('Invalid languageId or type. Template not found.');
    }
  }

  private getTemplateForUser(user: UserRessource, type: string): any {
    let template = this.getTemplate(user.language, type);
    template = TemplatingService.insertUserPayload(template, user);
    return template;
  }

  // endregion
}<commit_msg>Added mail and push message creation<commit_after>import { messaging as firebaseMessaging } from 'firebase-admin';
import Mustache from 'mustache';
import winston from 'winston';
import Mail from '@/interfaces/Mail';
import Template from '../interfaces/Template';
import LanguagePayload from '@/interfaces/LanguagePayload';
import UserRessource from '@/interfaces/UserRessource';
import Utils from '../utils';
import Payload from '@/interfaces/Payload';

const MAIL_MESSAGE = 'MAIL';
const PUSH_MESSAGE = 'PUSH';

export default class TemplatingService {
  // region public static methods
  // endregion

  // region private static methods
  private static initializeMessageTemplates(platformId: string, templateId: string): Template[] {
    return [MAIL_MESSAGE, PUSH_MESSAGE].map((type) => {
      const messageTemplate = Utils.loadTemplate(platformId, templateId, type);
      TemplatingService.parseMessageTemplate(messageTemplate);
      return messageTemplate;
    });
  }

  private static parseMessageTemplate(template: Template) {
    for (const key in template) {
      // && typeof template[key] === 'string'
      if (template.hasOwnProperty(key) && key !== 'type') {
        Mustache.parse(template[key]);
      }
    }
  }

  private static renderMessageTemplate(template: Template, payload: Payload): Template {
    const compiledTemplate: Template = template;
    for (const key in template) {
      if (template.hasOwnProperty(key) && key !== 'type') {
        compiledTemplate[key] = Mustache.render(template[key], payload);
      }
    }
    return compiledTemplate;
  }

  private static initializeMessagePayloads(payload: any, languagePayloads: LanguagePayload[]): Payload[] {
    return languagePayloads.map((languagePayload: LanguagePayload): Payload => {
      return {
        message: payload,
        languageId: languagePayload.language,
        language: languagePayload.payload,
        user: {},
      };
    });
  }
  // endregion

  // region public members
  // endregion

  // region private members

  private parsedMessageTemplates: Template[];
  private messagePayloads: Payload[];

  // endregion

  // region constructor

  public constructor(platformId: string, templateId: string, payload: {}, languagePayloads: LanguagePayload[]) {
    this.parsedMessageTemplates = TemplatingService.initializeMessageTemplates(platformId, templateId);
    this.messagePayloads = TemplatingService.initializeMessagePayloads(payload, languagePayloads);
  }

  // endregion

  // region public methods

  public createMailMessage(user: UserRessource): Mail {
    const template = this.getTemplate(MAIL_MESSAGE);
    const payload = this.getUserPayload(user);
    const renderedTemplate = TemplatingService.renderMessageTemplate(template, payload);

    const mail = {
      from: renderedTemplate.from,
      to: user.mail,
      subject: renderedTemplate.subject,
      text: renderedTemplate.text,
      html: renderedTemplate.html,
    };
    return mail;
  }

  public createPushMessage(user: UserRessource, device: string): firebaseMessaging.Message {

    const template = this.getTemplate(MAIL_MESSAGE);
    const payload = this.getUserPayload(user);
    const renderedTemplate = TemplatingService.renderMessageTemplate(template, payload);
    const push = {
      token: device,
      data: template.data,
      notification: template.notification,
      android: template.android,
      webpush: template.webpush,
      apns: template.apns,
    };
    return push;
  }

  // endregion

  // region private methods
  private getTemplate(type: string): Template {
    const parsedMessageTemplate = this.parsedMessageTemplates.find( (template) => {
      return template.type === type;
    } );

    if (!parsedMessageTemplate) {
      const errorMessage = `Could not find message template for type ${type}`;
      winston.error(errorMessage);
      throw new Error(errorMessage);
    }

    return parsedMessageTemplate;
  }

  private getUserPayload(user: UserRessource): Payload {
    const messagePayload = this.messagePayloads.find( (payload) => payload.languageId === user.language );

    if (!messagePayload) {
      const errorMessage = `Could not find message payload for language ${user.language}`;
      winston.error(errorMessage);
      throw new Error(errorMessage);
    }

    messagePayload.user = user.payload;
    return messagePayload;
  }
  // endregion
}
{'fraction_non_alphanumeric': 0.10388225255972697, 'fraction_numerical': 0.00021331058020477816, 'mean_word_length': 3.6559086395233367, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1059893', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2468, 'n_words': 638}