From 6fa066f70adda9a289c282ab4283ef720facf5f7 Mon Sep 17 00:00:00 2001 From: serafettin Date: Mon, 5 Feb 2024 18:35:05 +0100 Subject: [PATCH 1/2] iterate --- api/src/controllers/did.js | 132 +++++++++++++++++++---------------- api/src/controllers/index.js | 6 +- api/src/packages/api.js | 6 +- 3 files changed, 82 insertions(+), 62 deletions(-) diff --git a/api/src/controllers/did.js b/api/src/controllers/did.js index a6fbff60..52359c9f 100644 --- a/api/src/controllers/did.js +++ b/api/src/controllers/did.js @@ -1,77 +1,91 @@ import { DIDService } from "../services/did.js"; -export const getIndexes = async (req, res, next) => { - // sendLit(req.params.id) //TODO Fix later. +export const getIndexes = async (req, res) => { - try { - const didService = new DIDService().setSession(req.session) - const { type, did } = req.params; - const indexes = await didService.getIndexes(did, type) - res.status(200).json(indexes); - } catch (error) { - res.status(500).json({ error: error.message }); - } + // sendLit(req.params.id) // TODO Fix later. + try { + const didService = new DIDService(); + const { type, did } = req.params; + const indexes = await didService.getIndexes(did, type); + res.status(200).json(indexes); + } catch (error) { + res.status(500).json({ error: error.message }); + } }; -export const addIndex = async (req, res, next) => { - - if(req.params.did !== req.session.did.parent) { - return res.status(500).json({ error: "Authorization error" }); - } - const type = req.params.type === 'own' ? 'owned' : 'starred'; - const { indexId } = req.params; - try { - const didService = new DIDService().setSession(req.session); - const newIndex = await didService.addIndex(indexId, type) - res.status(201).json(newIndex); - } catch (error) { - res.status(500).json({ error: error.message }); - } +export const addIndex = async (req, res) => { + if (req.params.did !== req.session.did.parent) { + return res.status(500).json({ error: "Authorization error" }); + } + const type = req.params.type === "own" ? "owned" : "starred"; + const { indexId } = req.params; + try { + const didService = new DIDService().setSession(req.session); + const newIndex = await didService.addIndex(indexId, type); + res.status(201).json(newIndex); + } catch (error) { + res.status(500).json({ error: error.message }); + } }; export const removeIndex = async (req, res, next) => { + if (req.params.did !== req.session.did.parent) { + return res.status(500).json({ error: "Authorization error" }); + } - if(req.params.did !== req.session.did.parent) { - return res.status(500).json({ error: "Authorization error" }); - } + const type = req.params.type === "own" ? "owned" : "starred"; + const { indexId } = req.params; + try { + const didService = new DIDService().setSession(req.session); + const newIndex = await didService.removeIndex(indexId, type); + res.status(200).json(newIndex); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; - const type = req.params.type === 'own' ? 'owned' : 'starred'; - const {indexId } = req.params; - try { - const didService = new DIDService().setSession(req.session) - const newIndex = await didService.removeIndex(indexId, type) - res.status(200).json(newIndex); - } catch (error) { - res.status(500).json({ error: error.message }); - } +export const createProfile = async (req, res, next) => { + if (req.params.did !== req.session.did.parent) { + return res.status(500).json({ error: "Authorization error" }); + } + try { + const didService = new DIDService().setSession(req.session); + const profile = await didService.createProfile(req.body); + res.status(201).json(profile); + } catch (error) { + res.status(500).json({ error: error.message }); + } }; +export const getProfileByDID = async (req, res, next) => { + try { + const didService = new DIDService(); + let profile = await didService.getProfile(req.params.did); -export const createProfile = async (req, res, next) => { - if(req.params.did !== req.session.did.parent) { - return res.status(500).json({ error: "Authorization error" }); - } - try { - const didService = new DIDService().setSession(req.session); - const profile = await didService.createProfile(req.body) - res.status(201).json(profile); - } catch (error) { - res.status(500).json({ error: error.message }); + if (!profile) { + profile = { + id: req.params.did, + }; } -}; -export const getProfile = async (req, res, next) => { - try { - const didService = new DIDService() - let profile = await didService.getProfile(req.params.did) + res.status(200).json(profile); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}; - if(!profile){ - profile = { - id: req.params.did, - } - } +export const getProfileFromSession = async (req, res, next) => { + try { + const didService = new DIDService(); + let profile = await didService.getProfile(req.session.did.parent); - res.status(200).json(profile); - } catch (error) { - res.status(500).json({ error: error.message }); + if (!profile) { + profile = { + id: req.params.did, + }; } + + res.status(200).json(profile); + } catch (error) { + res.status(500).json({ error: error.message }); + } }; diff --git a/api/src/controllers/index.js b/api/src/controllers/index.js index f65e1452..dc38a07e 100644 --- a/api/src/controllers/index.js +++ b/api/src/controllers/index.js @@ -13,7 +13,7 @@ export const getIndexById = async (req, res, next) => { const roles = getRolesFromSession(pkpSession); Object.assign(index, {roles}); }else{ - Object.assign(index, {owner: false, creator: false}); + Object.assign(index, {roles: owner: false, creator: false}); } } @@ -36,6 +36,10 @@ export const createIndex = async (req, res, next) => { owned: true, starred: false }; + newIndex.roles = { + owner: true, + creator: true + } res.status(201).json(newIndex); } catch (error) { diff --git a/api/src/packages/api.js b/api/src/packages/api.js index 039dab34..d569602b 100644 --- a/api/src/packages/api.js +++ b/api/src/packages/api.js @@ -65,7 +65,7 @@ app.delete('/dids/:did/indexes/:indexId/:type', authCheckMiddleware, validator.p type: Joi.string().valid('own', 'star').required(), })), didController.removeIndex) -app.patch('/dids/:did/profile', authCheckMiddleware, validator.body(Joi.object({ +app.patch('/profile', authCheckMiddleware, validator.body(Joi.object({ name: Joi.string().optional(), bio: Joi.string().optional(), avatar: Joi.custom(isCID, "Avatar").optional().allow(null), @@ -73,9 +73,11 @@ app.patch('/dids/:did/profile', authCheckMiddleware, validator.body(Joi.object({ did: Joi.custom(isDID, "DID").required(), })), didController.createProfile) +app.get('/profile', authCheckMiddleware, didController.getProfileFromSession) + app.get('/dids/:did/profile', validator.params(Joi.object({ did: Joi.custom(isDID, "DID").required(), -})), didController.getProfile) +})), didController.getProfileByDID) // Indexes app.get('/indexes/:id', validator.params(Joi.object({ From ccb1bdbc8a65ed2a091d543be8e93576b7a7a40c Mon Sep 17 00:00:00 2001 From: serafettin Date: Tue, 6 Feb 2024 16:14:52 +0100 Subject: [PATCH 2/2] Update index.js --- api/src/controllers/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/controllers/index.js b/api/src/controllers/index.js index dc38a07e..06efd162 100644 --- a/api/src/controllers/index.js +++ b/api/src/controllers/index.js @@ -13,7 +13,7 @@ export const getIndexById = async (req, res, next) => { const roles = getRolesFromSession(pkpSession); Object.assign(index, {roles}); }else{ - Object.assign(index, {roles: owner: false, creator: false}); + Object.assign(index, {roles: {owner: false, creator: false}}); } }