From e20041cd543dda40b1b1fe45bfc7d6643bf1ebe1 Mon Sep 17 00:00:00 2001
From: listlessbird <124798751+listlessbird@users.noreply.github.com>
Date: Sat, 9 Sep 2023 13:19:13 +0530
Subject: [PATCH 1/2] update .gitignore
---
.gitignore | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/.gitignore b/.gitignore
index e1274fa..47c2a1b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -129,4 +129,5 @@ dist
.yarn/install-state.gz
.pnp.*
tmp
-dump
\ No newline at end of file
+dump
+.vscode
\ No newline at end of file
From b592fe5746eaf7f97196246aae5f0bb2b293a8b7 Mon Sep 17 00:00:00 2001
From: listlessbird <124798751+listlessbird@users.noreply.github.com>
Date: Sat, 9 Sep 2023 13:23:00 +0530
Subject: [PATCH 2/2] feat: proshow reg
- add unique id for each proshow reg
- send a mail for verified proshow registrations along with the generated code
---
controllers/orderController.js | 37 +-
models/order.js | 79 +-
package-lock.json | 17 +
package.json | 1 +
utils/emailHelper.js | 236 ++---
utils/index.html | 1492 +++++++++++++++++++++++++++-----
utils/proshow.html | 1332 ++++++++++++++++++++++++++++
7 files changed, 2816 insertions(+), 378 deletions(-)
create mode 100644 utils/proshow.html
diff --git a/controllers/orderController.js b/controllers/orderController.js
index a5485ae..6b615db 100644
--- a/controllers/orderController.js
+++ b/controllers/orderController.js
@@ -1,10 +1,13 @@
-const Order = require("../models/order")
+const { Order } = require("../models/order")
const Events = require("../models/event")
const campusAmbassadors = require("../models/campusAmbassador")
const Users = require("../models/user")
const BigPromise = require("../middlewares/bigPromise")
const CustomError = require("../errors/customError")
-const mailHelper = require("../utils/emailHelper")
+const {
+ mailHelper,
+ sendMailForVerifiedProshow,
+} = require("../utils/emailHelper")
const cloudinary = require("cloudinary")
const order = require("../models/order")
const mongoose = require("mongoose")
@@ -26,14 +29,20 @@ exports.createOrder = BigPromise(async (req, res, next) => {
console.log(orderEvents)
const orderEventsWithParticipants = orderEvents.map((event) => {
- return {
+ const e = {
event: event.event,
participants: event.participants, // Add participants array here
name: event.name,
type: event.type,
price: event.price,
- ticketCount: event.ticketCount,
}
+
+ if (event.type === "proshow") {
+ e.ticketCount = event.ticketCount
+ return e
+ }
+
+ return e
})
const parsedAmount = parseInt(totalAmount)
@@ -117,6 +126,8 @@ exports.createOrder = BigPromise(async (req, res, next) => {
},
})
+ console.log("Order", order)
+
if (referalCode) {
const ambassador = await campusAmbassadors.findOne({ referalCode })
@@ -201,6 +212,9 @@ exports.verifyOrder = BigPromise(async (req, res, next) => {
const order = await Order.findById(id)
if (order) {
+ console.log("Order to verify", order)
+
+ // return
order.orderVerified = true
await order.save({ validateBeforeSave: false })
@@ -215,13 +229,22 @@ exports.verifyOrder = BigPromise(async (req, res, next) => {
}
}
- for await (const event of order.orderEvents) {
+ for (const event of order.orderEvents) {
const id = event.event
await updateEventTicket(id)
const singleEvent = await Events.findById(id)
- await mailHelper(order, singleEvent,"verified")
+ if (singleEvent.eventType === "proshow") {
+ await sendMailForVerifiedProshow({
+ email: order.email,
+ eventName: singleEvent.name,
+ uniqueId: event.uniqueId,
+ eventDate: singleEvent.date,
+ })
+ } else {
+ await mailHelper(order, singleEvent, "verified")
+ }
}
-
+
res.status(200).json({
message: "Order verified successfully",
status: true,
diff --git a/models/order.js b/models/order.js
index fef87ba..af5325d 100644
--- a/models/order.js
+++ b/models/order.js
@@ -1,5 +1,40 @@
const mongoose = require("mongoose")
const validator = require("validator")
+const { nanoid } = require("nanoid")
+
+const orderedEventsSchema = new mongoose.Schema(
+ {
+ event: {
+ type: mongoose.Schema.ObjectId,
+ ref: "Events",
+ required: true,
+ },
+ participants: [
+ {
+ type: String,
+ },
+ ],
+ name: {
+ type: String,
+ },
+ price: {
+ type: Number,
+ },
+ },
+ { discriminatorKey: "type" }
+)
+
+const orderedProshowSchema = new mongoose.Schema({
+ uniqueId: {
+ type: String,
+ required: true,
+ default: () => nanoid(6),
+ index: { unique: true },
+ },
+ ticketCount: {
+ type: Number,
+ },
+})
const orderSchema = new mongoose.Schema({
name: {
@@ -32,32 +67,7 @@ const orderSchema = new mongoose.Schema({
type: Boolean,
default: false,
},
- orderEvents: [
- {
- event: {
- type: mongoose.Schema.ObjectId,
- ref: "Events",
- required: true,
- },
- participants: [
- {
- type: String,
- },
- ],
- name: {
- type: String,
- },
- type: {
- type: String,
- },
- price: {
- type: Number,
- },
- ticketCount: {
- type: Number,
- },
- },
- ],
+ orderEvents: [orderedEventsSchema],
paymentInfo: {
id: {
type: String,
@@ -67,16 +77,6 @@ const orderSchema = new mongoose.Schema({
type: Number,
required: [true, "please provide the total amount"],
},
- // paymentProof: {
- // id: {
- // type: String,
- // required: [true, "please provide the payment proof"],
- // },
- // secure_url: {
- // type: String,
- // required: [true, "please provide the payment proof"],
- // },
- // },
createdAt: {
type: Date,
default: Date.now(),
@@ -92,4 +92,9 @@ const orderSchema = new mongoose.Schema({
},
})
-module.exports = mongoose.model("Order", orderSchema)
+orderSchema.path("orderEvents").discriminator("proshow", orderedProshowSchema)
+
+const Order = mongoose.model("Order", orderSchema)
+module.exports = { Order }
+// module.exports = orderedProshow
+// module.exports = orderedEvents
diff --git a/package-lock.json b/package-lock.json
index 8f25903..e031d28 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -23,6 +23,7 @@
"mongoose": "^7.1.0",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
+ "nanoid": "^3.3.4",
"nodemailer": "^6.9.2",
"razorpay": "^2.8.6",
"uuid": "^9.0.0",
@@ -1864,6 +1865,17 @@
"node": ">= 6.0.0"
}
},
+ "node_modules/nanoid": {
+ "version": "3.3.4",
+ "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz",
+ "integrity": "sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==",
+ "bin": {
+ "nanoid": "bin/nanoid.cjs"
+ },
+ "engines": {
+ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
+ }
+ },
"node_modules/negotiator": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
@@ -4341,6 +4353,11 @@
"xtend": "^4.0.0"
}
},
+ "nanoid": {
+ "version": "3.3.4",
+ "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz",
+ "integrity": "sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw=="
+ },
"negotiator": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
diff --git a/package.json b/package.json
index 1b357c9..9c872e2 100644
--- a/package.json
+++ b/package.json
@@ -34,6 +34,7 @@
"mongoose": "^7.1.0",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
+ "nanoid": "^3.3.4",
"nodemailer": "^6.9.2",
"razorpay": "^2.8.6",
"uuid": "^9.0.0",
diff --git a/utils/emailHelper.js b/utils/emailHelper.js
index e299594..c837b3b 100644
--- a/utils/emailHelper.js
+++ b/utils/emailHelper.js
@@ -1,11 +1,11 @@
-const sgMail = require("@sendgrid/mail");
-const fs = require("fs").promises;
+const sgMail = require("@sendgrid/mail")
+const fs = require("fs").promises
-sgMail.setApiKey(process.env.SENDGRID_API_KEY);
+sgMail.setApiKey(process.env.SENDGRID_API_KEY)
-const mailHelper = async (order, event,type) => {
- console.log("inside mail helper");
- const { email, name } = order;
+const mailHelper = async (order, event, type) => {
+ console.log("inside mail helper")
+ const { email, name } = order
const {
name: eventName,
category,
@@ -13,12 +13,11 @@ const mailHelper = async (order, event,type) => {
contactName,
contactNumber,
time,
- } = event;
-
+ } = event
//converting date to readable format : month date
- const options = { month: "long", day: "numeric" };
- const dateString = date.toLocaleDateString("en-US", options);
+ const options = { month: "long", day: "numeric" }
+ const dateString = date.toLocaleDateString("en-IN", options)
// const formattedTime = time.toLocaleString("en-US", {
// hour: "numeric",
@@ -26,101 +25,132 @@ const mailHelper = async (order, event,type) => {
// hour12: true,
// timeZone: "UTC"
// });
-
- // const {rules} = event
- // let rulesHtml = "";
-
- // for (let i =0 ; i < rules.length; i++) {
- // rulesHtml += `
`
- // }
-
- if(type === "unverified"){
- try {
- // Read HTML template file
- mailTemplate = await fs.readFile(
- __dirname + "/index.html",
- "utf-8"
- );
- console.log("html file read successful");
- } catch (error) {
- console.error("Error reading HTML template file:", error);
- return "error";
- }
- const messageHtml = mailTemplate
- .replace("{name}", name)
- .replace("{event}", eventName)
- // .replace("{{date}}", dateString)
- // .replace("{contactNumber}",contactNumber)
- // .replace("{rules}", rulesHtml);
- // .replace("{time}",formattedTime)
-
-
-
-
- // Send email with the modified HTML template
- const message = {
- to: email,
- from: {
- name: "Invento",
- email: process.env.EMAIL,
- },
- subject: `Registration Received | INVENTO'23`,
- html: messageHtml,
- };
-
- try {
- await sgMail.send(message);
- console.log("email sent");
- } catch (error) {
- console.log(error);
- }
+
+ // const {rules} = event
+ // let rulesHtml = "";
+
+ // for (let i =0 ; i < rules.length; i++) {
+ // rulesHtml += ``
+ // }
+
+ if (type === "unverified") {
+ try {
+ // Read HTML template file
+ mailTemplate = await fs.readFile(__dirname + "/index.html", "utf-8")
+ console.log("html file read successful")
+ } catch (error) {
+ console.error("Error reading HTML template file:", error)
+ return "error"
+ }
+ const messageHtml = mailTemplate
+ .replace("{name}", name)
+ .replace("{event}", eventName)
+ // .replace("{{date}}", dateString)
+ // .replace("{contactNumber}",contactNumber)
+ // .replace("{rules}", rulesHtml);
+ // .replace("{time}",formattedTime)
+
+ // Send email with the modified HTML template
+ const message = {
+ to: email,
+ from: {
+ name: "Invento",
+ email: process.env.EMAIL,
+ },
+ subject: `Registration Received | INVENTO'23`,
+ html: messageHtml,
}
- else if(type === "verified"){
- try {
- // Read HTML template file
- mailTemplate = await fs.readFile(
- __dirname + "/confirm.html",
- "utf-8"
- );
- console.log("html file read successful");
- } catch (error) {
- console.error("Error reading HTML template file:", error);
- return "error";
- }
- const messageHtml = mailTemplate
- .replace("{name}", name)
- .replace("{event}", eventName)
- // .replace("{{date}}", dateString)
- // .replace("{contactNumber}",contactNumber)
- // .replace("{rules}", rulesHtml);
- // .replace("{time}",formattedTime)
-
-
-
-
- // Send email with the modified HTML template
- const message = {
- to: email,
- from: {
- name: "Invento",
- email: process.env.EMAIL,
- },
- subject: `Payment Verified : ${eventName} | INVENTO'23`,
- html: messageHtml,
- };
-
- try {
- await sgMail.send(message);
- console.log("email sent");
- } catch (error) {
- console.log(error);
- }
+
+ try {
+ await sgMail.send(message)
+ console.log("email sent")
+ } catch (error) {
+ console.log(error)
+ }
+ } else if (type === "verified") {
+ try {
+ // Read HTML template file
+ mailTemplate = await fs.readFile(__dirname + "/confirm.html", "utf-8")
+ console.log("html file read successful")
+ } catch (error) {
+ console.error("Error reading HTML template file:", error)
+ return "error"
+ }
+ const messageHtml = mailTemplate
+ .replace("{name}", name)
+ .replace("{event}", eventName)
+ // .replace("{{date}}", dateString)
+ // .replace("{contactNumber}",contactNumber)
+ // .replace("{rules}", rulesHtml);
+ // .replace("{time}",formattedTime)
+
+ // Send email with the modified HTML template
+ const message = {
+ to: email,
+ from: {
+ name: "Invento",
+ email: process.env.EMAIL,
+ },
+ subject: `Payment Verified : ${eventName} | INVENTO'23`,
+ html: messageHtml,
}
-
+
+ try {
+ await sgMail.send(message)
+ console.log("email sent")
+ } catch (error) {
+ console.log(error)
}
+ }
+}
+
+async function sendMailForVerifiedProshow(mailparams) {
+ const { email, eventName, uniqueId, eventDate } = mailparams
+
+ if (!email || !eventName || !uniqueId || !eventDate) {
+ throw new Error("Invalid mail params")
+ }
+
+ const options = { month: "long", day: "numeric" }
+ const dateString =
+ eventName.toLowerCase() === "combo"
+ ? "15/09/23 ,16/09/23"
+ : eventDate.toLocaleDateString("en-IN", options)
+
+ const message = {
+ to: email,
+ from: {
+ name: "Invento",
+ email: process.env.EMAIL,
+ },
+ subject: `Payment Verified - ProShow -${eventName} | INVENTO'23`,
+ }
+
+ let mailTemplate = ""
+
+ try {
+ mailTemplate = await fs.readFile(__dirname + "/proshow.html", "utf-8")
+ } catch (error) {
+ console.error("Error reading HTML template file:", error)
+ return "error"
+ }
+
+ const messageHtml = mailTemplate
+ .replace("{date}", dateString)
+ .replace("{event}", eventName)
+ .replace("{code}", uniqueId)
+
+ message.html = messageHtml
-
+ try {
+ await sgMail.send(message)
+ console.log("email sent")
+ } catch (error) {
+ console.error("Error sending mail")
+ console.log(error)
+ }
+}
-module.exports = mailHelper;
+module.exports = { mailHelper, sendMailForVerifiedProshow }
diff --git a/utils/index.html b/utils/index.html
index 43d0059..61b22b8 100644
--- a/utils/index.html
+++ b/utils/index.html
@@ -1,236 +1,1266 @@
-
-
-
-
-
-
-
- New message
+
+
-
-
-
-
-
+