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: `

Welcome to Smellika Affiliate!

+ Hey ${ + result?.name + }, + + +
+

You can login into :${`https://smellika.com`}

+
+

Below are your Affiliate login credentials:

+

Email: ${result?.email}

+

Password: ${passwords}

+ Happy shopping,
`, + }); + } + const affiliate = new AffiliateModel(result); const savedData = await affiliate.save(); if (savedData) { @@ -419,3 +471,35 @@ export const affiliatPayOut = async (req, res) => { }); } }; + +//LIST ALL AFFILIATE +export const MyAllAffiliate = async (req, res) => { + try { + const user = await UserModel.findById(req.params.id); + if (user?.email) { + const affiliate = await AffiliateModel.find( + { email: user?.email }, + { + name: 1, + _id: 1, + email: 1, + coupon_claimed: 1, + coupon_code: 1, + total_earning: 1, + paid_amount: 1, + // is_affiliate_active: 1, + } + ).sort({ createdAt: -1 }); + // console.log("affiliate", affiliate); + return res.status(200).json({ + success: true, + message: affiliate, + }); + } + } catch (error) { + res.status(500).json({ + success: false, + messgae: error.message ? error.message : "Something went wrong!", + }); + } +}; diff --git a/resources/Affiliate&Coupon/Affiliate/AffiliateRoute.js b/resources/Affiliate&Coupon/Affiliate/AffiliateRoute.js index cd9b754..298328c 100644 --- a/resources/Affiliate&Coupon/Affiliate/AffiliateRoute.js +++ b/resources/Affiliate&Coupon/Affiliate/AffiliateRoute.js @@ -1,5 +1,6 @@ import express from "express"; import { + MyAllAffiliate, affiliatPayOut, affiliatePayHistory, createAffiliate, @@ -30,6 +31,8 @@ router.get( authorizeRoles("admin", "Employee"), listAllAffiliate ); +router.get("/my/:id", isAuthenticatedUser, MyAllAffiliate); + router.get( "/getone/:id", isAuthenticatedUser,