Skip to content

Commit fd25dcc

Browse files
Add ability to add/remove customers from groups and create CTEs (#16)
* Add ability to add/remove customers from groups and create CTEs * Add changeset * Update src/client.ts Co-authored-by: Andrew Blaney <andrew@plain.com> * Added _typename to query for entry --------- Co-authored-by: Andrew Blaney <andrew@plain.com>
1 parent 006f20a commit fd25dcc

File tree

12 files changed

+199
-45
lines changed

12 files changed

+199
-45
lines changed

.changeset/unlucky-pugs-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@team-plain/typescript-sdk': minor
3+
---
4+
5+
Add the ability to add and remove customers from groups as well as create custom timeline entries.

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"plugin:@typescript-eslint/recommended",
1313
"plugin:@typescript-eslint/recommended-requiring-type-checking"
1414
],
15+
"rules": {
16+
"@typescript-eslint/no-unused-vars": "error"
17+
},
1518
"overrides": [
1619
{
1720
"files": ["*.test.ts"],

.github/workflows/generated-files.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/client.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import { Context } from './context';
22
import { PlainSDKError } from './error';
3+
import {
4+
CustomerGroupMembershipPartsFragment,
5+
RemoveCustomerFromCustomerGroupsDocument,
6+
RemoveCustomerFromCustomerGroupsInput,
7+
} from './graphql/types';
8+
import { AddCustomerToCustomerGroupsDocument } from './graphql/types';
9+
import { AddCustomerToCustomerGroupsInput } from './graphql/types';
310
import {
411
CreateIssueDocument,
512
CreateIssueInput,
613
CustomerByIdDocument,
714
CustomerByIdQueryVariables,
815
CustomerPartsFragment,
916
IssuePartsFragment,
17+
TimelineEntryPartsFragment,
18+
UpsertCustomTimelineEntryDocument,
19+
UpsertCustomTimelineEntryInput,
1020
UpsertCustomerDocument,
1121
UpsertCustomerInput,
22+
UpsertResult,
1223
} from './graphql/types';
1324
import { request } from './request';
1425
import { Result } from './result';
@@ -103,4 +114,61 @@ export class PlainSDKClient {
103114

104115
return unwrapData(res, (q) => nonNullable(q.createIssue.issue));
105116
}
117+
118+
/**
119+
* Adds a customer to a customer groups.
120+
*/
121+
async addCustomerToCustomerGroups(
122+
input: AddCustomerToCustomerGroupsInput
123+
): SDKResult<CustomerGroupMembershipPartsFragment[]> {
124+
const res = await request(this.#ctx, {
125+
query: AddCustomerToCustomerGroupsDocument,
126+
variables: {
127+
input,
128+
},
129+
});
130+
131+
return unwrapData(res, (q) =>
132+
nonNullable(q.addCustomerToCustomerGroups.customerGroupMemberships)
133+
);
134+
}
135+
136+
/**
137+
* Remove a customer from customer groups.
138+
*/
139+
async removeCustomerFromCustomerGroups(
140+
input: RemoveCustomerFromCustomerGroupsInput
141+
): SDKResult<null> {
142+
const res = await request(this.#ctx, {
143+
query: RemoveCustomerFromCustomerGroupsDocument,
144+
variables: {
145+
input,
146+
},
147+
});
148+
149+
return unwrapData(res, () => null);
150+
}
151+
152+
/**
153+
* Add a custom timeline entry to a customer's timeline.
154+
*
155+
* This can be used to power custom contact forms, log events from your own systems and much more.
156+
*/
157+
async upsertCustomTimelineEntry(
158+
input: UpsertCustomTimelineEntryInput
159+
): SDKResult<{ result: UpsertResult; timelineEntry: TimelineEntryPartsFragment }> {
160+
const res = await request(this.#ctx, {
161+
query: UpsertCustomTimelineEntryDocument,
162+
variables: {
163+
input,
164+
},
165+
});
166+
167+
return unwrapData(res, (q) => {
168+
return {
169+
result: nonNullable(q.upsertCustomTimelineEntry.result),
170+
timelineEntry: nonNullable(q.upsertCustomTimelineEntry.timelineEntry),
171+
};
172+
});
173+
}
106174
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fragment CustomerGroupMembershipParts on CustomerGroupMembership {
2+
customerId
3+
createdAt {
4+
...DateTimeParts
5+
}
6+
createdBy {
7+
...InternalActorParts
8+
}
9+
updatedAt {
10+
...DateTimeParts
11+
}
12+
updatedBy {
13+
...InternalActorParts
14+
}
15+
customerGroup {
16+
...CustomerGroupParts
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fragment CustomerGroupParts on CustomerGroup {
2+
id
3+
name
4+
key
5+
color
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fragment DateTimeParts on DateTime {
22
__typename
33
iso8601
4+
unixTimestamp
45
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
fragment TimelineEntryParts on TimelineEntry {
2+
id
3+
customerId
4+
timestamp {
5+
iso8601
6+
}
7+
actor {
8+
...ActorParts
9+
}
10+
entry {
11+
__typename
12+
# Why are we not querying for the full entry?
13+
# --
14+
# We could be returning the full entry we just created here
15+
# but in practice this is of little value and it's a huge
16+
# union of all possible entries etc. This means that we'd have to
17+
# carefully keep it in sync with every newly created timeline entry.
18+
#
19+
# If we were to add an entry and forget to add it here the returned
20+
# response would be confusing since it would not include the created
21+
# content.
22+
#
23+
# For this reason, for now, we are omiting entry from this query.
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
mutation addCustomerToCustomerGroups($input:AddCustomerToCustomerGroupsInput!) {
2+
addCustomerToCustomerGroups(input:$input) {
3+
customerGroupMemberships {
4+
...CustomerGroupMembershipParts
5+
}
6+
error {
7+
...MutationErrorParts
8+
}
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mutation removeCustomerFromCustomerGroups($input:RemoveCustomerFromCustomerGroupsInput!) {
2+
removeCustomerFromCustomerGroups(input:$input) {
3+
error {
4+
...MutationErrorParts
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)