Skip to content

Commit 599e99e

Browse files
authored
Merge pull request #35 from ctrl-hub/customer-interactions-tweaks
Customer interactions tweaks
2 parents cb9970a + 4c3ab24 commit 599e99e

37 files changed

+552
-98
lines changed

dist/Client.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { EquipmentCategoriesService } from '@services/EquipmentCategoriesService
2020
import { EquipmentModelsService } from '@services/EquipmentModelsService';
2121
import { PropertiesService } from '@services/PropertiesService';
2222
import { VehicleModelSpecificationService } from '@services/VehicleModelSpecificationService';
23-
import { CustomersService } from '@services/CustomersService';
23+
import { ContactsService } from '@services/ContactsService';
24+
import { CustomerAccountsService } from '@services/CustomerAccountsService';
2425
import { CustomerInteractionsService } from '@services/CustomerInteractionsService';
2526
import { TeamsService } from '@services/TeamsService';
2627
import { SchemesService } from '@services/SchemesService';
@@ -44,8 +45,9 @@ export declare class Client {
4445
operations(schemeId: string, workOrderId: string): OperationsService;
4546
operationTemplates(): OperationTemplatesService;
4647
serviceAccountKeys(): ServiceAccountKeysService;
47-
customers(): CustomersService;
48-
customerInteractions(customerId: string): CustomerInteractionsService;
48+
customerAccounts(customerAccountId?: string): CustomerAccountsService;
49+
contacts(): ContactsService;
50+
customerInteractions(): CustomerInteractionsService;
4951
serviceAccounts(): ServiceAccountsService;
5052
formCategories(): FormCategoriesService;
5153
forms(): FormsService;

dist/Client.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { EquipmentCategoriesService } from '@services/EquipmentCategoriesService
1919
import { EquipmentModelsService } from '@services/EquipmentModelsService';
2020
import { PropertiesService } from '@services/PropertiesService';
2121
import { VehicleModelSpecificationService } from '@services/VehicleModelSpecificationService';
22-
import { CustomersService } from '@services/CustomersService';
22+
import { ContactsService } from '@services/ContactsService';
23+
import { CustomerAccountsService } from '@services/CustomerAccountsService';
2324
import { CustomerInteractionsService } from '@services/CustomerInteractionsService';
2425
import { TeamsService } from '@services/TeamsService';
2526
import { SchemesService } from '@services/SchemesService';
@@ -83,11 +84,14 @@ export class Client {
8384
serviceAccountKeys() {
8485
return new ServiceAccountKeysService(this);
8586
}
86-
customers() {
87-
return new CustomersService(this);
87+
customerAccounts(customerAccountId) {
88+
return new CustomerAccountsService(this, customerAccountId);
8889
}
89-
customerInteractions(customerId) {
90-
return new CustomerInteractionsService(this, customerId);
90+
contacts() {
91+
return new ContactsService(this);
92+
}
93+
customerInteractions() {
94+
return new CustomerInteractionsService(this);
9195
}
9296
serviceAccounts() {
9397
return new ServiceAccountsService(this);

dist/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export type { InternalResponse } from './types/Response';
55
/**
66
* Models
77
*/
8-
export { Customer } from './models/Customer';
8+
export { Contact } from './models/Contact';
9+
export { CustomerAccount } from './models/CustomerAccount';
910
export { CustomerInteraction } from './models/CustomerInteraction';
1011
export { Equipment } from './models/Equipment';
1112
export { EquipmentCategory } from './models/EquipmentCategory';
@@ -17,6 +18,7 @@ export { FormCategory } from './models/FormCategory';
1718
export { Group } from './models/Group';
1819
export { Log } from './models/Log';
1920
export { Permission } from './models/Permission';
21+
export { Property } from './models/Property';
2022
export { Role } from './models/Role';
2123
export { Scheme } from './models/Scheme';
2224
export { WorkOrder } from './models/WorkOrder';

dist/index.js

Lines changed: 146 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -617,16 +617,22 @@ class Property extends BaseModel {
617617
pressure_tests = { source: "", id: "" };
618618
mprn = 0;
619619
mpan = 0;
620-
static relationships = [];
620+
static relationships = [
621+
{
622+
name: "pressure_tests",
623+
type: "array",
624+
modelType: "pressure-tests"
625+
}
626+
];
621627
constructor(data) {
622628
super(data);
623-
this.uprn = data?.attributes?.uprn ?? data.uprn ?? 0;
624-
this.location = data?.attributes?.location ?? data.location ?? { type: "", coordinates: [] };
625-
this.address = data?.attributes?.address ?? data.address ?? { description: "", department: "", organisation: "", number: "", name: "", thoroughfare: "", dependent_thoroughfare: "", post_town: "", postcode: "", pobox: "", country: "" };
626-
this.psr = data?.attributes?.psr ?? data.psr ?? { indicator: false, priority: 0, notes: "", contact: "" };
627-
this.pressure_tests = data?.attributes?.pressure_tests ?? data.pressure_tests ?? { source: "", id: "" };
628-
this.mprn = data?.attributes?.mprn ?? data.mprn ?? 0;
629-
this.mpan = data?.attributes?.mpan ?? data.mpan ?? 0;
629+
this.uprn = data?.attributes?.uprn ?? data?.uprn ?? 0;
630+
this.location = data?.attributes?.location ?? data?.location ?? { type: "", coordinates: [] };
631+
this.address = data?.attributes?.address ?? data?.address ?? { description: "", department: "", organisation: "", number: "", name: "", thoroughfare: "", dependent_thoroughfare: "", post_town: "", postcode: "", pobox: "", country: "" };
632+
this.psr = data?.attributes?.psr ?? data?.psr ?? { indicator: false, priority: 0, notes: "", contact: "" };
633+
this.pressure_tests = data?.attributes?.pressure_tests ?? data?.pressure_tests ?? { source: "", id: "" };
634+
this.mprn = data?.attributes?.mprn ?? data?.mprn ?? 0;
635+
this.mpan = data?.attributes?.mpan ?? data?.mpan ?? 0;
630636
}
631637
}
632638

@@ -853,22 +859,25 @@ class FormVersion extends BaseModel {
853859
}
854860
}
855861

856-
// src/models/Customer.ts
857-
class Customer extends BaseModel {
858-
type = "customers";
859-
name = "";
862+
// src/models/Contact.ts
863+
class Contact extends BaseModel {
864+
type = "contacts";
865+
salutation = "";
866+
first_name = "";
867+
last_name = "";
860868
telephone = "";
861869
email = "";
862-
property = "";
863870
jsonApiMapping() {
864871
return {
865-
attributes: ["name", "telephone", "email", "property"],
866-
relationships: {
867-
model: "customer-interactions"
868-
}
872+
attributes: ["salutation", "first_name", "last_name", "telephone", "email"]
869873
};
870874
}
871875
static relationships = [
876+
{
877+
name: "customer_accounts",
878+
type: "array",
879+
modelType: "customer-accounts"
880+
},
872881
{
873882
name: "representative",
874883
type: "array",
@@ -882,10 +891,36 @@ class Customer extends BaseModel {
882891
];
883892
constructor(data) {
884893
super(data);
885-
this.name = data?.attributes?.name ?? data?.name ?? "";
894+
this.salutation = data?.attributes?.salutation ?? data?.salutation ?? "";
895+
this.first_name = data?.attributes?.first_name ?? data?.first_name ?? "";
896+
this.last_name = data?.attributes?.last_name ?? data?.last_name ?? "";
886897
this.telephone = data?.attributes?.telephone ?? data?.telephone ?? "";
887898
this.email = data?.attributes?.email ?? data?.email ?? "";
888-
this.property = data?.attributes?.property ?? data?.property ?? "";
899+
}
900+
}
901+
902+
// src/models/CustomerAccount.ts
903+
class CustomerAccount extends BaseModel {
904+
type = "customer-accounts";
905+
static relationships = [
906+
{
907+
name: "properties",
908+
type: "array",
909+
modelType: "properties"
910+
},
911+
{
912+
name: "contacts",
913+
type: "array",
914+
modelType: "contacts"
915+
},
916+
{
917+
name: "interactions",
918+
type: "array",
919+
modelType: "customer-interactions"
920+
}
921+
];
922+
constructor(data) {
923+
super(data);
889924
}
890925
}
891926

@@ -899,11 +934,17 @@ class CustomerInteraction extends BaseModel {
899934
status = "";
900935
notes = "";
901936
representative;
937+
property;
938+
contact;
939+
customer_account;
902940
jsonApiMapping() {
903941
return {
904942
attributes: ["method", "direction", "date_time", "contacted", "status", "notes"],
905943
relationships: {
906-
representative: "users"
944+
representative: "users",
945+
property: "properties",
946+
contact: "contacts",
947+
customer_account: "customer-accounts"
907948
}
908949
};
909950
}
@@ -912,6 +953,21 @@ class CustomerInteraction extends BaseModel {
912953
name: "representative",
913954
type: "single",
914955
modelType: "users"
956+
},
957+
{
958+
name: "property",
959+
type: "single",
960+
modelType: "properties"
961+
},
962+
{
963+
name: "contact",
964+
type: "single",
965+
modelType: "contacts"
966+
},
967+
{
968+
name: "customer_account",
969+
type: "single",
970+
modelType: "customer-accounts"
915971
}
916972
];
917973
constructor(data) {
@@ -1032,10 +1088,24 @@ class OperationTemplate extends BaseModel {
10321088
}
10331089
}
10341090

1091+
// src/models/Street.ts
1092+
class Street extends BaseModel {
1093+
type = "streets";
1094+
usrn = 0;
1095+
location = { type: "", coordinates: [] };
1096+
static relationships = [];
1097+
constructor(data) {
1098+
super(data);
1099+
this.usrn = data?.attributes?.usrn ?? data.usrn ?? 0;
1100+
this.location = data?.attributes?.location ?? data.location ?? { type: "", coordinates: [] };
1101+
}
1102+
}
1103+
10351104
// src/utils/Hydrator.ts
10361105
class Hydrator {
10371106
modelMap = {
1038-
customers: Customer,
1107+
contacts: Contact,
1108+
"customer-accounts": CustomerAccount,
10391109
"customer-interactions": CustomerInteraction,
10401110
"equipment-categories": EquipmentCategory,
10411111
"equipment-items": Equipment,
@@ -1054,6 +1124,7 @@ class Hydrator {
10541124
schemes: Scheme,
10551125
"service-accounts": ServiceAccount,
10561126
"service-account-keys": ServiceAccountKey,
1127+
streets: Street,
10571128
submissions: Submission,
10581129
teams: Team,
10591130
users: User,
@@ -1214,7 +1285,7 @@ class JsonApiSerializer {
12141285
payload.data.relationships[key] = {
12151286
data: {
12161287
type: relationshipType,
1217-
id: relationshipValue
1288+
id: relationshipValue.id ?? relationshipValue
12181289
}
12191290
};
12201291
}
@@ -1266,6 +1337,21 @@ class JsonApiSerializer {
12661337
}
12671338
return this.buildDefaultPayload(model);
12681339
}
1340+
buildRelationshipPayload(model, relationships) {
1341+
const ModelClass = this.modelMap[model.type];
1342+
if (!ModelClass) {
1343+
console.warn(`No model class found for type: ${model.type}`);
1344+
return { data: [] };
1345+
}
1346+
const data = relationships.filter((relationship) => relationship.id !== undefined).map((relationship) => ({
1347+
type: model.type,
1348+
id: relationship.id
1349+
}));
1350+
const payload = {
1351+
data
1352+
};
1353+
return payload;
1354+
}
12691355
buildDefaultPayload(model) {
12701356
const { type, id, meta, links, included, _relationships, ...attributes } = model;
12711357
return {
@@ -1601,17 +1687,35 @@ class VehicleModelSpecificationService extends BaseService {
16011687
}
16021688
}
16031689

1604-
// src/services/CustomersService.ts
1605-
class CustomersService extends BaseService {
1690+
// src/services/ContactsService.ts
1691+
class ContactsService extends BaseService {
16061692
constructor(client) {
1607-
super(client, "/v3/orgs/:orgId/customers");
1693+
super(client, "/v3/orgs/:orgId/contacts");
1694+
}
1695+
}
1696+
1697+
// src/services/CustomerAccountsService.ts
1698+
class CustomerAccountsService extends BaseService {
1699+
constructor(client, customerAccountId) {
1700+
const endpoint = customerAccountId ? `/v3/orgs/:orgId/customer-accounts/${customerAccountId}` : `/v3/orgs/:orgId/customer-accounts`;
1701+
super(client, endpoint);
1702+
}
1703+
async patchProperties(properties) {
1704+
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
1705+
const payload = jsonApiSerializer.buildRelationshipPayload(new Property, properties);
1706+
return await this.client.makePatchRequest(`${this.endpoint}/relationships/properties`, payload);
1707+
}
1708+
async patchContacts(contacts) {
1709+
const jsonApiSerializer = new JsonApiSerializer(this.hydrator.getModelMap());
1710+
const payload = jsonApiSerializer.buildRelationshipPayload(new Contact, contacts);
1711+
return await this.client.makePatchRequest(`${this.endpoint}/relationships/contacts`, payload);
16081712
}
16091713
}
16101714

16111715
// src/services/CustomerInteractionsService.ts
16121716
class CustomerInteractionsService extends BaseService {
1613-
constructor(client, customerId) {
1614-
super(client, `/v3/orgs/:orgId/customers/${customerId}/interactions`);
1717+
constructor(client) {
1718+
super(client, "/v3/orgs/:orgId/customer-interactions");
16151719
}
16161720
}
16171721

@@ -1720,11 +1824,14 @@ class Client {
17201824
serviceAccountKeys() {
17211825
return new ServiceAccountKeysService(this);
17221826
}
1723-
customers() {
1724-
return new CustomersService(this);
1827+
customerAccounts(customerAccountId) {
1828+
return new CustomerAccountsService(this, customerAccountId);
17251829
}
1726-
customerInteractions(customerId) {
1727-
return new CustomerInteractionsService(this, customerId);
1830+
contacts() {
1831+
return new ContactsService(this);
1832+
}
1833+
customerInteractions() {
1834+
return new CustomerInteractionsService(this);
17281835
}
17291836
serviceAccounts() {
17301837
return new ServiceAccountsService(this);
@@ -1916,6 +2023,11 @@ class Operation extends BaseModel {
19162023
name: "properties",
19172024
type: "array",
19182025
modelType: "properties"
2026+
},
2027+
{
2028+
name: "streets",
2029+
type: "array",
2030+
modelType: "streets"
19192031
}
19202032
];
19212033
constructor(data) {
@@ -1953,6 +2065,7 @@ export {
19532065
Scheme,
19542066
Role,
19552067
RequestOptions,
2068+
Property,
19562069
Permission,
19572070
Operation,
19582071
Log,
@@ -1965,7 +2078,8 @@ export {
19652078
EquipmentCategory,
19662079
Equipment,
19672080
CustomerInteraction,
1968-
Customer,
2081+
CustomerAccount,
2082+
Contact,
19692083
ClientConfig,
19702084
Client
19712085
};

dist/models/Contact.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { RelationshipDefinition } from '../types/RelationshipDefinition';
2+
import { BaseModel } from '@models/BaseModel';
3+
import type { JsonApiMapping } from '../types/JsonApiMapping';
4+
export declare class Contact extends BaseModel implements Partial<JsonApiMapping> {
5+
type: string;
6+
salutation: string;
7+
first_name: string;
8+
last_name: string;
9+
telephone: string;
10+
email: string;
11+
jsonApiMapping(): {
12+
attributes: string[];
13+
};
14+
static relationships: RelationshipDefinition[];
15+
constructor(data?: any);
16+
}

0 commit comments

Comments
 (0)