mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
## Description Support setting a document language that will control the language used for sending emails to recipients. Additional work has been done to convert all emails to using our i18n implementation so we can later add controls for sending other kinds of emails in a users target language. ## Related Issue N/A ## Changes Made - Added `<Trans>` and `msg` macros to emails - Introduced a new `renderEmailWithI18N` utility in the lib package - Updated all emails to use the `<Tailwind>` component at the top level due to rendering constraints - Updated the `i18n.server.tsx` file to not use a top level await ## Testing Performed - Configured document language and verified emails were sent in the expected language - Created a document from a template and verified that the templates language was transferred to the document
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import crypto from 'crypto';
|
|
import { DateTime } from 'luxon';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
import { ONE_HOUR } from '../../constants/time';
|
|
import { sendConfirmationEmail } from '../auth/send-confirmation-email';
|
|
import { getMostRecentVerificationTokenByUserId } from './get-most-recent-verification-token-by-user-id';
|
|
|
|
const IDENTIFIER = 'confirmation-email';
|
|
|
|
type SendConfirmationTokenOptions = { email: string; force?: boolean };
|
|
|
|
export const sendConfirmationToken = async ({
|
|
email,
|
|
force = false,
|
|
}: SendConfirmationTokenOptions) => {
|
|
const token = crypto.randomBytes(20).toString('hex');
|
|
|
|
const user = await prisma.user.findFirst({
|
|
where: {
|
|
email: email,
|
|
},
|
|
});
|
|
|
|
if (!user) {
|
|
throw new Error('User not found');
|
|
}
|
|
|
|
if (user.emailVerified) {
|
|
throw new Error('Email verified');
|
|
}
|
|
|
|
const mostRecentToken = await getMostRecentVerificationTokenByUserId({ userId: user.id });
|
|
|
|
// If we've sent a token in the last 5 minutes, don't send another one
|
|
if (
|
|
!force &&
|
|
mostRecentToken?.createdAt &&
|
|
DateTime.fromJSDate(mostRecentToken.createdAt).diffNow('minutes').minutes > -5
|
|
) {
|
|
// return;
|
|
}
|
|
|
|
const createdToken = await prisma.verificationToken.create({
|
|
data: {
|
|
identifier: IDENTIFIER,
|
|
token: token,
|
|
expires: new Date(Date.now() + ONE_HOUR),
|
|
user: {
|
|
connect: {
|
|
id: user.id,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!createdToken) {
|
|
throw new Error(`Failed to create the verification token`);
|
|
}
|
|
|
|
try {
|
|
await sendConfirmationEmail({ userId: user.id });
|
|
|
|
return { success: true };
|
|
} catch (err) {
|
|
console.log(err);
|
|
throw new Error(`Failed to send the confirmation email`);
|
|
}
|
|
};
|