Skip to content

Commit

Permalink
fix: get projects (#24)
Browse files Browse the repository at this point in the history
* fix: improve error handling

* fix: get project list

* chore: bump to 1.0.5
  • Loading branch information
DanielRamosAcosta committed Apr 1, 2024
1 parent 9e7be46 commit 27d2714
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "factorial-cli",
"version": "1.0.4",
"version": "1.0.5",
"type": "module",
"description": "Fill your factorial shifts with ease",
"main": "dist/src/infrastructure/cli/main.js",
Expand Down
15 changes: 15 additions & 0 deletions src/infrastructure/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createApp } from "./createApp.js";
import { DayRange } from "../../domain/models/DayRange.js";
import { MomentOfTheDay } from "../../domain/models/MomentOfTheDay.js";
import { Minute } from "../../domain/models/Minute.js";
import { HttpClientError } from "../http-client/HttpClientFetch.js";

const now = new Date();

Expand Down Expand Up @@ -89,4 +90,18 @@ yargs(hideBin(process.argv))
},
)
.demandCommand(1)
.fail((msg, err) => {
if (err) {
if (err instanceof HttpClientError) {
const data = err.response.data;
console.error(JSON.stringify(data, null, 2));
process.exit(1);
}

throw err;
}
console.error(msg);
console.error(err);
process.exit(1);
})
.parse();
16 changes: 12 additions & 4 deletions src/infrastructure/factorial-client/FactorialClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,26 @@ export class FactorialClient {
}

async getProjects(employeeId: number) {
const query = `query GetProjectsAssignedToProjectWorkers($employeeIds: [Int!]!, $onlyActiveProjects: Boolean!, $assigned: Boolean!) {
const query = `query GetProjectsAssignedToProjectWorkers($assigned: Boolean!, $employeeIds: [Int!]!, $includeSubprojects: Boolean = false, $onlyActiveProjects: Boolean!) {
projectManagement {
projectWorkers(
assigned: $assigned
employeeIds: $employeeIds
projectActive: $onlyActiveProjects
assigned: $assigned
) {
id
assigned
project {
employee {
id
}
imputableProject {
id
name
status
subprojects @include(if: $includeSubprojects) {
id
name
}
}
}
}
Expand All @@ -107,9 +114,10 @@ export class FactorialClient {
const response = await this.client.post("/graphql", {
operationName: "GetProjectsAssignedToProjectWorkersQuery",
variables: {
assigned: true,
employeeIds: [employeeId],
includeSubprojects: true,
onlyActiveProjects: true,
assigned: true,
},
query,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { Project } from "./Project.js";
export const ProjectWorker = z.object({
id: z.number(),
assigned: z.boolean(),
project: Project,
imputableProject: Project,
});
2 changes: 1 addition & 1 deletion src/infrastructure/http-client/HttpClientFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type PrivateHttpClientOptions = {
headers: Record<string, string>;
};

class HttpClientError<T> extends Error {
export class HttpClientError<T> extends Error {
constructor(public response: HttpResponse<T>) {
super(`Request failed with status code ${response.status}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class ProjectRepositoryFactorial implements ProjectRepository {
);

return projects.map(
(project) => new Project(new ProjectId(project.id), project.project.name),
(project) =>
new Project(new ProjectId(project.id), project.imputableProject.name),
);
}
}
10 changes: 6 additions & 4 deletions test/fixtures/projects.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
{
"id": 193161,
"assigned": true,
"project": {
"imputableProject": {
"id": 60154,
"name": "Apple",
"status": "active"
"status": "active",
"subprojects": []
}
},
{
"id": 691157,
"assigned": true,
"project": {
"imputableProject": {
"id": 11566,
"name": "Microsoft",
"status": "active"
"status": "active",
"subprojects": []
}
}
]
Expand Down

0 comments on commit 27d2714

Please sign in to comment.