From 01b17368576969b4dd19477302872027e79e6dda Mon Sep 17 00:00:00 2001 From: WovenCoast Date: Sun, 11 Feb 2024 12:12:46 +0500 Subject: [PATCH] [feat] Too many features to count.... --- .gitignore | 4 +++- server.js | 22 +++++++++++++++++----- src/telegram.js | 16 ++++++++++++++-- src/views/index.ejs | 44 ++++++++++++++++++++++++++++++++++++++------ 4 files changed, 72 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 32991d3..7d9a7d2 100644 --- a/.gitignore +++ b/.gitignore @@ -131,4 +131,6 @@ dist sarlink.jpg -uploads/ \ No newline at end of file +uploads/ + +registrations.csv \ No newline at end of file diff --git a/server.js b/server.js index 8608df8..8e0f49f 100644 --- a/server.js +++ b/server.js @@ -16,7 +16,8 @@ function loadEnv() { }, price: { normal: process.env.PRICE || "100", - roaming: process.env.ROAMING_PRICE || "200", + roaming: process.env.ROAMING_PRICE || "50", + gaming: process.env.GAMING_PRICE || "100", currency: process.env.PRICE_CURRENCY || "MVR", } } @@ -44,7 +45,8 @@ app.use(express.urlencoded({ extended: true })) // add multer const multer = require("multer") -fs.mkdirSync("uploads", { recursive: true }) +if (!fs.existsSync("uploads")) fs.mkdirSync("uploads", { recursive: true }) +if (!fs.existsSync("registrations.csv")) fs.writeFileSync("registrations.csv", "Customer Name,MAC Address,Device Name,Roaming,Gaming,Wired,Receipt\n", { encoding: "utf-8" }); const upload = multer({ storage: multer.diskStorage({ destination: (req, file, cb) => { @@ -68,15 +70,25 @@ app.post("/register", upload.single("transfer_receipt"), (req, res) => { return; } - const { customer_name, mac_address, device_name, is_roaming } = req.body + const { customer_name, mac_address, device_name, is_roaming, is_gaming } = req.body const receipt = req.file; - if (!customer_name || !mac_address || !device_name || !receipt || !is_roaming) { + if (!customer_name || !mac_address || !device_name || !receipt) { res.render("index", { branding, message: "Please fill all fields" }) return; } - sendInfo(receipt.path, customer_name, mac_address, device_name, is_roaming) + // save info to csv file + const csv = `${customer_name},${mac_address},${device_name},${is_roaming === "on" ? "true" : "false"},${is_gaming === "on" ? "true" : "false"},false,${receipt.path}\n` + fs.appendFile("registrations.csv", csv, (err) => { + if (err) { + console.error(err) + res.render("index", { branding, message: "An internal error occurred" }) + } + }); + + // send receipt to telegram + sendInfo(receipt.path, { customer_name, mac_address, device_name, is_roaming, is_gaming }) .then(() => { res.render("success", { branding }) }) diff --git a/src/telegram.js b/src/telegram.js index f422561..e6b1c93 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -2,8 +2,20 @@ const fs = require("fs") const FormData = require('form-data'); const axios = require('axios').default; -async function sendInfo(filepath, customer_name, mac_address, device_name, roaming) { - const caption = `Customer Name: \`${customer_name}\`\nMAC Address: \`${mac_address}\`\nDevice Name: \`${device_name}\`\nRoaming: **${roaming ? "Yes" : "No"}**` +function kebabToTitleCase(str) { + return str.split("_").map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(" ") +} + +async function sendInfo(filepath, details) { + // const caption = `Customer Name: \`${customer_name}\`\nMAC Address: \`${mac_address}\`\nDevice Name: \`${device_name}\`\nRoaming: **${roaming ? "Yes" : "No"}**\nGaming: **${gaming ? "Yes" : "No"}**` + let caption = ""; + for (const key in details) { + if (key.startsWith("is")) { + details[key] = details[key] === "on" ? "Yes" : "No" + } + + caption += `${kebabToTitleCase(key)}: \`${details[key]}\`\n` + } var formData = new FormData(); formData.append("photo", fs.createReadStream(filepath)); diff --git a/src/views/index.ejs b/src/views/index.ejs index 64752ea..82ca736 100644 --- a/src/views/index.ejs +++ b/src/views/index.ejs @@ -13,20 +13,28 @@ const customerName = document.getElementById("customer_name"); const macAddress = document.getElementById("mac_address"); const deviceName = document.getElementById("device_name"); + const isRoaming = document.getElementById("is_roaming"); + const isGaming = document.getElementById("is_gaming"); customerName.value = localStorage.getItem("customer_name") || ""; macAddress.value = localStorage.getItem("mac_address") || ""; deviceName.value = localStorage.getItem("device_name") || ""; + isRoaming.value = localStorage.getItem("is_roaming") === "true"; + isGaming.value = localStorage.getItem("is_gaming") === "true"; } function saveForm() { const customerName = document.getElementById("customer_name"); const macAddress = document.getElementById("mac_address"); const deviceName = document.getElementById("device_name"); + const isRoaming = document.getElementById("is_roaming"); + const isGaming = document.getElementById("is_gaming"); localStorage.setItem("customer_name", customerName.value); localStorage.setItem("mac_address", macAddress.value); localStorage.setItem("device_name", deviceName.value); + localStorage.setItem("is_roaming", isRoaming.checked? "true": "false"); + localStorage.setItem("is_gaming", isGaming.checked? "true": "false"); } function clearForm() { @@ -38,17 +46,23 @@ document.getElementById("device_name").value = ""; } - function setRoaming() { + function setPricing() { const normalPrice = <%- branding.price.normal %>; const roamingPrice = <%- branding.price.roaming %>; + const gamingPrice = <%- branding.price.gaming %>; const currency = "<%- branding.price.currency %>"; const isRoaming = document.getElementById("is_roaming"); - const amount = document.getElementById("amount"); + const isGaming = document.getElementById("is_gaming"); + const amountLabel = document.getElementById("amount"); + let amount = normalPrice; if (isRoaming.checked) { - amount.textContent = `Amount: ${roamingPrice} ${currency}`; - } else { - amount.textContent = `Amount: ${normalPrice} ${currency}`; + amount += roamingPrice; } + if (isGaming.checked) { + amount += gamingPrice; + } + amountLabel.textContent = `Amount: ${amount} ${currency}`; + saveForm(); } function fillDeviceName() { @@ -346,7 +360,24 @@ name="is_roaming" checked="checked" class="checkbox" - oninput="setRoaming()" + oninput="setPricing()" + /> + + + +
+
+
+
@@ -457,5 +488,6 @@