diff --git a/src/controllers/certified.js b/src/controllers/certified.js index 3ded9da..3aaad9d 100644 --- a/src/controllers/certified.js +++ b/src/controllers/certified.js @@ -1,5 +1,6 @@ const certificateService = require('../services/certificateService'); const certificates = require('../models/certificates'); +const { createHash } = require("crypto"); exports.get = (req, res, next) => { if (!req.params.hashId) { @@ -15,6 +16,32 @@ exports.get = (req, res, next) => { }) }; +exports.getValidHash = (req, res, next) => { + if (!req.params.hashId) { + res.status(400).json({"error": "Not found information hashId"}); + } + + certificates.findOne({ hash: req.params.hashId }).then(async (result) => { + res.status(200).json({"certified_valid": true}); + }).catch((error) => { + res.status(404).json(error); + }) +}; + +exports.getCertifiedByRG = (req, res, next) => { + if (!req.params.rg) { + res.status(400).json({"error": "Not found information RG"}); + } + + let rgCripted = createHash("sha256").update(req.params.rg).digest("hex") + + certificates.findOne({ documents: { rg: rgCripted } }).then(async (result) => { + res.status(200).json({"certified_valid": true}); + }).catch((error) => { + res.status(404).json(error); + }) +}; + exports.post = (req, res, next) => { certificates.create(req.body).then(async (result) => { await certificateService.createCertificate(req.body, result.hash).then(certificate => { diff --git a/src/routes/certifiedRoute.js b/src/routes/certifiedRoute.js index 1f01b5f..64fdd60 100644 --- a/src/routes/certifiedRoute.js +++ b/src/routes/certifiedRoute.js @@ -10,5 +10,7 @@ router.post('/', basicAuth({ }), controller.post); router.get('/:hashId', controller.get); +router.get('/valid/:hashId', controller.getValidHash); +router.get('/valid/:rg', controller.getCertifiedByRG); module.exports = router;