Skip to content

Commit 681e085

Browse files
committed
Added patch for vehicle equipment
1 parent b8561e5 commit 681e085

File tree

7 files changed

+116
-12
lines changed

7 files changed

+116
-12
lines changed

dist/Client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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', // @todo only required for cookie based auth,
234+
credentials: 'include',
235235
headers: headers,
236236
});
237237
let json = await fetchResponse.json();

dist/index.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,15 +1482,13 @@ class BaseService extends RequestBuilder {
14821482
};
14831483
}
14841484
async create(model, params) {
1485-
if (params) {
1486-
}
1485+
if (params) {}
14871486
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
14881487
const payload = jsonApiSerializer.buildCreatePayload(model);
14891488
return await this.client.makePostRequest(this.endpoint, payload);
14901489
}
14911490
async update(id, model, params) {
1492-
if (params) {
1493-
}
1491+
if (params) {}
14941492
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
14951493
const payload = jsonApiSerializer.buildUpdatePayload(model);
14961494
return await this.client.makePatchRequest(`${this.endpoint}/${id}`, payload);
@@ -1661,6 +1659,29 @@ class VehiclesService extends BaseService {
16611659
resp.data = hydrator.hydrateResponse(resp.data, resp.included);
16621660
return resp;
16631661
}
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;
1684+
}
16641685
}
16651686

16661687
// src/services/EquipmentService.ts
@@ -2132,9 +2153,6 @@ class ClientConfig {
21322153
}
21332154
// src/models/Organisation.ts
21342155
class Organisation extends BaseModel {
2135-
constructor() {
2136-
super(...arguments);
2137-
}
21382156
type = "organisations";
21392157
static relationships = [];
21402158
}

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;
16+
id?: string | undefined;
1717
type: string;
1818
attributes: Record<string, any>;
1919
relationships?: Record<string, {
2020
data: {
2121
type: string;
2222
id: string;
2323
};
24-
}>;
24+
}> | undefined;
2525
};
2626
};
2727
get(): Promise<InternalResponse<T[]>>;

dist/services/VehiclesService.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,25 @@ 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+
};
620
export declare class VehiclesService extends BaseService<Vehicle> {
721
constructor(client: Client);
822
enquiry(registration: string): Promise<InternalResponse<any[]>>;
923
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;
1026
}
27+
export {};

dist/services/VehiclesService.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,29 @@ export class VehiclesService extends BaseService {
2525
resp.data = hydrator.hydrateResponse(resp.data, resp.included);
2626
return resp;
2727
}
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;
52+
}
2853
}

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, // @todo convert to own version
15+
ok: fetchResponse.ok,
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, // If there's no response, status code is 0
32+
statusCode: error.statusCode || 0,
3333
headers: error.headers,
3434
data: null,
3535
errors: {

src/services/VehiclesService.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ import type { InternalResponse } from '../types/Response';
55
import { Hydrator } from "@utils/Hydrator";
66
import type { MotRecord } from "@models/MotRecord";
77

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+
};
22+
823
export class VehiclesService extends BaseService<Vehicle> {
924
constructor(client: Client) {
1025
super(client, "/v3/orgs/:orgId/assets/vehicles");
@@ -34,4 +49,33 @@ export class VehiclesService extends BaseService<Vehicle> {
3449

3550
return resp;
3651
}
52+
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);
57+
}
58+
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+
}
3781
}

0 commit comments

Comments
 (0)