56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const DATA_DIR = path.join(__dirname, 'data');
|
|
const SEARCHES_FILE = path.join(DATA_DIR, 'searches.json');
|
|
const LEADS_FILE = path.join(DATA_DIR, 'leads.json');
|
|
|
|
function ensureDataDir() {
|
|
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
function readJSON(file) {
|
|
ensureDataDir();
|
|
if (!fs.existsSync(file)) return {};
|
|
try { return JSON.parse(fs.readFileSync(file, 'utf8')); }
|
|
catch { return {}; }
|
|
}
|
|
|
|
function writeJSON(file, data) {
|
|
ensureDataDir();
|
|
fs.writeFileSync(file, JSON.stringify(data, null, 2));
|
|
}
|
|
|
|
// Searches
|
|
function getSearches() { return readJSON(SEARCHES_FILE); }
|
|
function saveSearch(key, data) {
|
|
const all = getSearches();
|
|
all[key] = data;
|
|
writeJSON(SEARCHES_FILE, all);
|
|
}
|
|
function deleteSearch(key) {
|
|
const all = getSearches();
|
|
delete all[key];
|
|
writeJSON(SEARCHES_FILE, all);
|
|
}
|
|
function getSearch(key) { return getSearches()[key] || null; }
|
|
|
|
// Leads
|
|
function getLeads() { return readJSON(LEADS_FILE); }
|
|
function saveLead(key, data) {
|
|
const all = getLeads();
|
|
all[key] = data;
|
|
writeJSON(LEADS_FILE, all);
|
|
}
|
|
function deleteLead(key) {
|
|
const all = getLeads();
|
|
delete all[key];
|
|
writeJSON(LEADS_FILE, all);
|
|
}
|
|
function isLead(placeId) {
|
|
const all = getLeads();
|
|
return Object.values(all).some(l => l.placeId === placeId);
|
|
}
|
|
|
|
module.exports = { getSearches, saveSearch, deleteSearch, getSearch, getLeads, saveLead, deleteLead, isLead };
|