-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #407 from zikriya/develop
Develop
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
attachAddressWithRandomKey, | ||
findRandomKeyByTag, | ||
} from "../../../helpers/multiSwapHelpers/randomOrdinalsPassportKeyHelper"; | ||
let asyncMiddleware = require("../../../lib/response/asyncMiddleware"); | ||
|
||
module.exports = function (router: any) { | ||
router.post( | ||
"/attach/address", | ||
asyncMiddleware(async (req: any, res: any) => { | ||
const { address } = req.body; | ||
if (!address) { | ||
return res.http400("address is required."); | ||
} | ||
return res.http200({ | ||
data: await attachAddressWithRandomKey(address), | ||
}); | ||
}) | ||
); | ||
|
||
router.get( | ||
"/:tag", | ||
asyncMiddleware(async (req: any, res: any) => { | ||
const { tag } = req.params; | ||
if (!tag) { | ||
return res.http400("tag is required."); | ||
} | ||
return res.http200({ | ||
data: await findRandomKeyByTag(tag), | ||
}); | ||
}) | ||
); | ||
}; |
22 changes: 22 additions & 0 deletions
22
app/controllers/api/v1/super-admin/randomOrdinalsPassportKey.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { isValidObjectId } from "mongoose"; | ||
import { saveRandomKeys } from "../../../../helpers/multiSwapHelpers/randomOrdinalsPassportKeyHelper"; | ||
let stringHelper = (global as any).stringHelper; | ||
|
||
module.exports = function (router: any) { | ||
router.post("/import", async (req: any, res: any) => { | ||
if (!req.body.data || req.body.data.length == 0) { | ||
return res.http400("data is required."); | ||
} | ||
await saveRandomKeys(req.body.data); | ||
return res.http200({ | ||
message: stringHelper.strSuccess, | ||
}); | ||
}); | ||
|
||
router.delete("/:id", async (req: any, res: any) => { | ||
await db.RandomOrdinalsPassportKeys.remove({ _id: req.params.id }); | ||
return res.http200({ | ||
message: stringHelper.strSuccess, | ||
}); | ||
}); | ||
}; |
69 changes: 69 additions & 0 deletions
69
app/helpers/multiSwapHelpers/randomOrdinalsPassportKeyHelper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
let stringHelper = (global as any).stringHelper; | ||
const TAGS = [ | ||
"BadKarma", | ||
"ShadowPulse", | ||
"NeoBlade", | ||
"YellowSnowman", | ||
"BlueSnowman", | ||
"HeartTicker", | ||
"Crazy-Cat-Lady", | ||
"unfriendme", | ||
"Babushka", | ||
"SaintBroseph", | ||
"iNeed2p", | ||
"FrostedCupcake", | ||
]; | ||
|
||
export const saveRandomKeys = async (data: any) => { | ||
let count = await db.RandomOrdinalsPassportKeys.countDocuments({}); | ||
for (let item of data) { | ||
if (item && (await isNewRandomKey(item))) { | ||
count = count + 1; | ||
let body = { | ||
tag: getTag(count), | ||
key: item, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}; | ||
await db.RandomOrdinalsPassportKeys.create(body); | ||
} | ||
} | ||
}; | ||
|
||
export const attachAddressWithRandomKey = async (address: string) => { | ||
let keyItem = await db.RandomOrdinalsPassportKeys.findOne({ | ||
address: address, | ||
}); | ||
if (keyItem) { | ||
return keyItem; | ||
} | ||
let data = await db.RandomOrdinalsPassportKeys.findOne({ address: "" }); | ||
if (!data) { | ||
throw stringHelper.strErrorNoRandmonKeyAvailable; | ||
} | ||
data = await db.RandomOrdinalsPassportKeys.findOneAndUpdate( | ||
{ _id: data._id }, | ||
{ address: address }, | ||
{ new: true } | ||
); | ||
return data; | ||
}; | ||
|
||
export const findRandomKeyByTag = async (tag: string) => { | ||
let data = await db.RandomOrdinalsPassportKeys.findOne({ tag: tag }); | ||
return data; | ||
}; | ||
|
||
const isNewRandomKey = async (key: any): Promise<boolean> => { | ||
let count = await db.RandomOrdinalsPassportKeys.countDocuments({ key: key }); | ||
return count > 0 ? false : true; | ||
}; | ||
|
||
const getTag = (count: number): string => { | ||
let index = 0; | ||
try { | ||
index = Math.floor(Math.random() * TAGS.length); | ||
index = index < TAGS.length ? index : 0; | ||
} catch (e) {} | ||
return `${TAGS[index]}-${count}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use strict"; | ||
var mongoose = require("mongoose"); | ||
|
||
var schema = mongoose.Schema( | ||
{ | ||
key: { type: String, default: "" }, | ||
address: { type: String, default: "" }, | ||
tag: { type: String, default: "" }, | ||
isActive: { type: Boolean, default: true }, | ||
isNonEVM: { type: Boolean, default: false }, | ||
createdAt: { type: Date, default: new Date() }, | ||
updatedAt: { type: Date, default: new Date() }, | ||
}, | ||
{ collection: "randomOrdinalsPassportKeys" } | ||
); | ||
|
||
var randomOrdinalsPassportKeys = mongoose.model( | ||
"randomOrdinalsPassportKeys", | ||
schema | ||
); | ||
module.exports = randomOrdinalsPassportKeys; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters