elecrticty work and plumbing work pictures apis

master^2
Bhaskar 6 months ago
parent e6a72ec0a6
commit cdb6afd11d

@ -3,7 +3,7 @@ const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const customJwtAuth = require("../customAuthJwt"); const customJwtAuth = require("../customAuthJwt");
const { Deparments } = require("../models/Department"); const { Deparments } = require("../models/Department");
const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData } = require("../models/store"); const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures } = require("../models/store");
const { Counter } = require("../models/User"); const { Counter } = require("../models/User");
const { IotData, Tank } = require("../models/tanks"); const { IotData, Tank } = require("../models/tanks");
const fastify = require("fastify")({ const fastify = require("fastify")({
@ -674,18 +674,20 @@ exports.getAllocatedSensorsByTank = async (req, reply) => {
} }
}; };
exports.createMasterSlaveData = async (req, reply) => { exports.createMasterSlaveData = async (req, reply) => {
try { try {
const { installationId } = req.params; const { installationId } = req.params;
const { const {
type, type,
customerId,
hardwareId, hardwareId,
batchno, batchno,
masterId, masterId,
tankName, tankName,
tankLocation, tankLocation,
materialRecived, materialRecived,
electicityWork, electricityWork,
plumbingWork, plumbingWork,
loraCheck loraCheck
} = req.body; } = req.body;
@ -694,25 +696,53 @@ exports.createMasterSlaveData = async (req, reply) => {
return reply.status(400).send({ message: "installationId, hardwareId, and masterId are required." }); return reply.status(400).send({ message: "installationId, hardwareId, and masterId are required." });
} }
// 🔹 Fetch stored electricity work pictures
const electricityWorkData = await ElectrictyWorkPictures.findOne({
installationId,
customerId
});
const electricityWorkPictures = electricityWorkData
? electricityWorkData.pictureUrl.map(pic => ({ url: pic.url, uploadedAt: new Date() }))
: [];
// 🔹 Fetch stored plumbing work pictures
const plumbingWorkData = await PlumbingWorkPictures.findOne({
installationId,
customerId
});
const plumbingWorkPictures = plumbingWorkData
? plumbingWorkData.pictureUrl.map(pic => ({ url: pic.url, uploadedAt: new Date() }))
: [];
// 🔹 Save all data to MasterSlaveData
const newData = new MasterSlaveData({ const newData = new MasterSlaveData({
installationId, installationId,
type, type,
customerId,
hardwareId, hardwareId,
batchno, batchno,
masterId, masterId,
tankName, tankName,
tankLocation, tankLocation,
materialRecived, materialRecived,
electicityWork, electricityWork,
plumbingWork, plumbingWork,
loraCheck loraCheck,
electricityWorkPictures,
plumbingWorkPictures
}); });
// Save to database
await newData.save(); await newData.save();
reply.status(201).send({ message: "Master-Slave data created successfully", data: newData }); reply.status(201).send({
message: "Master-Slave data created successfully",
data: newData
});
} catch (err) { } catch (err) {
reply.status(500).send({ message: err.message }); reply.status(500).send({ message: err.message });
} }
}; };

@ -7,7 +7,7 @@ const createConnectionController = require("./controllers/createConnectionContro
const storeController = require("./controllers/storeController.js") const storeController = require("./controllers/storeController.js")
const boom = require("boom"); const boom = require("boom");
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const { ProfilePictureStore,generateinstallationId,Store, Survey} = require("./models/store"); const { ProfilePictureStore,generateinstallationId,Store, Survey, PlumbingWorkPictures, ElectrictyWorkPictures} = require("./models/store");
const cors = require('fastify-cors'); const cors = require('fastify-cors');
@ -908,6 +908,122 @@ fastify.post('/api/uploads-user/:customerId', async (request, reply) => {
} }
}); });
fastify.post("/api/uploads-electricty-work/:customerId/:installationId", async (request, reply) => {
try {
const { customerId, installationId } = request.params;
const files = await request.files(); // Await files properly
if (!files || files.length === 0) {
return reply.code(400).send({ error: "No files uploaded" });
}
const bucketName = "arminta_profile_pictures";
const publicUrls = [];
for await (const file of files) {
const uniqueFileName = `${Date.now()}-${Math.random().toString(36).substring(7)}-${file.filename}`;
const filePath = `electricty_work_picture/${uniqueFileName}`;
console.log(`Uploading file: ${file.filename}${filePath}`);
const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream();
file.file.pipe(writeStream);
await new Promise((resolve, reject) => {
writeStream.on("finish", async () => {
try {
await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
publicUrls.push(publicUrl);
console.log(`File uploaded: ${publicUrl}`);
resolve();
} catch (error) {
console.error("Failed to make file public:", error);
reject(error);
}
});
writeStream.on("error", (err) => {
console.error("Failed to upload file:", err);
reject(err);
});
});
}
// Update MongoDB: Convert URLs to { url: "..." } objects
const updatedRecord = await ElectrictyWorkPictures.findOneAndUpdate(
{ customerId, installationId },
{ $push: { pictureUrl: { $each: publicUrls.map(url => ({ url })) } } }, // Append new images
{ new: true, upsert: true }
);
reply.send({ success: true, pictures: publicUrls, details: updatedRecord });
} catch (err) {
console.error("Upload Error:", err);
reply.code(500).send({ error: "An error occurred", details: err.message });
}
});
fastify.post("/api/uploads-plumbing-work/:customerId/:installationId", async (request, reply) => {
try {
const { customerId, installationId } = request.params;
const files = await request.files(); // Await files properly
if (!files || files.length === 0) {
return reply.code(400).send({ error: "No files uploaded" });
}
const bucketName = "arminta_profile_pictures";
const publicUrls = [];
for await (const file of files) {
const uniqueFileName = `${Date.now()}-${Math.random().toString(36).substring(7)}-${file.filename}`;
const filePath = `plumbing_work_picture/${uniqueFileName}`;
console.log(`Uploading file: ${file.filename}${filePath}`);
const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream();
file.file.pipe(writeStream);
await new Promise((resolve, reject) => {
writeStream.on("finish", async () => {
try {
await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
publicUrls.push(publicUrl);
console.log(`File uploaded: ${publicUrl}`);
resolve();
} catch (error) {
console.error("Failed to make file public:", error);
reject(error);
}
});
writeStream.on("error", (err) => {
console.error("Failed to upload file:", err);
reject(err);
});
});
}
// Update MongoDB: Convert URLs to { url: "..." } objects
const updatedRecord = await PlumbingWorkPictures.findOneAndUpdate(
{ customerId, installationId },
{ $push: { pictureUrl: { $each: publicUrls.map(url => ({ url })) } } }, // Append new images
{ new: true, upsert: true }
);
reply.send({ success: true, pictures: publicUrls, details: updatedRecord });
} catch (err) {
console.error("Upload Error:", err);
reply.code(500).send({ error: "An error occurred", details: err.message });
}
});
fastify.post("/api/installLogin", { fastify.post("/api/installLogin", {
schema: { schema: {
description: "This is for Login Install", description: "This is for Login Install",

@ -655,6 +655,7 @@ const salesSchema = new mongoose.Schema({
const masterSlaveDataSchema = new mongoose.Schema({ const masterSlaveDataSchema = new mongoose.Schema({
installationId: {type: String}, installationId: {type: String},
customerId: {type: String},
type: { type: String}, type: { type: String},
hardwareId: { type: String }, hardwareId: { type: String },
batchno: { type: String, default: null }, batchno: { type: String, default: null },
@ -662,17 +663,73 @@ const masterSlaveDataSchema = new mongoose.Schema({
tankName: { type: String }, tankName: { type: String },
tankLocation: { type: String }, tankLocation: { type: String },
materialRecived: { type: String }, materialRecived: { type: String },
electicityWork: { type: String }, electricityWork: { type: String },
plumbingWork: { type: String }, plumbingWork: { type: String },
electricityWorkPictures: [
{
url: { type: String },
uploadedAt: { type: Date, default: Date.now }
}
],
plumbingWorkPictures: [
{
url: { type: String },
uploadedAt: { type: Date, default: Date.now }
}
],
loraCheck: { type: String }, loraCheck: { type: String },
}, { }, {
timestamps: true, timestamps: true,
}); });
const electrictyWorkPicturesSchema = new Schema({
installationId: {
type: String,
//required: true,
//unique: true
},
customerId: {
type: String,
//required: true
},
pictureUrl: [{
url: {
type: String,
},
}],
createdAt: {
type: Date,
default: Date.now
}
});
const plumbingWorkPicturesSchema = new Schema({
installationId: {
type: String,
//required: true,
//unique: true
},
customerId: {
type: String,
//required: true
},
pictureUrl: [{
url: {
type: String,
},
}],
createdAt: {
type: Date,
default: Date.now
}
});
const Iotprice = mongoose.model('Iotprice', iotpriceSchema); const Iotprice = mongoose.model('Iotprice', iotpriceSchema);
const Insensors = mongoose.model('Insensors', insensorsSchema); const Insensors = mongoose.model('Insensors', insensorsSchema);
const MasterSlaveData = mongoose.model('MasterSlaveData', masterSlaveDataSchema); const MasterSlaveData = mongoose.model('MasterSlaveData', masterSlaveDataSchema);
const ElectrictyWorkPictures = mongoose.model('ElectrictyWorkPictures', electrictyWorkPicturesSchema);
const PlumbingWorkPictures = mongoose.model('PlumbingWorkPictures', plumbingWorkPicturesSchema);
const Order = mongoose.model('Order', orderSchema); const Order = mongoose.model('Order', orderSchema);
@ -695,4 +752,4 @@ const Iotprice = mongoose.model('Iotprice', iotpriceSchema);
module.exports = {MasterSlaveData,SensorStock,Order,EstimationOrder,Iotprice,Sales, Install,Survey, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart}; module.exports = {PlumbingWorkPictures,ElectrictyWorkPictures,MasterSlaveData,SensorStock,Order,EstimationOrder,Iotprice,Sales, Install,Survey, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart};

@ -288,29 +288,54 @@ module.exports = function (fastify, opts, next) {
type: "object", type: "object",
required: ["installationId"], required: ["installationId"],
properties: { properties: {
installationId: { type: "string", description: "Installation ID to associate the master-slave data with" } installationId: { type: "string", description: "Installation ID" }
} }
}, },
body: { body: {
type: "object", type: "object",
required: ["hardwareId", "masterId"], required: ["hardwareId", "masterId"],
properties: { properties: {
type: { type: "string", description: "Type of the device (master/slave)" }, type: { type: "string" },
hardwareId: { type: "string", description: "Hardware ID of the device" }, customerId: { type: "string" },
batchno: { type: "string", description: "Batch number (optional)" }, hardwareId: { type: "string" },
masterId: { type: "string", description: "Master ID associated with the device" }, batchno: { type: "string" },
tankName: { type: "string", description: "Name of the tank" }, masterId: { type: "string" },
tankLocation: { type: "string", description: "Location of the tank" }, tankName: { type: "string" },
materialRecived: { type: "string", description: "Material received status" }, tankLocation: { type: "string" },
electicityWork: { type: "string", description: "Electricity work status" }, materialRecived: { type: "string" },
plumbingWork: { type: "string", description: "Plumbing work status" }, electricityWork: { type: "string" },
loraCheck: { type: "string", description: "LoRa check status" } plumbingWork: { type: "string" },
loraCheck: { type: "string" },
// ✅ Removing `format: "uri"`
electricityWorkPictures: {
type: "array",
items: {
type: "object",
properties: {
url: { type: "string", description: "Image URL" }, // No format validation
uploadedAt: { type: "string", format: "date-time", description: "Upload timestamp" }
}
}
},
plumbingWorkPictures: {
type: "array",
items: {
type: "object",
properties: {
url: { type: "string", description: "Image URL" }, // No format validation
uploadedAt: { type: "string", format: "date-time", description: "Upload timestamp" }
}
}
}
} }
} }
}, },
handler: installationController.createMasterSlaveData handler: installationController.createMasterSlaveData
}); });
next(); next();
} }
Loading…
Cancel
Save