Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unlucky-pugs-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@team-plain/typescript-sdk': minor
---

Add the ability to add and remove customers from groups as well as create custom timeline entries.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"rules": {
"@typescript-eslint/no-unused-vars": "error"
},
"overrides": [
{
"files": ["*.test.ts"],
Expand Down
33 changes: 0 additions & 33 deletions .github/workflows/generated-files.yml

This file was deleted.

68 changes: 68 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { Context } from './context';
import { PlainSDKError } from './error';
import {
CustomerGroupMembershipPartsFragment,
RemoveCustomerFromCustomerGroupsDocument,
RemoveCustomerFromCustomerGroupsInput,
} from './graphql/types';
import { AddCustomerToCustomerGroupsDocument } from './graphql/types';
import { AddCustomerToCustomerGroupsInput } from './graphql/types';
import {
CreateIssueDocument,
CreateIssueInput,
CustomerByIdDocument,
CustomerByIdQueryVariables,
CustomerPartsFragment,
IssuePartsFragment,
TimelineEntryPartsFragment,
UpsertCustomTimelineEntryDocument,
UpsertCustomTimelineEntryInput,
UpsertCustomerDocument,
UpsertCustomerInput,
UpsertResult,
} from './graphql/types';
import { request } from './request';
import { Result } from './result';
Expand Down Expand Up @@ -103,4 +114,61 @@ export class PlainSDKClient {

return unwrapData(res, (q) => nonNullable(q.createIssue.issue));
}

/**
* Adds a customer to a customer groups.
*/
async addCustomerToCustomerGroups(
input: AddCustomerToCustomerGroupsInput
): SDKResult<CustomerGroupMembershipPartsFragment[]> {
const res = await request(this.#ctx, {
query: AddCustomerToCustomerGroupsDocument,
variables: {
input,
},
});

return unwrapData(res, (q) =>
nonNullable(q.addCustomerToCustomerGroups.customerGroupMemberships)
);
}

/**
* Remove a customer from customer groups.
*/
async removeCustomerFromCustomerGroups(
input: RemoveCustomerFromCustomerGroupsInput
): SDKResult<null> {
const res = await request(this.#ctx, {
query: RemoveCustomerFromCustomerGroupsDocument,
variables: {
input,
},
});

return unwrapData(res, () => null);
}

/**
* Add a custom timeline entry to a customer's timeline.
*
* This can be used to power custom contact forms, log events from your own systems and much more.
*/
async upsertCustomTimelineEntry(
input: UpsertCustomTimelineEntryInput
): SDKResult<{ result: UpsertResult; timelineEntry: TimelineEntryPartsFragment }> {
const res = await request(this.#ctx, {
query: UpsertCustomTimelineEntryDocument,
variables: {
input,
},
});

return unwrapData(res, (q) => {
return {
result: nonNullable(q.upsertCustomTimelineEntry.result),
timelineEntry: nonNullable(q.upsertCustomTimelineEntry.timelineEntry),
};
});
}
}
18 changes: 18 additions & 0 deletions src/graphql/fragments/customerGroupMembershipParts.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fragment CustomerGroupMembershipParts on CustomerGroupMembership {
customerId
createdAt {
...DateTimeParts
}
createdBy {
...InternalActorParts
}
updatedAt {
...DateTimeParts
}
updatedBy {
...InternalActorParts
}
customerGroup {
...CustomerGroupParts
}
}
6 changes: 6 additions & 0 deletions src/graphql/fragments/customerGroupParts.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fragment CustomerGroupParts on CustomerGroup {
id
name
key
color
}
1 change: 1 addition & 0 deletions src/graphql/fragments/dateTimeParts.gql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fragment DateTimeParts on DateTime {
__typename
iso8601
unixTimestamp
}
25 changes: 25 additions & 0 deletions src/graphql/fragments/timelineEntryParts.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
fragment TimelineEntryParts on TimelineEntry {
id
customerId
timestamp {
iso8601
}
actor {
...ActorParts
}
entry {
__typename
# Why are we not querying for the full entry?
# --
# We could be returning the full entry we just created here
# but in practice this is of little value and it's a huge
# union of all possible entries etc. This means that we'd have to
# carefully keep it in sync with every newly created timeline entry.
#
# If we were to add an entry and forget to add it here the returned
# response would be confusing since it would not include the created
# content.
#
# For this reason, for now, we are omiting entry from this query.
}
}
10 changes: 10 additions & 0 deletions src/graphql/mutations/addCustomerToCustomerGroup.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mutation addCustomerToCustomerGroups($input:AddCustomerToCustomerGroupsInput!) {
addCustomerToCustomerGroups(input:$input) {
customerGroupMemberships {
...CustomerGroupMembershipParts
}
error {
...MutationErrorParts
}
}
}
7 changes: 7 additions & 0 deletions src/graphql/mutations/removeCustomerFromCustomerGroups.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation removeCustomerFromCustomerGroups($input:RemoveCustomerFromCustomerGroupsInput!) {
removeCustomerFromCustomerGroups(input:$input) {
error {
...MutationErrorParts
}
}
}
11 changes: 11 additions & 0 deletions src/graphql/mutations/upsertCustomTimelineEntry.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mutation upsertCustomTimelineEntry($input: UpsertCustomTimelineEntryInput!) {
upsertCustomTimelineEntry(input: $input) {
result
timelineEntry {
...TimelineEntryParts
}
error {
...MutationErrorParts
}
}
}
Loading