Skip to content

Commit

Permalink
feat: order by priority column (#2260)
Browse files Browse the repository at this point in the history
- Introduce a new column to handle ordering based on our demand.
- Apparently, there are some changes with the categories. Rather than
reusing an existing entity and just updating the title, it might become
inaccurate. Hence, deleting the rows instead. Good thing that we also
have no Squads with any category yet. Although they should be safe as
cascading would take effect and null would be set in any case.
  - Another option is to turn the `enabled` to `false` values.
  - ~~Removed `['General', 'DevOps', 'Cloud', 'Data']`.~~
- ~~Added `['Basics', 'DevOps & Cloud', 'AI', 'Open Source',
'DevRel']`~~
- Removed every category (good thing no one is using any of the items
yet). This is to make it easier to manage in the future and better
devex. We will run a once off script to insert the data along with the
Squad updates. This would make seeding our local env consistent.
- Created the migration for the column and did the necessary
insert/delete/update commands at the same time.
  • Loading branch information
sshanzel committed Sep 18, 2024
1 parent 1f7d365 commit 4f41927
Show file tree
Hide file tree
Showing 8 changed files with 793 additions and 14 deletions.
55 changes: 48 additions & 7 deletions __tests__/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ beforeAll(async () => {

const getSourceCategories = () => [
{
title: 'General',
title: 'Basics',
enabled: true,
},
{
Expand All @@ -74,19 +74,19 @@ const getSourceCategories = () => [
enabled: true,
},
{
title: 'DevOps',
title: 'DevOps & Cloud',
enabled: true,
},
{
title: 'Cloud',
title: 'Open Source',
enabled: true,
},
{
title: 'Career',
enabled: true,
},
{
title: 'Data',
title: 'AI',
enabled: true,
},
{
Expand All @@ -97,6 +97,10 @@ const getSourceCategories = () => [
title: 'DevTools',
enabled: true,
},
{
title: 'DevRel',
enabled: true,
},
];

beforeEach(async () => {
Expand Down Expand Up @@ -252,6 +256,43 @@ describe('query sourceCategories', () => {
);
expect(isAllFound).toBeTruthy();
});

it('should return categories ordered by priority', async () => {
await con.createQueryRunner().query(`
DO $$
DECLARE
categories TEXT[] := ARRAY['Basics','Web','Mobile','DevOps & Cloud','AI','Games','DevTools','Career','Open Source','DevRel','Fun'];
i INT;
BEGIN
-- Iterate over the array and update the table
FOR i IN 1..array_length(categories, 1) LOOP
UPDATE source_category
SET priority = i
WHERE slug = trim(BOTH '-' FROM regexp_replace(lower(trim(COALESCE(LEFT(categories[i],100),''))), '[^a-z0-9-]+', '-', 'gi'));
END LOOP;
END $$;
`);
const expected = [
'Basics',
'Web',
'Mobile',
'DevOps & Cloud',
'AI',
'Games',
'DevTools',
'Career',
'Open Source',
'DevRel',
'Fun',
];
loggedUser = '1';
const res = await client.query(QUERY);
expect(res.errors).toBeFalsy();
const mapped = res.data.sourceCategories.edges.map(
({ node }) => node.title,
);
expect(mapped).toEqual(expected);
});
});

describe('query sources', () => {
Expand Down Expand Up @@ -312,13 +353,13 @@ describe('query sources', () => {

it('should filter by category', async () => {
const repo = con.getRepository(Source);
const general = await con
const anyOther = await con
.getRepository(SourceCategory)
.findOneByOrFail({ title: 'General' });
.findOneByOrFail({ title: Not('Web') });
const web = await con
.getRepository(SourceCategory)
.findOneByOrFail({ title: 'Web' });
await repo.update({ id: 'a' }, { categoryId: general.id });
await repo.update({ id: 'a' }, { categoryId: anyOther.id });
await repo.update({ id: 'b' }, { categoryId: web.id });
const res = await client.query(QUERY({ first: 10, categoryId: web.id }));
const isAllWeb = res.data.sources.edges.every(
Expand Down
Loading

0 comments on commit 4f41927

Please sign in to comment.