Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26a0ece
commit fdf8441
Showing
3 changed files
with
73 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
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,42 @@ | ||
import { Pool } from "pg"; | ||
import { Request, Response } from "express"; | ||
|
||
export const handlePolicyIdExists = | ||
(pool: Pool) => | ||
async (req: Request, res: Response): Promise<void> => { | ||
if (!req.body || !req.body.policyIds) { | ||
throw new Error("error, no policyIds informed."); | ||
} | ||
if (!Array.isArray(req.body.policyIds)) { | ||
throw new Error("'policyIds should be an array."); | ||
} | ||
|
||
const policyIds: string[] = req.body.policyIds; | ||
|
||
if (policyIds.length > 100) { | ||
throw new Error("Max limit of 100 policyIds exceeded."); | ||
} | ||
if (policyIds.length === 0) { | ||
throw new Error("error, at least 1 policyId should be informed."); | ||
} | ||
|
||
const result = await pool.query(query, [policyIds]); | ||
|
||
const policyIdResults: { [key: string]: boolean } = {}; | ||
|
||
policyIds.forEach((policyId: string) => { | ||
policyIdResults[policyId] = false; | ||
}); | ||
|
||
result.rows.forEach((row: any) => { | ||
policyIdResults[row.policy_hex] = true; | ||
}); | ||
|
||
res.send({ | ||
policyIdResults, | ||
}); | ||
}; | ||
|
||
const query = ` | ||
SELECT encode(policy, 'hex') as policy_hex FROM multi_asset | ||
WHERE encode(policy, 'hex') = ANY($1);`; |