60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import { EngineModel } from '@seed/engine/EngineModel';
|
|
import { AvailableTranslation } from '@src/__components/components';
|
|
import _ from 'lodash';
|
|
import { NotificationInput } from '../services/NotificationService';
|
|
import { NotificationType, ReceiverData } from '../schemas/notifications.schema';
|
|
import { IEngineSchema } from '@seed/engine/EngineSchema';
|
|
|
|
export const getNotificationsTypesFromInput = <T extends IEngineSchema>(input: NotificationInput<T>): NotificationType[] => {
|
|
const types: NotificationType[] = [];
|
|
|
|
if (input.emails) types.push(NotificationType.mail);
|
|
if (input.pushTokens) types.push(NotificationType.mail);
|
|
if (input.sms) types.push(NotificationType.sms);
|
|
|
|
return types;
|
|
};
|
|
|
|
export const getReceiverDataFromLanguage = <T extends IEngineSchema>(input: NotificationInput<T>): typeof receiverData => {
|
|
const receiverData: {
|
|
emails?: ReceiverData[];
|
|
pushTokens?: ReceiverData[];
|
|
sms?: ReceiverData[];
|
|
} = {};
|
|
|
|
const finalLanguage = input.language || (process.env.DEFAULT_LANGUAGE as any) || AvailableTranslation.en;
|
|
|
|
if (input.emails) {
|
|
if(!_.isArray(input.emails)) input.emails = [input.emails];
|
|
|
|
receiverData.emails = _.map(input.emails, (email) => {
|
|
return {
|
|
value: email,
|
|
language: finalLanguage,
|
|
};
|
|
});
|
|
}
|
|
if (input.pushTokens) {
|
|
if(!_.isArray(input.pushTokens)) input.pushTokens = [input.pushTokens];
|
|
|
|
receiverData.pushTokens = _.map(input.pushTokens, (pushTokens) => {
|
|
return {
|
|
value: pushTokens,
|
|
language: finalLanguage,
|
|
};
|
|
});
|
|
}
|
|
if (input.sms) {
|
|
if(!_.isArray(input.sms)) input.sms = [input.sms];
|
|
|
|
receiverData.sms = _.map(input.sms, (sms) => {
|
|
return {
|
|
value: sms,
|
|
language: finalLanguage,
|
|
};
|
|
});
|
|
}
|
|
|
|
return receiverData;
|
|
};
|