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

removeAllCabnByUser and removeCabnByCabnIdByUser apis implemented #366

Merged
merged 1 commit into from
Jan 30, 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
30 changes: 30 additions & 0 deletions app/controllers/api/v1/community-member/multiSwap/cabns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
let asyncMiddleware = require("../../../../../lib/response/asyncMiddleware");
let stringHelper = require("../../../../../helpers/stringHelper");
import {
doValidationForCreateUserCabn,
createUserCabn,
deleteUserIdFromCabn,
deleteUserIdFromAllCabns,
} from "../../../../../helpers/multiSwapHelpers/cabnsHelper";

module.exports = function (router: any) {
router.delete(
"/all",
asyncMiddleware(async (req: any, res: any) => {
await deleteUserIdFromAllCabns(req.user);
return res.http200({
message: stringHelper.strSuccess,
});
})
);

router.delete(
"/:cabnId",
asyncMiddleware(async (req: any, res: any) => {
await deleteUserIdFromCabn(req.user, req.params.cabnId);
return res.http200({
message: stringHelper.strSuccess,
});
})
);
};
68 changes: 65 additions & 3 deletions app/helpers/multiSwapHelpers/cabnsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const createUserCabn = async (req: any): Promise<any> => {
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);
Expand All @@ -39,6 +38,69 @@ export const createUserCabn = async (req: any): Promise<any> => {
}
};

export const deleteUserIdFromAllCabns = async (user: any): Promise<any> => {
let cabnFilter: any = {};
cabnFilter.createdByusers = { $in: user._id };
cabnFilter.isDefault = false;
let cabns = await db.CurrencyAddressesByNetwork.find(cabnFilter);
if (cabns && cabns.length > 0) {
for (let cabn of cabns) {
await deleteUserIdFromCabn(user, cabn._id);
}
}
};

export const deleteUserIdFromCabn = async (
user: any,
cabnId: string
): Promise<any> => {
let cabnFilter: any = {};
cabnFilter.createdByusers = { $in: user._id };
cabnFilter._id = cabnId;
cabnFilter.isDefault = false;
let cabn = await findCabnByFilter(cabnFilter);
if (cabn) {
let users = cabn.createdByusers;
users = removeItemFromList(users, user._id);
cabn.createdByusers = users;
cabn.updatedAt = new Date();
cabn = await db.CurrencyAddressesByNetwork.findOneAndUpdate(
cabnFilter,
cabn,
{ new: true }
);
}
};

export const makeCabnDefault = async (cabnId: string): Promise<any> => {
let cabnFilter: any = {};
cabnFilter._id = cabnId;
cabnFilter.isDefault = false;
let cabn = await findCabnByFilter(cabnFilter);
if (cabn) {
cabn.createdByusers = [];
cabn.isDefault = true;
cabn.updatedAt = new Date();
cabn = await db.CurrencyAddressesByNetwork.findOneAndUpdate(
cabnFilter,
cabn,
{ new: true }
);
}
};

const removeItemFromList = (arr: any, value: any) => {
var i = 0;
while (i < arr.length) {
if (arr[i].toString() == value.toString()) {
arr.splice(i, 1);
} else {
++i;
}
}
return arr;
};

const countCabnByFilter = async (cabnFilter: any): Promise<any> => {
console.log(cabnFilter);
return await db.CurrencyAddressesByNetwork.countDocuments(cabnFilter);
Expand All @@ -59,8 +121,8 @@ const getNonDefaultCurrencyInformationObject = (body: any): any => {
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) {
let temp = true;
if ((await countCabnByFilter(cabnFilter)) == 0 || temp) {
delete cabnFilter.createdByusers;
cabn = await findCabnByFilter(cabnFilter);
if (cabn) {
Expand Down
Loading