From 42539b954f02f2f91d283da625cf36172b8c8f50 Mon Sep 17 00:00:00 2001 From: Naz Date: Fri, 23 Jun 2023 13:53:25 +0700 Subject: [PATCH] Added collection editing method refs https://github.com/TryGhost/Team/issues/3169 - The collection entity properties used to be modified, which is not a good practice for logic encapsulation. Extracted editing logic to it's own method which can also contain validation logic when wrong data is passed when editing a collection. --- ghost/collections/src/Collection.ts | 37 +++++++++++++++++++ ghost/collections/src/CollectionsService.ts | 7 ++-- ghost/collections/test/Collection.test.ts | 39 +++++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/ghost/collections/src/Collection.ts b/ghost/collections/src/Collection.ts index b9870b0b9c..d9ce673dc4 100644 --- a/ghost/collections/src/Collection.ts +++ b/ghost/collections/src/Collection.ts @@ -12,6 +12,12 @@ const messages = { } }; +type CollectionPost = { + id: string; + featured?: boolean; + published_at?: Date; +} + export class Collection { id: string; title: string; @@ -40,6 +46,37 @@ export class Collection { } } + public edit(data: Partial) { + if (this.type === 'automatic' && (data.filter === null || data.filter === '')) { + throw new ValidationError({ + message: tpl(messages.invalidFilterProvided.message), + context: tpl(messages.invalidFilterProvided.context) + }); + } + + if (data.title !== undefined) { + this.title = data.title; + } + + if (data.slug !== undefined) { + this.slug = data.slug; + } + + if (data.description !== undefined) { + this.description = data.description; + } + + if (data.filter !== undefined) { + this.filter = data.filter; + } + + if (data.featureImage !== undefined) { + this.featureImage = data.featureImage; + } + + return this; + } + /** * @param post {{id: string}} - The post to add to the collection * @param index {number} - The index to insert the post at, use negative numbers to count from the end. diff --git a/ghost/collections/src/CollectionsService.ts b/ghost/collections/src/CollectionsService.ts index 4c177aac7f..4ef4e49b71 100644 --- a/ghost/collections/src/CollectionsService.ts +++ b/ghost/collections/src/CollectionsService.ts @@ -251,6 +251,9 @@ export class CollectionsService { return null; } + const collectionData = this.fromDTO(data); + await collection.edit(collectionData); + if (collection.type === 'manual' && data.posts) { for (const post of data.posts) { collection.addPost(post); @@ -261,10 +264,6 @@ export class CollectionsService { await this.updateAutomaticCollectionItems(collection, data.filter); } - const collectionData = this.fromDTO(data); - - Object.assign(collection, collectionData); - await this.collectionsRepository.save(collection); return this.toDTO(collection); diff --git a/ghost/collections/test/Collection.test.ts b/ghost/collections/test/Collection.test.ts index ded24c946a..9c09f256fe 100644 --- a/ghost/collections/test/Collection.test.ts +++ b/ghost/collections/test/Collection.test.ts @@ -122,6 +122,45 @@ describe('Collection', function () { }); }); + describe('edit', function () { + it('Can edit Collection values', async function () { + const collection = await Collection.create({ + slug: 'test-collection', + title: 'Testing edits', + type: 'automatic', + filter: 'featured:true' + }); + + assert.equal(collection.title, 'Testing edits'); + + collection.edit({ + title: 'Edited title', + slug: 'edited-slug' + }); + + assert.equal(collection.title, 'Edited title'); + assert.equal(collection.slug, 'edited-slug'); + }); + + it('Throws when the collection filter is empty', async function () { + const collection = await Collection.create({ + title: 'Testing edits', + type: 'automatic', + filter: 'featured:true' + }); + + assert.rejects(async () => { + await collection.edit({ + filter: null + }); + }, (err: any) => { + assert.equal(err.message, 'Invalid filter provided for automatic Collection', 'Error message should match'); + assert.equal(err.context, 'Automatic type of collection should always have a filter value', 'Error message should match'); + return true; + }); + }); + }); + it('Can add posts to different positions', async function () { const collection = await Collection.create({ title: 'Testing adding posts'