Skip to content
Snippets Groups Projects
Select Git revision
  • e3cc96feb2b89d8f9161a7ebe13d1d7c8aaa62a8
  • master default protected
2 results

formatter.ts

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    formatter.ts 3.44 KiB
    import type { NotificationData, NotificationType, PossibleNotificationType } from "../../notifications/types";
    
    export function getMessageTitle<T extends PossibleNotificationType>(type: NotificationType<T>): string {
    	switch(type.name){
    		case "buy": {
    			return "Dein Einkauf";
    		}
    		case "refund": {
    			return "Geld erstattet";
    		}
    		case "deposit": {
    			return "Geld eingezahlt";
    		}
    		case "withdraw": {
    			return "Geld ausgezahlt";
    		}
    		case "use-voucher": {
    			return "Gutschein eingelöst";
    		}
    		case "send-transfer": {
    			return "Überweisung gesendet";
    		}
    		case "receive-transfer": {
    			return "Überweisung erhalten";
    		}
    		default: {
    			console.error(`Unknown notification type`, type);
    			return `Unknown notification type ${type}`;
    		}
    	}
    }			
    
    export function formatMessage<T extends PossibleNotificationType>(type: NotificationType<T>, data: NotificationData<T>): string {
    	switch(type.name){
    		case "buy": {
    			const {total, items, balanceBefore, balanceAfter} = data as NotificationData<"buy">;
    			const premiums = items.filter(item => item.premium && item.premium > 0).map(item => item.premium).reduce((a,b) => a+b, 0);
    			return `Du hast folgende ${items.length} Artikel gekauft:
    ${items.map(item => `- ${item.name} (${item.code}) - ${(item.price/100).toFixed(2)}€` + (item.premium && item.premium > 0 ? ` (+${(item.premium/100).toFixed(2)}€)` : "")).join("\n")}
    Gesamt: ${(total/100).toFixed(2)}€` + (premiums > 0 ? ` (davon ${(premiums/100).toFixed(2)}€ Negativaufschlag)` : "") + `\nDein neuer Kontostand beträgt ${(balanceAfter/100).toFixed(2)}€.`;
    		}
    		case "refund": {
    			const {refund, item, balanceBefore, balanceAfter, timeBought} = data as NotificationData<"refund">;
    			const premiumMessage = refund.premium && refund.premium > 0 ? ` (+${(refund.premium/100).toFixed(2)}€)` : "";
    			return `Dir wurden ${(refund.price/100).toFixed(2)}€${premiumMessage} für ${item.name} (${item.code}) erstattet, gekauft am ${timeBought.toLocaleString()}.
    Dein neuer Kontostand beträgt ${(balanceAfter/100).toFixed(2)}€.`;
    		}
    		case "deposit": {
    			const {amount, balanceBefore, balanceAfter} = data as NotificationData<"deposit">;
    			return `Du hast ${(amount/100).toFixed(2)}€ eingezahlt.
    Dein neuer Kontostand beträgt ${(balanceAfter/100).toFixed(2)}€.`;
    		}
    		case "withdraw": {
    			const {amount, balanceBefore, balanceAfter} = data as NotificationData<"withdraw">;
    			return `Du hast ${(amount/100).toFixed(2)}€ ausgezahlt.
    Dein neuer Kontostand beträgt ${(balanceAfter/100).toFixed(2)}€.`;
    		}
    		case "use-voucher": {
    			const {voucher, balanceBefore, balanceAfter} = data as NotificationData<"use-voucher">;
    			return `Du hast einen Gutschein im Wert von ${(voucher.value/100).toFixed(2)}€ eingelöst.
    Dein neuer Kontostand beträgt ${(balanceAfter/100).toFixed(2)}€.`;
    		}
    		case "send-transfer": {
    			const {amount, balanceBefore, balanceAfter, receiver} = data as NotificationData<"send-transfer">;
    			return `Du hast ${(amount/100).toFixed(2)}€ an ${receiver.name} überwiesen.
    Dein neuer Kontostand beträgt ${(balanceAfter/100).toFixed(2)}€.`;
    		}
    		case "receive-transfer": {
    			const {amount, balanceBefore, balanceAfter, sender} = data as NotificationData<"receive-transfer">;
    			return `Du hast ${(amount/100).toFixed(2)}€ von ${sender.name} erhalten.
    Dein neuer Kontostand beträgt ${(balanceAfter/100).toFixed(2)}€.`;
    		}
    		default: {
    			console.error(`Unknown notification type`, type);
    			return `Unknown notification type ${type}`;
    		}
    	}
    }