Skip to content

Commit

Permalink
Fix bug on internallabel
Browse files Browse the repository at this point in the history
See strapi/strapi#13216 for more details
  • Loading branch information
oriooctopus committed May 2, 2022
1 parent a4d1ba8 commit 49fe987
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 55 deletions.
42 changes: 15 additions & 27 deletions src/api/code-challenge/content-types/code-challenge/lifecycles.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
const getInternalLabel = async (event) => {
const {
data: { challengeMeta, name },
} = event.params;

const defaultName = `Unassigned - ${name}`;
const challengeMetaId = challengeMeta ? challengeMeta.id : undefined;

if (!challengeMetaId) {
return defaultName;
}

const { lesson } = await strapi.db.query("challenge.challenge-meta").findOne({
where: {
id: challengeMetaId,
},
populate: ["challengeMeta", "lesson"],
});

if (!lesson) {
return defaultName;
}

return `${lesson.name} -- name`;
};
const { handleInternalLabel } = require("../../../../utils/general");

// runMetaTestsNeed more specific name later, maybe use "validateInternalTests"
async function runMetaTests(eventTests, eventMetaTests) {
Expand Down Expand Up @@ -61,8 +37,6 @@ async function getMetaTests(eventMetaTests) {
}

const beforeCreateOrUpdate = async (event) => {
const internalLabel = await getInternalLabel(event);
// event.params.data.internalLabel = internalLabel;
runMetaTests(event.params.data.tests, event.params.data.MetaTest);
};

Expand All @@ -74,4 +48,18 @@ module.exports = {
async beforeUpdate(event) {
await beforeCreateOrUpdate(event);
},

async afterFindOne(event) {
const challengeMeta = event.result.challengeMeta;
if (challengeMeta) {
/**
* This is a temporary hack u
* https://github.com/strapi/strapi/issues/13216 gets resolved
*/
await handleInternalLabel({
sublesson: challengeMeta.sublesson,
populateLesson: true,
});
}
},
};
37 changes: 9 additions & 28 deletions src/api/sublesson/content-types/sublesson/lifecycles.js
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 }
);
},
};
45 changes: 45 additions & 0 deletions src/utils/general.js
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,
};

0 comments on commit 49fe987

Please sign in to comment.