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.
This commit is contained in:
Naz 2023-06-23 13:53:25 +07:00 committed by naz
parent 58a18d37ea
commit 42539b954f
3 changed files with 79 additions and 4 deletions

View File

@ -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<Collection>) {
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.

View File

@ -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);

View File

@ -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'