mirror of
https://gitlab.com/sarlink/kyc.git
synced 2025-02-21 18:32:03 +00:00
114 lines
3.4 KiB
JavaScript
114 lines
3.4 KiB
JavaScript
require("dotenv").config()
|
|
|
|
let registrationOpen, branding;
|
|
|
|
const { saveInfo } = require("./src/storage")
|
|
|
|
function loadEnv() {
|
|
registrationOpen = process.env.REGISTRATION_OPEN === "true"
|
|
branding = {
|
|
title: process.env.BRANDING_TITLE || "SAR Link",
|
|
logo: process.env.BRANDING_LOGO || "/public/logo.png",
|
|
bankDetails: {
|
|
name: process.env.BANK_NAME || "Bank Name",
|
|
accountNumber: process.env.ACCOUNT_NUMBER || "1234567890",
|
|
accountName: process.env.ACCOUNT_NAME || "Account Name",
|
|
},
|
|
price: {
|
|
normal: process.env.PRICE || "100",
|
|
roaming: process.env.ROAMING_PRICE || "50",
|
|
gaming: process.env.GAMING_PRICE || "100",
|
|
currency: process.env.PRICE_CURRENCY || "MVR",
|
|
}
|
|
}
|
|
console.log(`Registration is now ${registrationOpen ? "open" : "closed"}`)
|
|
}
|
|
loadEnv()
|
|
|
|
// watch for changes to .env file
|
|
const path = require("path")
|
|
const fs = require("fs")
|
|
fs.watchFile(path.join(__dirname, ".env"), (curr, prev) => {
|
|
delete require.cache[require.resolve("dotenv")]
|
|
require("dotenv").config({ override: true })
|
|
loadEnv()
|
|
})
|
|
|
|
// initialize express
|
|
const express = require("express")
|
|
const app = express()
|
|
app.set("view engine", "ejs")
|
|
app.set("views", "./src/views")
|
|
app.use("/public", express.static("./src/public"))
|
|
app.use("/scripts", express.static("./src/scripts"))
|
|
app.use(express.json())
|
|
app.use(express.urlencoded({ extended: true }))
|
|
|
|
// add multer
|
|
const multer = require("multer")
|
|
if (!fs.existsSync("uploads")) fs.mkdirSync("uploads", { recursive: true })
|
|
if (!fs.existsSync("registrations.csv")) fs.writeFileSync("registrations.csv", "Customer Name,Phone Number,MAC Address,Device Name,Roaming,Gaming,Wired,Receipt\n", { encoding: "utf-8" });
|
|
const upload = multer({
|
|
storage: multer.diskStorage({
|
|
destination: (req, file, cb) => {
|
|
cb(null, "./uploads")
|
|
},
|
|
filename: (req, file, cb) => {
|
|
cb(null, `${Date.now()}- ${req.body["device_name"]} - ${file.originalname}`)
|
|
},
|
|
}),
|
|
fileFilter: (req, file, cb) => {
|
|
if (file.mimetype === "image/jpeg" || file.mimetype === "image/png") {
|
|
cb(null, true)
|
|
} else {
|
|
cb(null, false)
|
|
return cb(new Error("Only .png and .jpg format allowed!"))
|
|
}
|
|
},
|
|
limits: {
|
|
fileSize: 1024 * 1024 * 2 // 2MB
|
|
}
|
|
})
|
|
// app.use(upload.single("receipt"))
|
|
|
|
// routes
|
|
app.get("/", (req, res) => {
|
|
res.render(registrationOpen ? "index" : "closed", { branding, message: "" })
|
|
});
|
|
|
|
app.get("/register", (req, res) => {
|
|
res.redirect("/")
|
|
})
|
|
|
|
app.post("/register", upload.single("transfer_receipt"), (req, res) => {
|
|
if (!registrationOpen) {
|
|
res.render("closed")
|
|
return;
|
|
}
|
|
|
|
let { customer_name, phone_number, mac_address, device_name, is_roaming, is_gaming } = req.body
|
|
const receipt = req.file;
|
|
|
|
if (!is_roaming || is_roaming === "false") is_roaming = false
|
|
if (!is_gaming || is_gaming === "false") is_gaming = false
|
|
|
|
if (!customer_name || !phone_number || !mac_address || !device_name || !receipt) {
|
|
res.render("index", { branding, message: "Please fill all fields" })
|
|
return;
|
|
}
|
|
|
|
// send receipt to all storages
|
|
saveInfo(receipt.path, { customer_name, phone_number, mac_address, device_name, is_roaming, is_gaming })
|
|
.then(() => {
|
|
res.render("success", { branding })
|
|
})
|
|
.catch((error) => {
|
|
console.error(error)
|
|
res.render("index", { branding, message: "An internal error occurred" })
|
|
})
|
|
})
|
|
|
|
const port = process.env.PORT || 4818;
|
|
app.listen(port, '0.0.0.0', () => {
|
|
console.log(`Server is running on port ${port}`);
|
|
}); |