Skip to content

Commit

Permalink
CR+
Browse files Browse the repository at this point in the history
  • Loading branch information
hadard committed Mar 12, 2020
1 parent 2dda8ca commit 298efd6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import wu from 'wu'
import {
Element, ElemID, findInstances, InstanceElement,
Element, ElemID, findInstances, InstanceElement, INSTANCE_ANNOTATIONS, ReferenceExpression,
} from '@salto-io/adapter-api'
import { collections } from '@salto-io/lowerdash'
import _ from 'lodash'
Expand Down Expand Up @@ -51,12 +51,19 @@ const filterCreator = (): FilterWith<'onFetch'> => ({
.forEach(customTranslation => {
const customObjApiName = apiNameParts(customTranslation)[0]

// Change fields to reference
// Add parent annotation
const customObj = apiNameToCustomObject.get(customObjApiName)
if (customObj) {
customTranslation.annotate({
[INSTANCE_ANNOTATIONS.PARENT]: new ReferenceExpression(customObj.elemID),
})
}

// Change fields to reference
makeArray(customTranslation.value[FIELDS]).forEach(field => {
const customField = customObj?.fields[field[NAME]]
if (customField) {
field[NAME] = customField.elemID
field[NAME] = new ReferenceExpression(customField.elemID)
}
})

Expand All @@ -65,7 +72,7 @@ const filterCreator = (): FilterWith<'onFetch'> => ({
makeArray(customTranslation.value[VALIDATION_RULES]).forEach(rule => {
const ruleInstance = objRules?.find(r => _.isEqual(ruleShortName(r), rule[NAME]))
if (ruleInstance) {
rule[NAME] = ruleInstance.elemID
rule[NAME] = new ReferenceExpression(ruleInstance.elemID)
}
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import _ from 'lodash'
import {
ObjectType, InstanceElement, Field, BuiltinTypes, ElemID,
ObjectType, InstanceElement, Field, BuiltinTypes, ElemID, ReferenceExpression,
} from '@salto-io/adapter-api'
import filterCreator from '../../src/filters/custom_object_translation'
import {
Expand Down Expand Up @@ -70,11 +70,11 @@ describe('custom object translation filter', () => {
beforeAll(async () => {
const filter = filterCreator() as FilterWith<'onFetch'>
const testElements = [
_.clone(objTranslationInstance),
_.clone(customObject),
_.clone(objTranslationType),
_.clone(validationRuleType),
_.clone(validationRuleInstance),
objTranslationInstance.clone(),
customObject.clone(),
objTranslationType.clone(),
validationRuleType.clone(),
validationRuleInstance.clone(),
]
await filter.onFetch(testElements)
postFilter = testElements[0] as InstanceElement
Expand All @@ -83,7 +83,7 @@ describe('custom object translation filter', () => {
describe('fields reference', () => {
it('should transform fields to reference', async () => {
expect(postFilter.value.fields[0].name)
.toEqual(customObject.fields[customFieldName].elemID)
.toEqual(new ReferenceExpression(customObject.fields[customFieldName].elemID))
})
it('should keep name as is if no referenced field was found', async () => {
expect(postFilter.value.fields[1].name).toBe('not-exists')
Expand All @@ -93,7 +93,7 @@ describe('custom object translation filter', () => {
describe('validation rules reference', () => {
it('should transform validation rules to reference', async () => {
expect(postFilter.value.validationRules[0].name)
.toEqual(validationRuleInstance.elemID)
.toEqual(new ReferenceExpression(validationRuleInstance.elemID))
})
it('should keep name as is if no referenced validation rules was found', async () => {
expect(postFilter.value.validationRules[1].name).toBe('not-exists')
Expand Down

0 comments on commit 298efd6

Please sign in to comment.