Skip to content

Commit 0f306bd

Browse files
authored
Merge pull request #25 from klerick/fix-relation-link
fix(json-api-sdk): fix relationships link
2 parents 1f9865d + a70ba8e commit 0f306bd

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ export class TransformMixinService<T> {
217217
};
218218
}
219219
}
220+
const idNameField =
221+
table === this.currentResourceName
222+
? this.currentPrimaryField
223+
: this.relationPrimaryField.get(table);
220224
for (const itemRelation of relationList.values()) {
221225
const field = camelToKebab(itemRelation);
222226
if (relationships[itemRelation]) {
@@ -226,29 +230,21 @@ export class TransformMixinService<T> {
226230
links: {
227231
self: this.getLink(
228232
urlTable,
229-
data[this.relationPrimaryField.get(table)],
233+
data[idNameField],
230234
'relationships',
231235
field
232236
),
233-
related: this.getLink(
234-
urlTable,
235-
data[this.relationPrimaryField.get(table)],
236-
field
237-
),
237+
related: this.getLink(urlTable, data[idNameField], field),
238238
},
239239
};
240240
}
241-
const idNameField =
242-
table === this.currentResourceName
243-
? this.currentPrimaryField
244-
: this.relationPrimaryField.get(table);
245241
return {
246242
id: data[idNameField].toString(),
247243
type: urlTable,
248244
attributes,
249245
relationships,
250246
links: {
251-
self: this.getLink(urlTable, data['id']),
247+
self: this.getLink(urlTable, data[idNameField]),
252248
},
253249
};
254250
}
@@ -273,7 +269,7 @@ export class TransformMixinService<T> {
273269
: [itemRow[field]];
274270

275271
for (const includeItem of includeArray) {
276-
const idName = this.relationPrimaryField.get(field)
272+
const idName = this.relationPrimaryField.get(field);
277273
const id = includeItem[idName];
278274
if (result[field][id]) {
279275
continue;

0 commit comments

Comments
 (0)