Skip to content

Commit 4bf0756

Browse files
committed
Simplify JsonSerializer relationship logic
1 parent 95c0ac1 commit 4bf0756

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

src/utils/JsonSerializer.ts

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,12 @@ export class JsonApiSerializer {
7070
} else {
7171
const value = (model as any)[relationship.name];
7272
if (value) {
73-
// value could be a string (ID) or an object
74-
if (typeof value === 'string') {
75-
payload.data.relationships![relationship.name] = {
76-
data: {
77-
type: relationship.modelType,
78-
id: value,
79-
},
80-
};
81-
} else {
82-
payload.data.relationships![relationship.name] = {
83-
data: {
84-
type: relationship.modelType,
85-
id: value.id,
86-
},
87-
};
88-
}
73+
payload.data.relationships![relationship.name] = {
74+
data: {
75+
type: relationship.modelType,
76+
id: value.id ?? value,
77+
},
78+
};
8979
}
9080
}
9181
});
@@ -145,22 +135,12 @@ export class JsonApiSerializer {
145135
} else {
146136
const value = (model as any)[relationship.name];
147137
if (value) {
148-
// value could be a string (ID) or an object
149-
if (typeof value === 'string') {
150-
payload.data.relationships![relationship.name] = {
151-
data: {
152-
type: relationship.modelType,
153-
id: value,
154-
},
155-
};
156-
} else {
157-
payload.data.relationships![relationship.name] = {
158-
data: {
159-
type: relationship.modelType,
160-
id: value.id,
161-
},
162-
};
163-
}
138+
payload.data.relationships![relationship.name] = {
139+
data: {
140+
type: relationship.modelType,
141+
id: value.id ?? value,
142+
},
143+
};
164144
}
165145
}
166146
});

tests/utils/JsonSerializer/CustomerAccountAndRelationships.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { CustomerAccount, Property, Contact } from '../../../src';
55

66
describe('JsonApiSerializer for Customer Account and Relationships', () => {
77
const hydrator = new Hydrator();
8-
const jsonApiSerializer = new JsonApiSerializer(hydrator.getModelMap());
98

109
describe('buildCreatePayload', () => {
1110
it('should transform customer account and relationships to correct JSONAPI format', () => {

0 commit comments

Comments
 (0)