summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Ukaykhingmarma28 <ukaykhingmarma28@gmail.com>2025-02-03 13:31:12 +0600
committerLibravatar Ukaykhingmarma28 <ukaykhingmarma28@gmail.com>2025-02-03 13:31:12 +0600
commit4755541b711d3b19af00780e26be0c91223842cc (patch)
tree3edc377821f6c6b03808d2a7877a7756b561d1ef
parentb23c9aa681c820a569587ccdaaf018c6b9c19f1c (diff)
downloadinventory-4755541b711d3b19af00780e26be0c91223842cc.tar.gz
inventory-4755541b711d3b19af00780e26be0c91223842cc.zip
hash change
-rw-r--r--Dockerfile21
-rw-r--r--docker-compose.yml15
-rw-r--r--index.js8
-rwxr-xr-xinventory.sh116
-rw-r--r--package.json4
5 files changed, 42 insertions, 122 deletions
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..55ba4ee
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,21 @@
+# Use an official Node.js runtime as a parent image
+FROM node:23
+
+# Set the working directory in the container
+WORKDIR /usr/src/app
+
+# Copy package.json and package-lock.json (or yarn.lock)
+COPY package*.json ./
+COPY yarn.lock ./
+
+# Install dependencies
+RUN npm install
+
+# Copy the rest of the application code
+COPY . .
+
+# Expose the port the app runs on
+EXPOSE 3000
+
+# Define the command to run the app
+CMD ["npm", "start"] \ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..5508fc7
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,15 @@
+version: '3.8'
+
+services:
+ app:
+ build: .
+ ports:
+ - "3000:3000"
+ volumes:
+ - .:/usr/src/app
+ - /usr/src/app/node_modules
+ environment:
+ - MONGO_URI=mongodb://root:xsZuVgVoA8WIUQgs68JImrYv5BqaaU8a0G7MoDiiPps%3D@192.168.0.2:27017/
+ - GOOGLE_CREDENTIALS_PATH=./key.json
+ - SPREADSHEET_ID=1ZbJRxgN8undxjcXUQ2oRcRe1hw_xYRGL9WtrGcvqc30
+
diff --git a/index.js b/index.js
index 017c4c8..d9ba827 100644
--- a/index.js
+++ b/index.js
@@ -49,8 +49,8 @@ async function connectDB() {
connectDB();
-function generateSerialNumber() {
- return `Jadupc${crypto.randomInt(100000, 999999)}`;
+function generateSerialNumber(mac) {
+ return `JPC${crypto.createHash('sha3-224').update(mac).digest('hex').toUpperCase().substring(0, 6)}`;
}
app.post('/upload', async (req, res) => {
@@ -67,10 +67,10 @@ app.post('/upload', async (req, res) => {
const existingDevice = await collection.findOne({ mac });
if (existingDevice) {
- return res.status(400).send('Device with the same MAC address is already registered');
+ return res.status(400).send(`Device with the same MAC address is already registered, ${existingDevice.serialNumber}`);
}
- const serialNumber = generateSerialNumber();
+ const serialNumber = generateSerialNumber(mac);
const newItem = {
serialNumber,
diff --git a/inventory.sh b/inventory.sh
deleted file mode 100755
index ed7a037..0000000
--- a/inventory.sh
+++ /dev/null
@@ -1,116 +0,0 @@
-#!/bin/bash
-
-# Function to get MAC address of the default network interface
-get_mac_address() {
- INTERFACE=$(ip route | awk '/default/ {print $5; exit}')
-
- if [[ -n "$INTERFACE" ]]; then
- MAC=$(cat /sys/class/net/$INTERFACE/address)
- echo "Detected MAC Address: $MAC"
- check_device_status "$MAC"
- else
- echo "No active network interface found!"
- exit 1
- fi
-}
-
-# Function for manual input
-manual_input() {
- read -p "Enter MAC Address (manually or via barcode scan): " MAC
- if [[ -n "$MAC" ]]; then
- check_device_status "$MAC"
- else
- echo "Invalid input! MAC Address cannot be empty."
- exit 1
- fi
-}
-
-# Function to check device status using the API
-check_device_status() {
- local MAC_ADDRESS="$1"
- API_URL="http://localhost:3000/check-status?mac=$MAC_ADDRESS"
-
- RESPONSE=$(curl -s "$API_URL")
-
- if [[ -z "$RESPONSE" ]]; then
- echo "No response from server. Check if the API is running."
- exit 1
- fi
-
- # Check if the device is registered or new
- STATUS=$(echo "$RESPONSE" | jq -r '.status')
-
- if [[ "$STATUS" == "registered" ]]; then
- SERIAL_NUMBER=$(echo "$RESPONSE" | jq -r '.serialNumber')
- echo "Device is already registered. Serial number is $SERIAL_NUMBER."
- elif [[ "$STATUS" == "new" ]]; then
- echo "Device is new. Proceeding to registration."
- register_device "$MAC_ADDRESS"
- else
- echo "Unknown status: $STATUS"
- fi
-}
-
-# Function to register the device
-register_device() {
- local MAC_ADDRESS="$1"
-
- # Ask user for variant choice (pro or starter)
- echo "Select variant (pro or starter):"
- read -p "Enter your choice: " VARIANT
-
- if [[ "$VARIANT" != "pro" && "$VARIANT" != "starter" ]]; then
- echo "Invalid variant choice. Exiting."
- exit 1
- fi
-
- # Get OS name from /etc/os-release
- OS_NAME=$(grep "^NAME=" /etc/os-release | cut -d= -f2 | tr -d '"')
-
- # Optional customer information
- read -p "Enter customer name (or press Enter to skip): " CUSTOMER_NAME
- read -p "Enter customer address (or press Enter to skip): " CUSTOMER_ADDRESS
- read -p "Enter customer phone number (or press Enter to skip): " CUSTOMER_NUMBER
-
- # Prepare JSON body for the POST request
- JSON_BODY=$(jq -n \
- --arg verient "$VARIANT" \
- --arg mac "$MAC_ADDRESS" \
- --arg os "$OS_NAME" \
- --arg status "registered" \
- --arg customerName "$CUSTOMER_NAME" \
- --arg customerAddress "$CUSTOMER_ADDRESS" \
- --arg customerNumber "$CUSTOMER_NUMBER" \
- '{
- Verient: $verient,
- mac: $mac,
- os: $os,
- status: $status,
- customerName: $customerName,
- customerAddress: $customerAddress,
- customerNumber: $customerNumber
- }')
-
- # Send the registration request
- REGISTER_URL="http://localhost:3000/upload"
- REGISTER_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d "$JSON_BODY" "$REGISTER_URL")
-
- if [[ -n "$REGISTER_RESPONSE" ]]; then
- echo "Device successfully registered. Response: $REGISTER_RESPONSE"
- else
- echo "No response from the registration API. Check if the API is running."
- fi
-}
-
-# Menu for user selection
-echo "Select an option:"
-echo "1. Input MAC manually (type manually or barcode scan)"
-echo "2. Auto-detect device MAC"
-
-read -p "Enter your choice (1 or 2): " CHOICE
-
-case $CHOICE in
- 1) manual_input ;;
- 2) get_mac_address ;;
- *) echo "Invalid option. Please select 1 or 2." ;;
-esac
diff --git a/package.json b/package.json
index c356d04..a578b89 100644
--- a/package.json
+++ b/package.json
@@ -8,11 +8,11 @@
},
"dependencies": {
"cors": "^2.8.5",
- "crypto": "^1.0.1",
"dotenv": "^16.4.7",
"express": "^4.17.1",
"fs": "^0.0.1-security",
"googleapis": "^39.2.0",
- "mongodb": "^4.17.2"
+ "mongodb": "^4.17.2",
+ "nodemon": "^3.1.9"
}
}