From 7efc94cbc65e278cd32a8a85352a1156be75c88c Mon Sep 17 00:00:00 2001 From: pawan-dot <71133473+pawan-dot@users.noreply.github.com> Date: Thu, 30 May 2024 13:28:34 +0530 Subject: [PATCH] coupon and affiliate --- .../Affiliate/AffiliateController.js | 84 +++++++++++++++++++ .../Affiliate/AffiliateRoute.js | 3 + 2 files changed, 87 insertions(+) diff --git a/resources/Affiliate&Coupon/Affiliate/AffiliateController.js b/resources/Affiliate&Coupon/Affiliate/AffiliateController.js index e3f75d8..1045d26 100644 --- a/resources/Affiliate&Coupon/Affiliate/AffiliateController.js +++ b/resources/Affiliate&Coupon/Affiliate/AffiliateController.js @@ -2,16 +2,68 @@ import axios from "axios"; import { AffiliateModel } from "./AffiliateModel.js"; import Razorpay from "razorpay"; +import UserModel from "../../user/userModel.js"; const razorpay = new Razorpay({ key_id: process.env.RAZERPAY_KEY_ID, key_secret: process.env.RAZERPAY_SECRET_KEY, }); +import password from "secure-random-password"; +import sendEmail from "../../../Utils/sendEmail.js"; // -----------------------------AFFILIATE & COUPONS ARE HARDLY BINDED DATA-------------------------------------------------------- //Create Affiliate export const createAffiliate = async (req, res) => { try { const result = req.body; + let findAffiliate = await AffiliateModel.findOne({ email: result?.email }); + if (findAffiliate) { + return res.status(400).json({ + success: false, + message: "This Email Id Affiliate Already exists", + }); + } + // Check if email already exists in User collection + let findUser = await UserModel.findOne({ email: result?.email }); + if (!findUser) { + const passwords = password.randomPassword({ + length: 10, + characters: [ + { characters: password.upper, exactly: 1 }, + { characters: password.symbols, exactly: 1 }, + password.lower, + password.digits, + ], + }); + + // req.body.password = passwords; + const user = await UserModel.create({ + password: passwords, + phone: result?.mobile, + ...result, + }); + // console.log("user", user); + await sendEmail({ + to: `${result?.email}`, // Change to your recipient + + from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender + + subject: `Welcome to Smellika Affiliate!`, + html: `
You can login into :${`https://smellika.com`}
+Below are your Affiliate login credentials:
+Email: ${result?.email}
+Password: ${passwords}
+ Happy shopping,