Skip to content

Commit

Permalink
AR to csv: Simplify transformRelatedModel and begin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcloud committed Mar 23, 2021
1 parent 191dc92 commit 9fab939
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 62 deletions.
91 changes: 29 additions & 62 deletions src/lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,28 @@ function transformSimpleValue(instance, field) {
}

/*
* @param {ActivityReport} ActivityReport instance
* @returns {Promise<object>} author name
*/
async function transformAuthor(report) {
const authorRecord = await report.get('author');
const author = authorRecord ? authorRecord.get('name') : '';
return { author };
}

/*
* @param {ActivityReport} ActivityReport instance
* @returns {Promise<object>} approving manager name
*/
async function transformApprovingManager(report) {
const managerRecord = await report.get('approvingManager');
const approvingManager = managerRecord ? managerRecord.get('name') : '';
return { approvingManager };
}

/*
* @param {ActivityReport} ActivityReport instance
* @returns {Promise<object>} Name of user who last updated report
* Generates a function that can transform values of a related model
* @param {string} field The field of the related model
* @param {string} key The key on the related model to transform
* @returns {function} A function that will perform the transformation
*/
async function transformLastUpdatedBy(report) {
const lastUpdatedByRecord = await report.get('lastUpdatedBy');
const lastUpdatedBy = lastUpdatedByRecord ? lastUpdatedByRecord.get('name') : '';
return { lastUpdatedBy };
function transformRelatedModel(field, prop) {
async function transformer(instance) {
const obj = {};
let records = await instance.get(field);
if (records) {
if (!Array.isArray(records)) {
records = [records];
}
const value = records.map((r) => (r[prop] || '')).join('\n');
Object.defineProperty(obj, field, {
value,
enumerable: true,
});
}
return Promise.resolve(obj);
}
return transformer;
}

/*
Expand Down Expand Up @@ -121,41 +116,13 @@ async function transformGoalsAndObjectives(report) {
return obj;
}

function transformManyModel(field, prop) {
async function transformer(instance) {
const obj = {};
const records = await instance.get(field);
if (records) {
const value = records.map((r) => (r[prop] || '')).join('\n');
Object.defineProperty(obj, field, {
value,
enumerable: true,
});
}
return Promise.resolve(obj);
}
return transformer;
}

async function transformActivityRecipients(report) {
const records = await report.get('activityRecipients');
const activityRecipients = records.map((r) => r.name || '').join('\n');
return { activityRecipients };
}

async function transformCollaborators(report) {
const records = await report.get('collaborators');
const collaborators = records.map((r) => r.name || '').join('\n');
return { collaborators };
}

const arBuilders = [
'displayId',
transformAuthor,
transformApprovingManager,
transformLastUpdatedBy,
transformRelatedModel('author', 'name'),
transformRelatedModel('approvingManager', 'name'),
transformRelatedModel('lastUpdatedBy', 'name'),
'requester',
transformCollaborators,
transformRelatedModel('collaborators', 'name'),
'programTypes',
'targetPopulations',
'virtualDeliveryType',
Expand All @@ -169,14 +136,14 @@ const arBuilders = [
'duration',
'endDate',
'startDate',
transformActivityRecipients,
transformRelatedModel('activityRecipients', 'name'),
'activityRecipientType',
'ECLKCResourcesUsed',
'nonECLKCResourcesUsed',
transformManyModel('attachments', 'originalFileName'),
transformRelatedModel('attachments', 'originalFileName'),
transformGoalsAndObjectives,
transformManyModel('granteeNextSteps', 'note'),
transformManyModel('specialistNextSteps', 'note'),
transformRelatedModel('granteeNextSteps', 'note'),
transformRelatedModel('specialistNextSteps', 'note'),
'context',
'managerNotes',
'additionalNotes',
Expand Down
48 changes: 48 additions & 0 deletions src/lib/transform.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ActivityReport, User, sequelize } from '../models';
import activityReportToCsvRecord from './transform';

describe('activityReportToCsvRecord', () => {
const mockAuthor = {
id: 2099,
name: 'Arthur',
hsesUserId: '2099',
hsesUsername: 'arthur.author',
};

const mockReport = {
id: 209914,
regionId: 14,
reason: 'Test CSV Export',
status: 'approved',
numberOfParticipants: 12,
deliveryMethod: 'virtual',
duration: 4.5,
startDate: '2021-10-31',
endSDate: '2021-11-31',
ECLKCResourcesUsed: ['https://one.test', 'https://two.test'],
nonECLKCResourcesUsed: ['one', 'two'],
author: mockAuthor,
lastUpdatedBy: mockAuthor,
};

it('transforms arrays of strings into strings', async () => {
const report = ActivityReport.build({
ECLKCResourcesUsed: ['https://one.test', 'https://two.test'],
nonECLKCResourcesUsed: ['one', 'two'],
});
const output = await activityReportToCsvRecord(report);
const expectedOutput = {
ECLKCResourcesUsed: 'https://one.test\nhttps://two.test',
nonECLKCResourcesUsed: 'one\ntwo',
};
expect(output).toMatchObject(expectedOutput);
});

it('transforms related models into string values', async () => {
const report = await ActivityReport.build(mockReport, { include: [{ model: User, as: 'author' }, { model: User, as: 'lastUpdatedBy' }] });
const output = await activityReportToCsvRecord(report);
const { author: authorOutput, lastUpdatedBy: lastUpdatedByOutput } = output;
expect(authorOutput).toEqual('Arthur');
expect(lastUpdatedByOutput).toEqual('Arthur');
});
});

0 comments on commit 9fab939

Please sign in to comment.