Skip to content

Commit

Permalink
feed worker fix for failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ikethecoder committed Mar 1, 2024
1 parent c5029a4 commit 0ace2ab
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 6 deletions.
57 changes: 55 additions & 2 deletions src/batch/feed-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ export const syncRecords = async function (
transformInfo,
null,
json,
transformKey
transformKey,
parentRecord
);
if (transformMutation != null) {
logger.debug(
Expand Down Expand Up @@ -536,7 +537,8 @@ export const syncRecords = async function (
transformInfo,
localRecord,
json,
transformKey
transformKey,
parentRecord
);
if (transformMutation && transformMutation != null) {
logger.debug(
Expand Down Expand Up @@ -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]) =>
Expand Down
13 changes: 11 additions & 2 deletions src/batch/transformations/connectExclusiveListCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/batch/transformations/connectOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'] } };
}
}
8 changes: 7 additions & 1 deletion src/test/integrated/batchworker/testsuite/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
92 changes: 92 additions & 0 deletions src/test/integrated/batchworker/testsuite/testdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
},
},
],
};

0 comments on commit 0ace2ab

Please sign in to comment.