Skip to content

Commit 16e05c2

Browse files
committed
Update vehicle service, add updated patchEquipment
1 parent 681e085 commit 16e05c2

File tree

9 files changed

+40
-123
lines changed

9 files changed

+40
-123
lines changed

dist/Client.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export declare class Client {
5959
submissions(): SubmissionsService;
6060
permissions(): PermissionsService;
6161
groups(groupId?: string): GroupsService;
62-
vehicles(): VehiclesService;
62+
vehicles(vehicleId?: string): VehiclesService;
6363
vehicleManufacturers(): VehicleManufacturersService;
6464
vehicleCategories(): VehicleCategoriesService;
6565
vehicleModels(): VehicleModelsService;

dist/Client.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ export class Client {
120120
groups(groupId) {
121121
return new GroupsService(this, groupId);
122122
}
123-
vehicles() {
124-
return new VehiclesService(this);
123+
vehicles(vehicleId) {
124+
return new VehiclesService(this, vehicleId);
125125
}
126126
vehicleManufacturers() {
127127
return new VehicleManufacturersService(this);
@@ -231,7 +231,7 @@ export class Client {
231231
try {
232232
// @todo switch on cookie, "X-Session-Token" or client_credentials
233233
const fetchResponse = await fetch(url, {
234-
credentials: 'include',
234+
credentials: 'include', // @todo only required for cookie based auth,
235235
headers: headers,
236236
});
237237
let json = await fetchResponse.json();

dist/index.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,8 +1637,9 @@ class GroupsService extends BaseService {
16371637

16381638
// src/services/VehiclesService.ts
16391639
class VehiclesService extends BaseService {
1640-
constructor(client) {
1641-
super(client, "/v3/orgs/:orgId/assets/vehicles");
1640+
constructor(client, vehicleId) {
1641+
const endpoint = vehicleId ? `/v3/orgs/:orgId/assets/vehicles/${vehicleId}` : `/v3/orgs/:orgId/assets/vehicles`;
1642+
super(client, endpoint);
16421643
}
16431644
async enquiry(registration) {
16441645
const enquiryEndpoint = "/v3/assets/vehicles/enquiries";
@@ -1659,28 +1660,10 @@ class VehiclesService extends BaseService {
16591660
resp.data = hydrator.hydrateResponse(resp.data, resp.included);
16601661
return resp;
16611662
}
1662-
async patchEquipment(vehicleId, equipment) {
1663-
const payload = this.buildEquipmentRelationshipPayload(vehicleId, equipment, "equipment-items");
1664-
payload.data.id = vehicleId;
1665-
return await this.client.makePatchRequest(`${this.endpoint}/${vehicleId}`, payload);
1666-
}
1667-
buildEquipmentRelationshipPayload(vehicleId, relationships, relationshipType) {
1668-
const data = relationships.filter((relationship) => relationship !== undefined).map((relationship) => ({
1669-
type: relationshipType,
1670-
id: relationship
1671-
}));
1672-
const payload = {
1673-
data: {
1674-
id: vehicleId,
1675-
type: "vehicles",
1676-
relationships: {
1677-
equipment: {
1678-
data
1679-
}
1680-
}
1681-
}
1682-
};
1683-
return payload;
1663+
async patchEquipment(equipmentItems) {
1664+
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
1665+
const payload = jsonApiSerializer.buildRelationshipPayload(new Equipment, equipmentItems);
1666+
return await this.client.makePatchRequest(`${this.endpoint}/relationships/equipment`, payload);
16841667
}
16851668
}
16861669

@@ -1995,8 +1978,8 @@ class Client {
19951978
groups(groupId) {
19961979
return new GroupsService(this, groupId);
19971980
}
1998-
vehicles() {
1999-
return new VehiclesService(this);
1981+
vehicles(vehicleId) {
1982+
return new VehiclesService(this, vehicleId);
20001983
}
20011984
vehicleManufacturers() {
20021985
return new VehicleManufacturersService(this);

dist/services/BaseService.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export declare class BaseService<T extends Model> extends RequestBuilder {
1313
constructor(client: Client, endpoint: string);
1414
convertToJsonApi(model: Model): {
1515
data: {
16-
id?: string | undefined;
16+
id?: string;
1717
type: string;
1818
attributes: Record<string, any>;
1919
relationships?: Record<string, {
2020
data: {
2121
type: string;
2222
id: string;
2323
};
24-
}> | undefined;
24+
}>;
2525
};
2626
};
2727
get(): Promise<InternalResponse<T[]>>;

dist/services/VehiclesService.d.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,10 @@ import { BaseService } from "./BaseService";
33
import { Vehicle } from "../models/Vehicle";
44
import type { InternalResponse } from '../types/Response';
55
import type { MotRecord } from "../models/MotRecord";
6-
type JsonApiRelationshipsPayload = {
7-
data: {
8-
id: string;
9-
type: string;
10-
relationships: {
11-
equipment: {
12-
data: Array<{
13-
type: string;
14-
id: string;
15-
}>;
16-
};
17-
};
18-
};
19-
};
6+
import { Equipment } from "../models/Equipment";
207
export declare class VehiclesService extends BaseService<Vehicle> {
21-
constructor(client: Client);
8+
constructor(client: Client, vehicleId?: string);
229
enquiry(registration: string): Promise<InternalResponse<any[]>>;
2310
motRecords(vehicleId: string): Promise<InternalResponse<MotRecord[]>>;
24-
patchEquipment(vehicleId: string, equipment: Array<string>): Promise<any>;
25-
buildEquipmentRelationshipPayload(vehicleId: string, relationships: Array<string>, relationshipType: string): JsonApiRelationshipsPayload;
11+
patchEquipment(equipmentItems: Array<Equipment>): Promise<any>;
2612
}
27-
export {};

dist/services/VehiclesService.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { Client } from "../Client";
22
import { BaseService } from "./BaseService";
33
import { Vehicle } from "../models/Vehicle";
44
import { Hydrator } from "../utils/Hydrator";
5+
import { Equipment } from "../models/Equipment";
6+
import { JsonApiSerializer } from "../utils/JsonSerializer";
57
export class VehiclesService extends BaseService {
6-
constructor(client) {
7-
super(client, "/v3/orgs/:orgId/assets/vehicles");
8+
constructor(client, vehicleId) {
9+
const endpoint = vehicleId ? `/v3/orgs/:orgId/assets/vehicles/${vehicleId}` : `/v3/orgs/:orgId/assets/vehicles`;
10+
super(client, endpoint);
811
}
912
async enquiry(registration) {
1013
const enquiryEndpoint = '/v3/assets/vehicles/enquiries';
@@ -25,29 +28,9 @@ export class VehiclesService extends BaseService {
2528
resp.data = hydrator.hydrateResponse(resp.data, resp.included);
2629
return resp;
2730
}
28-
async patchEquipment(vehicleId, equipment) {
29-
const payload = this.buildEquipmentRelationshipPayload(vehicleId, equipment, 'equipment-items');
30-
payload.data.id = vehicleId;
31-
return await this.client.makePatchRequest(`${this.endpoint}/${vehicleId}`, payload);
32-
}
33-
buildEquipmentRelationshipPayload(vehicleId, relationships, relationshipType) {
34-
const data = relationships
35-
.filter(relationship => relationship !== undefined)
36-
.map(relationship => ({
37-
type: relationshipType,
38-
id: relationship,
39-
}));
40-
const payload = {
41-
data: {
42-
id: vehicleId,
43-
type: 'vehicles',
44-
relationships: {
45-
equipment: {
46-
data: data
47-
}
48-
}
49-
}
50-
};
51-
return payload;
31+
async patchEquipment(equipmentItems) {
32+
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
33+
const payload = jsonApiSerializer.buildRelationshipPayload(new Equipment, equipmentItems);
34+
return await this.client.makePatchRequest(`${this.endpoint}/relationships/equipment`, payload);
5235
}
5336
}

dist/utils/Requests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class Requests {
1212
}
1313
static buildInternalResponse(fetchResponse, json) {
1414
return {
15-
ok: fetchResponse.ok,
15+
ok: fetchResponse.ok, // @todo convert to own version
1616
statusCode: fetchResponse.status,
1717
headers: fetchResponse.headers,
1818
meta: json?.meta || null,
@@ -29,7 +29,7 @@ export class Requests {
2929
static buildInternalErrorResponse(error) {
3030
return {
3131
ok: false,
32-
statusCode: error.statusCode || 0,
32+
statusCode: error.statusCode || 0, // If there's no response, status code is 0
3333
headers: error.headers,
3434
data: null,
3535
errors: {

src/Client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ export class Client {
149149
return new GroupsService(this, groupId);
150150
}
151151

152-
public vehicles(): VehiclesService {
153-
return new VehiclesService(this);
152+
public vehicles(vehicleId?: string): VehiclesService {
153+
return new VehiclesService(this, vehicleId);
154154
}
155155

156156
public vehicleManufacturers(): VehicleManufacturersService {

src/services/VehiclesService.ts

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,13 @@ import {Vehicle} from "../models/Vehicle";
44
import type { InternalResponse } from '../types/Response';
55
import { Hydrator } from "@utils/Hydrator";
66
import type { MotRecord } from "@models/MotRecord";
7-
8-
type JsonApiRelationshipsPayload = {
9-
data: {
10-
id: string,
11-
type: string,
12-
relationships: {
13-
equipment: {
14-
data: Array<{
15-
type: string;
16-
id: string;
17-
}>;
18-
}
19-
}
20-
}
21-
};
7+
import { Equipment } from "@models/Equipment";
8+
import { JsonApiSerializer } from "@utils/JsonSerializer";
229

2310
export class VehiclesService extends BaseService<Vehicle> {
24-
constructor(client: Client) {
25-
super(client, "/v3/orgs/:orgId/assets/vehicles");
11+
constructor(client: Client, vehicleId?: string) {
12+
const endpoint = vehicleId ? `/v3/orgs/:orgId/assets/vehicles/${vehicleId}` : `/v3/orgs/:orgId/assets/vehicles`;
13+
super(client, endpoint);
2614
}
2715

2816
async enquiry(registration: string): Promise<InternalResponse<any[]>> {
@@ -50,32 +38,10 @@ export class VehiclesService extends BaseService<Vehicle> {
5038
return resp;
5139
}
5240

53-
public async patchEquipment(vehicleId: string, equipment: Array<string>) {
54-
const payload = this.buildEquipmentRelationshipPayload(vehicleId, equipment, 'equipment-items');
55-
payload.data.id = vehicleId;
56-
return await this.client.makePatchRequest(`${this.endpoint}/${vehicleId}`, payload);
41+
public async patchEquipment(equipmentItems: Array<Equipment>) {
42+
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
43+
const payload = jsonApiSerializer.buildRelationshipPayload(new Equipment, equipmentItems);
44+
return await this.client.makePatchRequest(`${this.endpoint}/relationships/equipment`, payload);
5745
}
5846

59-
buildEquipmentRelationshipPayload(vehicleId: string, relationships: Array<string>, relationshipType: string): JsonApiRelationshipsPayload {
60-
const data = relationships
61-
.filter(relationship => relationship !== undefined)
62-
.map(relationship => ({
63-
type: relationshipType,
64-
id: relationship,
65-
}));
66-
67-
const payload: JsonApiRelationshipsPayload = {
68-
data: {
69-
id: vehicleId,
70-
type: 'vehicles',
71-
relationships: {
72-
equipment: {
73-
data: data
74-
}
75-
}
76-
}
77-
};
78-
79-
return payload;
80-
}
8147
}

0 commit comments

Comments
 (0)