Skip to content

Commit

Permalink
implements endpoint to retrieve credential info
Browse files Browse the repository at this point in the history
  • Loading branch information
kezike committed Mar 16, 2024
1 parent 721eccb commit acc1696
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
56 changes: 32 additions & 24 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,7 @@ export async function build(opts = {}) {
app.use(cors());

app.get('/', function (req, res, next) {
res.send({ message: 'status-service-db server status: ok.' });
});

// Get status credential
app.get('/:statusCredentialId', async function (req, res, next) {
const statusCredentialId = req.params.statusCredentialId;
try {
const statusCredential = await status.getStatusCredential(statusCredentialId);
if (!statusCredential) {
next({
message: `Unable to find Status Credential with ID "${statusCredentialId}".`,
code: 404
});
}
return res.status(200).json(statusCredential);
} catch (error) {
next({
message: error.message,
code: error.code
});
}
res.json({ message: 'status-service-db server status: ok.' });
});

// Allocate status
Expand All @@ -52,7 +32,7 @@ export async function build(opts = {}) {
code: 400
});
}
const vcWithStatus = await status.allocateAllStatuses(vc);
const vcWithStatus = await status.allocateSupportedStatuses(vc);
return res.json(vcWithStatus);
} catch (e) {
// We catch the async errors and pass them to the error handler.
Expand Down Expand Up @@ -86,8 +66,8 @@ export async function build(opts = {}) {
code: 400
});
}
const statusResponse = await status.updateStatus(credentialId, statusId);
return res.status(statusResponse.code).send(statusResponse);
const updateStatusResponse = await status.updateStatus(credentialId, statusId);
return res.status(updateStatusResponse.code).json(updateStatusResponse);
} catch (e) {
// We catch the async errors and pass them to the error handler.
if (!e.message) {e.message = "Error updating credential status position."}
Expand All @@ -97,6 +77,34 @@ export async function build(opts = {}) {
}
});

// Get credential info
app.get('/credentials/:credentialId', async function (req, res, next) {
const credentialId = req.params.credentialId;
try {
const credentialInfo = await status.getCredentialInfo(credentialId);
return res.status(200).json(credentialInfo);
} catch (error) {
next({
message: error.message,
code: error.code
});
}
});

// Get status credential
app.get('/:statusCredentialId', async function (req, res, next) {
const statusCredentialId = req.params.statusCredentialId;
try {
const statusCredential = await status.getStatusCredential(statusCredentialId);
return res.status(200).json(statusCredential);
} catch (error) {
next({
message: error.message,
code: error.code
});
}
});

// Attach the error handling middleware calls, in the order that they should run
app.use(errorLogger);
app.use(errorHandler);
Expand Down
26 changes: 16 additions & 10 deletions src/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,11 @@ async function getStatusManager() {
return STATUS_LIST_MANAGER;
}

async function getStatusCredential(statusCredentialId) {
const statusManager = await getStatusManager();
return statusManager.getStatusCredential(statusCredentialId);
}

async function allocateAllStatuses(verifiableCredential) {
async function allocateSupportedStatuses(verifiableCredential) {
const statusManager = await getStatusManager();
const result = verifiableCredential.credentialStatus ?
verifiableCredential :
await statusManager.allocateAllStatuses(verifiableCredential);
await statusManager.allocateSupportedStatuses(verifiableCredential);
return result;
}

Expand Down Expand Up @@ -110,10 +105,21 @@ async function updateStatus(credentialId, credentialStatus) {
}
}

async function getCredentialInfo(credentialId) {
const statusManager = await getStatusManager();
return statusManager.getCredentialInfo(credentialId);
}

async function getStatusCredential(statusCredentialId) {
const statusManager = await getStatusManager();
return await statusManager.getStatusCredential(statusCredentialId);
}

export default {
initializeStatusManager,
getStatusManager,
getStatusCredential,
allocateAllStatuses,
updateStatus
allocateSupportedStatuses,
updateStatus,
getCredentialInfo,
getStatusCredential
};

0 comments on commit acc1696

Please sign in to comment.