From 813cdab70a96e32e572d4bc472f839c8c5d4c6b1 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 31 Mar 2021 17:32:31 -0400 Subject: [PATCH 1/5] fix: fixed break on empty cohort --- api/mod/modHelpers/clusterGeneration.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/api/mod/modHelpers/clusterGeneration.js b/api/mod/modHelpers/clusterGeneration.js index ddd3cff..bb53af3 100644 --- a/api/mod/modHelpers/clusterGeneration.js +++ b/api/mod/modHelpers/clusterGeneration.js @@ -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); @@ -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)); + } } } From b8b0a8e6229e64b18d18cd8ee76f5a1e0a5295aa Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 31 Mar 2021 19:44:21 -0400 Subject: [PATCH 2/5] chore: fleshed out set-all tasks to add and remove submissions accordingly --- api/submission/submissionModel.js | 42 +++++++++++++++++++++++++++--- api/submission/submissionRouter.js | 1 - 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/api/submission/submissionModel.js b/api/submission/submissionModel.js index 191ef4e..21331f5 100644 --- a/api/submission/submissionModel.js +++ b/api/submission/submissionModel.js @@ -1,3 +1,5 @@ +const faker = require('faker'); + const db = require('../../data/db-config'); const _omit = require('lodash.omit'); @@ -60,7 +62,7 @@ const markAsRead = (ID, flag = true) => { }; /** - * This query marks the submission with the given ID as having HasRead, HasWritten, and HasDrawn set to false. + * 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 @@ -68,9 +70,41 @@ const markAsRead = (ID, flag = true) => { * @returns {Promise} returns a promise that resolves to the count of fields updated */ const updateAll = async (ID, hasRead, hasDrawn, hasWritten) => { - res = await db('Submissions').where({ ID }).update({ HasRead: hasRead, HasWritten: hasWritten, HasDrawn: hasDrawn }); - console.log('res: ', res); - return res; + /** + * 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){ + const newDraw = 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); + } + }); + } /** diff --git a/api/submission/submissionRouter.js b/api/submission/submissionRouter.js index 227db49..220acd6 100644 --- a/api/submission/submissionRouter.js +++ b/api/submission/submissionRouter.js @@ -404,7 +404,6 @@ router.post('/draw/:id', authRequired, fileUpload, async (req, res) => { router.put('/update-all/:id', authRequired, validateUpdateAllTasksParams, async (req, res) => { const { id } = req.params; const { hasRead, hasDrawn, hasWritten } = req.body; - console.log('tasks: ', hasRead, hasDrawn, hasWritten); return crudOperationsManager.update(res, Submissions.updateAll, 'Submission', id, hasRead, hasDrawn, hasWritten); }) From a5a20a288116806efb19478f06164e673158b8f9 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 31 Mar 2021 20:03:59 -0400 Subject: [PATCH 3/5] ref: migrated relevant files over to dev router, integrated into app.js --- api/app.js | 2 ++ api/dev/devModeModel.js | 52 +++++++++++++++++++++++++++ api/dev/devModeRouter.js | 58 ++++++++++++++++++++++++++++++ api/submission/submissionModel.js | 49 ------------------------- api/submission/submissionRouter.js | 50 -------------------------- 5 files changed, 112 insertions(+), 99 deletions(-) create mode 100644 api/dev/devModeModel.js create mode 100644 api/dev/devModeRouter.js diff --git a/api/app.js b/api/app.js index 1a29250..a8d14a0 100644 --- a/api/app.js +++ b/api/app.js @@ -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(); @@ -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) { diff --git a/api/dev/devModeModel.js b/api/dev/devModeModel.js new file mode 100644 index 0000000..a3fc06f --- /dev/null +++ b/api/dev/devModeModel.js @@ -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){ + const newDraw = 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, + } \ No newline at end of file diff --git a/api/dev/devModeRouter.js b/api/dev/devModeRouter.js new file mode 100644 index 0000000..f961efb --- /dev/null +++ b/api/dev/devModeRouter.js @@ -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; \ No newline at end of file diff --git a/api/submission/submissionModel.js b/api/submission/submissionModel.js index 21331f5..91d718e 100644 --- a/api/submission/submissionModel.js +++ b/api/submission/submissionModel.js @@ -1,5 +1,3 @@ -const faker = require('faker'); - const db = require('../../data/db-config'); const _omit = require('lodash.omit'); @@ -61,52 +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, 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){ - const newDraw = 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); - } - }); - -} - /** * This query is transactional, and runs a series of requests: * - Updates the HasDrawn field for the relevant submission to be true @@ -205,7 +157,6 @@ module.exports = { getOrInitSubmission, getAllSubmissionsByChild, markAsRead, - updateAll, submitDrawingTransaction, submitWritingTransaction, deleteWritingSubmission, diff --git a/api/submission/submissionRouter.js b/api/submission/submissionRouter.js index 220acd6..de1f813 100644 --- a/api/submission/submissionRouter.js +++ b/api/submission/submissionRouter.js @@ -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}: From 881f753fa09a222387f3b3052119d5e523a2e093 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 31 Mar 2021 23:43:22 -0400 Subject: [PATCH 4/5] fix: fixed a non-used variable --- api/dev/devModeModel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/dev/devModeModel.js b/api/dev/devModeModel.js index a3fc06f..6c64ff0 100644 --- a/api/dev/devModeModel.js +++ b/api/dev/devModeModel.js @@ -21,7 +21,7 @@ const db = require('../../data/db-config'); const preExistingUserDrawing = await db('Drawing').where({ SubmissionID: ID }); if(hasDrawn){ if(preExistingUserDrawing.length < 1){ - const newDraw = await trx('Drawing').insert({ URL: faker.image.imageUrl(), SubmissionID: ID }); + await trx('Drawing').insert({ URL: faker.image.imageUrl(), SubmissionID: ID }); } } else { if(preExistingUserDrawing.length > 0){ From 3a9fb106e7b1deb28274fd17c41ab8aaf1f447e2 Mon Sep 17 00:00:00 2001 From: Trevor Martin Date: Thu, 8 Apr 2021 18:32:58 -0400 Subject: [PATCH 5/5] Update devModeModel.js removed faker --- api/dev/devModeModel.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/api/dev/devModeModel.js b/api/dev/devModeModel.js index 6c64ff0..2d5369a 100644 --- a/api/dev/devModeModel.js +++ b/api/dev/devModeModel.js @@ -1,4 +1,3 @@ -const faker = require('faker'); const db = require('../../data/db-config'); /** @@ -21,7 +20,7 @@ const db = require('../../data/db-config'); const preExistingUserDrawing = await db('Drawing').where({ SubmissionID: ID }); if(hasDrawn){ if(preExistingUserDrawing.length < 1){ - await trx('Drawing').insert({ URL: faker.image.imageUrl(), SubmissionID: ID }); + await trx('Drawing').insert({ URL: "https://placekitten.com/200/300", SubmissionID: ID }); } } else { if(preExistingUserDrawing.length > 0){ @@ -32,7 +31,7 @@ const db = require('../../data/db-config'); 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 }); + await trx('Writing').insert({ URL: "https://placekitten.com/200/300", PageNum: 1, SubmissionID: ID }); } } else { if(preExistingUserWriting.length > 0) { @@ -49,4 +48,4 @@ const db = require('../../data/db-config'); module.exports = { updateAll, - } \ No newline at end of file + }