diff --git a/app/controllers/api/v1/super-admin/currencies.ts b/app/controllers/api/v1/super-admin/currencies.ts index 58b576d..470891b 100755 --- a/app/controllers/api/v1/super-admin/currencies.ts +++ b/app/controllers/api/v1/super-admin/currencies.ts @@ -562,4 +562,14 @@ module.exports = function (router: any) { message: stringHelper.success, }); }); + + router.post( + "/create/bulk/cabns", + asyncMiddleware(async (req: any, res: any) => { + await currencyHelper.createBulkCabns(req, res); + return res.http200({ + message: stringHelper.strSuccess, + }); + }) + ); }; diff --git a/app/helpers/currencyHelper.ts b/app/helpers/currencyHelper.ts index 9ef0fbe..2f83ca0 100755 --- a/app/helpers/currencyHelper.ts +++ b/app/helpers/currencyHelper.ts @@ -1,60 +1,158 @@ module.exports = { async createCurrencyAddresses(req: any, model: any, body: any) { - - let results = [] + let results = []; if (model && body.networks && body.networks.length > 0) { for (let i = 0; i < body.networks.length; i++) { - let organization = '' + let organization = ""; if (body.networks[i].tokenContractAddress) { - body.networks[i].tokenContractAddress = (body.networks[i].tokenContractAddress).toLowerCase() + body.networks[i].tokenContractAddress = + body.networks[i].tokenContractAddress.toLowerCase(); } - if(req.user && req.user.organization){ - organization = req.user.organization - }else { - organization = req.body.organization + if (req.user && req.user.organization) { + organization = req.user.organization; + } else { + organization = req.body.organization; } let innerBody = { network: body.networks[i].network, currency: model._id, networkDex: body.networks[i].networkDex, tokenContractAddress: body.networks[i].tokenContractAddress, - createdByOrganization: organization - } - let result = await db.CurrencyAddressesByNetwork.create(innerBody) - results.push(result._id) + createdByOrganization: organization, + }; + let result = await db.CurrencyAddressesByNetwork.create(innerBody); + results.push(result._id); // let count = await db.CurrencyAddressesByNetwork.count({ network: body.networks[i].network, currency: model._id }) // if (count == 0) { // } } } - return results + return results; }, async currencyAssociationWithNetwork(req: any, res: any) { - var filter = {networkCurrencyAddressByNetwork: req.params.id} + var filter = { networkCurrencyAddressByNetwork: req.params.id }; return await db.Networks.countDocuments(filter); }, async currencyAssociationWithLeaderboardssss(req: any, res: any) { - var filter = {networkCurrencyAddressByNetwork: req.params.id} - return await db.LeaderboardCurrencyAddressesByNetwork.countDocuments(filter); + var filter = { networkCurrencyAddressByNetwork: req.params.id }; + return await db.LeaderboardCurrencyAddressesByNetwork.countDocuments( + filter + ); }, async currencyAssociationWithLeaderboard(req: any, res: any) { - var filter = {currency: req.params.id} - var cabnIds = await db.CurrencyAddressesByNetwork.find(filter).distinct('_id') - return await db.LeaderboardCurrencyAddressesByNetwork.countDocuments({currencyAddressesByNetwork: {$in: cabnIds}}); + var filter = { currency: req.params.id }; + var cabnIds = await db.CurrencyAddressesByNetwork.find(filter).distinct( + "_id" + ); + return await db.LeaderboardCurrencyAddressesByNetwork.countDocuments({ + currencyAddressesByNetwork: { $in: cabnIds }, + }); }, - async currencyAssociationWithTokenHoldersBalanceSnapshots(req: any, res: any) { - var filter = {currency: req.params.id} - var cabnIds = await db.CurrencyAddressesByNetwork.find(filter).distinct('_id') - return await db.TokenHoldersBalanceSnapshots.countDocuments({currencyAddressesByNetwork: {$in: cabnIds}}); + async currencyAssociationWithTokenHoldersBalanceSnapshots( + req: any, + res: any + ) { + var filter = { currency: req.params.id }; + var cabnIds = await db.CurrencyAddressesByNetwork.find(filter).distinct( + "_id" + ); + return await db.TokenHoldersBalanceSnapshots.countDocuments({ + currencyAddressesByNetwork: { $in: cabnIds }, + }); }, - async validateCabnToAddBaseFeeToken(req: any, res: any, isFromUpdate = false) { - let filter:any = {} + async validateCabnToAddBaseFeeToken( + req: any, + res: any, + isFromUpdate = false + ) { + let filter: any = {}; filter.network = req.body.networkId; filter.isBaseFeeToken = true; - if(isFromUpdate){ - filter._id = {$ne: req.body.oldCabnId} + if (isFromUpdate) { + filter._id = { $ne: req.body.oldCabnId }; } return await db.CurrencyAddressesByNetwork.countDocuments(filter); }, -} + async createBulkCabns(req: any, res: any) { + await this.validationCreateBulkCabns(req, res); + req = await this.createCurrencyObjectsForBulkCabns(req); + console.log(req.body.cabns); + for (let i = 0; i < req.body.cabns.length; i++) { + let cabn = await this.createCabnFromBulk(req, req.body.cabns[i]); + let currency = await db.Currencies.findOne({ _id: cabn.currency }); + if (currency) { + currency.currencyAddressesByNetwork.push(cabn?._id); + currency = await db.Currencies.findOneAndUpdate( + { _id: currency._id }, + { currencyAddressesByNetwork: currency.currencyAddressesByNetwork }, + { new: true } + ); + } + } + return ""; + }, + async validationCreateBulkCabns(req: any, res: any) { + if (!req.body.network || !req.body.cabns) { + throw "network & cabns are required."; + } + if (req.body.cabns.length == 0) { + throw "cabns are required."; + } + for (let i = 0; i < req.body.cabns.length; i++) { + let address = req.body.cabns[i].tokenContractAddress; + req.body.cabns[i].tokenContractAddress = address + ? address.toLowerCase() + : ""; + req.body.cabns[i].network = req.body.network; + req.body.cabns[i].networkDex = req.body.networkDex; + req.body.cabns[i].isCreatedFromBulk = true; + req.body.cabns[i].isAllowedOnMultiSwap = true; + } + req.body.networks = req.body.cabns; + let error = await commonFunctions.validationForUniqueCBN(req, res); + if (error) { + throw error; + } + }, + async createCurrencyObjectsForBulkCabns(req: any) { + for (let i = 0; i < req.body.cabns.length; i++) { + let currencySymbol = req.body.cabns[i].currencySymbol; + let currency = await this.getCurrentByShortName(currencySymbol); + if (!currency) { + let currencyBody: any = {}; + currencyBody.createdByUser = req.user._id; + currencyBody.updatedByUser = req.user._id; + currencyBody.name = req.body.cabns[i].currencyName + ? req.body.cabns[i].currencyName + : ""; + currencyBody.nameInLower = req.body.cabns[i].currencyName + ? req.body.cabns[i].currencyName.toLowerCase() + : ""; + currencyBody.logo = req.body.cabns[i].currencyLogo; + currencyBody.symbol = req.body.cabns[i].currencySymbol; + currencyBody.isCreatedFromBulk = true; + currencyBody.isVisibleForPublicMenuItem = false; + currencyBody.createdByOrganization = req.user.organization; + currencyBody.createdAt = new Date(); + currencyBody.updatedAt = new Date(); + currency = await db.Currencies.create(currencyBody); + } + req.body.cabns[i].currency = currency._id; + } + return req; + }, + async getCurrentByShortName(currencySymbol: string) { + return await db.Currencies.findOne({ symbol: currencySymbol }); + }, + async createCabnFromBulk(req: any, cabn: any) { + let cabnBody: any = {}; + cabnBody = { ...cabn }; + cabnBody.createdByUser = req.user._id; + cabnBody.createdByOrganization = req.user.organization; + cabnBody.createdAt = new Date(); + let result = await db.CurrencyAddressesByNetwork.create(cabnBody); + console.log("cabnBody", result); + return result; + }, +}; diff --git a/app/models/currencies.ts b/app/models/currencies.ts index 11d0594..988a619 100755 --- a/app/models/currencies.ts +++ b/app/models/currencies.ts @@ -1,25 +1,37 @@ -'use strict'; -var mongoose = require('mongoose'); +"use strict"; +var mongoose = require("mongoose"); -var schema = mongoose.Schema({ - createdByUser: { type: mongoose.Schema.Types.ObjectId, ref: 'users' }, - updatedByUser: { type: mongoose.Schema.Types.ObjectId, ref: "users" }, - createdByOrganization: { type: mongoose.Schema.Types.ObjectId, ref: 'organizations' }, - currencyAddressesByNetwork: [{ type: mongoose.Schema.Types.ObjectId, ref: 'currencyAddressesByNetwork'}], - name: { type: String, default: "" }, - nameInLower: { type: String, default: ""}, - symbol: { type: String, default: "" }, - logo: { type: String, default: "" }, - totalSupply: { type: Number, default: 0 }, - isActive: { type: Boolean, default: true }, - isVisibleForPublicMenuItem: { type: Boolean, default: true }, - valueInUsd: {type: Number, default: 0}, - valueInUsdUpdatedAt: { type: Date }, - coinGeckoId: {type: String}, - usdValueConversionPath: [{type: String}], - createdAt: { type: Date, default: new Date() }, - updatedAt: { type: Date, default: new Date() }, -},{ collection: 'currencies' }); +var schema = mongoose.Schema( + { + createdByUser: { type: mongoose.Schema.Types.ObjectId, ref: "users" }, + updatedByUser: { type: mongoose.Schema.Types.ObjectId, ref: "users" }, + createdByOrganization: { + type: mongoose.Schema.Types.ObjectId, + ref: "organizations", + }, + currencyAddressesByNetwork: [ + { + type: mongoose.Schema.Types.ObjectId, + ref: "currencyAddressesByNetwork", + }, + ], + name: { type: String, default: "" }, + nameInLower: { type: String, default: "" }, + symbol: { type: String, default: "" }, + logo: { type: String, default: "" }, + totalSupply: { type: Number, default: 0 }, + isActive: { type: Boolean, default: true }, + isVisibleForPublicMenuItem: { type: Boolean, default: true }, + valueInUsd: { type: Number, default: 0 }, + valueInUsdUpdatedAt: { type: Date }, + coinGeckoId: { type: String }, + usdValueConversionPath: [{ type: String }], + isCreatedFromBulk: { type: Boolean, default: false }, + createdAt: { type: Date, default: new Date() }, + updatedAt: { type: Date, default: new Date() }, + }, + { collection: "currencies" } +); -var currenciesModel = mongoose.model("currencies",schema); +var currenciesModel = mongoose.model("currencies", schema); module.exports = currenciesModel; diff --git a/app/models/currencyAddressesByNetwork.ts b/app/models/currencyAddressesByNetwork.ts index b8a9c4f..c63df2d 100755 --- a/app/models/currencyAddressesByNetwork.ts +++ b/app/models/currencyAddressesByNetwork.ts @@ -34,6 +34,7 @@ var schema = mongoose.Schema( name: { type: String, default: "" }, symbol: { type: String, default: "" }, }, + isCreatedFromBulk: { type: Boolean, default: false }, createdAt: { type: Date, default: new Date() }, updatedAt: { type: Date, default: new Date() }, },