Skip to content

Commit

Permalink
Make groups and items be listed by default
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Aug 28, 2024
1 parent 88112ac commit 6112d66
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/routes/api/group/[groupId]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { object, string, validate } from 'superstruct';
import { getPortfolioGlobals, invalidatePortfolioGlobals } from '$lib/server/data/index';
import { validateId, validateName } from '$lib/validators';
import { removeAllLinksToItem } from '$lib/server/links.js';
import { setConfig } from '$lib/server/data/config.js';

export async function GET({ params, request, cookies }) {
const groupId = params.groupId;
Expand Down Expand Up @@ -38,6 +39,8 @@ export async function POST({ params, request, cookies }) {
}

await createGroup(groupId, name, description);
data.config.listedGroups.push(groupId);
await setConfig(data.config);
invalidatePortfolioGlobals();

return json({}, { status: 200 });
Expand Down
6 changes: 5 additions & 1 deletion src/routes/api/group/[groupId]/item/[itemId]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error, json } from '@sveltejs/kit';
import { getGroupInfo } from '$lib/server/data/group';
import { getGroupInfo, setGroupInfo } from '$lib/server/data/group';
import { validateTokenFromRequest } from '$lib/server/auth';
import { object, string, validate } from 'superstruct';
import { createItem, setItemInfo, ItemInfoFullStruct, deleteItem } from '$lib/server/data/item.js';
Expand Down Expand Up @@ -45,6 +45,10 @@ export async function POST({ params, request, cookies }) {
}

await createItem(groupId, itemId, name, description);

const groupInfo = data.groups[groupId].info;
groupInfo.listedItems.push(itemId);
await setGroupInfo(groupId, groupInfo);
invalidatePortfolioGlobals();

return json({}, { status: 200 });
Expand Down
8 changes: 8 additions & 0 deletions tests/backend/group/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ describe('Other test cases', () => {
() => api,
api => api.group.withId(groupId).create('Group name', 'Group description'),
);

test('New groups are listed by default', async () => {
await api.group.withId(groupId).create('Group name', 'Group description');
// Group should be listed
await expect(api.admin.config.get()).resolves.toMatchObject({
listedGroups: [groupId]
});
});
});
8 changes: 8 additions & 0 deletions tests/backend/group/item/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ describe('Other test cases', async () => {
.rejects.toMatchObject({ code: 404 });
});

test('New items are listed in their group by default', async () => {
await api.group.withId(groupId).item.withId('item-id').create('Item name', 'Item description');
// Item should be listed
await expect(api.group.withId(groupId).info.get()).resolves.toMatchObject({
listedItems: ['item-id']
});
});

genTokenTests(
() => api,
api => api.group.withId(groupId).item.withId('item-id').create('My item', ''),
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"moduleResolution": "bundler",
},
// Add Jest extended to vitest
"files": ["tests/setup/jestExtended.d.ts"],
"files": [
"tests/setup/jestExtended.d.ts"
],
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
// except $lib which is handled by https://kit.svelte.dev/docs/configuration#files
//
Expand Down

0 comments on commit 6112d66

Please sign in to comment.