64 lines
1.3 KiB
JavaScript
64 lines
1.3 KiB
JavaScript
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const dataFile = path.resolve(__dirname, "data", "tokens.json");
|
|
const dataDir = path.dirname(dataFile);
|
|
|
|
if (!fs.existsSync(dataDir)) {
|
|
fs.mkdirSync(dataDir, { recursive: true });
|
|
}
|
|
|
|
if (!fs.existsSync(dataFile)) {
|
|
fs.writeFileSync(dataFile, "{}", "utf8");
|
|
}
|
|
|
|
function readStore() {
|
|
return JSON.parse(fs.readFileSync(dataFile, "utf8"));
|
|
}
|
|
|
|
function saveStore(store) {
|
|
fs.writeFileSync(dataFile, JSON.stringify(store, null, 2), "utf8");
|
|
}
|
|
|
|
function saveToken(shop, accessToken, scope, fulfillmentService = null, locationId = null) {
|
|
if (!shop || accessToken == null || scope == null) {
|
|
return;
|
|
}
|
|
|
|
const store = readStore();
|
|
store[shop] = {
|
|
accessToken,
|
|
scope,
|
|
savedAt: new Date().toISOString(),
|
|
locationId: locationId || store[shop]?.locationId || null,
|
|
fulfillmentService: fulfillmentService || store[shop]?.fulfillmentService || null,
|
|
};
|
|
saveStore(store);
|
|
}
|
|
|
|
function getToken(shop) {
|
|
const store = readStore();
|
|
return store[shop] || null;
|
|
}
|
|
|
|
function deleteToken(shop) {
|
|
if (!shop) {
|
|
return;
|
|
}
|
|
|
|
const store = readStore();
|
|
delete store[shop];
|
|
saveStore(store);
|
|
}
|
|
|
|
function listTokens() {
|
|
return readStore();
|
|
}
|
|
|
|
module.exports = {
|
|
saveToken,
|
|
getToken,
|
|
deleteToken,
|
|
listTokens,
|
|
};
|