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
37 changes: 37 additions & 0 deletions components/gorgias/gorgias.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,35 @@ export default {
cursor = meta.next_cursor;
} while (cursor);
},
async createWebhook({
url,
eventType,
}) {
return this._makeRequest({
path: "integrations",
method: "post",
data: {
name: `pipedream-${url}`,
type: "http",
http: {
url,
method: "POST",
request_content_type: "application/json",
response_content_type: "application/json",
form: "{{context}}",
triggers: {
[eventType]: true,
},
},
},
});
},
async deleteWebhook({ id }) {
return this._makeRequest({
path: `integrations/${id}`,
method: "delete",
});
},
async getEvents({
$, params,
}) {
Expand Down Expand Up @@ -218,5 +247,13 @@ export default {
params,
});
},
async retrieveTicket({
$, id,
}) {
return this._makeRequest({
$,
path: `tickets/${id}`,
});
},
},
};
105 changes: 69 additions & 36 deletions components/gorgias/sources/common/base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,82 @@ export default {
props: {
gorgias,
db: "$.service.db",
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: 15 * 60,
},
http: "$.interface.http",
},
hooks: {
async deploy() {
console.log("Retrieving historical events...");
const events = this.gorgias.paginate({
fn: this.gorgias.getEvents,
params: {
types: this.getEventType(),
},
});
for await (const event of events) {
await this.processHistoricalEvent(event);
}
},
async activate() {
console.log("Creating webhook...");
const { id } = await this.gorgias.createWebhook({
url: this.http.endpoint,
eventType: this.getEventType(),
});
this.setWebhookId(id);
console.log(`Webhook ${id} created successfully`);
},
async deactivate() {
const id = this.getWebhookId();
console.log(`Deleting webhook ${id}...`);
await this.gorgias.deleteWebhook({
id,
});
this.setWebhookId();
console.log(`Webhook ${id} deleted successfully`);
},
},
methods: {
getNextCursor() {
return this.db.get("nextCursor");
getTimestampKey() {
return this.getEventType().includes("updated")
? "updated_datetime"
: "created_datetime";
},
setNextCursor(nextCursor) {
if (nextCursor) {
this.db.set("nextCursor", nextCursor);
}
getEventType() {
throw new Error("getEventType is not implemented");
},
getEventTypes() {
throw new Error("getEventTypes is not implemented");
processEvent() {
throw new Error("processEvent is not implemented");
},
async emitEvents(events) {
for (const event of events) {
this.$emit(event, {
id: event.id,
ts: Date.parse(event.created_datetime),
summary: `New ${event.type} event: ${event.id}`,
});
}
processHistoricalEvent() {
throw new Error("processHistoricalEvent is not implemented");
},
getWebhookId() {
return this.db.get("webhookId");
},
setWebhookId(webhookId) {
this.db.set("webhookId", webhookId);
},
async retrieveTicket(id) {
console.log(`Received ${this.getEventType()} for ticket ${id}`);
console.log(`Fetching data for ticket ${id}`);
return this.gorgias.retrieveTicket({
id,
});
},
emitEvent(event) {
console.log(`Emitting event ${event.id}:`);
console.log(event);
const ts = Date.parse(event[this.getTimestampKey()]);
this.$emit(event, {
id: `${event.id}_${ts}`,
ts,
summary: `New ${this.getEventType()}: ${event.id}`,
});
},
},
async run() {
const params = {
cursor: this.getNextCursor(),
...this.getEventTypes(),
};

const {
data: events,
meta,
} = await this.gorgias.getEvents({
params,
});

this.emitEvents(events);
this.setNextCursor(meta.next_cursor);
async run(event) {
console.log("Raw received event:");
console.log(event);
return this.processEvent(event.body);
},
};
16 changes: 0 additions & 16 deletions components/gorgias/sources/new-events/new-events.mjs

This file was deleted.

15 changes: 10 additions & 5 deletions components/gorgias/sources/ticket-created/ticket-created.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ export default {
key: "gorgias-ticket-created",
name: "New Ticket",
description: "Emit new event when a ticket is created. [See the docs](https://developers.gorgias.com/reference/the-event-object)",
version: "0.0.1",
version: "0.1.0",
type: "source",
methods: {
...base.methods,
getEventTypes() {
return {
types: eventTypes.TICKET_CREATED,
};
getEventType() {
return eventTypes.TICKET_CREATED;
},
async processHistoricalEvent(event) {
const ticket = await this.retrieveTicket(event.object_id);
this.emitEvent(ticket);
},
async processEvent(event) {
this.emitEvent(event.ticket);
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ export default {
key: "gorgias-ticket-messaged-created",
name: "New Ticket Message",
description: "Emit new event when a ticket message is created. [See the docs](https://developers.gorgias.com/reference/the-event-object)",
version: "0.0.1",
version: "0.1.0",
type: "source",
methods: {
...base.methods,
getEventTypes() {
return {
types: eventTypes.TICKET_MESSAGE_CREATED,
};
getEventType() {
return eventTypes.TICKET_MESSAGE_CREATED;
},
async processHistoricalEvent(event) {
// event doesn't contain ticket id to fetch message
this.emitEvent(event);
},
async processEvent(event) {
this.emitEvent(event.message);
},
},
};
15 changes: 10 additions & 5 deletions components/gorgias/sources/ticket-updated/ticket-updated.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ export default {
key: "gorgias-ticket-updated",
name: "New Updated Ticket",
description: "Emit new event when a ticket is updated. [See the docs](https://developers.gorgias.com/reference/the-event-object)",
version: "0.0.1",
version: "0.1.0",
type: "source",
methods: {
...base.methods,
getEventTypes() {
return {
types: eventTypes.TICKET_UPDATED,
};
getEventType() {
return eventTypes.TICKET_UPDATED;
},
async processHistoricalEvent(event) {
const ticket = await this.retrieveTicket(event.object_id);
this.emitEvent(ticket);
},
async processEvent(event) {
this.emitEvent(event.ticket);
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import overrideApp from "../../common/override-app.mjs";

overrideApp(base);

// same version as base
// eslint-disable-next-line pipedream/required-properties-version
export default {
...base,
key: "gorgias_oauth-create-customer",
name: "Create Customer",
description: "Create a new customer. [See the docs](https://developers.gorgias.com/reference/post_api-customers)",
version: "0.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I don't get it exactly even with the comment. Why are you removing the version here and in other files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This action has the same behaviour defined in gorgias-create-customer. If the code from gorgias-create-customer is changed and published, we have to rev the version of gorgias_oauth-create-customer and publish it too.

To avoid forgetting to do this, removing version will make this action always have the same version as the base and be published together.

type: "action",
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import overrideApp from "../../common/override-app.mjs";

overrideApp(base);

// same version as base
// eslint-disable-next-line pipedream/required-properties-version
export default {
...base,
key: "gorgias_oauth-create-ticket",
name: "Create Ticket",
description: "Create a new ticket. [See the docs](https://developers.gorgias.com/reference/post_api-tickets)",
version: "0.0.1",
type: "action",
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import overrideApp from "../../common/override-app.mjs";

overrideApp(base);

// same version as base
// eslint-disable-next-line pipedream/required-properties-version
export default {
...base,
key: "gorgias_oauth-list-tickets",
name: "List Tickets",
description: "List all tickets. [See the docs](https://developers.gorgias.com/reference/get_api-tickets)",
version: "0.0.1",
type: "action",
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import overrideApp from "../../common/override-app.mjs";

overrideApp(base);

// same version as base
// eslint-disable-next-line pipedream/required-properties-version
export default {
...base,
key: "gorgias_oauth-retrieve-customer",
name: "Retrieve a Customer",
description: "Retrieve a customer. [See the docs](https://developers.gorgias.com/reference/get_api-customers-id-)",
version: "0.0.1",
type: "action",
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import overrideApp from "../../common/override-app.mjs";

overrideApp(base);

// same version as base
// eslint-disable-next-line pipedream/required-properties-version
export default {
...base,
key: "gorgias_oauth-update-customer",
name: "Update Customer",
description: "Update a customer. [See the docs](https://developers.gorgias.com/reference/put_api-customers-id-)",
version: "0.0.1",
type: "action",
};
13 changes: 0 additions & 13 deletions components/gorgias_oauth/sources/new-events/new-events.mjs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import overrideApp from "../../common/override-app.mjs";

overrideApp(base);

// same version as base
// eslint-disable-next-line pipedream/required-properties-version
export default {
...base,
key: "gorgias_oauth-ticket-created",
name: "New Ticket",
description: "Emit new event when a ticket is created. [See the docs](https://developers.gorgias.com/reference/the-event-object)",
version: "0.0.1",
type: "source",
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import overrideApp from "../../common/override-app.mjs";

overrideApp(base);

// same version as base
// eslint-disable-next-line pipedream/required-properties-version
export default {
...base,
key: "gorgias_oauth-ticket-messaged-created",
name: "New Ticket Message",
description: "Emit new event when a ticket message is created. [See the docs](https://developers.gorgias.com/reference/the-event-object)",
version: "0.0.1",
type: "source",
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import overrideApp from "../../common/override-app.mjs";

overrideApp(base);

// same version as base
// eslint-disable-next-line pipedream/required-properties-version
export default {
...base,
key: "gorgias_oauth-ticket-updated",
name: "New Updated Ticket",
description: "Emit new event when a ticket is updated. [See the docs](https://developers.gorgias.com/reference/the-event-object)",
version: "0.0.1",
type: "source",
};