Skip to content

Commit

Permalink
tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredgalanis committed Jun 26, 2024
1 parent 1f0da8c commit f05c6f0
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 121 deletions.
7 changes: 4 additions & 3 deletions app/components/policy-card/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,19 @@ export default class PolicyCard extends Component {
async _addEffectivePolicy(policy) {
const effectivePolicies = await this.args.submission.effectivePolicies;
// Only add the policy if it is not already in the list of effective policies
if (!this._hasEffectivePolicy(policy.id)) {
const hasEffectivePolicy = await this._hasEffectivePolicy(policy.id);
if (!hasEffectivePolicy) {
this.args.submission.effectivePolicies = [...effectivePolicies, policy];
}
}

async _removeEffectivePolicy(policy) {
let effectivePolicies = await this.args.submission.effectivePolicies;
this.args.submission.effectivePolicies = effectivePolicies.filter((p) => p.id === policy.id);
this.args.submission.effectivePolicies = effectivePolicies.filter((p) => p.id !== policy.id);
}

async _hasEffectivePolicy(policyId) {
const effectivePolicies = await this.args.submission.effectivePolicies;
return effectivePolicies && effectivePolicies.some((policy) => policy.id === policyId);
return !!effectivePolicies && effectivePolicies.some((policy) => policy.id === policyId);
}
}
8 changes: 4 additions & 4 deletions app/components/workflow-repositories/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable ember/no-get */
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action, get, set } from '@ember/object';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';

/**
Expand Down Expand Up @@ -59,9 +59,9 @@ export default class WorkflowRepositories extends Component {
this.addedRepos = this.getAddedRepositories();
const currentRepos = await this.args.submission.repositories;

const opt = this.args.optionalRepositories;
const req = this.args.requiredRepositories;
const choice = this.args.choiceRepositories;
const opt = await this.args.optionalRepositories;
const req = await this.args.requiredRepositories;
const choice = await this.args.choiceRepositories;

if (currentRepos && currentRepos.length > 0) {
/**
Expand Down
62 changes: 1 addition & 61 deletions app/controllers/submissions/new/repositories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable ember/no-computed-properties-in-native-classes, ember/no-get, ember/require-computed-property-dependencies */
import Controller, { inject as controller } from '@ember/controller';
import { tracked } from '@glimmer/tracking';
import { action, computed, get, set } from '@ember/object';
import { action, get, set } from '@ember/object';
import { alias } from '@ember/object/computed';
import { inject as service } from '@ember/service';

Expand All @@ -19,55 +19,14 @@ export default class SubmissionsNewRepositories extends Controller {
@tracked maxStep = this.workflow.maxStep;
@tracked loadingNext = false;

@computed('workflow.maxStep')
get nextTabIsActive() {
return get(this, 'workflow').getMaxStep() > 6;
}

@computed('nextTabIsActive', 'loadingNext')
get needValidation() {
return this.nextTabIsActive || this.loadingNext;
}

/*
* Do some light processing on the repository containers, such as adding the names of funders
* that both are associated with the submission AND associated with each repository.
*/
get requiredRepositories() {
let req = this.model.requiredRepositories;
const submission = this.submission;

return req.map((repo) => ({
repository: repo,
funders: this._getFunderNamesForRepo(repo, submission),
}));
}

@computed('model.optionalRepositories')
get optionalRepositories() {
const submission = this.submission;
let optionals = get(this, 'model.optionalRepositories');

return optionals.map((repo) => ({
repository: repo,
funders: this._getFunderNamesForRepo(repo, submission),
}));
}

@computed('model.choiceRepositories')
get choiceRespositories() {
const submission = this.submission;
let choices = get(this, 'model.choiceRepositories');

choices.forEach((group) => {
group.map((repo) => ({
repository: repo,
funders: this._getFunderNamesForRepo(repo, submission),
}));
});
return choices;
}

@action
loadNext() {
set(this, 'loadingNext', true);
Expand Down Expand Up @@ -119,23 +78,4 @@ export default class SubmissionsNewRepositories extends Controller {
updateCovidSubmission() {
this.parent.updateCovidSubmission();
}

async _getFunderNamesForRepo(repo, submission) {
const grants = await submission.grants;

const funders = grants.map((grant) => get(grant, 'primaryFunder'));
const fundersWithRepos = funders.filter((funder) => get(funder, 'policy.repositories'));
// List of funders that include this repository
const fundersWithOurRepo = fundersWithRepos.filter(
(funder) => get(funder, 'policy') && funder.get('policy.repositories').includes(repo)
);

if (fundersWithRepos && fundersWithOurRepo.length > 0) {
return fundersWithOurRepo
.map((funder) => get(funder, 'name'))
.filter((item, index, arr) => arr.indexOf(item) == index)
.join(', ');
}
return '';
}
}
4 changes: 2 additions & 2 deletions app/models/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export default class SubmissionModel extends Model {
@attr('number') version;

@belongsTo('user', { async: false, inverse: null }) submitter;
@belongsTo('publication', { async: false, inverse: null }) publication;
@belongsTo('publication', { async: true, inverse: null }) publication;

@hasMany('user', { async: false, inverse: null }) preparers;
@hasMany('repository', { async: false, inverse: null }) repositories;
@hasMany('policy', { async: false, inverse: null }) effectivePolicies;
@hasMany('policy', { async: true, inverse: null }) effectivePolicies;
// not on this model on API
@hasMany('submissionEvent', {
async: true,
Expand Down
84 changes: 73 additions & 11 deletions app/routes/submissions/new/repositories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,89 @@ import { hash } from 'rsvp';
import CheckSessionRoute from '../../check-session-route';

export default class RepositoriesRoute extends CheckSessionRoute {
@service('workflow')
workflow;

@service('policies')
policyService;
@service workflow;
@service policies;

async model() {
const parentModel = this.modelFor('submissions.new');
const submission = parentModel.newSubmission;
this.submission = parentModel.newSubmission;

const repoPromise = await this.policyService.getRepositories.perform(submission);
this.repositories = await this.policies.getRepositories.perform(this.submission);

return hash({
newSubmission: submission,
newSubmission: this.submission,
preLoadedGrant: parentModel.preLoadedGrant,
requiredRepositories: repoPromise.required,
optionalRepositories: repoPromise.optional,
choiceRepositories: repoPromise['one-of'],
requiredRepositories: this.requiredRepositories(),
optionalRepositories: this.optionalRepositories(),
choiceRepositories: this.choiceRepositories(),
});
}

async _getFunderNamesForRepo(repo, submission) {
const grants = await submission.grants;

const funders = grants.map((grant) => get(grant, 'primaryFunder'));
const fundersWithRepos = funders.filter((funder) => get(funder, 'policy.repositories'));
// List of funders that include this repository
const fundersWithOurRepo = fundersWithRepos.filter(
(funder) => get(funder, 'policy') && funder.get('policy.repositories').includes(repo)
);

if (fundersWithRepos && fundersWithOurRepo.length > 0) {
return fundersWithOurRepo
.map((funder) => funder.get('name'))
.filter((item, index, arr) => arr.indexOf(item) == index)
.join(', ');
}
return '';
}

async requiredRepositories() {
return Promise.all(
this.repositories?.required.map(async (repo) => {
const funders = await this._getFunderNamesForRepo(repo, this.submission);
return {
repository: repo,
funders,
};
})
);
}

async optionalRepositories() {
return Promise.all(
this.repositories?.optional.map(async (repo) => {
const funders = await this._getFunderNamesForRepo(repo, this.submission);

return {
repository: repo,
funders,
};
})
);
}

async choiceRepositories() {
let formattedChoices = [];
const choices = this.repositories['one-of'] ?? [];

for (const group of choices) {
const formattedGroup = [];
for (const repo of group) {
const funders = await this._getFunderNamesForRepo(repo, this.submission);

formattedGroup.push({
repository: repo,
funders,
});
}

formattedChoices.push(formattedGroup);
}

return formattedChoices;
}

@action
didTransition() {
this.workflow.setCurrentStep(4);
Expand Down
6 changes: 3 additions & 3 deletions app/templates/submissions/new/repositories.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
>
<WorkflowRepositories
@submission={{this.submission}}
@requiredRepositories={{this.requiredRepositories}}
@optionalRepositories={{this.optionalRepositories}}
@choiceRepositories={{this.choiceRepositories}}
@requiredRepositories={{this.model.requiredRepositories}}
@optionalRepositories={{this.model.optionalRepositories}}
@choiceRepositories={{this.model.choiceRepositories}}
@next={{action "loadNext"}}
@back={{action "loadPrevious"}}
@abort={{action "abort"}}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@ember/test-helpers": "^2.8.1",
"@faker-js/faker": "^8.1.0",
"@fortawesome/ember-fontawesome": "^0.4.3",
"@fortawesome/fontawesome-svg-core": "^6.5.2",
"@fortawesome/free-regular-svg-icons": "^6.4.0",
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2",
Expand All @@ -57,7 +58,7 @@
"ember-cli-content-security-policy": "^2.0.3",
"ember-cli-dependency-checker": "^3.3.2",
"ember-cli-dependency-lint": "^2.0.1",
"ember-cli-deprecation-workflow": "github:mixonic/ember-cli-deprecation-workflow#master",
"ember-cli-deprecation-workflow": "github:ember-cli/ember-cli-deprecation-workflow#main",
"ember-cli-flash": "^4.0.0",
"ember-cli-htmlbars": "^6.1.1",
"ember-cli-inject-live-reload": "^2.1.0",
Expand Down Expand Up @@ -130,7 +131,7 @@
"edition": "octane"
},
"resolutions": {
"@embroider/macros": "1.9.0"
"@embroider/macros": "1.16.0"
},
"dependencies": {}
}
2 changes: 1 addition & 1 deletion tests/integration/components/policy-card-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module('Integration | Component | policy card', (hooks) => {
assert.dom('[data-test-workflow-policies-radio-no-direct-deposit]').exists();
assert.dom('[data-test-workflow-policies-radio-direct-deposit]').exists();

const effectivePolicies = this.submission.effectivePolicies.slice();
const effectivePolicies = await this.submission.effectivePolicies;

assert.strictEqual(effectivePolicies.length, 1, 'Should be ONE effective policy on submission');
assert.ok(effectivePolicies.some((p) => p.title === 'Moo title'));
Expand Down
Loading

0 comments on commit f05c6f0

Please sign in to comment.