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

Merge fixes that weren't merged during Labs #1

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const gameRouter = require('./game/gameRouter');
const resetRouter = require('./reset/resetRouter');
const leadBoard = require('./leaderboard/leadboardRouter');
const achievements = require('./Achievements/achieveRouter');
const dev = require('./dev/devModeRouter');

const app = express();

Expand Down Expand Up @@ -75,6 +76,7 @@ app.use('/game', gameRouter);
app.use('/reset', resetRouter);
app.use('/leaderboard', leadBoard);
app.use('/achievements', achievements);
app.use('/dev', dev);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
Expand Down
52 changes: 52 additions & 0 deletions api/dev/devModeModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const faker = require('faker');
const db = require('../../data/db-config');

/**
* This query marks the submission with the given ID as having HasRead, HasWritten, and HasDrawn set to false, and uploads/deletes submissions accordingly.
* @param {number} ID the ID of the submission to be marked as read
* @param {boolean} hasRead boolean to indicate whether db should set hasRead to true or false
* @param {boolean} hasDrawn boolean to indicate whether db should set hasDrawn to true or false
* @param {boolean} hasWritten boolean to indicate whether db should set hasWritten to true or false
* @returns {Promise} returns a promise that resolves to the count of fields updated
*/
const updateAll = async (ID, hasRead, hasDrawn, hasWritten) => {
/**
* This monolithic function is a transaction
* uploads submissions depending on booleans (hasDrawn, hasWritten), either adding/removing/ignoring
* updates submission tasks depending on booleans
*/
return db.transaction(async (trx) => {
try {

const preExistingUserDrawing = await db('Drawing').where({ SubmissionID: ID });
if(hasDrawn){
if(preExistingUserDrawing.length < 1){
await trx('Drawing').insert({ URL: faker.image.imageUrl(), SubmissionID: ID });
}
} else {
if(preExistingUserDrawing.length > 0){
await trx('Drawing').where({ SubmissionID: ID }).del();
};
};

const preExistingUserWriting = await db('Writing').where({ SubmissionID: ID });
if(hasWritten){
if(preExistingUserWriting.length < 1){
await trx('Writing').insert({ URL:faker.image.imageUrl(), PageNum: 1, SubmissionID: ID });
}
} else {
if(preExistingUserWriting.length > 0) {
await trx('Writing').where({ SubmissionID: ID }).del();
}
}
await trx('Submissions').where({ ID }).update({ HasRead: hasRead, HasWritten: hasWritten, HasDrawn: hasDrawn });
} catch(err) {
throw new Error(err.message);
}
});

}

module.exports = {
updateAll,
}
58 changes: 58 additions & 0 deletions api/dev/devModeRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const router = require('express').Router();

const { authRequired, validateUpdateAllTasksParams } = require('../middleware');
const { crudOperationsManager } = require('../../lib');
const DevModel = require('./devModeModel');


/**
* @swagger
* /submit/update-all/{id}:
* put:
* summary: Attempts to mark the submission with the given ID as hasRead as 'false', hasWritten as 'false', hasDrawn as 'false'
* security:
* - okta: []
* tags:
* - Submissions
* parameters:
* - $ref: '#/components/parameters/submissionId'
* - in: formData
* name: hasRead
* type: boolean
* description: boolean to set users task hasRead to
* - in: formData
* name: hasDrawn
* type: boolean
* description: boolean to set users task hasDrawn to
* - in: formData
* name: hasWritten
* type: boolean
* description: boolean to set users task hasWritten to
* responses:
* 204:
* $ref: '#/components/responses/EmptySuccess'
* 400:
* description: Invalid request missing/invalid arguments
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* description: Required inputs hasRead, hasDrawn, and hasWritten must be of boolean type
* 401:
* $ref: '#/components/responses/UnauthorizedError'
* 404:
* $ref: '#/components/responses/NotFound'
* 500:
* $ref: '#/components/responses/DatabaseError'
*/
router.put('/update-all/:id', authRequired, validateUpdateAllTasksParams, async (req, res) => {
const { id } = req.params;
const { hasRead, hasDrawn, hasWritten } = req.body;

return crudOperationsManager.update(res, DevModel.updateAll, 'Submission', id, hasRead, hasDrawn, hasWritten);
})

module.exports = router;
25 changes: 15 additions & 10 deletions api/mod/modHelpers/clusterGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ const clusterGeneration = () => {
// Pull a list of all cohorts
const cohorts = await trx('Cohorts');
// Iterate over the cohorts

for (let { ID } of cohorts) {
// Get all submissions for every cohort
const unformatted = await dbOps.getAllSubmissionsByCohort(trx, ID);
// Store each cohorts' submissions in the dsReq hash table with the key being the cohort id
dsReq[ID] = formatCohortSubmissions(unformatted);
if (unformatted.length > 0){
dsReq[ID] = formatCohortSubmissions(unformatted);
}
}
// Send the submissions to data science for clustering
const { data } = await dsApi.getClusters(dsReq);
Expand All @@ -32,15 +35,17 @@ const clusterGeneration = () => {
// Iterate over cohorts again
for (let { ID } of cohorts) {
// For every squad returned in each cohort from data science, where squad is an array of 4 submission IDs
for (let squad of clusters[ID]) {
// Create a squad in the database for the current cohort
const [SquadID] = await addSquad(trx, ID);
// Create two teams for the newly created squad
const [t1, t2] = await addTeams(trx, SquadID);
// Create 4 new team member entries for the database
const newMembers = await addMembers(trx, t1, t2, squad);
// [[1], [2], [3], [4]] => [1, 2, 3, 4]
members.push([].concat.apply([], newMembers));
if(clusters[ID]) {
for (let squad of clusters[ID]) {
// Create a squad in the database for the current cohort
const [SquadID] = await addSquad(trx, ID);
// Create two teams for the newly created squad
const [t1, t2] = await addTeams(trx, SquadID);
// Create 4 new team member entries for the database
const newMembers = await addMembers(trx, t1, t2, squad);
// [[1], [2], [3], [4]] => [1, 2, 3, 4]
members.push([].concat.apply([], newMembers));
}
}
}

Expand Down
13 changes: 0 additions & 13 deletions api/submission/submissionModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ const markAsRead = (ID, flag = true) => {
return db('Submissions').where({ ID }).update({ HasRead: flag });
};

/**
* This query marks the submission with the given ID as having HasRead, HasWritten, and HasDrawn set to false.
* @param {number} ID the ID of the submission to be marked as read
* @param {boolean} hasRead boolean to indicate whether db should set hasRead to true or false
* @param {boolean} hasDrawn boolean to indicate whether db should set hasDrawn to true or false
* @param {boolean} hasWritten boolean to indicate whether db should set hasWritten to true or false
* @returns {Promise} returns a promise that resolves to the count of fields updated
*/
const updateAll = (ID, hasRead, hasDrawn, hasWritten) => {
return db('Submissions').where({ ID }).update({ HasRead: hasRead, HasWritten: hasWritten, HasDrawn: hasDrawn });
}

/**
* This query is transactional, and runs a series of requests:
* - Updates the HasDrawn field for the relevant submission to be true
Expand Down Expand Up @@ -169,7 +157,6 @@ module.exports = {
getOrInitSubmission,
getAllSubmissionsByChild,
markAsRead,
updateAll,
submitDrawingTransaction,
submitWritingTransaction,
deleteWritingSubmission,
Expand Down
50 changes: 0 additions & 50 deletions api/submission/submissionRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,56 +358,6 @@ router.post('/draw/:id', authRequired, fileUpload, async (req, res) => {
);
});

/**
* @swagger
* /submit/update-all/{id}:
* put:
* summary: Attempts to mark the submission with the given ID as hasRead as 'false', hasWritten as 'false', hasDrawn as 'false'
* security:
* - okta: []
* tags:
* - Submissions
* parameters:
* - $ref: '#/components/parameters/submissionId'
* - in: formData
* name: hasRead
* type: boolean
* description: boolean to set users task hasRead to
* - in: formData
* name: hasDrawn
* type: boolean
* description: boolean to set users task hasDrawn to
* - in: formData
* name: hasWritten
* type: boolean
* description: boolean to set users task hasWritten to
* responses:
* 204:
* $ref: '#/components/responses/EmptySuccess'
* 400:
* description: Invalid request missing/invalid arguments
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* description: Required inputs hasRead, hasDrawn, and hasWritten must be of boolean type
* 401:
* $ref: '#/components/responses/UnauthorizedError'
* 404:
* $ref: '#/components/responses/NotFound'
* 500:
* $ref: '#/components/responses/DatabaseError'
*/
router.put('/update-all/:id', authRequired, validateUpdateAllTasksParams, async (req, res) => {
const { id } = req.params;
const { hasRead, hasDrawn, hasWritten } = req.body;

return crudOperationsManager.update(res, Submissions.updateAll, 'Submission', id, hasRead, hasDrawn, hasWritten);
})

/**
* @swagger
* /submission/write/{id}:
Expand Down