2026-04-13 05:23:25 +00:00

250 lines
7.6 KiB
JavaScript
Executable File

// server.js
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const { log } = require('./logger');
const auth = require('./auth');
const manageBrands = require('./routes/manageBrands');
const manageProducts = require('./routes/manageProducts');
const managepricing = require('./routes/managePricing');
const privacyLawWebhooks = require('./routes/privacyLawWebhooks');
const { getToken } = require('./tokenStore');
const app = express();
const PORT = process.env.PORT || 3002;
// 0) CORS (safe before everything)
app.use(cors());
app.get("/checkisshopdataexists/:shop", (req, res) => {
const shop = req.params.shop;
console.log("GET /checkisshopdataexists:", shop);
const tokenRecord = getToken(shop);
if (!tokenRecord) {
return res.json({ status: 0, message: "Shop not found" });
}
// Expected fields
const expectedFields = [
"accessToken",
"scope",
"savedAt",
"locationId",
"fulfillmentService"
];
const result = {};
expectedFields.forEach((field) => {
result[field] = tokenRecord[field] ? "present" : "missing";
});
res.json({
status: 1,
shop,
fields: result
});
});
// 1) COMPLIANCE WEBHOOKS (raw body) — MUST be before any JSON body parser
app.use('/webhooks', privacyLawWebhooks);
// 2) OAuth / other routes
app.use('/', auth);
// 3) Body parsers for the rest of your app
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ limit: '10mb', extended: true }));
// 4) Your other endpoints
app.post('/fulfillment', (req, res) => {
console.log('POST /fulfillment:', req.body);
res.sendStatus(200);
});
app.use('/managebrands', manageBrands);
app.use('/manageproducts', manageProducts);
app.use('/managepricing', managepricing);
const server = app.listen(PORT, () => {
log('general', `🖥️ Server listening on port ${PORT}`);
console.log(`Server running on https://backend.data4autos.com/`);
});
server.on('error', err => {
if (err.code === 'EADDRINUSE') {
console.error(`Port ${PORT} is already in use. Choose a different PORT or kill the process using it.`);
process.exit(1);
} else {
console.error('Server error:', err);
process.exit(1);
}
});
// // server.js
// require('dotenv').config();
// const express = require('express');
// const { log } = require('./logger');
// // OAuth callback
// const auth = require('./auth');
// // Your job-routes
// const manageBrands = require('./routes/manageBrands');
// const manageProducts = require('./routes/manageProducts');
// const managepricing = require('./routes/managePricing');
// // const syncInventory = require('./routes/syncInventory');
// // const syncCustomers = require('./routes/syncCustomers');
// // // …etc, one per file in routes/
// const app = express();
// const PORT = process.env.PORT || 3002;
// const cors = require('cors');
// app.use(express.json({ limit: '10mb' }));
// app.use(express.urlencoded({ limit: '10mb', extended: true }));
// app.use(express.json());
// // 1) OAuth
// app.use('/', auth);
// app.use(cors());
// // 2) Job endpoints (manually mapped)
// app.post('/fulfillment', (req, res) => {
// console.log('POST request received:', req.body); // Optional logging
// res.sendStatus(200); // Sends 200 OK
// });
// app.use('/managebrands', manageBrands);
// app.use('/manageproducts', manageProducts);
// app.use('/managepricing', managepricing);
// const server = app.listen(PORT, () => {
// log('general', `🖥️ Server listening on port ${PORT}`);
// console.log(`Server running on https://backend.data4autos.com/`);
// });
// server.on('error', err => {
// if (err.code === 'EADDRINUSE') {
// console.error(`Port ${PORT} is already in use. Choose a different PORT or kill the process using it.`);
// process.exit(1);
// } else {
// console.error('Server error:', err);
// process.exit(1);
// }
// });
// // app.use('/syncinventory', syncInventory);
// // app.use('/synccustomers', syncCustomers);
// // add more here as you create new files in routes/
// // app.listen(PORT, () => {
// // log('general', `🖥️ Server listening on port ${PORT}`);
// // console.log(`Server running on https://backend.dine360.ca/`);
// // });
// // // server.js
// // require('dotenv').config();
// // const express = require('express');
// // const auth = require('./auth');
// // const { log } = require('./logger');
// // const app = express();
// // const PORT = process.env.PORT || 3002;
// // // mount the auth routes
// // app.use('/', auth);
// // app.listen(PORT, () => {
// // log('general', `🖥️ Server listening on port ${PORT}`);
// // console.log(`Server running on http://localhost:${PORT}`);
// // });
// // const express = require('express');
// // const axios = require('axios');
// // const app = express();
// // const PORT = 3002;
// // // Replace these with your app's credentials
// // const CLIENT_ID = 'b7534c980967bad619cfdb9d3f837cfa';
// // const CLIENT_SECRET = 'ed6882a4fc5839df0677ad1bb3c92f2b';
// // app.get('/auth/callback', async (req, res) => {
// // console.log('🔔 [Callback] Received OAuth callback');
// // const { shop, code } = req.query;
// // if (!shop || !code) {
// // console.warn('⚠️ [Callback] Missing shop or code in query:', req.query);
// // return res.status(400).send('Missing shop or code parameter.');
// // }
// // console.log(`🔍 [Callback] shop=${shop}, code=${code}`);
// // try {
// // console.log('🚀 [OAuth] Exchanging authorization code for access token...');
// // const tokenResponse = await axios.post(
// // `https://${shop}/admin/oauth/access_token`,
// // {
// // client_id: CLIENT_ID,
// // client_secret: CLIENT_SECRET,
// // code: code,
// // },
// // {
// // headers: { 'Content-Type': 'application/json' },
// // }
// // );
// // console.log('✅ [OAuth] Token endpoint responded:', tokenResponse.data);
// // const { access_token, scope } = tokenResponse.data;
// // console.log('🔑 [OAuth] Access Token:', access_token);
// // console.log('📜 [OAuth] Granted Scopes:', scope);
// // // TODO: Persist access_token securely in your database here
// // console.log(`💾 [Store] Storing access token for shop ${shop} (simulate DB save)`);
// // res.send('Access token received and logged. You can close this window.');
// // } catch (error) {
// // console.error(
// // '❌ [OAuth] Error exchanging code for access token:',
// // error.response?.data || error.message
// // );
// // res.status(500).send('Failed to get access token');
// // }
// // });
// // app.listen(PORT, () => {
// // console.log(`🖥️ [Server] Listening on http://localhost:${PORT}`);
// // });
// // // 🔔 [Callback] Received OAuth callback
// // // 🔍 [Callback] shop=veloxautomotive.myshopify.com, code=03f875ca02185dea8e60226c9263f3ba
// // // 🚀 [OAuth] Exchanging authorization code for access token...
// // // ✅ [OAuth] Token endpoint responded: {
// // // access_token: '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',
// // // scope: 'write_inventory,write_products,write_publications'
// // // }
// // // 🔑 [OAuth] Access Token: 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
// // // 📜 [OAuth] Granted Scopes: write_inventory,write_products,write_publications
// // // 💾 [Store] Storing access token for shop veloxautomotive.myshopify.com (simulate DB save)