Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createBulkCabn api for superAdmin #380

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/controllers/api/v1/super-admin/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
})
);
};
154 changes: 126 additions & 28 deletions app/helpers/currencyHelper.ts
Original file line number Diff line number Diff line change
@@ -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;
},
};
56 changes: 34 additions & 22 deletions app/models/currencies.ts
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions app/models/currencyAddressesByNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
},
Expand Down
Loading