-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom obj translation refrence fields and validation rules
- Loading branch information
Showing
6 changed files
with
185 additions
and
8 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
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
75 changes: 75 additions & 0 deletions
75
packages/salesforce-adapter/src/filters/custom_object_translation.ts
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,75 @@ | ||
/* | ||
* Copyright 2020 Salto Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import wu from 'wu' | ||
import { | ||
Element, ElemID, findInstances, InstanceElement, | ||
} from '@salto-io/adapter-api' | ||
import { collections } from '@salto-io/lowerdash' | ||
import _ from 'lodash' | ||
import { FilterWith } from '../filter' | ||
import { generateApiNameToCustomObject, apiNameParts, getInstancesOfMetadataType } from './utils' | ||
import { SALESFORCE, CUSTOM_OBJECT_TRANSLATION_METADATA_TYPE, VALIDATION_RULES_METADATA_TYPE } from '../constants' | ||
|
||
const { makeArray } = collections.array | ||
|
||
export const CUSTOM_OBJ_METADATA_TYPE_ID = new ElemID(SALESFORCE, | ||
CUSTOM_OBJECT_TRANSLATION_METADATA_TYPE) | ||
|
||
const FIELDS = 'fields' | ||
const NAME = 'name' | ||
const VALIDATION_RULES = 'validationRules' | ||
|
||
/** | ||
* This filter change CustomObjectTranslation logical references to fields and validation rules to | ||
* Salto referenes | ||
*/ | ||
const filterCreator = (): FilterWith<'onFetch'> => ({ | ||
onFetch: async (elements: Element[]) => { | ||
const ruleObj = (rule: InstanceElement): string => apiNameParts(rule)[0] | ||
const ruleShortName = (rule: InstanceElement): string => apiNameParts(rule)[1] | ||
|
||
const apiNameToCustomObject = generateApiNameToCustomObject(elements) | ||
const apiNameToRules = _.groupBy( | ||
getInstancesOfMetadataType(elements, VALIDATION_RULES_METADATA_TYPE), | ||
ruleObj | ||
) | ||
|
||
wu(findInstances(elements, CUSTOM_OBJ_METADATA_TYPE_ID)) | ||
.forEach(customTranslation => { | ||
const customObjApiName = apiNameParts(customTranslation)[0] | ||
|
||
// Change fields to reference | ||
const customObj = apiNameToCustomObject.get(customObjApiName) | ||
makeArray(customTranslation.value[FIELDS]).forEach(field => { | ||
const customField = customObj?.fields[field[NAME]] | ||
if (customField) { | ||
field[NAME] = customField.elemID | ||
} | ||
}) | ||
|
||
// Change validation rules to refs | ||
const objRules = apiNameToRules[customObjApiName] | ||
makeArray(customTranslation.value[VALIDATION_RULES]).forEach(rule => { | ||
const ruleInstance = objRules?.find(r => _.isEqual(ruleShortName(r), rule[NAME])) | ||
if (ruleInstance) { | ||
rule[NAME] = ruleInstance.elemID | ||
} | ||
}) | ||
}) | ||
}, | ||
}) | ||
|
||
export default filterCreator |
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
103 changes: 103 additions & 0 deletions
103
packages/salesforce-adapter/test/filters/custom_object_translation.test.ts
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,103 @@ | ||
/* | ||
* Copyright 2020 Salto Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import _ from 'lodash' | ||
import { | ||
ObjectType, InstanceElement, Field, BuiltinTypes, ElemID, | ||
} from '@salto-io/adapter-api' | ||
import filterCreator from '../../src/filters/custom_object_translation' | ||
import { | ||
SALESFORCE, CUSTOM_OBJECT_TRANSLATION_METADATA_TYPE, METADATA_TYPE, CUSTOM_OBJECT, API_NAME, | ||
VALIDATION_RULES_METADATA_TYPE, | ||
INSTANCE_FULL_NAME_FIELD, | ||
} from '../../src/constants' | ||
import { FilterWith } from '../../src/filter' | ||
|
||
describe('custom object translation filter', () => { | ||
const customObjName = 'MockCustomObject' | ||
const customFieldName = 'MockField' | ||
const customObjElemID = new ElemID(SALESFORCE, customObjName) | ||
const customObject = new ObjectType( | ||
{ | ||
elemID: customObjElemID, | ||
fields: { | ||
[customFieldName]: new Field(customObjElemID, customFieldName, BuiltinTypes.STRING), | ||
}, | ||
annotations: { | ||
[METADATA_TYPE]: CUSTOM_OBJECT, | ||
[API_NAME]: customObjName, | ||
}, | ||
} | ||
) | ||
const validationRuleName = 'Last_Price' | ||
const validationRuleType = new ObjectType({ | ||
elemID: new ElemID(SALESFORCE, VALIDATION_RULES_METADATA_TYPE), | ||
annotations: { [METADATA_TYPE]: VALIDATION_RULES_METADATA_TYPE }, | ||
}) | ||
const validationRuleInstance = new InstanceElement( | ||
`${customObjName}_${validationRuleName}`, | ||
validationRuleType, | ||
{ [INSTANCE_FULL_NAME_FIELD]: `${customObjName}.${validationRuleName}` } | ||
) | ||
const objTranslationType = new ObjectType({ | ||
elemID: new ElemID(SALESFORCE, CUSTOM_OBJECT_TRANSLATION_METADATA_TYPE), | ||
}) | ||
const objTranslationInstance = new InstanceElement( | ||
`${customObjName}-en_US`, | ||
objTranslationType, | ||
{ | ||
[INSTANCE_FULL_NAME_FIELD]: `${customObjName}-en_US`, | ||
fields: [{ name: customFieldName }, { name: 'not-exists' }], | ||
validationRules: [{ name: validationRuleName }, { name: 'not-exists' }], | ||
}, | ||
) | ||
|
||
describe('on fetch', () => { | ||
let postFilter: InstanceElement | ||
|
||
beforeAll(async () => { | ||
const filter = filterCreator() as FilterWith<'onFetch'> | ||
const testElements = [ | ||
_.clone(objTranslationInstance), | ||
_.clone(customObject), | ||
_.clone(objTranslationType), | ||
_.clone(validationRuleType), | ||
_.clone(validationRuleInstance), | ||
] | ||
await filter.onFetch(testElements) | ||
postFilter = testElements[0] as InstanceElement | ||
}) | ||
|
||
describe('fields reference', () => { | ||
it('should transform fields to reference', async () => { | ||
expect(postFilter.value.fields[0].name) | ||
.toEqual(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') | ||
}) | ||
}) | ||
|
||
describe('validation rules reference', () => { | ||
it('should transform validation rules to reference', async () => { | ||
expect(postFilter.value.validationRules[0].name) | ||
.toEqual(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') | ||
}) | ||
}) | ||
}) | ||
}) |