Skip to content

Commit 7770034

Browse files
committed
Create/update person adjustments
1 parent 0d15f59 commit 7770034

File tree

2 files changed

+51
-139
lines changed

2 files changed

+51
-139
lines changed
Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
1-
import gainsight_nxt from "../../gainsight_nxt.app.mjs";
1+
import app from "../../gainsight_nxt.app.mjs";
22

33
export default {
44
key: "gainsight_nxt-create-or-update-person",
55
name: "Create or Update Person",
6-
description: "Adds or updates a person's record in Gainsight NXT. [See the documentation]()",
6+
description: "Create or update a person's record. [See the documentation](https://support.gainsight.com/gainsight_nxt/API_and_Developer_Docs/Person_API/People_API_Documentation#Person)",
77
version: "0.0.{{ts}}",
88
type: "action",
99
props: {
10-
gainsight_nxt: {
11-
type: "app",
12-
app: "gainsight_nxt",
10+
app,
11+
email: {
12+
type: "string",
13+
label: "Email",
14+
description: "The email address of the person. If a record with this email exists, it will be updated, otherwise a new one will be created.",
1315
},
14-
personFields: {
15-
propDefinition: [
16-
gainsight_nxt,
17-
"personFields",
18-
],
16+
firstName: {
17+
type: "string",
18+
label: "First Name",
19+
description: "The first name of the person.",
20+
optional: true,
21+
},
22+
lastName: {
23+
type: "string",
24+
label: "Last Name",
25+
description: "The last name of the person.",
26+
optional: true,
27+
},
28+
linkedinUrl: {
29+
type: "string",
30+
label: "LinkedIn URL",
31+
description: "The LinkedIn URL of the person.",
32+
optional: true,
33+
},
34+
location: {
35+
type: "string",
36+
label: "Location",
37+
description: "The location of the person.",
38+
optional: true,
39+
},
40+
additionalFields: {
41+
type: "object",
42+
label: "Additional Fields",
43+
description: "Additional fields to include in the request body. [See the documentation](https://support.gainsight.com/gainsight_nxt/API_and_Developer_Docs/Person_API/People_API_Documentation#Person) for all available fields.",
44+
optional: true,
1945
},
2046
},
2147
async run({ $ }) {
22-
const personData = this.personFields.reduce((acc, field) => {
23-
const parsed = JSON.parse(field);
24-
return {
25-
...acc,
26-
...parsed,
27-
};
28-
}, {});
29-
30-
if (!personData.email) {
31-
throw new Error("Person 'email' field is required.");
32-
}
33-
34-
const response = await this.gainsight_nxt.createOrUpdatePerson(personData);
48+
const response = await this.app.createOrUpdatePerson({
49+
$,
50+
data: {
51+
Email: this.email,
52+
FirstName: this.firstName,
53+
LastName: this.lastName,
54+
LinkedinUrl: this.linkedinUrl,
55+
Location: this.location,
56+
...this.additionalFields,
57+
},
58+
});
3559

36-
$.export("$summary", `Created or updated person with email ${personData.email}`);
60+
$.export("$summary", `Successfully upserted person with email ${this.email}`);
3761
return response;
3862
},
3963
};

components/gainsight_nxt/gainsight_nxt.app.mjs

Lines changed: 3 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -54,124 +54,12 @@ export default {
5454
...args,
5555
});
5656
},
57-
// Custom Object Methods
58-
async listCustomObjects(opts = {}) {
57+
async createOrUpdatePerson(args) {
5958
return this._makeRequest({
60-
path: "/custom_objects",
61-
method: "GET",
62-
params: opts.params,
63-
});
64-
},
65-
async getCustomObject(opts = {}) {
66-
return this._makeRequest({
67-
path: `/custom_objects/${opts.id}`,
68-
method: "GET",
69-
});
70-
},
71-
async createCustomObject(opts = {}) {
72-
return this._makeRequest({
73-
path: "/custom_objects",
74-
method: "POST",
75-
data: opts.data,
76-
});
77-
},
78-
async updateCustomObject(opts = {}) {
79-
return this._makeRequest({
80-
path: `/custom_objects/${opts.id}`,
81-
method: "PUT",
82-
data: opts.data,
83-
});
84-
},
85-
async createOrUpdateCustomObject(fields = {}) {
86-
const customObjectData = fields;
87-
const customObjectName = customObjectData.name;
88-
if (!customObjectName) {
89-
throw new Error("Custom Object 'name' field is required for createOrUpdate.");
90-
}
91-
const response = await this.paginate(this.listCustomObjects, {
92-
name: customObjectName,
93-
});
94-
if (response.length > 0) {
95-
const customObject = response[0];
96-
return this.updateCustomObject({
97-
id: customObject.id,
98-
data: customObjectData,
99-
});
100-
} else {
101-
return this.createCustomObject({
102-
data: customObjectData,
103-
});
104-
}
105-
},
106-
// Person Methods
107-
async listPersons(opts = {}) {
108-
return this._makeRequest({
109-
path: "/persons",
110-
method: "GET",
111-
params: opts.params,
112-
});
113-
},
114-
async getPerson(opts = {}) {
115-
return this._makeRequest({
116-
path: `/persons/${opts.id}`,
117-
method: "GET",
118-
});
119-
},
120-
async createPerson(opts = {}) {
121-
return this._makeRequest({
122-
path: "/persons",
123-
method: "POST",
124-
data: opts.data,
125-
});
126-
},
127-
async updatePerson(opts = {}) {
128-
return this._makeRequest({
129-
path: `/persons/${opts.id}`,
59+
path: "/peoplemgmt/v1.0/people",
13060
method: "PUT",
131-
data: opts.data,
132-
});
133-
},
134-
async createOrUpdatePerson(fields = {}) {
135-
const personData = fields;
136-
const personEmail = personData.email;
137-
if (!personEmail) {
138-
throw new Error("Person 'email' field is required for createOrUpdate.");
139-
}
140-
const response = await this.paginate(this.listPersons, {
141-
email: personEmail,
61+
...args,
14262
});
143-
if (response.length > 0) {
144-
const person = response[0];
145-
return this.updatePerson({
146-
id: person.id,
147-
data: personData,
148-
});
149-
} else {
150-
return this.createPerson({
151-
data: personData,
152-
});
153-
}
154-
},
155-
// Pagination Method
156-
async paginate(fn, params = {}) {
157-
let results = [];
158-
let page = 1;
159-
let hasMore = true;
160-
while (hasMore) {
161-
const response = await fn({
162-
params: {
163-
...params,
164-
page,
165-
},
166-
});
167-
if (response.length > 0) {
168-
results.push(...response);
169-
page += 1;
170-
} else {
171-
hasMore = false;
172-
}
173-
}
174-
return results;
17563
},
17664
},
17765
};

0 commit comments

Comments
 (0)