Skip to content

Commit e1d541a

Browse files
authored
Merge pull request #54 from ctrl-hub/add-vehicle-user-assignment
Allow patching of vehicle assignee
2 parents b8561e5 + c291102 commit e1d541a

File tree

11 files changed

+88
-28
lines changed

11 files changed

+88
-28
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: 2 additions & 2 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);

dist/index.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,17 @@ class JsonApiSerializer {
14351435
console.warn(`No model class found for type: ${model.type}`);
14361436
return { data: [] };
14371437
}
1438+
if (!Array.isArray(relationships)) {
1439+
if (relationships?.id !== undefined) {
1440+
return {
1441+
data: {
1442+
type: model.type,
1443+
id: relationships.id
1444+
}
1445+
};
1446+
}
1447+
return { data: null };
1448+
}
14381449
const data = relationships.filter((relationship) => relationship.id !== undefined).map((relationship) => ({
14391450
type: model.type,
14401451
id: relationship.id
@@ -1482,15 +1493,13 @@ class BaseService extends RequestBuilder {
14821493
};
14831494
}
14841495
async create(model, params) {
1485-
if (params) {
1486-
}
1496+
if (params) {}
14871497
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
14881498
const payload = jsonApiSerializer.buildCreatePayload(model);
14891499
return await this.client.makePostRequest(this.endpoint, payload);
14901500
}
14911501
async update(id, model, params) {
1492-
if (params) {
1493-
}
1502+
if (params) {}
14941503
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
14951504
const payload = jsonApiSerializer.buildUpdatePayload(model);
14961505
return await this.client.makePatchRequest(`${this.endpoint}/${id}`, payload);
@@ -1639,8 +1648,9 @@ class GroupsService extends BaseService {
16391648

16401649
// src/services/VehiclesService.ts
16411650
class VehiclesService extends BaseService {
1642-
constructor(client) {
1643-
super(client, "/v3/orgs/:orgId/assets/vehicles");
1651+
constructor(client, vehicleId) {
1652+
const endpoint = vehicleId ? `/v3/orgs/:orgId/assets/vehicles/${vehicleId}` : `/v3/orgs/:orgId/assets/vehicles`;
1653+
super(client, endpoint);
16441654
}
16451655
async enquiry(registration) {
16461656
const enquiryEndpoint = "/v3/assets/vehicles/enquiries";
@@ -1661,6 +1671,11 @@ class VehiclesService extends BaseService {
16611671
resp.data = hydrator.hydrateResponse(resp.data, resp.included);
16621672
return resp;
16631673
}
1674+
async patchAssignee(user) {
1675+
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
1676+
const payload = jsonApiSerializer.buildRelationshipPayload(new User, user);
1677+
return await this.client.makePatchRequest(`${this.endpoint}/relationships/assignee`, payload);
1678+
}
16641679
}
16651680

16661681
// src/services/EquipmentService.ts
@@ -1974,8 +1989,8 @@ class Client {
19741989
groups(groupId) {
19751990
return new GroupsService(this, groupId);
19761991
}
1977-
vehicles() {
1978-
return new VehiclesService(this);
1992+
vehicles(vehicleId) {
1993+
return new VehiclesService(this, vehicleId);
19791994
}
19801995
vehicleManufacturers() {
19811996
return new VehicleManufacturersService(this);
@@ -2132,9 +2147,6 @@ class ClientConfig {
21322147
}
21332148
// src/models/Organisation.ts
21342149
class Organisation extends BaseModel {
2135-
constructor() {
2136-
super(...arguments);
2137-
}
21382150
type = "organisations";
21392151
static relationships = [];
21402152
}

dist/services/VehiclesService.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Client } from "../Client";
22
import { BaseService } from "./BaseService";
33
import { Vehicle } from "../models/Vehicle";
4+
import { User } from "../models/User";
45
import type { InternalResponse } from '../types/Response';
56
import type { MotRecord } from "../models/MotRecord";
67
export declare class VehiclesService extends BaseService<Vehicle> {
7-
constructor(client: Client);
8+
constructor(client: Client, vehicleId?: string);
89
enquiry(registration: string): Promise<InternalResponse<any[]>>;
910
motRecords(vehicleId: string): Promise<InternalResponse<MotRecord[]>>;
11+
patchAssignee(user: User): Promise<any>;
1012
}

dist/services/VehiclesService.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Client } from "../Client";
22
import { BaseService } from "./BaseService";
33
import { Vehicle } from "../models/Vehicle";
4+
import { User } from "../models/User";
45
import { Hydrator } from "../utils/Hydrator";
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,4 +28,9 @@ export class VehiclesService extends BaseService {
2528
resp.data = hydrator.hydrateResponse(resp.data, resp.included);
2629
return resp;
2730
}
31+
async patchAssignee(user) {
32+
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
33+
const payload = jsonApiSerializer.buildRelationshipPayload(new User, user);
34+
return await this.client.makePatchRequest(`${this.endpoint}/relationships/assignee`, payload);
35+
}
2836
}

dist/utils/JsonSerializer.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ type JsonApiRelationship = {
77
};
88
};
99
type JsonApiRelationshipsPayload = {
10-
data: Array<{
10+
data: {
11+
type: string;
12+
id: string;
13+
} | Array<{
1114
type: string;
1215
id: string;
13-
}>;
16+
}> | null;
1417
};
1518
type JsonApiPayload = {
1619
data: {
@@ -26,7 +29,7 @@ export declare class JsonApiSerializer {
2629
buildCreatePayload(model: Model & Partial<JsonApiMapping>): JsonApiPayload;
2730
buildUpdatePayload(model: Model & Partial<JsonApiMapping>): JsonApiPayload;
2831
private buildPayload;
29-
buildRelationshipPayload(model: Model, relationships: Array<Model>): JsonApiRelationshipsPayload;
32+
buildRelationshipPayload(model: Model, relationships: Model | Array<Model>): JsonApiRelationshipsPayload;
3033
private buildDefaultPayload;
3134
}
3235
export {};

dist/utils/JsonSerializer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ export class JsonApiSerializer {
7070
console.warn(`No model class found for type: ${model.type}`);
7171
return { data: [] };
7272
}
73+
if (!Array.isArray(relationships)) {
74+
if (relationships?.id !== undefined) {
75+
return {
76+
data: {
77+
type: model.type,
78+
id: relationships.id
79+
}
80+
};
81+
}
82+
return { data: null };
83+
}
7384
const data = relationships
7485
.filter(relationship => relationship.id !== undefined)
7586
.map(relationship => ({

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.128",
7+
"version": "0.1.129",
88
"main": "dist/index.js",
99
"types": "dist/index.d.ts",
1010
"type": "module",

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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import {Client} from "Client";
22
import {BaseService} from "./BaseService";
33
import {Vehicle} from "../models/Vehicle";
4+
import {User} from "../models/User";
45
import type { InternalResponse } from '../types/Response';
56
import { Hydrator } from "@utils/Hydrator";
67
import type { MotRecord } from "@models/MotRecord";
8+
import { JsonApiSerializer } from '@utils/JsonSerializer';
79

810
export class VehiclesService extends BaseService<Vehicle> {
9-
constructor(client: Client) {
10-
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);
1114
}
1215

1316
async enquiry(registration: string): Promise<InternalResponse<any[]>> {
@@ -34,4 +37,10 @@ export class VehiclesService extends BaseService<Vehicle> {
3437

3538
return resp;
3639
}
40+
41+
public async patchAssignee(user: User) {
42+
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
43+
const payload = jsonApiSerializer.buildRelationshipPayload(new User, user);
44+
return await this.client.makePatchRequest(`${this.endpoint}/relationships/assignee`, payload);
45+
}
3746
}

0 commit comments

Comments
 (0)