-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
764 additions
and
3,318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,4 @@ scratch | |
# nix | ||
.direnv | ||
result | ||
packages/mdctl-axon-tools/__tests__/MIG-166/env/i18ns/authTasks/ |
122 changes: 122 additions & 0 deletions
122
packages/mdctl-axon-tools/__tests__/MIG-166/MIG-166.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
/* eslint-disable import/order */ | ||
|
||
const { expect } = require('chai'), | ||
path = require('path'), | ||
fs = require('fs'), | ||
StudyDataTranslations = require('../../lib/StudyDataTranslations'), | ||
mockTasks = [ | ||
{ | ||
c_key: 'taskKey', | ||
_id: 'taskId', | ||
object: 'c_task', | ||
c_steps: { | ||
object: 'list', | ||
data: [ | ||
{ | ||
_id: 'stepId', | ||
c_parent_step: { | ||
_id: 'parentStepId', | ||
object: 'c_step', | ||
path: '/c_steps/parentStepId' | ||
}, | ||
c_key: 'stepKey', | ||
c_account_map: 'name.first', | ||
object: 'c_step' | ||
} | ||
] | ||
} | ||
} | ||
], | ||
org = { | ||
objects: { | ||
c_tasks: { | ||
find: () => ({ | ||
limit: () => ({ | ||
paths: () => ({ | ||
toArray: () => mockTasks | ||
}) | ||
}) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
describe('MIG-166', () => { | ||
let studyTranslations | ||
|
||
beforeAll(() => { | ||
studyTranslations = new StudyDataTranslations({}) | ||
}) | ||
|
||
it('can read the new manifest option "authenticationTaskTranslations".', () => { | ||
const studyDataTranslations = studyTranslations.isAuthTaskTranslations({ | ||
manifest: { | ||
object: 'manifest', | ||
i18ns: { | ||
includes: ['*'] | ||
}, | ||
authenticationTaskTranslations: true | ||
} | ||
}) | ||
|
||
// eslint-disable-next-line no-unused-expressions | ||
expect(studyDataTranslations) | ||
.to.be.true | ||
}) | ||
|
||
it('should throw an error if the manifest file is missing "i18ns".', () => { | ||
let err | ||
try { | ||
studyTranslations.isAuthTaskTranslations({ | ||
manifest: { | ||
object: 'manifest', | ||
authenticationTaskTranslations: true | ||
} | ||
}) | ||
} catch (error) { | ||
err = error | ||
} | ||
|
||
expect(err.errCode) | ||
.to.equal('mdctl.kInvalidArgument.missingI18nObjects') | ||
}) | ||
|
||
it('should write the required i18n files.', async() => { | ||
jest.spyOn(StudyDataTranslations.prototype, 'getOrg').mockImplementation(() => org) | ||
|
||
await studyTranslations.writeAuthTaskTranslationsToDisk( | ||
{ input: __dirname } | ||
) | ||
|
||
const output = path.join(__dirname, './env/i18ns/authTasks/axon_data_af-ZA.json'), | ||
data = JSON.parse(fs.readFileSync(output)) | ||
|
||
expect(fs.existsSync(output)) | ||
.to.be.true | ||
|
||
expect(data) | ||
.to.deep.equal({ | ||
locale: 'af_ZA', | ||
name: 'axon__af_ZA_authTasks', | ||
namespace: 'axon', | ||
object: 'i18n', | ||
tags: [ | ||
'authTasks' | ||
], | ||
weight: 0, | ||
data: { | ||
c_task: { | ||
taskKey: { | ||
c_name: 'Waarmerking' | ||
} | ||
}, | ||
c_step: { | ||
stepKey: { | ||
c_text: 'Voornaam' | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
}) |
11 changes: 11 additions & 0 deletions
11
packages/mdctl-axon-tools/__tests__/MIG-166/i18ns/test/af-ZA.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"task": "Waarmerking", | ||
"password": "Wagwoord", | ||
"parent": "Waarmerkingsvorm-stap", | ||
"email": "E-pos", | ||
"name.first": "Voornaam", | ||
"name.last": "Van", | ||
"mobile": "Telefoonnommer", | ||
"username": "Gebruikersnaam", | ||
"dob": "Geboortedatum" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
const StudyManifestTools = require('./lib/StudyManifestTools') | ||
const StudyDataTranslations = require('./lib/StudyDataTranslations') | ||
|
||
module.exports = { | ||
StudyManifestTools | ||
StudyManifestTools, | ||
StudyDataTranslations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* eslint-disable camelcase */ | ||
const globby = require('globby'), | ||
path = require('path'), | ||
fs = require('fs'), | ||
_ = require('lodash'), | ||
{ privatesAccessor } = require('@medable/mdctl-core-utils/privates'), | ||
{ Driver } = require('@medable/mdctl-api-driver'), | ||
{ Org } = require('@medable/mdctl-api-driver/lib/cortex.object'), | ||
{ Fault } = require('@medable/mdctl-core') | ||
|
||
class StudyDataTranslations { | ||
|
||
constructor(client, options = {}) { | ||
Object.assign(privatesAccessor(this), { | ||
client, | ||
options | ||
}) | ||
} | ||
|
||
getOrg() { | ||
const { client } = privatesAccessor(this), | ||
driver = new Driver(client), | ||
org = new Org(driver) | ||
|
||
return org | ||
} | ||
|
||
isAuthTaskTranslations({ input = process.cwd(), manifest }) { | ||
let manifestData = manifest | ||
if (!manifestData) { | ||
const location = globby.sync(['manifest.{json,yaml}'], { cwd: input }) | ||
if (location.length > 0 && fs.existsSync(`${input}/${location[0]}`)) { | ||
manifestData = JSON.parse(fs.readFileSync(`${input}/${location[0]}`)) | ||
} | ||
} | ||
|
||
if (manifestData.authenticationTaskTranslations && !_.has(manifestData, 'i18ns')) { | ||
throw Fault.create('mdctl.kInvalidArgument.missingI18nObjects', { | ||
message: 'The manifest is missing "i18ns" objects', | ||
reason: 'Incase of using "authenticationTaskTranslations" option. The manifest file must include "i18ns" objects.' | ||
}) | ||
} | ||
|
||
return manifestData.authenticationTaskTranslations | ||
} | ||
|
||
async writeAuthTaskTranslationsToDisk({ input = process.cwd(), format = 'json' }) { | ||
const location = globby.sync(['i18ns/**/*.{json,yaml}'], { cwd: input }), | ||
tasks = await this.readAuthenticationTasks(), | ||
keys = _.concat( | ||
tasks.map(({ c_key, object, _id }) => ({ c_key, object, _id })), | ||
_.flatMap( | ||
tasks.filter(({ c_steps }) => Boolean(c_steps)), | ||
({ c_steps: { data } }) => data | ||
) | ||
), | ||
parentSteps = keys.filter(({ c_parent_step }) => c_parent_step) | ||
.map(({ c_parent_step: { _id } }) => _id) | ||
|
||
if (!location.length) return | ||
|
||
if (!fs.existsSync( | ||
path.join(input, 'env/i18ns/authTasks') | ||
)) { | ||
fs.mkdirSync(path.join(input, 'env/i18ns/authTasks'), { recursive: true }) | ||
} | ||
|
||
location.forEach((l) => { | ||
const lang = path.basename(l, `.${format}`), | ||
data = JSON.parse(fs.readFileSync(`${input}/${l}`)) | ||
|
||
this.addTaskStepNameTranslations({ | ||
lang, translations: data, input, keys, parentSteps | ||
}) | ||
}) | ||
|
||
} | ||
|
||
async readAuthenticationTasks() { | ||
const org = this.getOrg(), | ||
tasks = await org.objects.c_tasks.find({ c_type: 'authentication' }).limit(false).paths( | ||
'c_key', | ||
'c_steps._id', | ||
'c_steps.c_parent_step', | ||
'c_steps.c_key', | ||
'c_steps.c_account_map' | ||
).toArray() | ||
|
||
return tasks | ||
} | ||
|
||
addTaskStepNameTranslations({ | ||
lang, translations, input, keys, parentSteps | ||
}) { | ||
const locale = lang.replace(/-/g, '_'), | ||
content = { | ||
locale, | ||
name: `axon__${locale}_authTasks`, | ||
namespace: 'axon', | ||
object: 'i18n', | ||
tags: ['authTasks'], | ||
weight: 0, | ||
data: {} | ||
} | ||
|
||
keys.forEach(({ | ||
object, c_key, c_account_map, _id | ||
}) => { | ||
let data | ||
if (object === 'c_task') { | ||
data = { | ||
c_task: { | ||
[c_key]: { | ||
c_name: translations.task | ||
} | ||
} | ||
} | ||
} else if (c_account_map || parentSteps.includes(_id)) { | ||
data = { | ||
c_step: { | ||
[c_key]: { | ||
c_text: parentSteps.includes(_id) ? translations.parent : translations[c_account_map] | ||
} | ||
} | ||
} | ||
} | ||
_.merge( | ||
content, | ||
{ | ||
data | ||
} | ||
) | ||
}) | ||
|
||
fs.writeFileSync( | ||
path.join(input, `env/i18ns/authTasks/axon_data_${lang}.json`), | ||
JSON.stringify(content, null, 2) | ||
) | ||
} | ||
|
||
} | ||
|
||
module.exports = StudyDataTranslations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.