@@ -7,7 +7,7 @@ import slugify from 'slugify';
77
88import { UpdateAreaDto } from '../../dtos' ;
99
10- import type { NewArea } from '../../../domain/entities' ;
10+ import type { Area } from '../../../domain/entities' ;
1111
1212@Injectable ( )
1313export class UpdateAreaUseCase {
@@ -24,7 +24,7 @@ export class UpdateAreaUseCase {
2424 'owner' ,
2525 ] ) ;
2626
27- const area = await this . areaRepo . findBySlug ( project . id , key ) ;
27+ const area = await this . areaRepo . findBySlug ( key , project . id ) ;
2828
2929 if ( ! area ) {
3030 throw new BaseException (
@@ -36,75 +36,13 @@ export class UpdateAreaUseCase {
3636 ) ;
3737 }
3838
39- const updateData : Partial < NewArea > = {
40- updatedAt : new Date ( ) . toISOString ( ) ,
41- ...( dto . title && dto . title !== area . title && { title : dto . title . trim ( ) } ) ,
42- ...( dto . description &&
43- dto . description !== area . description && {
44- description : dto . description ?. trim ( ) || null ,
45- } ) ,
46- ...( dto . descriptionHtml &&
47- dto . descriptionHtml !== area . descriptionHtml && {
48- descriptionHtml : dto . descriptionHtml ?. trim ( ) || null ,
49- } ) ,
50- ...( dto . color && dto . color !== area . color && { color : dto . color || null } ) ,
51- ...( dto . icon && dto . icon !== area . icon && { icon : dto . icon || null } ) ,
52- ...( dto . defaultView &&
53- dto . defaultView !== area . defaultView && { defaultView : dto . defaultView } ) ,
54- ...( dto . position &&
55- dto . position !== area . position &&
56- dto . position >= 0 && { position : dto . position } ) ,
57- ...( dto . maxTasksLimit &&
58- dto . maxTasksLimit !== area . maxTasksLimit &&
59- dto . maxTasksLimit > 0 && { maxTasksLimit : dto . maxTasksLimit } ) ,
60- ...( dto . isLocked && dto . isLocked !== area . isLocked && { isLocked : dto . isLocked } ) ,
61- } ;
62-
63- let hasChanges = false ;
39+ const updateData = this . buildUpdateData ( area , dto ) ;
6440
6541 if ( dto . slug && dto . slug !== area . slug ) {
66- let newSlug = dto . slug ;
67-
68- if ( newSlug ) {
69- newSlug = slugify ( newSlug , {
70- lower : true ,
71- strict : true ,
72- trim : true ,
73- } ) ;
74-
75- if ( ! newSlug ) {
76- throw new BaseException (
77- {
78- code : AreaErrorCodes . SLUG_INVALID ,
79- message : AreaErrorMessages [ AreaErrorCodes . SLUG_INVALID ] ,
80- } ,
81- HttpStatus . BAD_REQUEST ,
82- ) ;
83- }
84-
85- const existingArea = await this . areaRepo . findBySlug ( project . id , newSlug ) ;
86- if ( existingArea && existingArea . id !== area . id ) {
87- throw new BaseException (
88- {
89- code : AreaErrorCodes . SLUG_DUPLICATE ,
90- message : AreaErrorMessages [ AreaErrorCodes . SLUG_DUPLICATE ] ,
91- } ,
92- HttpStatus . CONFLICT ,
93- ) ;
94- }
95-
96- updateData . slug = newSlug ;
97- } else {
98- updateData . slug = slugify ( updateData . title || area . title , {
99- lower : true ,
100- strict : true ,
101- trim : true ,
102- } ) ;
103- }
104- hasChanges = true ;
42+ await this . updateSlug ( project . id , area , dto . slug , updateData ) ;
10543 }
10644
107- if ( ! hasChanges ) {
45+ if ( Object . keys ( updateData ) . length === 0 ) {
10846 return {
10947 success : true ,
11048 message : 'Нет изменений для обновления' ,
@@ -131,4 +69,86 @@ export class UpdateAreaUseCase {
13169 ) ;
13270 }
13371 }
72+
73+ private buildUpdateData ( area : Area , dto : UpdateAreaDto ) : Partial < Area > {
74+ const updateData : Partial < Area > = { } ;
75+
76+ if ( dto . title !== undefined && dto . title !== area . title ) {
77+ updateData . title = dto . title . trim ( ) ;
78+ }
79+
80+ if ( dto . description !== undefined && dto . description !== area . description ) {
81+ updateData . description = dto . description ?. trim ( ) ?? null ;
82+ }
83+
84+ if ( dto . descriptionHtml !== undefined && dto . descriptionHtml !== area . descriptionHtml ) {
85+ updateData . descriptionHtml = dto . descriptionHtml ?. trim ( ) ?? null ;
86+ }
87+
88+ if ( dto . color !== undefined && dto . color !== area . color ) {
89+ updateData . color = dto . color ?? null ;
90+ }
91+
92+ if ( dto . icon !== undefined && dto . icon !== area . icon ) {
93+ updateData . icon = dto . icon ?? null ;
94+ }
95+
96+ if ( dto . defaultView !== undefined && dto . defaultView !== area . defaultView ) {
97+ updateData . defaultView = dto . defaultView ;
98+ }
99+
100+ if ( dto . position !== undefined && dto . position !== area . position && dto . position >= 0 ) {
101+ updateData . position = dto . position ;
102+ }
103+
104+ if (
105+ dto . maxTasksLimit &&
106+ dto . maxTasksLimit !== area . maxTasksLimit &&
107+ dto . maxTasksLimit > 0
108+ ) {
109+ updateData . maxTasksLimit = dto . maxTasksLimit ;
110+ }
111+
112+ if ( dto . isLocked !== undefined && dto . isLocked !== area . isLocked ) {
113+ updateData . isLocked = dto . isLocked ;
114+ }
115+
116+ return updateData ;
117+ }
118+
119+ private async updateSlug (
120+ projectId : string ,
121+ area : Area ,
122+ slug : string ,
123+ updateData : Partial < Area > ,
124+ ) : Promise < void > {
125+ const newSlug = slugify ( slug , {
126+ lower : true ,
127+ strict : true ,
128+ trim : true ,
129+ } ) ;
130+
131+ if ( ! newSlug ) {
132+ throw new BaseException (
133+ {
134+ code : AreaErrorCodes . SLUG_INVALID ,
135+ message : AreaErrorMessages [ AreaErrorCodes . SLUG_INVALID ] ,
136+ } ,
137+ HttpStatus . BAD_REQUEST ,
138+ ) ;
139+ }
140+
141+ const existingArea = await this . areaRepo . findBySlug ( projectId , slug ) ;
142+ if ( existingArea && existingArea . id !== area . id ) {
143+ throw new BaseException (
144+ {
145+ code : AreaErrorCodes . SLUG_DUPLICATE ,
146+ message : AreaErrorMessages [ AreaErrorCodes . SLUG_DUPLICATE ] ,
147+ } ,
148+ HttpStatus . CONFLICT ,
149+ ) ;
150+ }
151+
152+ updateData . slug = slug ;
153+ }
134154}
0 commit comments