Skip to content

Commit d9bf3f6

Browse files
authored
Merge pull request #38 from ctrl-hub/update-tests
Update tests
2 parents a4a487b + 03e94a3 commit d9bf3f6

File tree

10 files changed

+353
-206
lines changed

10 files changed

+353
-206
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "git",
55
"url": "https://github.com/ctrl-hub/sdk.ts"
66
},
7-
"version": "0.1.117",
7+
"version": "0.1.118",
88
"main": "dist/index.js",
99
"types": "dist/index.d.ts",
1010
"type": "module",

src/Client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { OperationsService } from '@services/OperationsService';
3131
import { OperationTemplatesService } from '@services/OperationTemplatesService';
3232
import { VehicleInspectionService } from '@services/VehicleInspectionService';
3333
import { VehicleInventoryCheckService } from '@services/VehicleInventoryCheckService';
34+
import { AppointmentsService } from '@services/AppointmentsService';
3435

3536
export class Client {
3637
readonly config: ClientConfigInterface;
@@ -126,6 +127,10 @@ export class Client {
126127
return new FormsService(this);
127128
}
128129

130+
public appointments(): AppointmentsService {
131+
return new AppointmentsService(this);
132+
}
133+
129134
public teams(): TeamsService {
130135
return new TeamsService(this);
131136
}

src/models/CustomerAccount.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ export class CustomerAccount extends BaseModel {
2525
constructor(data?: any) {
2626
super(data);
2727
}
28+
29+
jsonApiMapping() {
30+
return {
31+
relationships: ['contacts', 'properties'],
32+
};
33+
}
2834
}

src/utils/Hydrator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { CustomerInteraction } from '@models/CustomerInteraction';
3131
import { Team } from '@models/Team';
3232
import { Scheme } from '@models/Scheme';
3333
import { WorkOrder } from '@models/WorkOrder';
34-
import { Operation } from 'index';
34+
import { Operation } from '@models/Operation';
3535
import { OperationTemplate } from '@models/OperationTemplate';
3636
import { Street } from '@models/Street';
3737

src/utils/JsonSerializer.ts

Lines changed: 23 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -32,75 +32,19 @@ export class JsonApiSerializer {
3232
}
3333

3434
buildCreatePayload(model: Model & Partial<JsonApiMapping>): JsonApiPayload {
35-
const ModelClass = this.modelMap[model.type];
36-
37-
if (!ModelClass) {
38-
console.warn(`No model class found for type: ${model.type}`);
39-
return this.buildDefaultPayload(model);
40-
}
41-
42-
const prototype = ModelClass.prototype;
43-
44-
if (typeof prototype.jsonApiMapping === 'function') {
45-
const mapping = prototype.jsonApiMapping.call(model);
46-
47-
const payload: JsonApiPayload = {
48-
data: {
49-
type: model.type,
50-
attributes: {},
51-
relationships: {},
52-
},
53-
};
54-
55-
prototype.constructor.relationships.forEach((relationship: {
56-
name: string;
57-
type: string;
58-
modelType: string;
59-
}) => {
60-
if (relationship.type === 'array') {
61-
const value = (model as any)[relationship.name];
62-
if (value) {
63-
payload.data.relationships![relationship.name] = {
64-
data: value.map((item: any) => ({
65-
type: relationship.modelType,
66-
id: item.id,
67-
})),
68-
};
69-
}
70-
} else {
71-
const value = (model as any)[relationship.name];
72-
if (value) {
73-
payload.data.relationships![relationship.name] = {
74-
data: {
75-
type: relationship.modelType,
76-
id: value.id,
77-
},
78-
};
79-
}
80-
}
81-
});
82-
83-
if (mapping.attributes) {
84-
mapping.attributes.forEach((attr: string) => {
85-
const value = (model as any)[attr];
86-
if (value !== undefined && value !== '') {
87-
payload.data.attributes[attr] = value;
88-
}
89-
});
90-
}
91-
92-
return payload;
93-
}
94-
95-
return this.buildDefaultPayload(model);
35+
return this.buildPayload(model, false);
9636
}
9737

9838
buildUpdatePayload(model: Model & Partial<JsonApiMapping>): JsonApiPayload {
39+
return this.buildPayload(model, true);
40+
}
41+
42+
private buildPayload(model: Model & Partial<JsonApiMapping>, isUpdate: boolean): JsonApiPayload {
9943
const ModelClass = this.modelMap[model.type];
10044

10145
if (!ModelClass) {
10246
console.warn(`No model class found for type: ${model.type}`);
103-
return this.buildDefaultPayload(model);
47+
return this.buildDefaultPayload(model, isUpdate);
10448
}
10549

10650
const prototype = ModelClass.prototype;
@@ -110,13 +54,16 @@ export class JsonApiSerializer {
11054

11155
const payload: JsonApiPayload = {
11256
data: {
113-
id: model.id,
11457
type: model.type,
11558
attributes: {},
11659
relationships: {},
11760
},
11861
};
11962

63+
if (isUpdate && model.id) {
64+
payload.data.id = model.id;
65+
}
66+
12067
prototype.constructor.relationships.forEach((relationship: {
12168
name: string;
12269
type: string;
@@ -138,7 +85,7 @@ export class JsonApiSerializer {
13885
payload.data.relationships![relationship.name] = {
13986
data: {
14087
type: relationship.modelType,
141-
id: value.id,
88+
id: typeof value === 'string' ? value : value.id,
14289
},
14390
};
14491
}
@@ -157,7 +104,7 @@ export class JsonApiSerializer {
157104
return payload;
158105
}
159106

160-
return this.buildDefaultPayload(model);
107+
return this.buildDefaultPayload(model, isUpdate);
161108
}
162109

163110
buildRelationshipPayload(model: Model, relationships: Array<Model>): JsonApiRelationshipsPayload {
@@ -174,20 +121,24 @@ export class JsonApiSerializer {
174121
type: model.type,
175122
id: relationship.id!,
176123
}));
177-
const payload: JsonApiRelationshipsPayload = {
178-
data: data,
179-
};
180124

181-
return payload;
125+
return { data };
182126
}
183127

184-
private buildDefaultPayload(model: Model): JsonApiPayload {
128+
private buildDefaultPayload(model: Model, includeId: boolean): JsonApiPayload {
185129
const { type, id, meta, links, included, _relationships, ...attributes } = model;
186-
return {
130+
131+
const payload: JsonApiPayload = {
187132
data: {
188133
type: model.type,
189134
attributes,
190135
},
191136
};
137+
138+
if (includeId && id) {
139+
payload.data.id = id;
140+
}
141+
142+
return payload;
192143
}
193-
}
144+
}

tests/models/ServiceAccountKey.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ describe('ServiceAccountKey', () => {
2525
const serviceAccountKey = new ServiceAccountKey(data);
2626

2727
expect(serviceAccountKey.id).toBe(data.id);
28-
expect(serviceAccountKey.client_id).toBe(data.attributes.client_id);
2928
expect(serviceAccountKey.meta).toEqual(data.meta);
3029
});
3130

tests/services/EquipementService/GetEquipment.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Equipment } from "@models/Equipment";
33
import { EquipmentModel } from "@models/EquipmentModel";
44
import { EquipmentManufacturer } from "@models/EquipmentManufacturer";
55
import { EquipmentService } from "@services/EquipmentService";
6-
import { Client } from "../src/Client";
6+
import { Client } from '../../../src';
77
import { Hydrator } from "@utils/Hydrator";
88

99
describe('Equipment Service', () => {

0 commit comments

Comments
 (0)