Skip to content
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

added onlyForFemale field #76

Closed
Closed
140 changes: 140 additions & 0 deletions managers/opportunity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,146 @@ class opportunityManager {

async getOpportunities(queryObject) {
try {
// Filter based on opportunityType
if (queryObject.type) {
queryObject['opportunityType'] = queryObject.type;
delete queryObject.type;
}
if (queryObject.female) {
queryObject['onlyForFemale'] = queryObject.female === 'true';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, why a boolean value is written as a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will fix that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

delete queryObject.female;
}
console.log('Values in QueryString', queryObject);

let fetchedOpportunitiesQuery = this.opportunity.find(queryObject);
// fetchedOpportunitiesQuery.select('-__v');
// fetchedOpportunitiesQuery.select('-_id');
return fetchedOpportunitiesQuery;

//console.log('Values of QueryString', queryObject);

// Here page means which page to start from and limit specifies the number of documnents to be
// rendered starting from this page
let { limit, page } = queryObject;

// The case when both page and limit are not specified we assign both of them default values
if (page == undefined && limit == undefined) {
limit = 10;
page = 1;
}

//Only limit is 'undefined or 0' , so it is assigned a default value of 1
if (limit == undefined) {
limit = 10;
}

// The case when starting page is not mentioned or it is Put in as 0 , we use default value
if (page == undefined) {
page = 1;
}

// The query objects are by default "string" , so we convert them to "INT" to work ahead
limit = parseInt(limit);
page = parseInt(page);

// The startIndex is the first index to be rendered from the database
// skip is the total no of documents/items on each page as per the user
const startIndex = page - 1;
const skip = (page - 1) * limit;

// Object initialised to store the results for various queries
const result = {};

// The case when the queries are invalid , we just return a result with valid error message
// Number of Total Documents Saved in a variable
let numDocs = await this.opportunity.countDocuments().exec();

// TotalPages denote the total number of pages that are possible with the given limit and Documents available
let totalPages;

// When limit is less than or equal to the number of Docs
if (numDocs >= limit) {
totalPages = Math.floor(numDocs / limit) + (numDocs % limit);
}

// this is the case when limit exceeds the documents , so we only have a single page
else {
totalPages = 1;
}

// Begin here shows the first index of next Page and helps in determining wether or not it is possible to go on the next page
let begin = page * limit;

// If any of the paramter becomes neagtive or Zero
if (page <= 0 || limit <= 0) {
result.results = 'The Parameters cannot be Negative or Zero';
return result;
}

// If user enters a Page number greater than the number of Documents present in the Database
if (startIndex >= numDocs) {
result.results = 'The maximum page value should be ' + numDocs;
return result;
}

// If the page entered exceeds the Max allowed value(totalPages)
if (page > totalPages) {
let maxAllowedPages = totalPages;
result.results = 'The maximum allowed pages are ' + maxAllowedPages;
return result;
}

// StartIndex should be atleast 0 and for the min val of Page should be 1
if (startIndex < 0) {
result.results = 'The minimum page value should be 1';
return result;
}

// Page can't be entered more than the Number of documents present in DataBase
if (startIndex >= numDocs) {
result.results = 'The maximum page value should be ' + numDocs;
return result;
}

// If we get valid data we also render the Next and Previous page to the User along with the Results
else {
// If there is valid next page in the Database , then we let the user know about it

if (begin < numDocs) {
result.next = {
page: page + 1,
limit: limit,
};
}

// If there is valid Previous page in the Database , then we let the user know about it
if (startIndex > 0) {
result.previous = {
page: page - 1,
limit: limit,
};
}
}

// Once a Valid Query(The one which is inside the Range) is entered, we render the Results
try {
result.results = await this.opportunity.find(
{
/* Everything*/
},
{
/* No constraints */
},
{
skip: skip,
limit: limit,
}
);

return result;
} catch (e) {
console.log(`ERR: `, e.stack);
}
return pagination(queryObject, this.opportunity);
} catch (err) {
console.log('ERR getOpportunities: ', err.stack);
Expand Down
15 changes: 4 additions & 11 deletions routes/opportunity.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,12 @@ const opportunityManager = new OpportunityManager(),
* - SCHOLARSHIP
* - CONFERENCE
* - CODINGCOMPETITION
* description: Retrieve a list of opportunities based on particular type.
* description: Retrieve a list of opportunities based on particular type or only for female.
* parameters:
* - in: query
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In documentation also, you should add female query parameter,
Visiting http://localhost:3030/playground/#/default/get_opportunity shall have female parameter added.

* name: page
* name: female
* schema:
* type: Integer
* default: 1
* description: The Current Page for which the data User Wants
* - in: query
* name: limit
* schema:
* type: Integer
* default: 10
* description: The count of items/documents that should be returned on each page
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

* type: boolean
* responses:
* 200:
* description: A list of opportunities.
Expand Down
1 change: 1 addition & 0 deletions tests/controller/opportunity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('OpportunityController', function () {
req = {
query: {
type: stubValue.opportunityType,
female: stubValue.onlyForFemale,
},
};
status = sinon.stub();
Expand Down
28 changes: 11 additions & 17 deletions tests/managers/opportunity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,11 @@ describe('OpportunityManager', function () {
describe('getOpportunities', function () {
it('should retrieve Opportunities with specific opportunityType', async function () {
const stub = sinon.stub(Opportunity, 'find').returns(stubValue);
const stubCountDocuments = sinon
.stub(Opportunity, 'countDocuments')
.returns({
exec: async () => 10,
});

const opportunityManager = new OpportunityManager();
const opportunity = (
await opportunityManager.getOpportunities({
type: stubValue.opportunityType,
})
).results;

expect(stubCountDocuments.calledOnce).to.be.true;
const opportunity = await opportunityManager.getOpportunities({
type: stubValue.opportunityType,
female: stubValue.onlyForFemale
});
expect(stub.calledOnce).to.be.true;
expect(opportunity.opportunityTitle).to.equal(stubValue.opportunityTitle);
expect(opportunity.opportunityType).to.equal(stubValue.opportunityType);
Expand All @@ -79,15 +70,18 @@ describe('OpportunityManager', function () {
expect(opportunity.opportunityRegistrationDeadline).to.equal(
stubValue.opportunityRegistrationDeadline
);
expect(opportunity.organisationLogoURL).to.equal(
stubValue.organisationLogoURL
);
expect(opportunity.opportunityDate).to.equal(stubValue.opportunityDate);
expect(opportunity.opportunityURL).to.equal(stubValue.opportunityURL);
expect(opportunity.createdAt).to.equal(stubValue.createdAt);
expect(opportunity.updatedAt).to.equal(stubValue.updatedAt);
expect(opportunity.organisationLogoURL).to.equal(
stubValue.organisationLogoURL
);
expect(opportunity.onlyForFemale).to.equal(stubValue.onlyForFemale);
});
});
});


describe('updateOpportunity', function () {
it('should update existing Opportunity', async function () {
Expand Down Expand Up @@ -178,4 +172,4 @@ describe('OpportunityManager', function () {
);
});
});
});

3 changes: 3 additions & 0 deletions tests/services/opportunity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('OpportunityService', function () {
expect(stub.calledOnce).to.be.true;
expect(opportunity.opportunityTitle).to.equal(stubValue.opportunityTitle);
expect(opportunity.opportunityType).to.equal(stubValue.opportunityType);
expect(opportunity.onlyForFemale).to.equal(stubValue.onlyForFemale);
expect(opportunity.opportunityOrganisation).to.equal(
stubValue.opportunityOrganisation
);
Expand Down Expand Up @@ -57,10 +58,12 @@ describe('OpportunityService', function () {
const opportunityService = new OpportunityService(opportunityManager);
const opportunity = await opportunityService.getOpportunities({
type: stubValue.opportunityType,
female: stubValue.onlyForFemale,
});
expect(stub.calledOnce).to.be.true;
expect(opportunity.opportunityTitle).to.equal(stubValue.opportunityTitle);
expect(opportunity.opportunityType).to.equal(stubValue.opportunityType);
expect(opportunity.onlyForFemale).to.equal(stubValue.onlyForFemale);
expect(opportunity.opportunityOrganisation).to.equal(
stubValue.opportunityOrganisation
);
Expand Down