@agnostack/next-shopify
Version:
Please contact agnoStack via info@agnostack.com for any questions
237 lines • 10.1 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findSessionIdsByShop = exports.deleteSessionsCallback = exports.deleteState = exports.storeState = exports.loadState = exports.loadCallback = exports.updateCallback = exports.storeCallback = exports.FirebaseSessionHandler = void 0;
const firebase_admin_1 = __importDefault(require("firebase-admin"));
const shared_1 = require("../../shared");
const singleton = Symbol('singleton');
const singletonEnforcer = Symbol('singletonEnforcer');
class FirebaseSessionHandler {
constructor(enforcer, { configurationId, collection = shared_1.COLLECTION_ROOT }) {
if (enforcer !== singletonEnforcer) {
throw new Error('Cannot instantiate FirebaseSessionHandler directly, please use getInstance()');
}
this.type = 'FirebaseSessionHandler';
this.collection = collection;
this.configurationId = configurationId;
}
get isInitialized() {
return (this.firestore != undefined);
}
initializeFireStore(firebaseConfig) {
if ((0, shared_1.objectNotEmpty)(firebaseConfig)) {
const project = !firebase_admin_1.default.apps.length
? firebase_admin_1.default.initializeApp({
credential: firebase_admin_1.default.credential.cert(firebaseConfig),
databaseURL: `https://${firebaseConfig.project_id}.firebaseio.com`,
}) : firebase_admin_1.default.app();
this.firestore = project.firestore();
}
}
static getInstance(firebaseConfig, { collection, configurationId }) {
if (!this[singleton]) {
this[singleton] = new FirebaseSessionHandler(singletonEnforcer, { collection, configurationId });
}
if (!this[singleton].isInitialized) {
this[singleton].initializeFireStore(firebaseConfig);
}
return this[singleton];
}
}
exports.FirebaseSessionHandler = FirebaseSessionHandler;
const storeCallback = (instance, shopId, session) => __awaiter(void 0, void 0, void 0, function* () {
try {
const docRef = instance.firestore
.collection(instance.collection)
.doc(shopId)
.collection(instance.configurationId)
.doc(session.id);
// HMMMM why the freeze here?
// TODO: do we need to remove/can we remove session.id here??
const _a = JSON.parse(JSON.stringify(session)), { onlineAccessInfo: _onlineAccessInfo } = _a, docData = __rest(_a, ["onlineAccessInfo"]);
// NOTE: destructuring out associated_user for DPA compliance
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _b = _onlineAccessInfo !== null && _onlineAccessInfo !== void 0 ? _onlineAccessInfo : {}, { associated_user } = _b, onlineAccessInfo = __rest(_b, ["associated_user"]);
const payload = Object.assign({ onlineAccessInfo }, docData);
yield docRef.set(payload);
return (yield docRef.get()).data();
}
catch (error) {
console.error('FirebaseSessionHandler.storeCallback error', error);
throw error;
}
});
exports.storeCallback = storeCallback;
const updateCallback = (instance, shopId, session) => __awaiter(void 0, void 0, void 0, function* () {
try {
const docRef = instance.firestore
.collection(instance.collection)
.doc(shopId)
.collection(instance.configurationId)
.doc(session.id);
// HMMMM why the freeze here?
// TODO: do we need to remove/can we remove session.id here??
const _c = JSON.parse(JSON.stringify(session)), { onlineAccessInfo: _onlineAccessInfo } = _c, docData = __rest(_c, ["onlineAccessInfo"]);
// NOTE: destructuring out associated_user for DPA compliance
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _d = _onlineAccessInfo !== null && _onlineAccessInfo !== void 0 ? _onlineAccessInfo : {}, { associated_user } = _d, onlineAccessInfo = __rest(_d, ["associated_user"]);
const payload = Object.assign({ onlineAccessInfo }, docData);
yield docRef.set(payload, { merge: true });
return (yield docRef.get()).data();
}
catch (error) {
console.error('FirebaseSessionHandler.updateCallback error', error);
throw error;
}
});
exports.updateCallback = updateCallback;
const loadCallback = (instance, shopId, sessionId) => __awaiter(void 0, void 0, void 0, function* () {
if ((0, shared_1.stringEmpty)(sessionId)) {
return undefined;
}
try {
const document = yield instance.firestore
.collection(instance.collection)
.doc(shopId)
.collection(instance.configurationId)
.doc(sessionId)
.get();
if (document.exists) {
return document.data();
}
return undefined;
}
catch (error) {
console.error('FirebaseSessionHandler.loadCallback error', error);
throw error;
}
});
exports.loadCallback = loadCallback;
const loadState = (instance, shop) => __awaiter(void 0, void 0, void 0, function* () {
if ((0, shared_1.stringEmpty)(shop)) {
return undefined;
}
try {
const document = yield instance.firestore
.collection(instance.collection)
.doc(shop)
.collection(instance.configurationId)
.doc(`${shared_1.STATE_PREFIX}${shop}`)
.get();
if (document.exists) {
return document.data();
}
return undefined;
}
catch (error) {
console.error('FirebaseSessionHandler.loadState error', error);
throw error;
}
});
exports.loadState = loadState;
const storeState = (instance, state) => __awaiter(void 0, void 0, void 0, function* () {
if ((0, shared_1.stringEmpty)(state === null || state === void 0 ? void 0 : state.shop)) {
return undefined;
}
try {
const docRef = instance.firestore
.collection(instance.collection)
.doc(state.shop)
.collection(instance.configurationId)
.doc(`${shared_1.STATE_PREFIX}${state.shop}`);
yield docRef.set(state);
return (yield docRef.get()).data();
}
catch (error) {
console.error('FirebaseSessionHandler.storeState error', error);
throw error;
}
});
exports.storeState = storeState;
const deleteState = (instance, shop) => __awaiter(void 0, void 0, void 0, function* () {
if ((0, shared_1.stringEmpty)(shop)) {
return undefined;
}
try {
const docRef = instance.firestore
.collection(instance.collection)
.doc(shop)
.collection(instance.configurationId)
.doc(`${shared_1.STATE_PREFIX}${shop}`);
return docRef.delete();
}
catch (error) {
console.error('FirebaseSessionHandler.deleteState error', error);
throw error;
}
});
exports.deleteState = deleteState;
const deleteSessionsCallback = (instance, shopId, sessionIds) => __awaiter(void 0, void 0, void 0, function* () {
const deleteSessionCallback = (_collectionRef, _sessionId) => __awaiter(void 0, void 0, void 0, function* () {
try {
return _collectionRef.doc(_sessionId).delete();
}
catch (error) {
console.error('FirebaseSessionHandler.deleteSessionCallback error', error);
throw error;
}
});
try {
const collectionRef = instance.firestore
.collection(instance.collection)
.doc(shopId)
.collection(instance.configurationId);
return Promise.all((0, shared_1.ensureArray)(sessionIds).map((sessionId) => deleteSessionCallback(collectionRef, sessionId)));
}
catch (error) {
console.error('FirebaseSessionHandler.deleteSessionsCallback error', error);
throw error;
}
});
exports.deleteSessionsCallback = deleteSessionsCallback;
const findSessionIdsByShop = (instance, shopId) => __awaiter(void 0, void 0, void 0, function* () {
if ((0, shared_1.stringEmpty)(shopId)) {
// NOTE: this happens on initial install
return [];
}
try {
const collection = yield instance.firestore
.collection(instance.collection)
.doc(shopId)
.collection(instance.configurationId)
.get();
const sessionIds = [];
collection.forEach((_doc) => {
sessionIds.push(_doc.id);
});
return sessionIds;
}
catch (error) {
console.error('FirebaseSessionHandler.findSessionIdsByShop error', error);
throw error;
}
});
exports.findSessionIdsByShop = findSessionIdsByShop;
//# sourceMappingURL=firebase.js.map