Skip to content

Commit

Permalink
Merge pull request #20 from indexnetwork/api-bugfix
Browse files Browse the repository at this point in the history
iterate
  • Loading branch information
serefyarar authored Feb 6, 2024
2 parents e314295 + ccb1bdb commit ef18fec
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 62 deletions.
132 changes: 73 additions & 59 deletions api/src/controllers/did.js
Original file line number Diff line number Diff line change
@@ -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 });
}
};
6 changes: 5 additions & 1 deletion api/src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}});
}
}

Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions api/src/packages/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ 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),
}).or('name', 'bio', 'avatar')), validator.params(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({
Expand Down

0 comments on commit ef18fec

Please sign in to comment.