Skip to content

Commit

Permalink
feat!: rename tags to item visibilities (#694)
Browse files Browse the repository at this point in the history
* feat: rename tags to item visibilities

* refactor: fix typo
  • Loading branch information
pyphilia authored Nov 6, 2024
1 parent aa8e034 commit 1d88253
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ export * from './itemRecycled/itemRecycled.js';
export * from './itemRecycled/itemRecycled.factory.js';

/**
* Item Tag
* Item Visibility
*/
export * from './itemTag/itemTag.js';
export * from './itemVisibility/itemVisibility.js';

/**
* Item Validation
Expand Down
44 changes: 27 additions & 17 deletions src/item/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
PackedFolderItemFactory,
} from './folderItem/folderItem.factory.js';
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';
import { ItemTagType } from '@/itemTag/itemTag.js';
import { ItemVisibilityType } from '@/itemVisibility/itemVisibility.js';

describe('Base Item Factory', () => {
it('Returns correct path for id', () => {
Expand Down Expand Up @@ -70,46 +70,56 @@ describe('Packed Item Factory', () => {
expect(info.hidden).toBeUndefined();
expect(info.public).toBeUndefined();
});
it('Returns correct packed info with given hidden tag', () => {
it('Returns correct packed info with given hidden visibility', () => {
const item = FolderItemFactory();
const hiddenTag = { type: ItemTagType.Hidden, item };
const hiddenVisibility = { item };

const info = PackedInformationFactory({ hiddenTag: hiddenTag }, item);
const info = PackedInformationFactory(
{ hiddenVisibility: hiddenVisibility },
item,
);
expect(info.permission).toEqual(PermissionLevel.Admin);
expect(info.hidden).toEqual(expect.objectContaining(hiddenTag));
expect(info.hidden).toEqual(expect.objectContaining(hiddenVisibility));
expect(info.hidden!.type).toEqual(ItemVisibilityType.Hidden);
expect(info.public).toBeUndefined();
});
it('Returns correct packed info inheriting parent hidden tag', () => {
it('Returns correct packed info inheriting parent hidden visibility', () => {
const parent = FolderItemFactory();
const hiddenTag = { type: ItemTagType.Hidden, item: parent };
const hiddenVisibility = { type: ItemVisibilityType.Hidden, item: parent };
const parentItem = PackedFolderItemFactory(parent, {
hiddenTag: hiddenTag,
hiddenVisibility: hiddenVisibility,
});
const item = FolderItemFactory({ parentItem });
const info = PackedInformationFactory({}, item, parentItem);
expect(info.permission).toEqual(PermissionLevel.Admin);
expect(info.hidden).toEqual(expect.objectContaining(hiddenTag));
expect(info.hidden).toEqual(expect.objectContaining(hiddenVisibility));
expect(info.hidden!.type).toEqual(ItemVisibilityType.Hidden);
expect(info.public).toBeUndefined();
});
it('Returns correct packed info with given public tag', () => {
it('Returns correct packed info with given public visibility', () => {
const item = FolderItemFactory();
const publicTag = { type: ItemTagType.Public, item };
const publicVisibility = { item };

const info = PackedInformationFactory({ publicTag: publicTag }, item);
const info = PackedInformationFactory(
{ publicVisibility: publicVisibility },
item,
);
expect(info.permission).toEqual(PermissionLevel.Admin);
expect(info.public).toEqual(expect.objectContaining(publicTag));
expect(info.public).toEqual(expect.objectContaining(publicVisibility));
expect(info.public!.type).toEqual(ItemVisibilityType.Public);
expect(info.hidden).toBeUndefined();
});
it('Returns correct packed info inheriting parent public tag', () => {
it('Returns correct packed info inheriting parent public visibility', () => {
const parent = FolderItemFactory();
const publicTag = { type: ItemTagType.Public, item: parent };
const publicVisibility = { type: ItemVisibilityType.Public, item: parent };
const parentItem = PackedFolderItemFactory(parent, {
publicTag: publicTag,
publicVisibility: publicVisibility,
});
const item = FolderItemFactory({ parentItem });
const info = PackedInformationFactory({}, item, parentItem);
expect(info.permission).toEqual(PermissionLevel.Admin);
expect(info.public).toEqual(expect.objectContaining(publicTag));
expect(info.public).toEqual(expect.objectContaining(publicVisibility));
expect(info.public!.type).toEqual(ItemVisibilityType.Public);
expect(info.hidden).toBeUndefined();
});
});
47 changes: 25 additions & 22 deletions src/item/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { buildPathFromIds } from './itemUtils.js';
import { PackedInformation, PackedItem } from './packedItem.js';
import { CCLicenseAdaptions } from '@/enums/ccLicenses.js';
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';
import { ItemTagFactory } from '@/itemTag/itemTag.factory.js';
import { ItemTag, ItemTagType } from '@/itemTag/itemTag.js';
import { ItemVisibilityFactory } from '@/itemVisibility/itemVisibility.factory.js';
import {
ItemVisibility,
ItemVisibilityType,
} from '@/itemVisibility/itemVisibility.js';
import { faker } from '@faker-js/faker';

export type ItemFactoryOutputType<IT extends DiscriminatedItem> = Pick<
Expand Down Expand Up @@ -79,44 +82,44 @@ export const PartialItemFactory = <IT extends DiscriminatedItem>(

export type PackedInformationFactoryInput = {
permission?: PermissionLevel | null;
hiddenTag?: Partial<ItemTag>;
publicTag?: Partial<ItemTag>;
hiddenVisibility?: Partial<ItemVisibility>;
publicVisibility?: Partial<ItemVisibility>;
};

export const PackedInformationFactory = (
{
permission = PermissionLevel.Admin,
hiddenTag,
publicTag,
hiddenVisibility,
publicVisibility,
}: PackedInformationFactoryInput,
item: DiscriminatedItem,
parentItem: Partial<Pick<PackedItem, 'hidden' | 'public'>> = {},
): PackedInformation => {
// use parent tag if exists
let hiddenItemTag = parentItem.hidden;
// use given hidden tag
if (hiddenTag) {
hiddenItemTag = ItemTagFactory({
type: ItemTagType.Hidden,
// use parent visibility if exists
let hiddenItemVisibility = parentItem.hidden;
// use given hidden visibility
if (hiddenVisibility) {
hiddenItemVisibility = ItemVisibilityFactory({
type: ItemVisibilityType.Hidden,
item,
...hiddenTag,
...hiddenVisibility,
});
}

// use parent tag if exists
let publicItemTag = parentItem.public;
// use given hidden tag
if (publicTag) {
publicItemTag = ItemTagFactory({
type: ItemTagType.Hidden,
// use parent visibility if exists
let publicItemVisibility = parentItem.public;
// use given hidden visibility
if (publicVisibility) {
publicItemVisibility = ItemVisibilityFactory({
type: ItemVisibilityType.Public,
item,
...publicTag,
...publicVisibility,
});
}

return {
permission,
...(hiddenItemTag ? { hidden: hiddenItemTag } : {}),
...(publicItemTag ? { public: publicItemTag } : {}),
...(hiddenItemVisibility ? { hidden: hiddenItemVisibility } : {}),
...(publicItemVisibility ? { public: publicItemVisibility } : {}),
};
};
6 changes: 3 additions & 3 deletions src/item/packedItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { ItemMembership } from '../itemMembership/itemMembership.js';
import { DiscriminatedItem } from './item.js';
import { ItemSettings } from './itemSettings.js';
import { ThumbnailsBySize } from '@/enums/thumbnailSizes.js';
import { ItemTag } from '@/itemTag/itemTag.js';
import { ItemVisibility } from '@/itemVisibility/itemVisibility.js';

export type PackedInformation = {
permission: ItemMembership['permission'] | null;
hidden?: ItemTag;
public?: ItemTag;
hidden?: ItemVisibility;
public?: ItemVisibility;
thumbnails?: ThumbnailsBySize;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ItemTag } from './itemTag.js';
import { ItemVisibility } from './itemVisibility.js';
import { FolderItemFactory } from '@/item/folderItem/folderItem.factory.js';
import { MemberFactory } from '@/member/factory.js';
import { faker } from '@faker-js/faker';

export const ItemTagFactory = (
it: Partial<ItemTag> & Pick<ItemTag, 'type'>,
): ItemTag => ({
export const ItemVisibilityFactory = (
it: Partial<ItemVisibility> & Pick<ItemVisibility, 'type'>,
): ItemVisibility => ({
id: it.id ?? faker.string.uuid(),
createdAt: faker.date.anytime().toISOString(),
item: it.item ?? FolderItemFactory(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { DiscriminatedItem } from '@/item/item.js';
import { Member } from '@/member/member.js';
import { UUID } from '@/types.js';

export enum ItemTagType {
export enum ItemVisibilityType {
Public = 'public',
Hidden = 'hidden',
}

export type ItemTag = {
export type ItemVisibility = {
id: UUID;
item: DiscriminatedItem;
type: `${ItemTagType}` | ItemTagType;
type: `${ItemVisibilityType}` | ItemVisibilityType;
createdAt: string;
creator: Member;
};

0 comments on commit 1d88253

Please sign in to comment.