55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
import mongoose from 'mongoose';
|
|
import dotenv from 'dotenv';
|
|
import Product from '../models/Product.model.js';
|
|
import logger from './logger.js';
|
|
|
|
// Load environment variables
|
|
dotenv.config();
|
|
|
|
// Connect to MongoDB
|
|
mongoose.connect(process.env.MONGO_URI)
|
|
.then(() => logger.info('MongoDB connected'))
|
|
.catch(err => {
|
|
logger.error('MongoDB connection error:', err);
|
|
process.exit(1);
|
|
});
|
|
|
|
async function disableStockTrackingForAllProducts() {
|
|
try {
|
|
logger.info('Disabling stock tracking for all products...');
|
|
|
|
const result = await Product.updateMany(
|
|
{}, // Empty filter matches all documents
|
|
{
|
|
stockTracking: false,
|
|
stockStatus: "in_stock" // Set a default status
|
|
}
|
|
);
|
|
|
|
logger.info(`Stock tracking disabled for ${result.modifiedCount} products out of ${result.matchedCount} total products`);
|
|
|
|
return result;
|
|
} catch (error) {
|
|
logger.error('Error disabling stock tracking:', error);
|
|
throw error;
|
|
} finally {
|
|
// Close database connection
|
|
mongoose.connection.close();
|
|
logger.info('Database connection closed');
|
|
}
|
|
}
|
|
|
|
// Execute the function if this script is run directly
|
|
if (process.argv[1].includes('disableStockTracking.js')) {
|
|
disableStockTrackingForAllProducts()
|
|
.then(() => {
|
|
logger.info('Script completed successfully');
|
|
process.exit(0);
|
|
})
|
|
.catch(err => {
|
|
logger.error('Script failed:', err);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
export default disableStockTrackingForAllProducts;
|