import Inject, { RegisterSingleton, ServiceProvider } from "@entity-access/entity-access/dist/di/di.js"; import {ICacheContainer, ObjectCache, cacheFor } from "../../../common/cacheFor.js"; import { globalServices } from "../../../globalServices.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import EncryptionService from "../encryption/EncryptionService.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import { chainToCamel } from "../../../common/chainToCamel.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; @RegisterSingleton export default class StoreConfigService implements ICacheContainer { cache: ObjectCache; @Inject encryptionService: EncryptionService; @cacheFor() async getConfigs(id: number, group: string) { const scope = globalServices.createScope(); const db = scope.resolve(SocialMailContext); const c = await db.storeConfigs .where({ id, group}, (x, p) => x.storeID === p.id && x.groupName === p.group) .toArray(); const config = {}; for (const iterator of c) { config[chainToCamel(iterator.configName)] = iterator.encrypted ? this.encryptionService.secure.decrypt(iterator.configValue) : iterator.configValue; } return config as { [key: string]: string}; } @cacheFor() async getRootFolder(folderID, host) { using scope = ServiceProvider.createScope(this); const db = scope.resolve(SocialMailContext); return await db.appFiles.where({ host, folderID }, (x, p) => x.appFileID === p.folderID && x.websiteFolder.hosts.some((h) => h.host === p.host) ).first(); } @cacheFor() async getPrice(itemID, currency, host?: string, storeID?) { using scope = ServiceProvider.createScope(this); const db = scope.resolve(SocialMailContext); const now = DateTime.now; const q = db.storeItemPrices.where({ currency, now }, (x, p) => x.currency === p.currency && x.startDate as any <= Sql.cast.asDateTime(p.now) && (x.endDate === null || x.endDate as any > p.now) ); let pq = q; if (host) { host = host.trim().toLowerCase(); if (storeID) { pq = pq.where({ host, storeID }, (x, p) => x.storeItem.storeID === p.storeID && x.storeItem.store.hosts.some((h) => h.host === p.host)); } else { pq = pq.where({ host }, (x, p) => x.storeItem.store.hosts.some((h) => h.host === p.host)); } } else { if (storeID) { pq = pq.where({storeID}, (x, p) => x.storeItem.storeID === p.storeID); } } const price = await pq.where({ itemID }, (x, p) => x.storeItemID === p.itemID) .include((x) => x.storeItem) .first(); const taxes = await q.where({ itemID} , (x, p) => x.storeItem.taxItems.some((ti) => ti.storeItemID === p.itemID)) .toArray(); return { price, taxes }; } }