diff --git a/src/index.ts b/src/index.ts index 8e8eb46b..3d0e8581 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -198,6 +199,7 @@ export * from './itemPublished/itemPublished.js'; * Item Recycled */ export * from './itemRecycled/itemRecycled.js'; +export * from './itemRecycled/itemRecycled.factory.js'; /** * Item Tag diff --git a/src/item/appItem/appItem.factory.ts b/src/item/appItem/appItem.factory.ts index 293cd68c..c26f1b18 100644 --- a/src/item/appItem/appItem.factory.ts +++ b/src/item/appItem/appItem.factory.ts @@ -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 = ( @@ -27,14 +27,12 @@ export const AppItemFactory = ( export const PackedAppItemFactory = ( item: ItemFactoryInputType = {}, - packedInfo: Partial, + packedInfo: PackedInformationFactoryInput = {}, ): ItemFactoryOutputType => { const newItem = AppItemFactory(item); + const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem); return { ...newItem, - - // default packed info - permission: PermissionLevel.Admin, - ...packedInfo, + ...packed, }; }; diff --git a/src/item/documentItem/documentItem.factory.ts b/src/item/documentItem/documentItem.factory.ts index e3a2bf26..8bd494b7 100644 --- a/src/item/documentItem/documentItem.factory.ts +++ b/src/item/documentItem/documentItem.factory.ts @@ -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 = ( @@ -28,14 +28,12 @@ export const DocumentItemFactory = ( export const PackedDocumentItemFactory = ( item: ItemFactoryInputType = {}, - packedInfo: Partial, + packedInfo: PackedInformationFactoryInput = {}, ): ItemFactoryOutputType => { const newItem = DocumentItemFactory(item); + const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem); return { ...newItem, - - // default packed info - permission: PermissionLevel.Admin, - ...packedInfo, + ...packed, }; }; diff --git a/src/item/etherpadItem/etherpadItem.factory.ts b/src/item/etherpadItem/etherpadItem.factory.ts index 2f6c169c..6bb5c395 100644 --- a/src/item/etherpadItem/etherpadItem.factory.ts +++ b/src/item/etherpadItem/etherpadItem.factory.ts @@ -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 = ( @@ -29,14 +29,12 @@ export const EtherpadItemFactory = ( export const PackedEtherpadItemFactory = ( item: ItemFactoryInputType = {}, - packedInfo: Partial, + packedInfo: PackedInformationFactoryInput = {}, ): ItemFactoryOutputType => { const newItem = EtherpadItemFactory(item); + const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem); return { ...newItem, - - // default packed info - permission: PermissionLevel.Admin, - ...packedInfo, + ...packed, }; }; diff --git a/src/item/factory.test.ts b/src/item/factory.test.ts index 1a7cc2d3..c88d739b 100644 --- a/src/item/factory.test.ts +++ b/src/item/factory.test.ts @@ -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', () => { @@ -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(); + }); +}); diff --git a/src/item/factory.ts b/src/item/factory.ts index 25b6a077..ec40d5d2 100644 --- a/src/item/factory.ts +++ b/src/item/factory.ts @@ -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 = Pick< @@ -25,7 +28,8 @@ export type PackedItemFactoryOutputType = ItemFactoryOutputType & PackedInformation; export type ItemFactoryInputType = Partial & { - parentItem?: Pick; + parentItem?: Pick & + Partial>; }; export const PartialItemFactory = ( @@ -70,3 +74,47 @@ export const PartialItemFactory = ( lang: item.lang ?? faker.helpers.arrayElement(['fr', 'en']), }; }; + +export type PackedInformationFactoryInput = { + permission?: PermissionLevel; + hiddenTag?: Partial; + publicTag?: Partial; +}; + +export const PackedInformationFactory = ( + { + permission = PermissionLevel.Admin, + hiddenTag, + publicTag, + }: PackedInformationFactoryInput, + item: DiscriminatedItem, + parentItem: Partial> = {}, +): 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 } : {}), + }; +}; diff --git a/src/item/fileItem/fileItem.factory.ts b/src/item/fileItem/fileItem.factory.ts index fbb7ad95..a0607262 100644 --- a/src/item/fileItem/fileItem.factory.ts +++ b/src/item/fileItem/fileItem.factory.ts @@ -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 = ( @@ -33,15 +33,13 @@ export const LocalFileItemFactory = ( export const PackedLocalFileItemFactory = ( item: ItemFactoryInputType = {}, - packedInfo: Partial, + packedInfo: PackedInformationFactoryInput = {}, ): ItemFactoryOutputType => { const newItem = LocalFileItemFactory(item); + const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem); return { ...newItem, - - // default packed info - permission: PermissionLevel.Admin, - ...packedInfo, + ...packed, }; }; @@ -69,14 +67,12 @@ export const S3FileItemFactory = ( export const PackedS3FileItemFactory = ( item: ItemFactoryInputType = {}, - packedInfo: Partial, + packedInfo: PackedInformationFactoryInput = {}, ): ItemFactoryOutputType => { const newItem = S3FileItemFactory(item); + const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem); return { ...newItem, - - // default packed info - permission: PermissionLevel.Admin, - ...packedInfo, + ...packed, }; }; diff --git a/src/item/folderItem/folderItem.factory.ts b/src/item/folderItem/folderItem.factory.ts index 4b26f924..bad2b4fe 100644 --- a/src/item/folderItem/folderItem.factory.ts +++ b/src/item/folderItem/folderItem.factory.ts @@ -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 = {}, @@ -26,14 +26,12 @@ export const FolderItemFactory = ( export const PackedFolderItemFactory = ( item: ItemFactoryInputType = {}, - packedInfo: Partial = {}, + packedInfo: PackedInformationFactoryInput = {}, ): PackedItemFactoryOutputType => { const newItem = FolderItemFactory(item); + const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem); return { ...newItem, - - // default packed info - permission: PermissionLevel.Admin, - ...packedInfo, + ...packed, }; }; diff --git a/src/item/h5pItem/h5pItem.factory.ts b/src/item/h5pItem/h5pItem.factory.ts index ebb2398e..6d9e7a9c 100644 --- a/src/item/h5pItem/h5pItem.factory.ts +++ b/src/item/h5pItem/h5pItem.factory.ts @@ -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 = ( @@ -31,14 +31,12 @@ export const H5PItemFactory = ( export const PackedH5PItemFactory = ( item: ItemFactoryInputType = {}, - packedInfo: Partial = {}, + packedInfo: PackedInformationFactoryInput = {}, ): PackedItemFactoryOutputType => { const newItem = H5PItemFactory(item); + const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem); return { ...newItem, - - // default packed info - permission: PermissionLevel.Admin, - ...packedInfo, + ...packed, }; }; diff --git a/src/item/linkItem/linkItem.factory.ts b/src/item/linkItem/linkItem.factory.ts index 4d4a8e04..c0426327 100644 --- a/src/item/linkItem/linkItem.factory.ts +++ b/src/item/linkItem/linkItem.factory.ts @@ -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 { LinkItemType } from './linkItem.js'; -import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js'; import { faker } from '@faker-js/faker'; export const LinkItemFactory = ( @@ -38,14 +38,12 @@ export const LinkItemFactory = ( export const PackedLinkItemFactory = ( item: ItemFactoryInputType = {}, - packedInfo: Partial = {}, + packedInfo: PackedInformationFactoryInput = {}, ): PackedItemFactoryOutputType => { const newItem = LinkItemFactory(item); + const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem); return { ...newItem, - - // default packed info - permission: PermissionLevel.Admin, - ...packedInfo, + ...packed, }; }; diff --git a/src/item/packedItem.ts b/src/item/packedItem.ts index 4e152910..a9009d45 100644 --- a/src/item/packedItem.ts +++ b/src/item/packedItem.ts @@ -1,9 +1,12 @@ import { ItemMembership } from '../itemMembership/itemMembership.js'; import { DiscriminatedItem } from './item.js'; import { ItemSettings } from './itemSettings.js'; +import { ItemTag } from '@/itemTag/itemTag.js'; export type PackedInformation = { permission: ItemMembership['permission'] | null; + hidden?: ItemTag; + public?: ItemTag; }; /** diff --git a/src/item/shortcutItem/shortcutItem.factory.ts b/src/item/shortcutItem/shortcutItem.factory.ts index 3bf107c7..abe54fe1 100644 --- a/src/item/shortcutItem/shortcutItem.factory.ts +++ b/src/item/shortcutItem/shortcutItem.factory.ts @@ -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 { ShortcutItemType } from './shortcutItem.js'; -import { PermissionLevel } from '@/enums/permissionLevel/permissionLevel.js'; import { faker } from '@faker-js/faker'; export const ShortcutItemFactory = ( @@ -29,14 +29,12 @@ export const ShortcutItemFactory = ( export const PackedShortcutItemFactory = ( item: ItemFactoryInputType = {}, - packedInfo: Partial = {}, + packedInfo: PackedInformationFactoryInput = {}, ): PackedItemFactoryOutputType => { const newItem = ShortcutItemFactory(item); + const packed = PackedInformationFactory(packedInfo, newItem, item.parentItem); return { ...newItem, - - // default packed info - permission: PermissionLevel.Admin, - ...packedInfo, + ...packed, }; }; diff --git a/src/itemBookmark/itemBookmark.factory.ts b/src/itemBookmark/itemBookmark.factory.ts new file mode 100644 index 00000000..e4204226 --- /dev/null +++ b/src/itemBookmark/itemBookmark.factory.ts @@ -0,0 +1,37 @@ +import { ItemFactoryInputType } from '@/item/factory.js'; +import { + FolderItemFactory, + PackedFolderItemFactory, +} from '@/item/folderItem/folderItem.factory.js'; +import { FolderItemType } from '@/item/folderItem/folderItem.js'; +import { faker } from '@faker-js/faker'; + +export const ItemBookmarkFactory = ( + args: { + createdAt?: string; + item?: ItemFactoryInputType; + } = {}, +) => { + const createdAt: string = + args.createdAt ?? faker.date.anytime().toISOString(); + return { + id: faker.string.uuid(), + createdAt, + item: FolderItemFactory(args.item), + }; +}; + +export const PackedItemBookmarkFactory = ( + args: { + createdAt?: string; + item?: ItemFactoryInputType; + } = {}, +) => { + const createdAt: string = + args.createdAt ?? faker.date.anytime().toISOString(); + return { + id: faker.string.uuid(), + createdAt, + item: PackedFolderItemFactory(args.item), + }; +}; diff --git a/src/itemBookmark/itemBookmark.ts b/src/itemBookmark/itemBookmark.ts index d9028e8e..a606cdf7 100644 --- a/src/itemBookmark/itemBookmark.ts +++ b/src/itemBookmark/itemBookmark.ts @@ -1,4 +1,5 @@ import { DiscriminatedItem } from '@/item/item.js'; +import { PackedItem } from '@/item/packedItem.js'; import { UUID } from '@/types.js'; export type ItemBookmark = { @@ -7,6 +8,12 @@ export type ItemBookmark = { createdAt: string; }; +export type PackedItemBookmark = { + id: UUID; + item: PackedItem; + createdAt: string; +}; + /** * @deprecated use ItemBookmark */ diff --git a/src/itemLike/itemLike.ts b/src/itemLike/itemLike.ts index 732aab9c..a5839b63 100644 --- a/src/itemLike/itemLike.ts +++ b/src/itemLike/itemLike.ts @@ -1,4 +1,5 @@ import { DiscriminatedItem } from '@/item/item.js'; +import { PackedItem } from '@/item/packedItem.js'; import { UUID } from '@/types.js'; export type ItemLike = { @@ -6,3 +7,9 @@ export type ItemLike = { item: DiscriminatedItem; createdAt: string; }; + +export type PackedItemLike = { + id: UUID; + item: PackedItem; + createdAt: string; +}; diff --git a/src/itemPublished/itemPublished.ts b/src/itemPublished/itemPublished.ts index fe7c92ce..02472ae2 100644 --- a/src/itemPublished/itemPublished.ts +++ b/src/itemPublished/itemPublished.ts @@ -1,4 +1,4 @@ -import { DiscriminatedItem, Member } from '../index.js'; +import { DiscriminatedItem, Member, PackedItem } from '../index.js'; import { UUID } from '@/types.js'; export interface ItemPublished { @@ -8,3 +8,11 @@ export interface ItemPublished { item: DiscriminatedItem; totalViews: number; } + +export interface PackedItemPublished { + id: UUID; + creator?: Member; + createdAt: string; + item: PackedItem; + totalViews: number; +} diff --git a/src/itemRecycled/itemRecycled.factory.ts b/src/itemRecycled/itemRecycled.factory.ts new file mode 100644 index 00000000..37087bac --- /dev/null +++ b/src/itemRecycled/itemRecycled.factory.ts @@ -0,0 +1,43 @@ +import { ItemFactoryInputType } from '@/item/factory.js'; +import { + FolderItemFactory, + PackedFolderItemFactory, +} from '@/item/folderItem/folderItem.factory.js'; +import { FolderItemType } from '@/item/folderItem/folderItem.js'; +import { MemberFactory } from '@/member/factory.js'; +import { Member } from '@/member/member.js'; +import { faker } from '@faker-js/faker'; + +export const RecycledItemDataFactory = ( + args: { + creator?: Member; + createdAt?: string; + item?: ItemFactoryInputType; + } = {}, +) => { + const createdAt: string = + args.createdAt ?? faker.date.anytime().toISOString(); + return { + id: faker.string.uuid(), + creator: MemberFactory(args.creator), + createdAt, + item: FolderItemFactory(args.item), + }; +}; + +export const PackedRecycledItemDataFactory = ( + args: { + creator?: Member; + createdAt?: string; + item?: ItemFactoryInputType; + } = {}, +) => { + const createdAt: string = + args.createdAt ?? faker.date.anytime().toISOString(); + return { + id: faker.string.uuid(), + creator: MemberFactory(args.creator), + createdAt, + item: PackedFolderItemFactory(args.item), + }; +}; diff --git a/src/itemRecycled/itemRecycled.ts b/src/itemRecycled/itemRecycled.ts index 89faeaf0..7cc191b9 100644 --- a/src/itemRecycled/itemRecycled.ts +++ b/src/itemRecycled/itemRecycled.ts @@ -1,4 +1,5 @@ import { DiscriminatedItem } from '@/item/item.js'; +import { PackedItem } from '@/item/packedItem.js'; import { Member } from '@/member/member.js'; export type RecycledItemData = { @@ -7,3 +8,10 @@ export type RecycledItemData = { createdAt: string; item: DiscriminatedItem; }; + +export type PackedRecycledItemData = { + id: string; + creator: Member; + createdAt: string; + item: PackedItem; +}; diff --git a/src/itemTag/itemTag.factory.ts b/src/itemTag/itemTag.factory.ts new file mode 100644 index 00000000..0231d7b1 --- /dev/null +++ b/src/itemTag/itemTag.factory.ts @@ -0,0 +1,14 @@ +import { ItemTag } from './itemTag.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 & Pick, +): ItemTag => ({ + id: it.id ?? faker.string.uuid(), + createdAt: faker.date.anytime().toISOString(), + item: it.item ?? FolderItemFactory(), + type: it.type, + creator: it.creator ?? MemberFactory(), +});