Skip to content

Commit bae3bcf

Browse files
zman81988GitHub Enterprise
authored and
GitHub Enterprise
committed
Merge pull request #8 from zwolfson/support-uniuque-id
New properties to hold unique ids
2 parents 5269b63 + 57a13a9 commit bae3bcf

File tree

5 files changed

+126
-20
lines changed

5 files changed

+126
-20
lines changed

prisma/schema.prisma

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ model Authorization {
3030
}
3131

3232
model Properties {
33-
name String @db.VarChar(255)
34-
label String? @db.VarChar(255)
35-
type PropertyType
36-
object Objects
37-
customerId String @db.VarChar(255)
38-
modificationMetadata Json
33+
34+
name String @db.VarChar(255)
35+
label String? @db.VarChar(255)
36+
type PropertyType
37+
object Objects
38+
customerId String @db.VarChar(255)
39+
unique Boolean?
40+
modificationMetadata Json
41+
3942
4043
@@unique([name, object, customerId])
4144
}

prisma/seed.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ async function main(): Promise {
2626
},
2727
});
2828
console.log(firstname);
29+
const contactIdentifier = await prisma.properties.upsert({
30+
where: {
31+
name_object_customerId: {
32+
name: "native_system_contact_identifier",
33+
object: "Contact",
34+
customerId: "1",
35+
},
36+
},
37+
update: {},
38+
create: {
39+
name: "native_system_contact_identifier",
40+
label: "Native System Contact Identifier",
41+
type: "String",
42+
object: "Contact",
43+
customerId: "1",
44+
unique:true,
45+
},
46+
});
47+
console.log(contactIdentifier);
2948

3049
const lastname = await prisma.properties.upsert({
3150
where: {
@@ -96,6 +115,25 @@ async function main(): Promise {
96115
},
97116
});
98117
console.log(exampleRequired);
118+
const companyIdentifier = await prisma.properties.upsert({
119+
where: {
120+
name_object_customerId: {
121+
name: "native_system_company_identifier",
122+
object: "Contact",
123+
customerId: "1",
124+
},
125+
},
126+
update: {},
127+
create: {
128+
name: "native_system_company_identifier",
129+
label: "Native System Company Identifier",
130+
type: "String",
131+
object: "Company",
132+
customerId: "1",
133+
unique:true,
134+
},
135+
});
136+
console.log(companyIdentifier);
99137
const customCompany = await prisma.properties.upsert({
100138
where: {
101139
name_object_customerId: {

src/app.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import { authUrl, redeemCode } from "./auth";
44
import {
55
getHubSpotProperties,
66
getNativeProperties,
7-
createPropertyGroup,
8-
createRequiredProperty,
7+
createPropertyGroupForContacts,
8+
createRequiredContactProperty,
9+
createPropertyGroupForCompanies,
10+
createContactIdProperty,
11+
createCompanyIdProperty
912
} from "./properties";
1013
import { saveMapping, getMappings, deleteMapping } from "./mappings";
1114
import { PORT, getCustomerId } from "./utils";
@@ -26,8 +29,13 @@ app.get("/oauth-callback", async (req: Request, res: Response):Promise =>
2629
try {
2730
const authInfo = await redeemCode(code.toString());
2831
const accessToken = authInfo.accessToken;
29-
await createPropertyGroup(accessToken);
30-
await createRequiredProperty(accessToken);
32+
33+
createPropertyGroupForContacts(accessToken);
34+
createPropertyGroupForCompanies(accessToken)
35+
createRequiredContactProperty(accessToken);
36+
createContactIdProperty(accessToken)
37+
createCompanyIdProperty(accessToken)
38+
3139
res.redirect(`http://localhost:${PORT - 1}/`);
3240
} catch (error: any) {
3341
res.redirect(`/?errMessage=${error.message}`);

src/properties.ts

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { PropertyCache } from 'default';
55

66
const TTL = 5 * 60 * 1000; // 5 Minute TTL in milliseconds
77

8-
const createPropertyGroup = async (accessToken: string):Promise<void> => {
8+
9+
export const createPropertyGroupForContacts = async (accessToken: string) => {
10+
911
hubspotClient.setAccessToken(accessToken);
1012
try {
1113
await hubspotClient.crm.properties.groupsApi.create("contact", {
@@ -18,7 +20,24 @@ const createPropertyGroup = async (accessToken: string):Promise => {
1820
}
1921
};
2022

21-
const createRequiredProperty = async (accessToken: string): Promise<void> => {
23+
24+
export const createPropertyGroupForCompanies = async (accessToken: string) => {
25+
hubspotClient.setAccessToken(accessToken);
26+
try {
27+
const propertyGroupCreateResponse =
28+
await hubspotClient.crm.properties.groupsApi.create("company", {
29+
name: "integration_properties",
30+
label: "Integration Properties",
31+
displayOrder: 13,
32+
});
33+
} catch (error) {
34+
console.error(error);
35+
}
36+
};
37+
38+
39+
export const createRequiredContactProperty = async (accessToken: string) => {
40+
2241
hubspotClient.setAccessToken(accessToken);
2342
try {
2443
await hubspotClient.crm.properties.coreApi.create("contact", {
@@ -34,8 +53,46 @@ const createRequiredProperty = async (accessToken: string): Promise => {
3453
}
3554
};
3655

56+
export const createContactIdProperty = async (accessToken: string) => {
57+
hubspotClient.setAccessToken(accessToken);
58+
try {
59+
const propertyCreateResponse =
60+
await hubspotClient.crm.properties.coreApi.create("contact", {
61+
name: "native_system_contact_identifier",
62+
label: "Native System Contact Identifier",
63+
type: "string",
64+
description: "This can be used in place of email adress ot uniquely identify a contact",
65+
fieldType: "text",
66+
groupName: "integration_properties",
67+
hasUniqueValue:true
68+
});
69+
} catch (error) {
70+
console.error(error);
71+
}
72+
};
73+
74+
export const createCompanyIdProperty = async (accessToken: string) => {
75+
hubspotClient.setAccessToken(accessToken);
76+
try {
77+
const propertyCreateResponse =
78+
await hubspotClient.crm.properties.coreApi.create("company", {
79+
name: "native_system_company_identifier",
80+
label: "Native System Company Identifier",
81+
type: "string",
82+
description: "This can be used in place of email adress ot uniquely identify a contact",
83+
fieldType: "text",
84+
groupName: "integration_properties",
85+
hasUniqueValue: true
86+
});
87+
} catch (error) {
88+
console.error(error);
89+
}
90+
};
91+
92+
3793
const checkPropertiesCache = async (customerId: string): Promise<PropertyCache | undefined> => {
3894
try{
95+
3996
const cacheResults = await prisma.hubSpotPropertiesCache.findFirst({
4097
where: { customerId },
4198
select: { updatedAt: true, propertyData: true },
@@ -72,7 +129,9 @@ const saveHubSpotPropertiesToCache = async (
72129
}
73130
};
74131

75-
const getHubSpotProperties = async (customerId: string, skipCache: boolean): Promise<{ contactProperties: any; companyProperties: any; } | undefined> => {
132+
133+
export const getHubSpotProperties = async (customerId: string, skipCache: boolean): Promise<{ contactProperties: any; companyProperties: any; } | undefined> => {
134+
76135
// const propertiesCacheIsValid = await checkPropertiesCache(customerId);
77136

78137
const accessToken: string = await getAccessToken(customerId);
@@ -106,8 +165,10 @@ const getHubSpotProperties = async (customerId: string, skipCache: boolean): Pro
106165
}
107166
};
108167

109-
const getNativeProperties = async (customerId: string): Promise<Properties[] | undefined> => {
168+
169+
export const getNativeProperties = async (customerId: string): Promise<Properties[] | undefined> => {
110170
try{
171+
111172
const properties = await prisma.properties.findMany({
112173
select: {
113174
name: true,
@@ -127,9 +188,3 @@ const getNativeProperties = async (customerId: string): Promise
127188
}
128189
};
129190

130-
export {
131-
getHubSpotProperties,
132-
getNativeProperties,
133-
createPropertyGroup,
134-
createRequiredProperty,
135-
};

types/common.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ declare module "default" {
44
label?: string | null;
55
type?: string;
66
object: Objects;
7+
unique?: boolean;
78
modificationMetadata: ModificationMetadata;
9+
810
}
911
// interface Mapping {
1012
// nativeName: string;

0 commit comments

Comments
 (0)