You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.1 KiB
94 lines
2.1 KiB
import {NextRouter} from 'next/router';
|
|
import {getLocaleShort} from './utils';
|
|
import {availableLanguages} from '@functions/i18next';
|
|
|
|
export const sendInviteMails = async (
|
|
mails: Array<string>,
|
|
name: string,
|
|
urlVotes: Array<string | URL>,
|
|
urlResult: string | URL,
|
|
router: NextRouter,
|
|
|
|
) => {
|
|
/**
|
|
* Send an invitation mail using a micro-service with Netlify
|
|
*/
|
|
if (!mails || !mails.length) {
|
|
throw new Error('No emails are provided.');
|
|
}
|
|
|
|
if (mails.length !== urlVotes.length) {
|
|
throw new Error('The number of emails differ from the number of tokens');
|
|
}
|
|
|
|
const recipients = {};
|
|
mails.forEach((_, index: number) => {
|
|
recipients[mails[index]] = {
|
|
urlVote: urlVotes[index],
|
|
urlResult: urlResult,
|
|
title: name,
|
|
};
|
|
});
|
|
|
|
const locale = getLocaleShort(router);
|
|
if (!availableLanguages.includes(locale)) {
|
|
throw Error(`{locale} is not available for mails`)
|
|
}
|
|
|
|
const req = await fetch('/.netlify/functions/send-emails', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
action: "invite",
|
|
recipients,
|
|
locale,
|
|
}),
|
|
});
|
|
|
|
return req;
|
|
};
|
|
|
|
|
|
export const sendAdminMail = async (
|
|
mail: string,
|
|
name: string,
|
|
urlAdmin: URL,
|
|
) => {
|
|
/**
|
|
* Send an invitation mail using a micro-service with Netlify
|
|
*/
|
|
if (!mail || !validateMail(mail)) {
|
|
throw new Error('Incorrect format for the email');
|
|
}
|
|
|
|
const req = await fetch('/.netlify/functions/send-emails', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
action: "admin",
|
|
recipients: {
|
|
[mail]: {
|
|
urlAdmin,
|
|
title: name,
|
|
},
|
|
}
|
|
})
|
|
});
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
export const validateMail = (email: string) => {
|
|
// https://stackoverflow.com/a/46181/4986615
|
|
return String(email)
|
|
.toLowerCase()
|
|
.match(
|
|
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
);
|
|
};
|