Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bookmark, like, recycled, published packed item #470

Merged
merged 13 commits into from
Apr 25, 2024
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export * from './item/shortcutItem/shortcutItem.factory.js';
* Item Bookmark
*/
export * from './itemBookmark/itemBookmark.js';
export * from './itemBookmark/itemBookmark.factory.js';

/**
* Item Flag
Expand Down Expand Up @@ -198,6 +199,7 @@ export * from './itemPublished/itemPublished.js';
* Item Recycled
*/
export * from './itemRecycled/itemRecycled.js';
export * from './itemRecycled/itemRecycled.factory.js';

/**
* Item Tag
Expand Down
12 changes: 5 additions & 7 deletions src/item/appItem/appItem.factory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
ItemFactoryInputType,
ItemFactoryOutputType,
PackedInformationFactory,
PackedInformationFactoryInput,
PartialItemFactory,
} from '../factory.js';
import { ItemType } from '../itemType.js';
import { PackedInformation } from '../packedItem.js';
import { AppItemType } from './appItem.js';
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';
import { faker } from '@faker-js/faker';

export const AppItemFactory = (
Expand All @@ -27,14 +27,12 @@ export const AppItemFactory = (

export const PackedAppItemFactory = (
item: ItemFactoryInputType<AppItemType> = {},
packedInfo: Partial<PackedInformation>,
packedInfo: PackedInformationFactoryInput = {},
): ItemFactoryOutputType<AppItemType> => {
const newItem = AppItemFactory(item);
const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem);
return {
...newItem,

// default packed info
permission: PermissionLevel.Admin,
...packedInfo,
...packed,
};
};
12 changes: 5 additions & 7 deletions src/item/documentItem/documentItem.factory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
ItemFactoryInputType,
ItemFactoryOutputType,
PackedInformationFactory,
PackedInformationFactoryInput,
PartialItemFactory,
} from '../factory.js';
import { ItemType } from '../itemType.js';
import { PackedInformation } from '../packedItem.js';
import { DocumentItemType } from './documentItem.js';
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';
import { faker } from '@faker-js/faker';

export const DocumentItemFactory = (
Expand All @@ -28,14 +28,12 @@ export const DocumentItemFactory = (

export const PackedDocumentItemFactory = (
item: ItemFactoryInputType<DocumentItemType> = {},
packedInfo: Partial<PackedInformation>,
packedInfo: PackedInformationFactoryInput = {},
): ItemFactoryOutputType<DocumentItemType> => {
const newItem = DocumentItemFactory(item);
const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem);
return {
...newItem,

// default packed info
permission: PermissionLevel.Admin,
...packedInfo,
...packed,
};
};
12 changes: 5 additions & 7 deletions src/item/etherpadItem/etherpadItem.factory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
ItemFactoryInputType,
ItemFactoryOutputType,
PackedInformationFactory,
PackedInformationFactoryInput,
PartialItemFactory,
} from '../factory.js';
import { ItemType } from '../itemType.js';
import { PackedInformation } from '../packedItem.js';
import { EtherpadItemType } from './etherpadItem.js';
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';
import { faker } from '@faker-js/faker';

export const EtherpadItemFactory = (
Expand All @@ -29,14 +29,12 @@ export const EtherpadItemFactory = (

export const PackedEtherpadItemFactory = (
item: ItemFactoryInputType<EtherpadItemType> = {},
packedInfo: Partial<PackedInformation>,
packedInfo: PackedInformationFactoryInput = {},
): ItemFactoryOutputType<EtherpadItemType> => {
const newItem = EtherpadItemFactory(item);
const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem);
return {
...newItem,

// default packed info
permission: PermissionLevel.Admin,
...packedInfo,
...packed,
};
};
60 changes: 59 additions & 1 deletion src/item/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { describe, expect, it } from 'vitest';

import { PartialItemFactory } from './factory.js';
import { PackedInformationFactory, PartialItemFactory } from './factory.js';
import {
FolderItemFactory,
PackedFolderItemFactory,
} from './folderItem/folderItem.factory.js';
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';
import { ItemTagType } from '@/itemTag/itemTag.js';

describe('Base Item Factory', () => {
it('Returns correct path for id', () => {
Expand Down Expand Up @@ -46,3 +52,55 @@ describe('Base Item Factory', () => {
expect(typeof item.displayName).toBe('string');
});
});

describe('Packed Item Factory', () => {
it('Returns correct default packed info', () => {
const item = FolderItemFactory();
const info = PackedInformationFactory({}, item);
expect(info.permission).toEqual(PermissionLevel.Admin);
expect(info.hidden).toBeUndefined();
expect(info.public).toBeUndefined();
});
it('Returns correct packed info with given hidden tag', () => {
const item = FolderItemFactory();
const hiddenTag = { type: ItemTagType.Hidden, item };

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

const info = PackedInformationFactory({ publicTag: publicTag }, item);
expect(info.permission).toEqual(PermissionLevel.Admin);
expect(info.public).toEqual(expect.objectContaining(publicTag));
expect(info.hidden).toBeUndefined();
});
it('Returns correct packed info inheriting parent public tag', () => {
const parent = FolderItemFactory();
const publicTag = { type: ItemTagType.Public, item: parent };
const parentItem = PackedFolderItemFactory(parent, {
publicTag: publicTag,
});
const item = FolderItemFactory({ parentItem });
const info = PackedInformationFactory({}, item, parentItem);
expect(info.permission).toEqual(PermissionLevel.Admin);
expect(info.public).toEqual(expect.objectContaining(publicTag));
expect(info.hidden).toBeUndefined();
});
});
52 changes: 50 additions & 2 deletions src/item/factory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { MemberFactory } from '../member/factory.js';
import { DiscriminatedItem } from './item.js';
import { buildPathFromIds } from './itemUtils.js';
import { PackedInformation } from './packedItem.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 { faker } from '@faker-js/faker';

export type ItemFactoryOutputType<IT extends DiscriminatedItem> = Pick<
Expand All @@ -25,7 +28,8 @@ export type PackedItemFactoryOutputType<IT extends DiscriminatedItem> =
ItemFactoryOutputType<IT> & PackedInformation;

export type ItemFactoryInputType<IT extends DiscriminatedItem> = Partial<IT> & {
parentItem?: Pick<IT, 'path'>;
parentItem?: Pick<PackedItem, 'path'> &
Partial<Pick<PackedItem, 'hidden' | 'public'>>;
};

export const PartialItemFactory = <IT extends DiscriminatedItem>(
Expand Down Expand Up @@ -70,3 +74,47 @@ export const PartialItemFactory = <IT extends DiscriminatedItem>(
lang: item.lang ?? faker.helpers.arrayElement(['fr', 'en']),
};
};

export type PackedInformationFactoryInput = {
permission?: PermissionLevel;
hiddenTag?: Partial<ItemTag>;
publicTag?: Partial<ItemTag>;
};

export const PackedInformationFactory = (
{
permission = PermissionLevel.Admin,
hiddenTag,
publicTag,
}: 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,
item,
...hiddenTag,
});
}

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

return {
permission,
...(hiddenItemTag ? { hidden: hiddenItemTag } : {}),
...(publicItemTag ? { public: publicItemTag } : {}),
};
};
20 changes: 8 additions & 12 deletions src/item/fileItem/fileItem.factory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
ItemFactoryInputType,
ItemFactoryOutputType,
PackedInformationFactory,
PackedInformationFactoryInput,
PartialItemFactory,
} from '../factory.js';
import { ItemType } from '../itemType.js';
import { PackedInformation } from '../packedItem.js';
import { LocalFileItemType, S3FileItemType } from './fileItem.js';
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';
import { faker } from '@faker-js/faker';

export const LocalFileItemFactory = (
Expand All @@ -33,15 +33,13 @@ export const LocalFileItemFactory = (

export const PackedLocalFileItemFactory = (
item: ItemFactoryInputType<LocalFileItemType> = {},
packedInfo: Partial<PackedInformation>,
packedInfo: PackedInformationFactoryInput = {},
): ItemFactoryOutputType<LocalFileItemType> => {
const newItem = LocalFileItemFactory(item);
const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem);
return {
...newItem,

// default packed info
permission: PermissionLevel.Admin,
...packedInfo,
...packed,
};
};

Expand Down Expand Up @@ -69,14 +67,12 @@ export const S3FileItemFactory = (

export const PackedS3FileItemFactory = (
item: ItemFactoryInputType<S3FileItemType> = {},
packedInfo: Partial<PackedInformation>,
packedInfo: PackedInformationFactoryInput = {},
): ItemFactoryOutputType<S3FileItemType> => {
const newItem = S3FileItemFactory(item);
const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem);
return {
...newItem,

// default packed info
permission: PermissionLevel.Admin,
...packedInfo,
...packed,
};
};
12 changes: 5 additions & 7 deletions src/item/folderItem/folderItem.factory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ItemFactoryInputType,
ItemFactoryOutputType,
PackedInformationFactory,
PackedInformationFactoryInput,
PackedItemFactoryOutputType,
PartialItemFactory,
} from '../factory.js';
import { ItemType } from '../itemType.js';
import { PackedInformation } from '../packedItem.js';
import { FolderItemType } from './folderItem.js';
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';

export const FolderItemFactory = (
item: ItemFactoryInputType<FolderItemType> = {},
Expand All @@ -26,14 +26,12 @@ export const FolderItemFactory = (

export const PackedFolderItemFactory = (
item: ItemFactoryInputType<FolderItemType> = {},
packedInfo: Partial<PackedInformation> = {},
packedInfo: PackedInformationFactoryInput = {},
): PackedItemFactoryOutputType<FolderItemType> => {
const newItem = FolderItemFactory(item);
const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem);
return {
...newItem,

// default packed info
permission: PermissionLevel.Admin,
...packedInfo,
...packed,
};
};
12 changes: 5 additions & 7 deletions src/item/h5pItem/h5pItem.factory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ItemFactoryInputType,
ItemFactoryOutputType,
PackedInformationFactory,
PackedInformationFactoryInput,
PackedItemFactoryOutputType,
PartialItemFactory,
} from '../factory.js';
import { ItemType } from '../itemType.js';
import { PackedInformation } from '../packedItem.js';
import { H5PItemType } from './h5pItem.js';
import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js';
import { faker } from '@faker-js/faker';

export const H5PItemFactory = (
Expand All @@ -31,14 +31,12 @@ export const H5PItemFactory = (

export const PackedH5PItemFactory = (
item: ItemFactoryInputType<H5PItemType> = {},
packedInfo: Partial<PackedInformation> = {},
packedInfo: PackedInformationFactoryInput = {},
): PackedItemFactoryOutputType<H5PItemType> => {
const newItem = H5PItemFactory(item);
const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem);
return {
...newItem,

// default packed info
permission: PermissionLevel.Admin,
...packedInfo,
...packed,
};
};
Loading