Skip to content

Commit

Permalink
Feat: create routes valid certified (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Reginaldo Junior <[email protected]>
  • Loading branch information
reginaldojunior and reginaldojuniorcreditas authored Jan 25, 2025
1 parent d9238d9 commit 38546b0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/controllers/certified.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 => {
Expand Down
2 changes: 2 additions & 0 deletions src/routes/certifiedRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 38546b0

Please sign in to comment.