feat: create addUserEmailToProduct function
This commit is contained in:
1 parent
8c4aed91ba
commit
43944e31e3
1 file changed
+24
@@ -5,6 +5,8 @@ import { scrapeAmazonProduct } from "../scraper";
|
||||
import { connectToDB } from '../mongoose';
|
||||
import Product from '../models/product.model'
|
||||
import { getAveragePrice, getHighestPrice, getLowestPrice } from "../utils";
|
||||
import { generateEmailBody, sendEmail } from '../nodemailer';
|
||||
import { User } from '@/types';
|
||||
|
||||
export async function scrapeAndStoreProduct(productUrl: string) {
|
||||
if (!productUrl) return;
|
||||
@@ -87,4 +89,26 @@ export async function getSimilarProducts(productId: string) {
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function addUserEmailToProduct(productId: string, userEmail: string) {
|
||||
try {
|
||||
const product = await Product.findById(productId);
|
||||
|
||||
if (!product) return;
|
||||
|
||||
const userExists = product.users.some((user: User) => user.email === userEmail);
|
||||
|
||||
if (!userExists) {
|
||||
product.users.push({ email: userEmail });
|
||||
|
||||
await product.save();
|
||||
|
||||
const emailContent = await generateEmailBody(product, "WELCOME");
|
||||
|
||||
await sendEmail(emailContent, [userEmail]);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user