Skip to content

Commit

Permalink
Issue #000 fix: copy textbook from platform
Browse files Browse the repository at this point in the history
  • Loading branch information
venkateshwarans committed Mar 25, 2020
1 parent 77f7cd1 commit 5c93668
Show file tree
Hide file tree
Showing 3 changed files with 589 additions and 400 deletions.
106 changes: 61 additions & 45 deletions src/helpers/updateHierarchy.helper.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
const { forkJoin } = require('rxjs');
const {
forkJoin
} = require('rxjs');
const _ = require("lodash");
const envVariables = require('../envVariables');
const axios = require('axios');
const headers = {
'Content-Type': 'application/json'
}
const dockHeader = {
'Content-Type': 'application/json'
}


class HierarchyService {
programCreated() {
filterExistingTextbooks(selectedTextbooks);
}


filterExistingTextbooks(collectionIds) {

filterExistingTextbooks(collectionIds, reqHeaders) {
const url = `${envVariables.baseURL}/api/composite/v1/search`;
const filterRequest = _.map(collectionIds, id => {
const option = {
url: url,
method: 'post',
'headers': dockHeader,
'headers': reqHeaders,
data: {
'request': {
'filters': {
Expand All @@ -45,61 +39,71 @@ class HierarchyService {

}

bulkUpdateHierarchy(collections) {
bulkUpdateHierarchy(collections, reqHeaders) {
const url = `${envVariables.baseURL}/action/content/v3/hierarchy/update`;

const bulkRequest = _.map(collections, collection => {
const createdBy = this.getCreatedBy(collection);
const option = {
url,
method: 'patch',
headers: _.merge({}, dockHeader, {'x-authenticated-userid': createdBy}),
headers: _.merge({}, reqHeaders, {
'x-authenticated-userid': createdBy
}),
data: {
request: {data: collection}
request: {
data: collection
}
}
};
console.log(option)
return axios(option);
});

return forkJoin(...bulkRequest);
}


getCreatedBy(collection) {
const nodesModified = _.get(collection, 'nodesModified');
const rootNode = _.findKey(nodesModified, item => {
const rootNode = _.findKey(nodesModified, item => {
return item.root === true;
})
})
return nodesModified[rootNode].metadata.createdBy || null;
}
createCollection(collections) {
const url = `${envVariables.baseURL}/action/content/v3/create`

const bulkRequest = _.map(collections, collection => {
const option = {
url,
method: 'post',
headers: dockHeader,
data: {
request: {
content: {..._.omit(collection.result.content, ['children', 'identifier', 'status', 'reservedDialcodes', 'license', 'framework', 'subject', 'medium', 'gradeLevel', 'board'])}
createCollection(collections, reqHeaders) {
const url = `${envVariables.baseURL}/action/content/v3/create`

const bulkRequest = _.map(collections, collection => {
const option = {
url,
method: 'post',
headers: reqHeaders,
data: {
request: {
content: {
..._.omit(collection.result.content, ['children', 'identifier', 'status', 'reservedDialcodes', 'dialcodes', 'license', 'framework', 'subject', 'medium', 'gradeLevel', 'board', 'sYS_INTERNAL_LAST_UPDATED_ON', 'contentCredits', 'consumerId', 'osId', 'qrCodeProcessId', 'idealScreenSize', 'contentDisposition', 'os', 'idealScreenDensity', 'depth',])
}
},
params: {
identifier: collection.result.content.identifier
}
};
return axios(option);
});
},
params: {
identifier: collection.result.content.identifier
}
};
console.log(option)
return axios(option);
});
return forkJoin(...bulkRequest);
}

getHierarchy(collectionIds) {
getHierarchy(collectionIds, reqHeaders) {

const collectiveRequest = _.map(collectionIds, id => {
const url = `${envVariables.SUNBIRD_URL}/action/content/v3/hierarchy/${id}?mode=edit`;
const option = {
url: url,
method: 'get'
method: 'get',
headers: reqHeaders
};
return axios(option)
});
Expand Down Expand Up @@ -137,12 +141,16 @@ class HierarchyService {
};
}

getHierarchyUpdateRequest(collection, additionalMetaData) {
getHierarchyUpdateRequest(collection, additionalMetaData) {
let instance = this;
this.hierarchy = {}
this.nodeModified = {}
const response = collection.hierarchy;
additionalMetaData = {...collection.creationResult.result, ...additionalMetaData, 'isFirstTime': true}
const response = collection.originHierarchy;
additionalMetaData = {
...collection.creationResult.result,
...additionalMetaData,
'isFirstTime': true
}
return {
nodesModified: instance.getFlatNodesModified(response.content, additionalMetaData),
hierarchy: instance.getFlatHierarchyObj(response.content, additionalMetaData),
Expand All @@ -153,15 +161,18 @@ class HierarchyService {
getFlatHierarchyObj(data, additionalMetaData) {
let instance = this;
if (data) {
if(additionalMetaData.isFirstTime && data.contentType === 'TextBook') {
if (additionalMetaData.isFirstTime && data.contentType === 'TextBook') {
data.identifier = additionalMetaData.identifier
}
instance.hierarchy[data.identifier] = {
'name': data.name,
'contentType': data.contentType,
'children': _.map(data.children, function (child) {
return child.identifier;
}),
'children': _.compact(_.map(data.children, function (child) {
if(child.mimeType === 'application/vnd.ekstep.content-collection'){
console.log(child.mimeType, child.identifier)
return child.identifier;
}
})),
'root': (data.contentType === 'TextBook') ? true : false
};
}
Expand All @@ -177,18 +188,23 @@ class HierarchyService {
getFlatNodesModified(data, additionalMetaData) {
let instance = this;
if (data) {
if(additionalMetaData.isFirstTime && data.contentType === 'TextBook') {
if (additionalMetaData.isFirstTime && data.contentType === 'TextBook') {
data.identifier = additionalMetaData.identifier
}
instance.nodeModified[data.identifier] = {
'isNew': true,
'root': (data.contentType === 'TextBook') ? true : false,
'metadata': {
..._.omit(data, ['children', 'identifier']),
...(data.contentType === 'TextBook' && {
..._.omit(data, ['children', 'identifier', 'framework', 'subject', 'medium', 'gradeLevel', 'board']),
'closedProgram': []
}),
...(data.contentType === 'TextBookUnit' && {
..._.omit(data, ['children', 'identifier'])
}),
'programId': additionalMetaData.programId,
'allowedContentTypes': additionalMetaData.allowedContentTypes,
'openForContribution': true,
...(data.contentType === 'Textbook' && { 'closedProgram': [] }),
'channel': additionalMetaData.channel,
'origin': data.identifier,
'originData': {
Expand Down
16 changes: 16 additions & 0 deletions src/service/messageUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,22 @@ exports.PROGRAM = {
MISSING_MESSAGE: 'Required fields like program_id, collections, allow_content_types, channel to copy textbook are missing',
FAILED_CODE: 'ERR_COPY_COLLECTIONS_FAILED',
FAILED_MESSAGE: 'Unable to copy the collection'
},
SEARCH_DOCK_COLLECTION: {
FAILED_CODE: 'ERR_SEARCHING_DOCK_FOR_COLLECTION',
FAILED_MESSAGE: 'Unable to search collections'
},
GET_HIERARCHY: {
FAILED_CODE: 'ERR_FETCHING_HIERARCHY_FOR_COLLECTION',
FAILED_MESSAGE: 'Unable to fetch hierarchy for collections'
},
BULK_UPDATE_HIERARCHY: {
FAILED_CODE: 'ERR_UPDATING_HIERARCHY',
FAILED_MESSAGE: 'Unable to update hierarchy for collections'
},
CREATE_COLLECTION: {
FAILED_CODE: 'ERR_CREATING_COLLECTION',
FAILED_MESSAGE: 'Unable to create collection'
}
}
}
Expand Down
Loading

0 comments on commit 5c93668

Please sign in to comment.