Skip to content

Commit 9df7026

Browse files
author
Mateus Garcia
committed
fix: reduce relationships to unique values
1 parent 4697da2 commit 9df7026

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

libs/json-api-nestjs/src/lib/mixin/service/transform/transform.mixin.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,17 @@ export class TransformMixinService<T> {
186186
const propsData = data[field];
187187
const typeName = getEntityName(this.relationTarget.get(field));
188188
if (Array.isArray(propsData)) {
189-
builtData.data = propsData.map((i) => ({
190-
type: camelToKebab(typeName),
191-
id: i[this.relationPrimaryField.get(field)].toString(),
192-
}));
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+
}, []);
193200
}
194201

195202
if (!Array.isArray(data[field]) && typeof data[field] !== 'undefined') {

libs/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-one/get-one.spec.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm';
33
import { BadRequestException, NotFoundException } from '@nestjs/common';
44

55
import { TypeormMixinService } from '../../typeorm.mixin';
6-
import { mockDBTestModule, Users, Addresses } from '../../../../../mock-utils';
6+
import {
7+
mockDBTestModule,
8+
Users,
9+
Addresses,
10+
Notes,
11+
Comments,
12+
Roles,
13+
} from '../../../../../mock-utils';
714
import { ConfigParam, QueryField, QueryParams } from '../../../../../types';
815
import { DataSource, Repository } from 'typeorm';
916
import {
@@ -84,11 +91,42 @@ describe('GetOne methode test', () => {
8491
})
8592
);
8693

94+
await repository.manager.getRepository(Roles).save(
95+
Object.assign(new Roles(), {
96+
name: 'user',
97+
key: 'USER',
98+
isDefault: true,
99+
})
100+
);
101+
102+
const comments = await repository.manager.getRepository(Comments).save([
103+
Object.assign(new Comments(), {
104+
kind: 'COMMENT',
105+
text: 'text',
106+
}),
107+
Object.assign(new Comments(), {
108+
kind: 'COMMENT',
109+
text: 'text',
110+
}),
111+
Object.assign(new Comments(), {
112+
kind: 'COMMENT',
113+
text: 'text',
114+
}),
115+
]);
116+
117+
const notes = await repository.manager.getRepository(Notes).save([
118+
Object.assign(new Notes(), {
119+
text: 'text',
120+
}),
121+
]);
122+
87123
const user = {
88124
login: 'login',
89125
lastName: 'lastName',
90126
isActive: true,
91127
addresses: addresses,
128+
comments,
129+
notes,
92130
};
93131
await repository.save(Object.assign(new Users(), user));
94132
});
@@ -188,6 +226,17 @@ describe('GetOne methode test', () => {
188226
expect(selectSpy).toBeCalledWith([...defaultField['include'], aliasString]);
189227
});
190228

229+
it('included relationships should only return unique values', async () => {
230+
defaultField['include'] = ['comments', 'notes'];
231+
const res = await typeormService.getOne({
232+
query: defaultField,
233+
route: { id: params },
234+
});
235+
236+
expect(res.data['relationships'].comments.data.length).toBe(3);
237+
expect(res.data['relationships'].notes.data.length).toBe(1);
238+
});
239+
191240
it('Should be correct query with params', async () => {
192241
let joinSpy;
193242
let selectSpy;

0 commit comments

Comments
 (0)