fix: content update

This commit is contained in:
yassinedorbozgithub 2024-10-03 04:12:38 +01:00
parent 6c02022d16
commit 764bd8c2b6
3 changed files with 18 additions and 10 deletions

View File

@ -39,7 +39,7 @@ import { SearchFilterPipe } from '@/utils/pipes/search-filter.pipe';
import { ContentTypeService } from './../services/content-type.service';
import { ContentService } from './../services/content.service';
import { ContentCreateDto } from '../dto/content.dto';
import { ContentCreateDto, ContentUpdateDto } from '../dto/content.dto';
import { ContentTransformInterceptor } from '../interceptors/content.interceptor';
import { ContentType } from '../schemas/content-type.schema';
import {
@ -319,16 +319,12 @@ export class ContentController extends BaseController<
* @returns The updated content document.
*/
@CsrfCheck(true)
@Patch('/:id')
@Patch(':id')
async updateOne(
@Body() contentDto: ContentCreateDto,
@Body() contentDto: ContentUpdateDto,
@Param('id') id: string,
): Promise<Content> {
const contentType = await this.contentTypeService.findOne(
contentDto.entity,
);
const newContent = this.filterDynamicFields(contentDto, contentType);
const updatedContent = await this.contentService.updateOne(id, newContent);
const updatedContent = await this.contentService.updateOne(id, contentDto);
if (!updatedContent) {
this.logger.warn(
`Failed to update content with id ${id}. Content not found.`,

View File

@ -32,3 +32,15 @@ export class ContentCreateDto {
@IsOptional()
dynamicFields?: Record<string, any>;
}
export class ContentUpdateDto {
@ApiPropertyOptional({ description: 'Content title', type: String })
@IsString()
@IsOptional()
title?: string;
@ApiPropertyOptional({ description: 'Content status', type: Boolean })
@IsBoolean()
@IsOptional()
status?: boolean;
}

View File

@ -14,9 +14,9 @@ export const preprocessDynamicFields = (
const { _csrf, title, status, entity, ...dynamicFields } = content;
const processed: ContentCreateDto & { _csrf?: string } = {
_csrf: _csrf?.toString(),
entity: entity.toString(),
entity: entity?.toString(),
status: !!status,
title: title.toString(),
title: title?.toString(),
dynamicFields,
};