MCPcopy Create free account
hub / github.com/adaptlearning/adapt_framework / checkIds

Method checkIds

grunt/helpers/data/Language.js:181–237  ·  view source on GitHub ↗

@returns {Language}

()

Source from the content-addressed store, hash-verified

179
180 /** @returns {Language} */
181 checkIds() {
182 const items = this.getAllFileItems().map(({ item }) => item);
183 // Index and group
184 const idIndex = _.keyBy(items, '_id');
185 const idGroups = _.groupBy(items, '_id');
186 const parentIdGroups = _.groupBy(items, '_parentId');
187 // Setup error collection arrays
188 let orphanedIds = {};
189 let emptyIds = {};
190 let duplicateIds = {};
191 let missingIds = {};
192 items.forEach((o) => {
193 const isCourseType = (o._type === 'course');
194 const isComponentType = (o._type === 'component');
195 if (idGroups[o._id].length > 1) {
196 duplicateIds[o._id] = true; // Id has more than one item
197 }
198 if (!isComponentType && !parentIdGroups[o._id]) {
199 emptyIds[o._id] = true; // Course has no children
200 }
201 if (!isCourseType && (!o._parentId || !idIndex[o._parentId])) {
202 orphanedIds[o._id] = o._type; // Item has no defined parent id or the parent id doesn't exist
203 }
204 if (!isCourseType && o._parentId && !idIndex[o._parentId]) {
205 missingIds[o._parentId] = o._type; // Referenced parent item does not exist
206 }
207 });
208 const orphanedIdsArray = Object.keys(orphanedIds);
209 const missingIdsArray = Object.keys(missingIds);
210 emptyIds = Object.keys(emptyIds);
211 duplicateIds = Object.keys(duplicateIds);
212 // Output for each type of error
213 const hasErrored = orphanedIdsArray.length || emptyIds.length || duplicateIds.length || missingIdsArray.length;
214 if (orphanedIdsArray.length) {
215 const orphanedIdString = orphanedIdsArray.map((id) => `${id} (${orphanedIds[id]})`);
216 this.log(chalk.yellow(`Orphaned _ids: ${orphanedIdString.join(', ')}`));
217 }
218 if (missingIdsArray.length) {
219 const missingIdString = missingIdsArray.map((id) => `${id} (${missingIds[id]})`);
220 this.log(chalk.yellow(`Missing _ids: ${missingIdString.join(', ')}`));
221 }
222 if (emptyIds.length) {
223 this.log(chalk.yellow(`Empty _ids: ${emptyIds}`));
224 }
225 if (duplicateIds.length) {
226 this.log(chalk.yellow(`Duplicate _ids: ${duplicateIds}`));
227 }
228 // If any error has occured, stop processing.
229 if (hasErrored) {
230 const err = new Error('Oops, looks like you have some json errors.');
231 err.number = 10011;
232 throw err;
233 }
234 this.log(`No issues found in course/${this.name}, your JSON is a-ok!`);
235
236 return this;
237 }
238

Callers

nothing calls this directly

Calls 1

getAllFileItemsMethod · 0.95

Tested by

no test coverage detected