-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
73 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,18 @@ | ||
const pMap = require("p-map"); | ||
|
||
/** | ||
* TODO: | ||
* This is pretty inefficient. Look into either simple or | ||
* more complex optimizations | ||
* Since it's a private field maybe we can just check if | ||
* it's present and only run this if it is present? | ||
*/ | ||
const addInternalLabel = async (sublesson) => { | ||
const populatedSublesson = await strapi.db | ||
.query("api::sublesson.sublesson") | ||
.findOne({ | ||
where: { | ||
id: sublesson.id, | ||
}, | ||
populate: ["lesson"], | ||
}); | ||
|
||
sublesson.internalLabel = populatedSublesson.lesson | ||
? `${populatedSublesson.lesson.name} - ${sublesson.name}` | ||
: `Unassigned - ${sublesson.name}`; | ||
}; | ||
const { handleInternalLabel } = require("../../../../utils/general"); | ||
|
||
module.exports = { | ||
async afterFindOne(event) { | ||
console.log("hey does this happen &*&*&>>????"); | ||
addInternalLabel(event.data); | ||
await handleInternalLabel({ | ||
sublesson: event.result, | ||
}); | ||
}, | ||
|
||
async afterFindMany(event) { | ||
console.log("hey does this happen &*&*&!!"); | ||
// await pMap(event.results, addInternalLabel, { concurrency: 20 }); | ||
await pMap(event.result, addInternalLabel, { concurrency: 20 }); | ||
// event.result.forEach(addInternalLabel); | ||
await pMap( | ||
event.result, | ||
(sublesson) => handleInternalLabel({ sublesson, populateLesson: true }), | ||
{ concurrency: 20 } | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* TODO: | ||
* This is pretty inefficient. Look into either simple or | ||
* more complex optimizations | ||
* Since it's a private field maybe we can just check if | ||
* it's present and only run this if it is present? | ||
*/ | ||
const handleInternalLabel = async ({ sublesson, populateLesson = false }) => { | ||
if (!sublesson) { | ||
return; | ||
} else if (sublesson.internalLabel === undefined) { | ||
/** | ||
* since internalLabel is a private field, we don't | ||
* need to populate it on public requests | ||
*/ | ||
return; | ||
} else if (typeof sublesson.internalLabel === "string") { | ||
/** | ||
* Avoid unnecessarily recalculating the internalLabel | ||
*/ | ||
return; | ||
} | ||
|
||
let { lesson, name: sublessonName } = sublesson; | ||
|
||
if (populateLesson) { | ||
const populatedSublesson = await strapi.db | ||
.query("api::sublesson.sublesson") | ||
.findOne({ | ||
where: { | ||
id: sublesson.id, | ||
}, | ||
populate: ["lesson"], | ||
}); | ||
lesson = populatedSublesson.lesson; | ||
} | ||
|
||
sublesson.internalLabel = lesson | ||
? `${lesson.name} - ${sublessonName}` | ||
: `Unassigned - ${sublessonName}`; | ||
}; | ||
|
||
module.exports = { | ||
handleInternalLabel, | ||
}; |