File tree 1 file changed +15
-11
lines changed
libs/json-api-nestjs/src/lib/mixin/service/transform
1 file changed +15
-11
lines changed Original file line number Diff line number Diff line change @@ -186,17 +186,21 @@ export class TransformMixinService<T> {
186
186
const propsData = data [ field ] ;
187
187
const typeName = getEntityName ( this . relationTarget . get ( field ) ) ;
188
188
if ( Array . isArray ( propsData ) ) {
189
- builtData . data = propsData
190
- . map ( ( i ) => ( {
191
- type : camelToKebab ( typeName ) ,
192
- id : i [ this . relationPrimaryField . get ( field ) ] . toString ( ) ,
193
- } ) )
194
- . reduce ( ( acum , i ) => {
195
- if ( acum . find ( ( a ) => a . id === i . id ) ) {
196
- return acum ;
197
- }
198
- return [ ...acum , i ] ;
199
- } , [ ] ) ;
189
+ const tmp = propsData . reduce ( ( acum , item ) => {
190
+ const key = `${ camelToKebab ( typeName ) } :${ item [
191
+ this . relationPrimaryField . get ( field )
192
+ ] . toString ( ) } `;
193
+
194
+ if ( ! acum . has ( key ) ) {
195
+ acum . set ( key , {
196
+ type : camelToKebab ( typeName ) ,
197
+ id : item [ this . relationPrimaryField . get ( field ) ] . toString ( ) ,
198
+ } ) ;
199
+ }
200
+
201
+ return acum ;
202
+ } , new Map ( ) ) ;
203
+ builtData . data = [ ...tmp . values ( ) ] ;
200
204
}
201
205
202
206
if ( ! Array . isArray ( data [ field ] ) && typeof data [ field ] !== 'undefined' ) {
You can’t perform that action at this time.
0 commit comments