summaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
authorLibravatar Ukaykhingmarma28 <ukaykhingmarma28@gmail.com>2025-02-08 13:04:01 +0600
committerLibravatar Ukaykhingmarma28 <ukaykhingmarma28@gmail.com>2025-02-08 13:04:01 +0600
commite5df3d1717bce1fdf4a77f67ab28debb1cb11f54 (patch)
tree47125d0af841dba8d2f79373ec6871549c59e177 /index.js
parentc79ee80a3ae9ba7f9ce667233c0fb26ff09975f3 (diff)
downloadinventory-main.tar.gz
inventory-main.zip
.........HEADmain
Diffstat (limited to 'index.js')
-rw-r--r--index.js29
1 files changed, 20 insertions, 9 deletions
diff --git a/index.js b/index.js
index 06b92d7..d5efb8d 100644
--- a/index.js
+++ b/index.js
@@ -33,7 +33,7 @@ const auth = new google.auth.JWT(
const spreadsheetId = process.env.SPREADSHEET_ID;
const sheets = google.sheets({ version: 'v4', auth });
-const range = 'Sheet1!A:H';
+const range = 'Sheet1!A:I';
// Connect to MongoDB
async function connectDB() {
@@ -71,9 +71,9 @@ async function generateSerialNumber() {
}
app.post('/upload', async (req, res) => {
- const { Verient, mac, os, status, customerAddress, customerNumber, customerName } = req.body;
+ const { Verient, mac, os, status, productionNumber, customerAddress, customerNumber, customerName } = req.body;
- if (!Verient || !mac || !os) {
+ if (!Verient || !mac || !os || !productionNumber) {
return res.status(400).send('Missing required fields: Verient, mac, os, or status');
}
@@ -99,6 +99,7 @@ app.post('/upload', async (req, res) => {
customerName,
customerNumber: customerNumbers,
customerAddress,
+ productionNumber
};
await collection.insertOne(newItem);
@@ -110,7 +111,7 @@ app.post('/upload', async (req, res) => {
range,
valueInputOption: 'USER_ENTERED',
requestBody: {
- values: [[serialNumber, Verient, mac, os, status, customerName, customerNumbers, customerAddress]],
+ values: [[serialNumber, Verient, mac, os, status, customerName, customerNumbers, customerAddress, productionNumber]],
},
});
console.log('Data successfully appended to Google Sheets');
@@ -164,7 +165,12 @@ app.get('/check-sn', async (req, res) => {
try {
const db = client.db(dbName);
const collection = db.collection(collectionName);
- const device = await collection.findOne({ serialNumber });
+ let device;
+ if (serialNumber.startsWith("JPC")){
+ device = await collection.findOne({ serialNumber });
+ }else{
+ device = await collection.findOne({productionNumber:serialNumber})
+ }
if (!device) {
return res.status(200).json({
@@ -195,7 +201,12 @@ app.put('/update', async (req, res) => {
try {
const db = client.db(dbName);
const collection = db.collection(collectionName);
- const existingDevice = await collection.findOne({ serialNumber });
+ let existingDevice;
+ if (serialNumber.startsWith("JPC")){
+ existingDevice = await collection.findOne({ serialNumber });
+ }else{
+ existingDevice = await collection.findOne({productionNumber:serialNumber})
+ }
if (!existingDevice) {
return res.status(404).json({ error: 'Device not found' });
@@ -219,7 +230,7 @@ app.put('/update', async (req, res) => {
...(customerAddress && { customerAddress }),
};
- await collection.updateOne({ serialNumber }, { $set: updatedData });
+ await collection.updateOne({ serialNumber:existingDevice.serialNumber }, { $set: updatedData });
console.log('Item successfully updated in MongoDB');
@@ -227,11 +238,11 @@ app.put('/update', async (req, res) => {
try {
const sheetData = await sheets.spreadsheets.values.get({ spreadsheetId, range });
const rows = sheetData.data.values;
- const rowIndex = rows.findIndex(row => row[0] === serialNumber);
+ const rowIndex = rows.findIndex(row => row[0] === existingDevice.serialNumber);
if (rowIndex !== -1) {
const updatedRow = [
- serialNumber,
+ existingDevice.serialNumber,
Verient || rows[rowIndex][1],
mac || rows[rowIndex][2],
os || rows[rowIndex][3],