Skip to content

Commit

Permalink
#16: sort items by due date
Browse files Browse the repository at this point in the history
  • Loading branch information
l004p committed Jun 30, 2024
1 parent da8688c commit f6d142e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/items/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,43 @@ export interface Item {
}

export const convertGithubItems = (items: ProjectV2Item[]): Item[] => {
return items.map((item: ProjectV2Item) => {
const assignedUsers = item.fieldValues.nodes
.filter((field) => field.users)
.flatMap((field) => field.users.nodes.map((user) => user.url));
const status = item.fieldValues.nodes
.filter((field) => field.name)
.map((field) => field.name)[0];
const labels = item.fieldValues.nodes
.filter((field) => field.labels)
.flatMap((field) => field.labels.nodes.map((label) => label.name));

// TODO: improve this
let dueDate: Date | undefined;
if (item.fieldValueByName?.date) {
dueDate = new Date(item.fieldValueByName.date);
dueDate.setDate(dueDate.getDate() + 1);
}
return items
.map((item: ProjectV2Item) => {
const assignedUsers = item.fieldValues.nodes
.filter((field) => field.users)
.flatMap((field) => field.users.nodes.map((user) => user.url));
const status = item.fieldValues.nodes
.filter((field) => field.name)
.map((field) => field.name)[0];
const labels = item.fieldValues.nodes
.filter((field) => field.labels)
.flatMap((field) => field.labels.nodes.map((label) => label.name));

// TODO: improve this
let dueDate: Date | undefined;
if (item.fieldValueByName?.date) {
dueDate = new Date(item.fieldValueByName.date);
dueDate.setDate(dueDate.getDate() + 1);
}

return {
title: item.content.title,
url: item.content.url,
assignedUsers,
labels,
dueDate: dueDate,
status: status,
};
})
.sort(sortByDate);
};

return {
title: item.content.title,
url: item.content.url,
assignedUsers,
labels,
dueDate: dueDate,
status: status,
};
});
export const sortByDate = (item1: Item, item2: Item): number => {
if (item1.dueDate === undefined && item2.dueDate === undefined) return 0;
if (item1.dueDate === undefined) return 1;
if (item2.dueDate === undefined) return -1;
if (item1.dueDate === item2.dueDate) return 0;
return item1.dueDate < item2.dueDate ? -1 : 1;
};

export const filterByDateRange = (
Expand Down
28 changes: 28 additions & 0 deletions test/items/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
filterForUrgentItems,
filterOutStatus,
filterUpcomingItems,
sortByDate,
} from "../../src/items";
import { itemFactory } from "./factories/item-factory";
import exp from "constants";
Expand Down Expand Up @@ -151,3 +152,30 @@ describe("filterByLabels", () => {
expect(result).toEqual([item2, item3, item4, item5]);
});
});

describe("orderByDate", () => {
it("will sort the items by date", () => {
const item1 = itemFactory({ dueDate: undefined });
const item2 = itemFactory({ dueDate: undefined });
const item3 = itemFactory({ dueDate: new Date("2024-06-29") });
const item4 = itemFactory({ dueDate: new Date("2024-07-02") });
const item5 = itemFactory({ dueDate: new Date("2024-07-05") });
const item6 = itemFactory({ dueDate: new Date("2024-09-22") });
const item7 = itemFactory({ dueDate: new Date("2024-09-22") });
const item8 = itemFactory({ dueDate: new Date("2024-06-29") });

const items = [item8, item4, item7, item2, item3, item6, item1, item5];
const sortedItems = items.sort(sortByDate);

expect(sortedItems).toEqual([
item3,
item8,
item4,
item5,
item6,
item7,
item1,
item2,
]);
});
});

0 comments on commit f6d142e

Please sign in to comment.