Skip to content

Fix challenge register / unregister calls #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions src/services/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,32 +450,56 @@ class ChallengesService {
return getApiResponsePayload(res);
}

/**
* Get the Resource Role ID from provided Role Name
* @param {String} roleName
* @return {Promise}
*/
async getResourceRoleId(roleName) {
const params = {
name: roleName,
isActive: true,
};
const roles = await this.private.apiV5.get(`/resource-roles?${qs.stringify(params)}`)
.then(checkErrorV5).then(res => res);

if (_.isEmpty(roles.result)) {
throw new Error('Resource Role not found!');
}

return roles.result[0].id;
}

/**
* Registers user to the specified challenge.
* @param {String} challengeId
* @param {String} memberHandle
* @param {String} roleId
* @return {Promise}
*/
async register(challengeId, memberHandle, roleId) {
async register(challengeId) {
const user = decodeToken(this.private.tokenV3);
const roleId = await this.getResourceRoleId('Submitter');
const params = {
challengeId, memberHandle, roleId,
challengeId,
memberHandle: user.handle,
roleId,
};
const res = await this.private.apiV5.post('/resources', params);
const res = await this.private.apiV5.postJson('/resources', params);
if (!res.ok) throw new Error(res.statusText);
return res.json();
}

/**
* Unregisters user from the specified challenge.
* @param {String} challengeId
* @param {String} memberHandle
* @param {String} roleId
* @return {Promise}
*/
async unregister(challengeId, memberHandle, roleId) {
async unregister(challengeId) {
const user = decodeToken(this.private.tokenV3);
const roleId = await this.getResourceRoleId('Submitter');
const params = {
challengeId, memberHandle, roleId,
challengeId,
memberHandle: user.handle,
roleId,
};
const res = await this.private.apiV5.delete('/resources', params);
if (!res.ok) throw new Error(res.statusText);
Expand Down