2025-05-14 21:45:16 +02:00

61 lines
1.9 KiB
TypeScript

import { fetchCall } from '@seed/helpers/FetchHelper';
import { interpolate, promiseAll } from '@seed/helpers/Utils';
import EmailSettingsModel from '@services/module-cms/functions/emails-settings/emails-settings.model';
import { AvailableTranslation } from '@src/__components/components';
export const sendToSlack = async (data, webhookUrl) => {
return fetchCall({
url: webhookUrl,
method: 'POST',
body: data,
raw: true,
headers: { 'Content-Type': 'application/json' },
});
};
export const sendSlackHelper = async (emailSettings: EmailSettingsModel, data: any): Promise<boolean> => {
const slackSettings = emailSettings.slackSettings;
if (!slackSettings) {
console.error('No slack settings found');
return false;
}
const webhooksUrls = slackSettings.webhookUrls;
const text = interpolate(slackSettings.text, data, { withEol: true });
const slackNotification: any = {
username: slackSettings.username || 'Project User',
text,
// eslint-disable-next-line @typescript-eslint/camelcase
icon_emoji: slackSettings.icon || ':rocket:', // User icon, you can also use custom icons here
};
if (slackSettings.actions) {
const actions: any[] = [];
slackSettings.actions.forEach((element) => {
const a: any = {
type: 'button',
text: element.text,
url: element.url,
};
if (element.style) a.style = element.style;
actions.push(a);
});
slackNotification.attachments = [
{
actions,
},
];
}
const promises: Promise<any>[] = [];
webhooksUrls.forEach((element) => {
promises.push(sendToSlack(slackNotification, element));
});
const promiseResults = await promiseAll(promises);
console.log(promiseResults);
return true;
};