Skip to content

Commit

Permalink
Update request for project related resources; Cleanup to match back-e…
Browse files Browse the repository at this point in the history
…nd (#408)

* Basic "clear type errors" commit

* Mark todos; cleanup Mirage config

* Add JiraIssue and HermesDocument

* Cleanup

* Fix v1 reference and related-resource URL

* Update config.ts
  • Loading branch information
jeffdaley authored Nov 8, 2023
1 parent 36ed830 commit 3eda168
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 61 deletions.
4 changes: 3 additions & 1 deletion web/app/components/new/project-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import FlashMessageService from "ember-cli-flash/services/flash-messages";
import { task } from "ember-concurrency";
import ConfigService from "hermes/services/config";
import FetchService from "hermes/services/fetch";
import { ProjectStatus } from "hermes/types/project-status";
import cleanString from "hermes/utils/clean-string";
Expand All @@ -14,6 +15,7 @@ interface NewProjectFormComponentSignature {}

export default class NewProjectFormComponent extends Component<NewProjectFormComponentSignature> {
@service("fetch") declare fetchSvc: FetchService;
@service("config") declare configSvc: ConfigService;
@service declare router: RouterService;
@service declare flashMessages: FlashMessageService;

Expand Down Expand Up @@ -74,7 +76,7 @@ export default class NewProjectFormComponent extends Component<NewProjectFormCom
try {
this.projectIsBeingCreated = true;
const project = await this.fetchSvc
.fetch("/api/v1/projects", {
.fetch(`/api/${this.configSvc.config.api_version}/projects`, {
method: "POST",
body: JSON.stringify({
title: cleanString(this.title),
Expand Down
10 changes: 6 additions & 4 deletions web/app/components/project/tile.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@
{{/each}}
</ul>
{{/if}}
{{#if this.jiraObject}}

{{#if @project.jiraIssue}}
<div class="flex items-center gap-1.5">
<span data-test-jira-type>
{{@project.jiraObject.type}}
{{@project.jiraIssue.type}}
</span>
<span
data-test-jira-key
class="text-body-100 text-color-foreground-faint
{{if (eq this.jiraObject.status 'Done') 'line-through'}}"
{{if (eq @project.jiraIssue.status 'Done') 'line-through'}}"
>
{{this.jiraObject.key}}
{{@project.jiraIssue.key}}
</span>
</div>
{{/if}}

</div>
</div>
</Hds::Card::Container>
10 changes: 1 addition & 9 deletions web/app/components/project/tile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@ interface ProjectTileComponentSignature {
}

export default class ProjectTileComponent extends Component<ProjectTileComponentSignature> {
protected get documents() {
return this.args.project.hermesDocuments;
}

protected get jiraObject() {
return this.args.project.jiraObject;
}

protected get productAreas() {
return this.documents?.map((doc) => doc.product).uniq();
return this.args.project.hermesDocuments?.map((doc) => doc.product).uniq();
}
}

Expand Down
22 changes: 19 additions & 3 deletions web/app/routes/authenticated/projects/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";
import ConfigService from "hermes/services/config";
import FetchService from "hermes/services/fetch";
import { HermesProject } from "hermes/types/project";

export default class AuthenticatedProjectsIndexRoute extends Route {
@service("fetch") declare fetchSvc: FetchService;
@service("config") declare configSvc: ConfigService;

async model(): Promise<Record<string, HermesProject>> {
return await this.fetchSvc
.fetch("/api/v1/projects")
async model() {
const projects = await this.fetchSvc
.fetch(`/api/${this.configSvc.config.api_version}/projects`)
.then((response) => response?.json());

return await Promise.all(
projects.map(async (project: HermesProject) => {
const resources = await this.fetchSvc
.fetch(
`/api/${this.configSvc.config.api_version}/projects/${project.id}/related-resources`,
)
.then((response) => response?.json());

const { hermesDocuments, externalLinks } = resources;

return { ...project, hermesDocuments, externalLinks };
}),
);
}
}
22 changes: 20 additions & 2 deletions web/app/routes/authenticated/projects/project.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";
import ConfigService from "hermes/services/config";
import FetchService from "hermes/services/fetch";
import { HermesProject } from "hermes/types/project";

export default class AuthenticatedProjectsProjectRoute extends Route {
@service("fetch") declare fetchSvc: FetchService;
@service("config") declare configSvc: ConfigService;

async model(params: { project_id: string }): Promise<HermesProject> {
return await this.fetchSvc
.fetch("/api/v1/projects/" + params.project_id)
const project = await this.fetchSvc
.fetch(
`/api/${this.configSvc.config.api_version}/projects/${params.project_id}`,
)
.then((response) => response?.json());

const projectResources = await this.fetchSvc
.fetch(
`/api/${this.configSvc.config.api_version}/projects/${params.project_id}/related-resources`,
)
.then((response) => response?.json());

const { hermesDocuments, externalLinks } = projectResources;

return {
...project,
hermesDocuments,
externalLinks,
};
}
}
13 changes: 7 additions & 6 deletions web/app/types/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "hermes/components/related-resources";
import { ProjectStatus } from "./project-status";

export interface JiraObject {
export interface JiraIssue {
key: string;
url: string;
priority: string;
Expand All @@ -15,14 +15,15 @@ export interface JiraObject {
}

export interface HermesProject {
id: string; // at least in Mirage...
id: string;
title: string;
status: ProjectStatus;
hermesDocuments?: RelatedHermesDocument[];
description?: string;
jiraObject?: JiraObject;
jiraIssueID?: string;
jiraIssue?: JiraIssue;
creator: string;
createdDate: number;
modifiedTime: number;
externalLinks?: RelatedExternalLink[];
creator: string; // maybe a Google/HermesUser
dateCreated: number;
dateModified: number;
}
37 changes: 18 additions & 19 deletions web/mirage/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ export default function (mirageConfig) {
// Fetch a list of projects.
this.get("/projects", () => {
const projects = this.schema.projects.all().models;
return new Response(200, {}, projects);
return new Response(
200,
{},
projects.map((project) => project.attrs),
);
});

// Fetch a single project.
Expand All @@ -199,6 +203,19 @@ export default function (mirageConfig) {
return new Response(200, {}, project.attrs);
});

/**
* Fetch a project's related resources.
* Since Mirage doesn't yet know the relationship between projects and resources,
* so simply return the documents and links created within tests via
* `project.update({ hermesDocuments, externalLinks })`.
*/
this.get("/projects/:project_id/related-resources", (schema, request) => {
const projectID = request.params.project_id;
const project = schema.projects.findBy({ id: projectID });
const { hermesDocuments, externalLinks } = project.attrs;
return new Response(200, {}, { hermesDocuments, externalLinks });
});

// Fetch a project's related resources
this.put("/projects/:project_id", (schema, request) => {
let project = schema.projects.findBy({
Expand Down Expand Up @@ -516,24 +533,6 @@ export default function (mirageConfig) {
);
});

/**
* Used by the /projects route to fetch a list of projects.
*/
this.get("/projects", () => {
const projects = this.schema.projects.all().models;
return new Response(200, {}, projects);
});

/**
* Used by the /projects/:project_id route to fetch a single project.
*/
this.get("/projects/:project_id", (schema, request) => {
const project = schema.projects.findBy({
id: request.params.project_id,
});
return new Response(200, {}, project.attrs);
});

/**
* Used by the Dashboard route to get a user's recently viewed documents.
*/
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions web/mirage/factories/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import { HermesProject } from "hermes/types/project";
export default Factory.extend({
id: (i: number) => i,
title: (i: number) => `Test Project ${i}`,
dateCreated: 1,
dateModified: 1,
createdDate: 1,
modifiedDate: 1,
creator: "[email protected]",
status: "active",

// @ts-ignore - Bug https://github.com/miragejs/miragejs/issues/1052
afterCreate(project: ModelInstance<HermesProject>, server: any): void {
server.createList("related-hermes-document", 1);
server.create("jira-object");
server.create("jira-issue");

const relatedHermesDocuments = server.schema.relatedHermesDocument
.all()
.models.map((doc: ModelInstance) => doc.attrs);

const jiraObject = server.schema.jiraObjects.first()?.attrs;
const jiraIssue = server.schema.jiraIssues.first()?.attrs;

project.update({
hermesDocuments: relatedHermesDocuments,
jiraObject,
jiraIssue,
});
},
});
File renamed without changes.
7 changes: 4 additions & 3 deletions web/tests/acceptance/authenticated/projects-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ module("Acceptance | authenticated/projects", function (hooks) {
expectedDescriptions.push(project.description);
}

if (project.jiraObject) {
expectedKeys.push(project.jiraObject.key);
expectedJiraTypes.push(project.jiraObject.type);
if (project.jiraIssue) {
expectedKeys.push(project.jiraIssue.key);
expectedJiraTypes.push(project.jiraIssue.type);
}

if (project.hermesDocuments) {
project.hermesDocuments.forEach((doc) => {
if (doc.product) {
Expand Down
18 changes: 9 additions & 9 deletions web/tests/integration/components/project/tile-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { find, findAll, render } from "@ember/test-helpers";
import { findAll, render } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import { MirageTestContext, setupMirage } from "ember-cli-mirage/test-support";
import { setupRenderingTest } from "ember-qunit";
Expand Down Expand Up @@ -34,7 +34,7 @@ module("Integration | Component | project/tile", function (hooks) {
product: "Bar",
},
],
jiraObject: {
jiraIssue: {
key: "TEST-123",
type: "Epic",
},
Expand All @@ -46,7 +46,7 @@ module("Integration | Component | project/tile", function (hooks) {
<Project::Tile @project={{this.project}} />
`);

const { title, description, hermesDocuments, jiraObject } = this.project;
const { title, description, hermesDocuments, jiraIssue } = this.project;
const documentProducts = hermesDocuments
?.map((doc) => doc.product as string)
.uniq();
Expand All @@ -62,11 +62,11 @@ module("Integration | Component | project/tile", function (hooks) {
documentProducts,
);

emberAssert("jiraObject must exist", jiraObject);
emberAssert("jiraIssue must exist", jiraIssue);

const { key, type } = jiraObject;
const { key, type } = jiraIssue;

emberAssert("jiraObject type must exist", type);
emberAssert("jiraIssue type must exist", type);

assert.dom(PROJECT_JIRA_KEY).hasText(key);
assert.dom(PROJECT_JIRA_TYPE).hasText(type);
Expand All @@ -78,7 +78,7 @@ module("Integration | Component | project/tile", function (hooks) {
project.update({
description: null,
hermesDocuments: null,
jiraObject: null,
jiraIssue: null,
});

this.set("project", project);
Expand All @@ -93,7 +93,7 @@ module("Integration | Component | project/tile", function (hooks) {
assert.dom(PROJECT_JIRA_TYPE).doesNotExist();
});

test('if the status of a jiraObject is "Done," the key is rendered with a line through it', async function (this: ProjectTileComponentTestContext, assert) {
test('if the status of a jiraIssue is "Done," the key is rendered with a line through it', async function (this: ProjectTileComponentTestContext, assert) {
await render<ProjectTileComponentTestContext>(hbs`
<Project::Tile @project={{this.project}} />
`);
Expand All @@ -103,7 +103,7 @@ module("Integration | Component | project/tile", function (hooks) {
const project = this.server.schema.projects.first();

project.update({
jiraObject: {
jiraIssue: {
key: "TEST-123",
type: "Epic",
status: "Done",
Expand Down

0 comments on commit 3eda168

Please sign in to comment.