From 0ace2ab47b9991616a30ad046f132cc476c6e7c4 Mon Sep 17 00:00:00 2001 From: ikethecoder Date: Fri, 1 Mar 2024 13:16:16 -0800 Subject: [PATCH] feed worker fix for failing tests --- src/batch/feed-worker.ts | 57 +++++++++++- .../connectExclusiveListCreate.ts | 13 ++- src/batch/transformations/connectOne.ts | 3 +- .../integrated/batchworker/testsuite/run.ts | 8 +- .../batchworker/testsuite/testdata.js | 92 +++++++++++++++++++ 5 files changed, 167 insertions(+), 6 deletions(-) diff --git a/src/batch/feed-worker.ts b/src/batch/feed-worker.ts index db1502972..2d0ce490a 100644 --- a/src/batch/feed-worker.ts +++ b/src/batch/feed-worker.ts @@ -426,7 +426,8 @@ export const syncRecords = async function ( transformInfo, null, json, - transformKey + transformKey, + parentRecord ); if (transformMutation != null) { logger.debug( @@ -536,7 +537,8 @@ export const syncRecords = async function ( transformInfo, localRecord, json, - transformKey + transformKey, + parentRecord ); if (transformMutation && transformMutation != null) { logger.debug( @@ -606,6 +608,57 @@ export const syncRecords = async function ( } }; +export const applyTransformationsToNewCreation = async ( + keystone: any, + transformInfo: any, + inputData: any, + parentRecord: any +) => { + if (!inputData) { + return; + } + const feedEntity = transformInfo['list']; + + const md = (metadata as any)[feedEntity]; + logger.debug('[applyTransformations] %j', md); + logger.debug('[applyTransformations] parent %j', parentRecord); + logger.debug('[applyTransformations] input %j', inputData); + + const transformKeys = + 'transformations' in md ? Object.keys(md.transformations) : []; + + for (const inputDataRecord of inputData) { + for (const transformKey of transformKeys) { + logger.debug( + ' -- (applyTransformations) changed trans? (%s)', + transformKey + ); + const transformInfo = md.transformations[transformKey]; + + if (transformInfo.filterByNamespace && parentRecord) { + inputDataRecord['_namespace'] = parentRecord['namespace']; + } + + const transformMutation = await transformations[transformInfo.name]( + keystone, + transformInfo, + null, + inputDataRecord, + transformKey + ); + delete inputDataRecord['_namespace']; + if (transformMutation && transformMutation != null) { + logger.debug( + ' -- (applyTransformations) trans (%s) %j', + transformKey, + transformMutation + ); + inputDataRecord[transformKey] = transformMutation; + } + } + } +}; + export const removeEmpty = (obj: object) => { Object.entries(obj).forEach( ([key, val]) => diff --git a/src/batch/transformations/connectExclusiveListCreate.ts b/src/batch/transformations/connectExclusiveListCreate.ts index e7e7c5ea3..88fa1949c 100644 --- a/src/batch/transformations/connectExclusiveListCreate.ts +++ b/src/batch/transformations/connectExclusiveListCreate.ts @@ -2,6 +2,7 @@ import { BatchService } from '../../services/keystone/batch-service'; import { Logger } from '../../logger'; import { strict as assert } from 'assert'; import { connectExclusiveList } from './connectExclusiveList'; +import { applyTransformationsToNewCreation } from '../feed-worker'; const logger = Logger('batch.connectExclusiveListCreate'); @@ -10,9 +11,17 @@ export async function connectExclusiveListCreate( transformInfo: any, currentData: any, inputData: any, - fieldKey: string + fieldKey: string, + parentRecord: any ) { - logger.debug('%s %j %j', fieldKey, currentData, inputData); + logger.debug('%s %j %j %j', fieldKey, currentData, inputData, parentRecord); + + await applyTransformationsToNewCreation( + keystone, + transformInfo, + inputData[fieldKey], + inputData + ); if (currentData != null) { return connectExclusiveList( diff --git a/src/batch/transformations/connectOne.ts b/src/batch/transformations/connectOne.ts index 475734755..8b8e02bdb 100644 --- a/src/batch/transformations/connectOne.ts +++ b/src/batch/transformations/connectOne.ts @@ -19,6 +19,7 @@ export async function connectOne( // fieldKey: The field that has the new value in the input const fieldKey = 'key' in transformInfo ? transformInfo['key'] : _fieldKey; + logger.debug('[connectOne] %j %s', inputData, fieldKey); const value = dot(inputData, fieldKey); // undefined value is one that was never passed in (rather than explicitely passed in null) @@ -70,7 +71,7 @@ export async function connectOne( ) { return null; } else { - logger.debug('Adding: ' + JSON.stringify({ connect: { id: lkup['id'] } })); + logger.debug('Adding: %s = %j', fieldKey, { connect: { id: lkup['id'] } }); return { connect: { id: lkup['id'] } }; } } diff --git a/src/test/integrated/batchworker/testsuite/run.ts b/src/test/integrated/batchworker/testsuite/run.ts index 7606c539b..378dac54a 100644 --- a/src/test/integrated/batchworker/testsuite/run.ts +++ b/src/test/integrated/batchworker/testsuite/run.ts @@ -42,7 +42,13 @@ async function cleanupDatabase() { pass: process.env.MONGO_PASSWORD, }); - for (const collection of ['products', 'environments', 'datasets', 'legals']) { + for (const collection of [ + 'products', + 'environments', + 'datasets', + 'legals', + 'credentialissuers', + ]) { await _mongoose.connection.collection(collection).deleteMany({}); } await _mongoose.disconnect(); diff --git a/src/test/integrated/batchworker/testsuite/testdata.js b/src/test/integrated/batchworker/testsuite/testdata.js index 0383af24a..d74468070 100644 --- a/src/test/integrated/batchworker/testsuite/testdata.js +++ b/src/test/integrated/batchworker/testsuite/testdata.js @@ -563,5 +563,97 @@ export default { }, }, }, + { + name: 'create a new product with legal', + entity: 'Product', + data: { + name: 'Product with Legal', + namespace: 'refactortime', + environments: [ + { name: 'dev', legal: 'terms-of-use-for-api-gateway-1' }, + ], + }, + expected: { + payload: { + status: 200, + result: 'created', + childResults: [], + }, + }, + }, + { + name: 'get the legal product', + entity: 'Product', + method: 'GET', + whereClause: { + query: '$name: String', + clause: '{ name: $name }', + variables: { + name: 'Product with Legal', + }, + }, + responseFields: ['environments', 'datasets'], + expected: { + payload: [ + { + name: 'Product with Legal', + description: null, + namespace: 'refactortime', + dataset: null, + environments: [ + { + name: 'dev', + active: false, + approval: false, + flow: 'public', + additionalDetailsToRequest: null, + services: [], + legal: { reference: 'terms-of-use-for-api-gateway-1' }, + credentialIssuer: null, + product: { namespace: 'refactortime' }, + }, + ], + }, + ], + }, + }, + { + name: 'create issuer', + entity: 'CredentialIssuer', + refKey: 'name', + data: { + name: 'issuer', + namespace: 'refactortime', + }, + expected: { + payload: { + status: 200, + result: 'created', + childResults: [], + }, + }, + }, + { + name: 'create a new product with issuer', + entity: 'Product', + data: { + name: 'Product with Issuer', + namespace: 'refactortime', + environments: [ + { + name: 'dev', + flow: 'client-credentials', + credentialIssuer: 'issuer', + }, + ], + }, + expected: { + payload: { + status: 200, + result: 'created', + childResults: [], + }, + }, + }, ], };