Fixed fromDTO collection mapping

refs https://github.com/TryGhost/Team/issues/3260

- Whenever properties of the DTO are undefined they should be removed to avoid unintentional empty assignments to the stored collection
This commit is contained in:
Naz 2023-06-01 13:43:20 +07:00 committed by naz
parent a0a7c3a61b
commit 253f144501
2 changed files with 21 additions and 2 deletions

View File

@ -74,13 +74,22 @@ export class CollectionsService {
}
fromDTO(data: any): any {
return {
const mappedDTO: {[index: string]:any} = {
title: data.title,
slug: data.slug,
description: data.description,
featureImage: data.feature_image,
filter: data.filter
};
// delete out keys that contain undefined values
for (const key of Object.keys(mappedDTO)) {
if (mappedDTO[key] === undefined) {
delete mappedDTO[key];
}
}
return mappedDTO;
}
async createCollection(data: CollectionInputDTO): Promise<CollectionDTO> {

View File

@ -1,5 +1,5 @@
import assert from 'assert';
import {CollectionsService, CollectionsRepositoryInMemory} from '../src/index';
import {CollectionsService, CollectionsRepositoryInMemory, Collection} from '../src/index';
import {posts} from './fixtures/posts';
describe('CollectionsService', function () {
@ -40,6 +40,16 @@ describe('CollectionsService', function () {
assert.equal(deletedCollection, null, 'Collection should be deleted');
});
describe('toDTO', function () {
it('Can map Collection entity to DTO object', async function () {
const collection = await Collection.create({});
const dto = collectionsService.toDTO(collection);
assert.equal(dto.id, collection.id, 'DTO should have the same id as the entity');
assert.equal(dto.title, null, 'DTO should return null if nullable property of the entity is unassigned');
});
});
describe('addPostToCollection', function () {
it('Can add a Post to a Collection', async function () {
const collection = await collectionsService.createCollection({