UNPKG

@unchainedshop/plugins

Version:

Official plugin collection for the Unchained Engine with payment, delivery, and pricing adapters

82 lines (81 loc) 2.93 kB
import { mongodb } from '@unchainedshop/mongodb'; import { CryptopayTransactionsCollection, } from "./db/CryptopayTransactions.js"; const configureCryptopayModule = ({ db }) => { const CryptoTransactions = CryptopayTransactionsCollection(db); const getWalletAddress = async (address, contract) => { const addressId = [address, contract].filter(Boolean).join(':'); return CryptoTransactions.findOne({ _id: addressId }); }; const updateMostRecentBlock = async (currencyCode, blockHeight) => { await CryptoTransactions.updateMany({ currencyCode, }, { $set: { mostRecentBlockHeight: blockHeight, }, }); }; const mapOrderPaymentToWalletAddress = async ({ address, contract, currencyCode, orderPaymentId, }) => { const addressId = [address, contract].filter(Boolean).join(':'); return (await CryptoTransactions.findOneAndUpdate({ _id: addressId, }, { $setOnInsert: { _id: addressId, currencyCode, mostRecentBlockHeight: 0, blockHeight: 0, amount: mongodb.Decimal128.fromString('0'), created: new Date(), }, $set: { orderPaymentId, updated: new Date(), }, }, { upsert: true, returnDocument: 'after' })); }; const getNextDerivationNumber = async (currencyCode) => { return (await CryptoTransactions.countDocuments({ currencyCode })) + 1; }; const getWalletAddressesByOrderPaymentId = async (orderPaymentId) => { return CryptoTransactions.find({ orderPaymentId, }, { sort: { created: 1, }, }).toArray(); }; const updateWalletAddress = async ({ address, blockHeight, amount, contract, currencyCode, decimals, }) => { const addressId = [address, contract].filter(Boolean).join(':'); return (await CryptoTransactions.findOneAndUpdate({ _id: addressId, }, { $setOnInsert: { _id: addressId, created: new Date(), }, $set: { currencyCode, decimals, blockHeight, mostRecentBlockHeight: blockHeight, amount: mongodb.Decimal128.fromString(typeof amount === 'number' ? `${amount}` : amount), updated: new Date(), }, }, { upsert: true, returnDocument: 'after' })); }; return { getWalletAddress, updateMostRecentBlock, updateWalletAddress, mapOrderPaymentToWalletAddress, getNextDerivationNumber, getWalletAddressesByOrderPaymentId, }; }; export default { cryptopay: { configure: configureCryptopayModule, }, };