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

CreateCabn completed in cabnHelper. Structure is also modified. #364

Merged
merged 1 commit into from
Jan 25, 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
62 changes: 57 additions & 5 deletions app/helpers/multiSwapHelpers/cabnsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,68 @@ export const doValidationForCreateUserCabn = async (req: any): Promise<any> => {
};

export const createUserCabn = async (req: any): Promise<any> => {
let countFilter: any = {};
let cabnFilter: any = {};
const userId = req.user._id;
const networkId = await db.Networks.countDocuments({
const network = await db.Networks.findOne({
chainId: req.body.chainId,
});
if (!networkId) {
if (!network) {
throw stringHelper.chainIdNotSupported;
}
req.body.tokenContractAddress = req.body.tokenContractAddress.toLowerCase();
cabnFilter.network = network._id;
cabnFilter.tokenContractAddress = req.body.tokenContractAddress;
console.log("cabn count", await countCabnByFilter(cabnFilter));
if ((await countCabnByFilter(cabnFilter)) == 0) {
req.body.nonDefaultCurrencyInformation =
getNonDefaultCurrencyInformationObject(req.body);
req.body.createdByusers = [userId];
req.body.network = network._id;
req.body.isAllowedOnMultiSwap = true;
req.body.isDefault = false;
req.body.createdAt = new Date();
req.body.updatedAt = new Date();
return await db.CurrencyAddressesByNetwork.create(req.body);
} else {
return addUserIdIntoCabn(cabnFilter, req);
}
};

const countCabnByFilter = async (cabnFilter: any): Promise<any> => {
console.log(cabnFilter);
return await db.CurrencyAddressesByNetwork.countDocuments(cabnFilter);
};

const findCabnByFilter = async (cabnFilter: any): Promise<any> => {
console.log(cabnFilter);
return await db.CurrencyAddressesByNetwork.findOne(cabnFilter);
};

const getNonDefaultCurrencyInformationObject = (body: any): any => {
return {
name: body.currencyName,
symbol: body.currencySymbol,
};
};

const countCabnByFilter = async (filter: any): Promise<any> => {
return await db.CurrencyAddressesByNetwork.countDocuments(filter);
const addUserIdIntoCabn = async (cabnFilter: any, req: any): Promise<any> => {
let cabn;
cabnFilter.createdByusers = { $in: req.user._id };
let byPass = true;
if ((await countCabnByFilter(cabnFilter)) == 0 || byPass) {
delete cabnFilter.createdByusers;
cabn = await findCabnByFilter(cabnFilter);
if (cabn) {
let users = cabn.createdByusers;
users.push(req.user._id);
cabn.createdByusers = users;
cabn.updatedAt = new Date();
cabn = await db.CurrencyAddressesByNetwork.findOneAndUpdate(
cabnFilter,
cabn,
{ new: true }
);
}
}
return cabn ? cabn : await findCabnByFilter(cabnFilter);
};
2 changes: 2 additions & 0 deletions app/helpers/stringHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ let transactionFailedMessageOne = "Backend_Transaction_Failed_Message_One";
let transactionFailedMessageTwo = "Backend_Transaction_Failed_Message_Two";
let invalidHashMessage = "Backend_Invalid_Hash_Message";
let swapFailedMessage = "Backend_Swap_Failed_Message";
var strErrorCabnAlreadyExist = "Backend_Error_Cabn_Already_Exist";
var strErrorNodePairAlreadyExist = "This pair already exist.";

// success messages
Expand Down Expand Up @@ -162,4 +163,5 @@ module.exports = {
withdrawlSuccessfulMessage,
strErrorNodePairAlreadyExist,
chainIdNotSupported,
strErrorCabnAlreadyExist,
};
3 changes: 2 additions & 1 deletion app/lib/stringsPhrase.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@
"Backend_Transaction_Failed_Message_Two": "We are having trouble processing the Transaction, please contact support to proceed further.",
"Backend_Invalid_Hash_Message": "Swap unsuccessful due to invalid hash.",
"Backend_Swap_Failed_Message": "Swap transaction has failed.",
"Backend_Withdrawl_Successful_Message": "Transaction withdrawal successful."
"Backend_Withdrawl_Successful_Message": "Transaction withdrawal successful.",
"Backend_Error_Cabn_Already_Exist": "This cabn already exist."
}
10 changes: 10 additions & 0 deletions app/models/currencyAddressesByNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ var schema = mongoose.Schema(
type: mongoose.Schema.Types.ObjectId,
ref: "organizations",
},
createdByusers: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "users",
},
],
tokenContractAddress: { type: String, default: "" },
isAllowedOnMultiSwap: { type: Boolean, default: false },
isFeeToken: { type: Boolean, default: false },
Expand All @@ -24,6 +30,10 @@ var schema = mongoose.Schema(
isNative: { type: Boolean, default: false },
oneInchAddress: { type: String, default: "" },
isDefault: { type: Boolean, default: true },
nonDefaultCurrencyInformation: {
name: { type: String, default: "" },
symbol: { type: String, default: "" },
},
createdAt: { type: Date, default: new Date() },
updatedAt: { type: Date, default: new Date() },
},
Expand Down
Loading