added mail service

This commit is contained in:
2024-04-10 21:24:25 +03:00
parent c76d4b9717
commit 78107d4ec7
80 changed files with 3478 additions and 329 deletions

10
lib/db.ts Normal file
View File

@@ -0,0 +1,10 @@
import { PrismaClient } from '@prisma/client'
import * as process from 'process'
declare global {
var prisma: PrismaClient | undefined
}
export const db = globalThis.prisma || new PrismaClient()
if (process.env.NODE_ENV !== 'production') globalThis.prisma = db

22
lib/mailer.ts Normal file
View File

@@ -0,0 +1,22 @@
'use server'
import { from, transportOptions } from '@/config/mailer'
import nodemailer, { Transporter } from 'nodemailer'
import type { Options } from 'nodemailer/lib/mailer'
import { SentMessageInfo } from 'nodemailer/lib/smtp-transport'
const transporter: Transporter<SentMessageInfo> = nodemailer.createTransport(transportOptions)
type Return = {
isOk: boolean, code?: number, info?: SentMessageInfo, error?: any
}
export default async function mailer ({ to, subject, text, html }: Options): Promise<Return> {
try {
const info: SentMessageInfo = await transporter.sendMail({ from, to, subject, text, html })
return { isOk: true, code: parseInt((info?.response ?? '0').substring(0, 3), 10), info }
} catch (error: any) {
return { isOk: false, error }
}
//TODO: MAILER LOGGING >> error.response || info?.messageId
}

32
lib/tokens.ts Normal file
View File

@@ -0,0 +1,32 @@
import { v4 as uuid } from 'uuid'
import {
VERIFICATION_TOKEN_EXPIRATION_DURATION,
} from '@/config/auth'
import { db } from '@/lib/db'
import { getVerificationTokenByEmail } from '@/data/verification-token'
export const generateVerificationToken = async (email: string) => {
const token = uuid()
const expires = new Date(
new Date().getTime() + VERIFICATION_TOKEN_EXPIRATION_DURATION)
const existingToken = await getVerificationTokenByEmail(email)
if (existingToken) {
await db.verificationToken.delete({
where: {
id: existingToken.id,
},
})
}
const verificationToken = await db.verificationToken.create({
data: {
email,
token,
expires,
},
})
return verificationToken
}

12
lib/utils.ts Normal file
View File

@@ -0,0 +1,12 @@
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
import { LC } from '@/config/locales'
import bcrypt from 'bcryptjs'
export function cn (...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function lc (locale: string) {
return LC.filter(lc => locale === lc.code)[0]
}