Skip to content

Commit

Permalink
Add old account deletion email endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
blazer82 committed Jun 21, 2024
1 parent b80640d commit 4b30c1d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
35 changes: 35 additions & 0 deletions helpers/sendOldAccountMail.ts
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;
56 changes: 56 additions & 0 deletions pages/api/mail/oldaccount.ts
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;
13 changes: 13 additions & 0 deletions schemas/oldAccountMailRequest.ts
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;

0 comments on commit 4b30c1d

Please sign in to comment.