';
document.body.appendChild(fab);
var win=document.createElement('div');win.id='d4a-win';
win.innerHTML='
💬
Chat with us
We typically reply within a few hours
Send us a message and we\'ll get back to you!
';
document.body.appendChild(win);
var open=false,unread=0,lastTs='',polling=null;
fab.onclick=function(){
open=!open;win.style.display=open?'flex':'none';
if(open){unread=0;document.getElementById('d4a-badge').style.display='none';loadMsgs(true);}
else if(polling){clearInterval(polling);polling=null;}
if(open&&!polling) polling=setInterval(function(){loadMsgs(false);},3000);
};
function loadMsgs(scroll){
fetch(BASE+'/chat/'+encodeURIComponent(SHOP)+'?vid='+VID).then(r=>r.json()).then(function(d){
var msgs=d.messages||[];var el=document.getElementById('d4a-msgs');
if(!msgs.length) return;
var latest=msgs[msgs.length-1]?.timestamp||'';
var isNew=latest&&latest!==lastTs;lastTs=latest;
if(!isNew&&!scroll) return;
el.innerHTML=msgs.map(function(m){
var isAdmin=m.from==='admin';
var t=new Date(m.timestamp).toLocaleTimeString(undefined,{hour:'2-digit',minute:'2-digit'});
return '
'+esc(m.text)+'
'+t+'
';
}).join('');
if(scroll||el.scrollHeight-el.scrollTop-el.clientHeight<80) el.scrollTop=el.scrollHeight;
if(!open&&isNew&&msgs[msgs.length-1]?.from==='admin'){
unread++;var b=document.getElementById('d4a-badge');b.textContent=unread>9?'9+':unread;b.style.display='flex';
}
}).catch(function(){});
}
window.__d4aChat.send=function(){
var inp=document.getElementById('d4a-inp');var text=inp.value.trim();if(!text) return;
inp.value='';
fetch(BASE+'/chat/'+encodeURIComponent(SHOP),{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({text:text,visitorId:VID})}).then(function(){loadMsgs(true);});
};
function esc(s){return String(s).replace(/&/g,'&').replace(//g,'>');}
loadMsgs(true);
})();`);
});
// Health check
app.get('/health', (req, res) => {
res.json({ ok: true, uptime: process.uptime(), timestamp: new Date().toISOString() });
});
// Top-level job endpoints (mirrors manageproducts/jobs/* but at /jobs/*)
app.get('/jobs', (req, res) => {
const shop = req.query.shop || null;
res.json({ jobs: listJobs(shop) });
});
app.get('/jobs/:jobId', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
res.json(job);
});
app.post('/jobs/:jobId/cancel', (req, res) => {
const job = cancelJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
res.json({ ok: true, job });
});
app.get('/shops', (req, res) => {
try {
const store = listTokens();
const shops = Object.keys(store).map(shop => ({
shop,
savedAt: store[shop].savedAt,
hasToken: !!store[shop].accessToken,
hasLocation: !!store[shop].locationId,
hasFulfillment: !!store[shop].fulfillmentService,
}));
res.json({ shops });
} catch {
res.json({ shops: [] });
}
});
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)