-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add old account deletion email endpoint
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import nodemailer from 'nodemailer'; | ||
import getConfig from 'next/config'; | ||
import {User} from '@/types/User'; | ||
import {logger} from './logger'; | ||
|
||
const {serverRuntimeConfig, publicRuntimeConfig} = getConfig(); | ||
|
||
const sendOldAccountMail = async (user: User) => { | ||
logger.info(`Send old account mail to ${user._id}`); | ||
|
||
try { | ||
const transporter = nodemailer.createTransport(serverRuntimeConfig.nodemailerTransport); | ||
await transporter.sendMail({ | ||
from: {name: publicRuntimeConfig.emailSenderName, address: publicRuntimeConfig.supportEmail}, | ||
to: user.email, | ||
subject: "You haven't been on Analytodon in a while - we'll be deleting your data soon!", | ||
text: | ||
`Hi and thanks for signing up to Analytodon!\n\n` + | ||
`We haven't seem you in a while and we'll be deleting your data soon.\n` + | ||
`If you don't want your data to be deleted just log in to your dashboard:\n` + | ||
`${publicRuntimeConfig.appURL}\n\n` + | ||
`If you don't intend to use Analytodon anymore you don't need to do anything.\n\n` + | ||
`Best regards,\n` + | ||
`Raphael Stäbler\n` + | ||
`Analytodon\n\n` + | ||
`Email: ${publicRuntimeConfig.supportEmail}\n` + | ||
`Website: ${publicRuntimeConfig.marketingURL}\n` + | ||
`Mastodon: https://undefined.social/@analytodon\n`, | ||
}); | ||
} catch (error: any) { | ||
logger.error(`Error while sending old account mail: ${error?.message}`); | ||
} | ||
}; | ||
|
||
export default sendOldAccountMail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type {NextApiRequest, NextApiResponse} from 'next'; | ||
import {setNoCache} from '@/helpers/setNoCache'; | ||
import dbConnect from '@/helpers/dbConnect'; | ||
import {logger} from '@/helpers/logger'; | ||
import UserModel from '@/models/UserModel'; | ||
import OldAccountMailRequestSchema, {OldAccountMailRequest} from '@/schemas/oldAccountMailRequest'; | ||
import getConfig from 'next/config'; | ||
import sendOldAccountMail from '@/helpers/sendOldAccountMail'; | ||
|
||
const {serverRuntimeConfig} = getConfig(); | ||
|
||
const handle = async (req: NextApiRequest, res: NextApiResponse): Promise<void | NextApiResponse> => { | ||
setNoCache(res); | ||
|
||
if (req.headers['authorization'] !== serverRuntimeConfig.emailAPIKey) { | ||
return res.status(401).end(); | ||
} | ||
|
||
switch (req.method) { | ||
case 'POST': | ||
return await handlePost(req, res); | ||
default: | ||
return res.status(405).end(); | ||
} | ||
}; | ||
|
||
const handlePost = async ({body}: NextApiRequest, res: NextApiResponse): Promise<void | NextApiResponse> => { | ||
try { | ||
const {value, error} = OldAccountMailRequestSchema.validate(body, {errors: {render: false}}); | ||
|
||
if ((error?.details?.length ?? 0) > 0) { | ||
return res.status(400).end(); | ||
} | ||
|
||
await dbConnect(); | ||
|
||
const {userID} = value as OldAccountMailRequest; | ||
|
||
logger.info(`Request old account email for user: ${userID}`); | ||
|
||
const user = await UserModel.findOne({_id: userID}); | ||
if (!user) { | ||
logger.error(`Request old account email: user not found: ${userID}`); | ||
return res.status(404).end(); | ||
} | ||
|
||
await sendOldAccountMail(user); | ||
|
||
res.end(); | ||
} catch (error: any) { | ||
logger.error(`Error while sending old account email: ${error?.message}`); | ||
return res.status(500).end(); | ||
} | ||
}; | ||
|
||
export default handle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Joi from 'joi'; | ||
|
||
export interface OldAccountMailRequest { | ||
userID: string; | ||
} | ||
|
||
export const schema = { | ||
userID: Joi.string().required(), | ||
}; | ||
|
||
const joiSchema = Joi.object(schema); | ||
|
||
export default joiSchema; |