Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "MIG-171 - Fixed ECBuilderMap and script" #471

Open
wants to merge 1 commit into
base: archive/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 6 additions & 39 deletions packages/mdctl-axon-tools/__tests__/MIG-148/MIG-148.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('MIG-148 - Test StudyManifestTools ', () => {
paths: () => ({
limit: () => ({
toArray: () => existingStudy
}),
})
})
})
},
Expand All @@ -212,26 +212,23 @@ describe('MIG-148 - Test StudyManifestTools ', () => {
},
ec__document_templates: {
find: () => ({
limit: () => ({
paths: () => ({
toArray: () => ([ecDocTemplate])
})
paths: () => ({
toArray: () => ([ecDocTemplate])
})
})
}
}
},
installAfterScript = `
import _ from 'lodash'
const { run } = require('expressions')

const mappings = [{"path":"ec__document_template.8c16e1ad-f3b4-41c8-a6fb-a68d1d28f188.ec__builder_data","mapTo":{"$let":{"vars":{"originalTemplate":{"$dbNext":{"object":"ec__document_template","operation":"cursor","where":{"ec__key":"8c16e1ad-f3b4-41c8-a6fb-a68d1d28f188"},"expand":["ec__knowledge_checks"],"passive":true}}},"in":{"$object":{"ck-widgets-data":{"$concatArrays":[{"$map":{"input":"$$originalTemplate.ec__requested_signatures","as":"entry","in":{"$object":{"ec__key":"$$entry.ec__key","_id":"$$entry._id"}}}},{"$map":{"input":"$$originalTemplate.ec__knowledge_checks.data","as":"entry","in":{"$object":{"ec__key":"$$entry.ec__key","_id":"$$entry._id"}}}},{"$map":{"input":"$$originalTemplate.ec__requested_data","as":"entry","in":{"$object":{"ec__key":"$$entry.ec__key","_id":"$$entry._id"}}}}]}}}}}}]
const mappings = [{"path":"ec__document_template.8c16e1ad-f3b4-41c8-a6fb-a68d1d28f188.ec__builder_data","mapTo":{"$let":{"vars":{"originalTemplate":{"$dbNext":{"object":"ec__document_template","operation":"cursor","where":{"ec__key":"8c16e1ad-f3b4-41c8-a6fb-a68d1d28f188"},"expand":["ec__knowledge_checks"],"passive":true}}},"in":{"$object":{"ck-widgets-data":{"$concatArrays":[{"$map":{"input":"$$originalTemplate.ec__requested_signatures","as":"entry","in":{"$object":{"data":"$$entry","id":"$$entry.ec__key","type":{"$literal":"signature"}}}}},{"$map":{"input":"$$originalTemplate.ec__knowledge_checks.data","as":"entry","in":{"$object":{"data":"$$entry","id":"$$entry.ec__key","type":{"$literal":"knowledgeCheck"}}}}},{"$map":{"input":"$$originalTemplate.ec__requested_data","as":"entry","in":{"$object":{"data":"$$entry","id":"$$entry.ec__key","type":{"$cond":["$$entry.ec__allow_multiple",{"$literal":"checkboxGroup"},{"$cond":[{"$eq":["$$entry.ec__allow_multiple",false]},{"$literal":"radioGroup"},{"$literal":"input"}]}]}}}}}]}}}}}}]

mappings.forEach(({ path, mapTo }) => {

const [entity, entityKey, property, ...rest] = path.split('.'),
isDocPropUpdate = !!rest.length
let value = run(mapTo)
isDocPropUpdate = !!rest.length,
value = run(mapTo)

const prop = entity.startsWith('ec__') ? 'ec__key' : 'c_key'

Expand Down Expand Up @@ -262,36 +259,6 @@ describe('MIG-148 - Test StudyManifestTools ', () => {

}

if (entity === 'ec__document_template' && prop === 'ec__key' && property === 'ec__builder_data') {

const idMapping = _.keyBy(value['ck-widgets-data'], 'ec__key')
const {
_id: template_id,
ec__builder_data: { "ck-widgets-data": originalBuilderData },
ec__status, creator, owner, updater
} = org.objects.ec__document_templates.find({ ec__key: entityKey })
.paths('ec__builder_data', 'ec__status', 'creator', 'owner', 'updater')
.next()

//We can update only draft templates
if (ec__status !== 'draft') {
return
}

//Map ids between builder_data and corresponding entities
let new_builder_data = originalBuilderData.map((obd) => {
const updatedId = _.get(idMapping, obd.id + '._id')
_.set(obd, 'data._id', updatedId)
_.get(obd, 'data.ec__document_template._id', false) && _.set(obd, 'data.ec__document_template._id', template_id)
_.get(obd, 'data.ec__document_template.path', false) && _.set(obd, 'data.ec__document_template.path', '/ec__document_templates/' + template_id)
_.get(obd, 'data.creator', false) && _.set(obd, 'data.creator', creator)
_.get(obd, 'data.owner', false) && _.set(obd, 'data.owner', owner)
_.get(obd, 'data.updater', false) && _.set(obd, 'data.updater', updater)
return obd
})
value = { "ck-widgets-data": new_builder_data }
}

//normal prop update
return org.objects[entity]
.updateOne({ [prop]: entityKey }, { $set: { [property]: value }})
Expand Down
5 changes: 0 additions & 5 deletions packages/mdctl-axon-tools/__tests__/MIG-154/MIG-154.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@

describe('MIG-154 - Check new methods', () => {

afterEach(() => {
jest.clearAllMocks()
})

beforeEach(() => {
/*
Resets the module registry - the cache of all required modules
Expand Down
42 changes: 28 additions & 14 deletions packages/mdctl-axon-tools/__tests__/MIG-63/MIG-63.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,8 @@ const MenuConfigMap = require('../../lib/mappings/maps/MenuConfigMap'),
},
ec__document_templates: {
find: () => ({
limit: () => ({
paths: () => ({
toArray: () => ([ecDocTemplate])
})
paths: () => ({
toArray: () => ([ecDocTemplate])
})
})
}
Expand All @@ -495,10 +493,6 @@ const MenuConfigMap = require('../../lib/mappings/maps/MenuConfigMap'),

describe('Export Mappings', () => {

afterEach(() => {
jest.clearAllMocks()
})

it('menuConfigMapping', async() => {
const menuConfigMapping = new MenuConfigMap(org),
mappings = await menuConfigMapping.getMappings()
Expand Down Expand Up @@ -676,8 +670,11 @@ describe('MIG-126: econsentDocumentTemplateAdjustments', () => {
as: 'entry',
in: {
$object: {
_id: '$$entry._id',
ec__key: '$$entry.ec__key'
data: '$$entry',
id: '$$entry.ec__key',
type: {
$literal: 'signature'
}
}
}
}
Expand All @@ -687,8 +684,11 @@ describe('MIG-126: econsentDocumentTemplateAdjustments', () => {
as: 'entry',
in: {
$object: {
_id: '$$entry._id',
ec__key: '$$entry.ec__key'
data: '$$entry',
id: '$$entry.ec__key',
type: {
$literal: 'knowledgeCheck'
}
}
}
}
Expand All @@ -698,8 +698,22 @@ describe('MIG-126: econsentDocumentTemplateAdjustments', () => {
as: 'entry',
in: {
$object: {
_id: '$$entry._id',
ec__key: '$$entry.ec__key'
data: '$$entry',
id: '$$entry.ec__key',
type: {
$cond: [
'$$entry.ec__allow_multiple', {
$literal: 'checkboxGroup'
}, {
$cond: [{
$eq: ['$$entry.ec__allow_multiple', false]
}, {
$literal: 'radioGroup'
}, {
$literal: 'input'
}]
}]
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('StudyManifestTools', () => {
[
'consent',
{
object: 'package', name: 'Consent export', version: '0.0.1', description: 'An export of consent template or multiple consent templates', pipes: { ingest: 'ingestTransform.js' }
object: 'package', name: 'Consent export', version: '0.0.1', description: 'An export of task or multiple consent templates', pipes: { ingest: 'ingestTransform.js' }
}
],
])('should writePackage for: %s', (packageType, expected) => {
Expand Down
5 changes: 2 additions & 3 deletions packages/mdctl-axon-tools/lib/StudyManifestTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class StudyManifestTools {

async buildConsentManifestAndDependencies(consentIds) {
const { org, orgReferenceProps } = await this.getOrgAndReferences(),
mappingScript = await getEcMappingScript(org, consentIds),
mappingScript = await getEcMappingScript(org),
allEntities = await this.getConsentManifestEntities(org, consentIds, orgReferenceProps),
{ manifest, removedEntities } = this.validateAndCreateManifest(allEntities, orgReferenceProps, ['ec__document_templates'])

Expand Down Expand Up @@ -927,5 +927,4 @@ class StudyManifestTools {

}

module.exports = StudyManifestTools

module.exports = StudyManifestTools
39 changes: 4 additions & 35 deletions packages/mdctl-axon-tools/lib/mappings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ const { getMappings, getEcMappings } = require('./maps')

function getScript(mappings) {
return `
import _ from 'lodash'
const { run } = require('expressions')

const mappings = ${JSON.stringify(mappings)}

mappings.forEach(({ path, mapTo }) => {

const [entity, entityKey, property, ...rest] = path.split('.'),
isDocPropUpdate = !!rest.length
let value = run(mapTo)
isDocPropUpdate = !!rest.length,
value = run(mapTo)

const prop = entity.startsWith('ec__') ? 'ec__key' : 'c_key'

Expand Down Expand Up @@ -42,36 +41,6 @@ function getScript(mappings) {

}

if (entity === 'ec__document_template' && prop === 'ec__key' && property === 'ec__builder_data') {

const idMapping = _.keyBy(value['ck-widgets-data'], 'ec__key')
const {
_id: template_id,
ec__builder_data: { "ck-widgets-data": originalBuilderData },
ec__status, creator, owner, updater
} = org.objects.ec__document_templates.find({ ec__key: entityKey })
.paths('ec__builder_data', 'ec__status', 'creator', 'owner', 'updater')
.next()

//We can update only draft templates
if (ec__status !== 'draft') {
return
}

//Map ids between builder_data and corresponding entities
let new_builder_data = originalBuilderData.map((obd) => {
const updatedId = _.get(idMapping, obd.id + '._id')
_.set(obd, 'data._id', updatedId)
_.get(obd, 'data.ec__document_template._id', false) && _.set(obd, 'data.ec__document_template._id', template_id)
_.get(obd, 'data.ec__document_template.path', false) && _.set(obd, 'data.ec__document_template.path', '/ec__document_templates/' + template_id)
_.get(obd, 'data.creator', false) && _.set(obd, 'data.creator', creator)
_.get(obd, 'data.owner', false) && _.set(obd, 'data.owner', owner)
_.get(obd, 'data.updater', false) && _.set(obd, 'data.updater', updater)
return obd
})
value = { "ck-widgets-data": new_builder_data }
}

//normal prop update
return org.objects[entity]
.updateOne({ [prop]: entityKey }, { $set: { [property]: value }})
Expand All @@ -90,9 +59,9 @@ module.exports = {
return getScript(mappings)
},

async getEcMappingScript(org, consentIds = []) {
async getEcMappingScript(org) {

const mappings = await getEcMappings(org, consentIds)
const mappings = await getEcMappings(org)

if (mappings.length === 0) return ''

Expand Down
58 changes: 35 additions & 23 deletions packages/mdctl-axon-tools/lib/mappings/maps/EcBuilderDataMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@ module.exports = class EcBuilderDataMap {
this.org = org
}

async getEcBuilderDataMaps(consentIds = []) {
async getEcBuilderDataMaps() {

const mapping = []
let ecTemplates
if (consentIds && consentIds.length) {
ecTemplates = await this.org.objects.ec__document_templates
.find({ _id: { $in: consentIds } })
.limit(false)
.paths('ec__key', 'ec__builder_data', 'ec__requested_data', 'ec__requested_signatures', 'ec__knowledge_checks')
.toArray()
} else {
ecTemplates = await this.org.objects.ec__document_templates
.find()
.limit(false)
.paths('ec__key', 'ec__builder_data', 'ec__requested_data', 'ec__requested_signatures', 'ec__knowledge_checks')
.toArray()
}

let ecTemplates = await this.org.objects.ec__document_templates
.find({ ec__status: 'draft' })
.paths('ec__key', 'ec__builder_data', 'ec__requested_data', 'ec__requested_signatures', 'ec__knowledge_checks')
.toArray()

ecTemplates = ecTemplates
.filter(template => template.ec__builder_data
Expand Down Expand Up @@ -62,8 +54,11 @@ module.exports = class EcBuilderDataMap {
as: 'entry',
in: {
$object: {
ec__key: '$$entry.ec__key',
_id: '$$entry._id'
data: '$$entry',
id: '$$entry.ec__key',
type: {
$literal: 'signature'
}
}
}
}
Expand All @@ -73,8 +68,11 @@ module.exports = class EcBuilderDataMap {
as: 'entry',
in: {
$object: {
ec__key: '$$entry.ec__key',
_id: '$$entry._id'
data: '$$entry',
id: '$$entry.ec__key',
type: {
$literal: 'knowledgeCheck'
}
}
}
}
Expand All @@ -84,8 +82,22 @@ module.exports = class EcBuilderDataMap {
as: 'entry',
in: {
$object: {
ec__key: '$$entry.ec__key',
_id: '$$entry._id'
data: '$$entry',
id: '$$entry.ec__key',
type: {
$cond: [
'$$entry.ec__allow_multiple', {
$literal: 'checkboxGroup'
}, {
$cond: [{
$eq: ['$$entry.ec__allow_multiple', false]
}, {
$literal: 'radioGroup'
}, {
$literal: 'input'
}]
}]
}
}
}
}
Expand All @@ -100,9 +112,9 @@ module.exports = class EcBuilderDataMap {
return mapping
}

async getMappings(consentIds = []) {
async getMappings() {
return [
...await this.getEcBuilderDataMaps(consentIds)
...await this.getEcBuilderDataMaps()
]
}

Expand Down
4 changes: 2 additions & 2 deletions packages/mdctl-axon-tools/lib/mappings/maps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ module.exports = {
]
},

async getEcMappings(org, consentIds = []) {
async getEcMappings(org) {
const ecBuilderDataMap = new EcBuilderDataMap(org)

return [
...await ecBuilderDataMap.getMappings(consentIds)
...await ecBuilderDataMap.getMappings()
]
}
}