Skip to content

Commit

Permalink
Merge pull request #70 from zarathustra323/consent-field-export
Browse files Browse the repository at this point in the history
Add regional consent policy answers to export
  • Loading branch information
zarathustra323 authored Jun 30, 2020
2 parents 6800d47 + 4ed2ed5 commit d71c6bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
16 changes: 12 additions & 4 deletions services/export/src/actions/user/export-for-app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { createError } = require('micro');
const { service } = require('@base-cms/micro');
const { applicationService } = require('@identity-x/service-clients');
const { applicationService, organizationService } = require('@identity-x/service-clients');
const { getAsArray } = require('@base-cms/object-path');
const { AsyncParser } = require('json2csv');
const newrelic = require('../../newrelic');
const { sendFailure, sendSuccess, streamResults } = require('../../utils');
Expand Down Expand Up @@ -35,10 +37,15 @@ module.exports = async ({
if (!applicationId) throw createRequiredParamError('applicationId');
if (!Array.isArray(fields) || !fields.length) throw createRequiredParamError('fields');

// load the app and org
const app = await applicationService.request('findById', { id: applicationId, fields: ['name', 'organizationId'] });
if (!app) throw createError(404, 'No application found for the provided ID.');
const org = await organizationService.request('findById', { id: app.organizationId, fields: ['name', 'regionalConsentPolicies'] });

try {
const contents = await new Promise((resolve, reject) => {
let csv = '';
const parser = new AsyncParser({ fields });
const parser = new AsyncParser();
parser.processor
// eslint-disable-next-line no-return-assign
.on('data', chunk => csv += chunk.toString())
Expand All @@ -47,10 +54,11 @@ module.exports = async ({
streamResults({
client: applicationService,
action: 'user.listForApp',
fields,
regionalConsentPolicies: getAsArray(org, 'regionalConsentPolicies'),
params: {
id: applicationId,
fields: fields.reduce((obj, k) => ({ ...obj, [k]: 1 }), {}),
sort: { field: 'id', direction: 'desc' },
sort: { field: 'updatedAt', order: 'desc' },
},
stream: parser.input,
});
Expand Down
14 changes: 13 additions & 1 deletion services/export/src/utils/stream-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const executor = async (args) => {
action,
params,
limit = 500,
fields,
regionalConsentPolicies,
stream,
} = args;
const pagination = getAsObject(params, 'pagination');
Expand All @@ -25,7 +27,17 @@ const executor = async (args) => {
limit,
},
});
const nodes = getAsArray(data, 'edges').map(edge => edge.node);
const nodes = getAsArray(data, 'edges').map((edge) => {
const { node } = edge;
const row = fields.reduce((o, field) => ({ ...o, [field]: node[field] }), {});
const consentAnswers = getAsArray(node, 'regionalConsentAnswers');
const answers = regionalConsentPolicies.reduce((o, policy) => {
const answer = consentAnswers.find(v => v._id === policy._id);
const given = answer ? answer.given : false;
return { ...o, [`Consent: ${policy.name}`]: given };
}, {});
return { ...row, ...answers };
});
stream.push(JSON.stringify(nodes));
const { hasNextPage, endCursor } = getAsObject(data, 'pageInfo');
if (hasNextPage) {
Expand Down
4 changes: 3 additions & 1 deletion services/graphql/src/graphql/resolvers/app-user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { applicationService, exportService, localeService } = require('@identity-x/service-clients');
const { UserInputError } = require('apollo-server-express');
const newrelic = require('../../newrelic');
const connectionProjection = require('../utils/connection-projection');
const typeProjection = require('../utils/type-projection');

Expand Down Expand Up @@ -183,7 +184,8 @@ module.exports = {
exportAppUsers: (_, __, { app, user }) => {
const applicationId = app.getId();
const email = user.get('email');
exportService.request('user.exportForApp', { applicationId, email });
exportService.request('user.exportForApp', { applicationId, email })
.catch(newrelic.noticeError.bind(newrelic));
return 'ok';
},

Expand Down

0 comments on commit d71c6bc

Please sign in to comment.