Skip to content

Commit

Permalink
Merge pull request #468 from Medable/bug/MIG-171
Browse files Browse the repository at this point in the history
MIG-171 - Fixed ECBuilderMap and script
  • Loading branch information
joejean authored Jun 6, 2023
2 parents f53e084 + ccef17d commit c6ac3e1
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 78 deletions.
45 changes: 39 additions & 6 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,23 +212,26 @@ describe('MIG-148 - Test StudyManifestTools ', () => {
},
ec__document_templates: {
find: () => ({
paths: () => ({
toArray: () => ([ecDocTemplate])
limit: () => ({
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":{"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"}]}]}}}}}]}}}}}}]
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"}}}}]}}}}}}]
mappings.forEach(({ path, mapTo }) => {
const [entity, entityKey, property, ...rest] = path.split('.'),
isDocPropUpdate = !!rest.length,
value = run(mapTo)
isDocPropUpdate = !!rest.length
let value = run(mapTo)
const prop = entity.startsWith('ec__') ? 'ec__key' : 'c_key'
Expand Down Expand Up @@ -259,6 +262,36 @@ 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: 5 additions & 0 deletions packages/mdctl-axon-tools/__tests__/MIG-154/MIG-154.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

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

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

beforeEach(() => {
/*
Resets the module registry - the cache of all required modules
Expand Down
42 changes: 14 additions & 28 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,8 +483,10 @@ const MenuConfigMap = require('../../lib/mappings/maps/MenuConfigMap'),
},
ec__document_templates: {
find: () => ({
paths: () => ({
toArray: () => ([ecDocTemplate])
limit: () => ({
paths: () => ({
toArray: () => ([ecDocTemplate])
})
})
})
}
Expand All @@ -493,6 +495,10 @@ 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 @@ -670,11 +676,8 @@ describe('MIG-126: econsentDocumentTemplateAdjustments', () => {
as: 'entry',
in: {
$object: {
data: '$$entry',
id: '$$entry.ec__key',
type: {
$literal: 'signature'
}
_id: '$$entry._id',
ec__key: '$$entry.ec__key'
}
}
}
Expand All @@ -684,11 +687,8 @@ describe('MIG-126: econsentDocumentTemplateAdjustments', () => {
as: 'entry',
in: {
$object: {
data: '$$entry',
id: '$$entry.ec__key',
type: {
$literal: 'knowledgeCheck'
}
_id: '$$entry._id',
ec__key: '$$entry.ec__key'
}
}
}
Expand All @@ -698,22 +698,8 @@ describe('MIG-126: econsentDocumentTemplateAdjustments', () => {
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'
}]
}]
}
_id: '$$entry._id',
ec__key: '$$entry.ec__key'
}
}
}
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 task or multiple consent templates', pipes: { ingest: 'ingestTransform.js' }
object: 'package', name: 'Consent export', version: '0.0.1', description: 'An export of consent template or multiple consent templates', pipes: { ingest: 'ingestTransform.js' }
}
],
])('should writePackage for: %s', (packageType, expected) => {
Expand Down
5 changes: 3 additions & 2 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),
mappingScript = await getEcMappingScript(org, consentIds),
allEntities = await this.getConsentManifestEntities(org, consentIds, orgReferenceProps),
{ manifest, removedEntities } = this.validateAndCreateManifest(allEntities, orgReferenceProps, ['ec__document_templates'])

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

}

module.exports = StudyManifestTools
module.exports = StudyManifestTools

39 changes: 35 additions & 4 deletions packages/mdctl-axon-tools/lib/mappings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ 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,
value = run(mapTo)
isDocPropUpdate = !!rest.length
let value = run(mapTo)
const prop = entity.startsWith('ec__') ? 'ec__key' : 'c_key'
Expand Down Expand Up @@ -41,6 +42,36 @@ 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 @@ -59,9 +90,9 @@ module.exports = {
return getScript(mappings)
},

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

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

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

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

async getEcBuilderDataMaps() {

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

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()
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()
}

ecTemplates = ecTemplates
.filter(template => template.ec__builder_data
Expand Down Expand Up @@ -54,11 +62,8 @@ module.exports = class EcBuilderDataMap {
as: 'entry',
in: {
$object: {
data: '$$entry',
id: '$$entry.ec__key',
type: {
$literal: 'signature'
}
ec__key: '$$entry.ec__key',
_id: '$$entry._id'
}
}
}
Expand All @@ -68,11 +73,8 @@ module.exports = class EcBuilderDataMap {
as: 'entry',
in: {
$object: {
data: '$$entry',
id: '$$entry.ec__key',
type: {
$literal: 'knowledgeCheck'
}
ec__key: '$$entry.ec__key',
_id: '$$entry._id'
}
}
}
Expand All @@ -82,22 +84,8 @@ module.exports = class EcBuilderDataMap {
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'
}]
}]
}
ec__key: '$$entry.ec__key',
_id: '$$entry._id'
}
}
}
Expand All @@ -112,9 +100,9 @@ module.exports = class EcBuilderDataMap {
return mapping
}

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

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) {
async getEcMappings(org, consentIds = []) {
const ecBuilderDataMap = new EcBuilderDataMap(org)

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

0 comments on commit c6ac3e1

Please sign in to comment.