diff --git a/components/peerdom/actions/assign-member-to-role/assign-member-to-role.mjs b/components/peerdom/actions/assign-member-to-role/assign-member-to-role.mjs new file mode 100644 index 0000000000000..c90374a89697c --- /dev/null +++ b/components/peerdom/actions/assign-member-to-role/assign-member-to-role.mjs @@ -0,0 +1,59 @@ +import peerdom from "../../peerdom.app.mjs"; + +export default { + key: "peerdom-assign-member-to-role", + name: "Assign Member to Role", + description: "Assigns a member to a role within a circle using the Peerdom API. [See the documentation](https://api.peerdom.org/v1/docs)", + version: "0.0.1", + type: "action", + props: { + peerdom, + roleId: { + propDefinition: [ + peerdom, + "roleId", + ], + }, + memberId: { + propDefinition: [ + peerdom, + "memberId", + ], + }, + percentage: { + type: "integer", + label: "Percentage", + description: "The percentage of the role assigned to the member", + optional: true, + min: 0, + max: 100, + }, + focus: { + type: "string", + label: "Focus", + description: "The focus of the role assigned to the member", + optional: true, + }, + electedUntil: { + type: "string", + label: "Elected Until", + description: "The date until which the member is elected to the role (YYYY-MM-DD)", + optional: true, + }, + }, + async run({ $ }) { + const response = await this.peerdom.assignMemberToRole({ + $, + roleId: this.roleId, + data: { + peerId: this.memberId, + percentage: this.percentage, + focus: this.focus, + electedUntil: this.electedUntil, + }, + }); + + $.export("$summary", `Successfully assigned member with ID ${this.memberId} to role with ID ${this.roleId}`); + return response; + }, +}; diff --git a/components/peerdom/actions/create-role/create-role.mjs b/components/peerdom/actions/create-role/create-role.mjs new file mode 100644 index 0000000000000..52d8fb522aaa9 --- /dev/null +++ b/components/peerdom/actions/create-role/create-role.mjs @@ -0,0 +1,96 @@ +import { ConfigurationError } from "@pipedream/platform"; +import { parseObject } from "../../common/utils.mjs"; +import peerdom from "../../peerdom.app.mjs"; + +export default { + key: "peerdom-create-role", + name: "Create Role", + description: "Create a new role within a specified circle. [See the documentation](https://api.peerdom.org/v1/docs)", + version: "0.0.1", + type: "action", + props: { + peerdom, + name: { + propDefinition: [ + peerdom, + "name", + ], + }, + mapId: { + propDefinition: [ + peerdom, + "mapId", + ], + }, + parentId: { + propDefinition: [ + peerdom, + "groupId", + ], + }, + electable: { + propDefinition: [ + peerdom, + "electable", + ], + optional: true, + }, + external: { + propDefinition: [ + peerdom, + "external", + ], + optional: true, + }, + color: { + propDefinition: [ + peerdom, + "color", + ], + optional: true, + }, + shape: { + propDefinition: [ + peerdom, + "shape", + ], + optional: true, + }, + customFields: { + propDefinition: [ + peerdom, + "customFields", + ], + optional: true, + }, + groupEmail: { + propDefinition: [ + peerdom, + "groupEmail", + ], + optional: true, + }, + }, + async run({ $ }) { + try { + const { + peerdom, + customFields, + ...data + } = this; + + const response = await peerdom.createRole({ + $, + data: { + ...data, + customFields: parseObject(customFields), + }, + }); + + $.export("$summary", `Successfully created role: ${response.name}`); + return response; + } catch ({ response }) { + throw new ConfigurationError(response.data.message); + } + }, +}; diff --git a/components/peerdom/actions/update-role/update-role.mjs b/components/peerdom/actions/update-role/update-role.mjs new file mode 100644 index 0000000000000..0b26a17dab159 --- /dev/null +++ b/components/peerdom/actions/update-role/update-role.mjs @@ -0,0 +1,99 @@ +import { ConfigurationError } from "@pipedream/platform"; +import { parseObject } from "../../common/utils.mjs"; +import peerdom from "../../peerdom.app.mjs"; + +export default { + key: "peerdom-update-role", + name: "Update Role", + description: "Update an existing role's attributes such as name, description, or linked domains. [See the documentation](https://api.peerdom.org/v1/docs)", + version: "0.0.1", + type: "action", + props: { + peerdom, + roleId: { + propDefinition: [ + peerdom, + "roleId", + ], + }, + name: { + propDefinition: [ + peerdom, + "name", + ], + optional: true, + }, + parentId: { + propDefinition: [ + peerdom, + "groupId", + ], + }, + electable: { + propDefinition: [ + peerdom, + "electable", + ], + optional: true, + }, + external: { + propDefinition: [ + peerdom, + "external", + ], + optional: true, + }, + color: { + propDefinition: [ + peerdom, + "color", + ], + optional: true, + }, + shape: { + propDefinition: [ + peerdom, + "shape", + ], + optional: true, + }, + customFields: { + propDefinition: [ + peerdom, + "customFields", + ], + optional: true, + }, + groupEmail: { + propDefinition: [ + peerdom, + "groupEmail", + ], + optional: true, + }, + }, + async run({ $ }) { + try { + const { + peerdom, + roleId, + customFields, + ...data + } = this; + + const response = await peerdom.updateRole({ + $, + roleId, + data: { + ...data, + customFields: parseObject(customFields), + }, + }); + + $.export("$summary", `Successfully updated role with ID ${this.roleId}`); + return response; + } catch ({ response }) { + throw new ConfigurationError(response.data.message); + } + }, +}; diff --git a/components/peerdom/common/constants.mjs b/components/peerdom/common/constants.mjs new file mode 100644 index 0000000000000..fd3d1ee72fa1d --- /dev/null +++ b/components/peerdom/common/constants.mjs @@ -0,0 +1,6 @@ +export const LIMIT = 100; + +export const SHAPE_OPTIONS = [ + "circle", + "hexagon", +]; diff --git a/components/peerdom/common/utils.mjs b/components/peerdom/common/utils.mjs new file mode 100644 index 0000000000000..dcc9cc61f6f41 --- /dev/null +++ b/components/peerdom/common/utils.mjs @@ -0,0 +1,24 @@ +export const parseObject = (obj) => { + if (!obj) return undefined; + + if (Array.isArray(obj)) { + return obj.map((item) => { + if (typeof item === "string") { + try { + return JSON.parse(item); + } catch (e) { + return item; + } + } + return item; + }); + } + if (typeof obj === "string") { + try { + return JSON.parse(obj); + } catch (e) { + return obj; + } + } + return obj; +}; diff --git a/components/peerdom/package.json b/components/peerdom/package.json index 062fe5f18d560..dca78856fc590 100644 --- a/components/peerdom/package.json +++ b/components/peerdom/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/peerdom", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Peerdom Components", "main": "peerdom.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } -} \ No newline at end of file +} diff --git a/components/peerdom/peerdom.app.mjs b/components/peerdom/peerdom.app.mjs index c38375fda2339..a638fda5a2569 100644 --- a/components/peerdom/peerdom.app.mjs +++ b/components/peerdom/peerdom.app.mjs @@ -1,11 +1,227 @@ +import { axios } from "@pipedream/platform"; +import { + LIMIT, SHAPE_OPTIONS, +} from "./common/constants.mjs"; + export default { type: "app", app: "peerdom", - propDefinitions: {}, + propDefinitions: { + groupId: { + type: "string", + label: "Parent ID", + description: "The ID should be a valid group ID.", + async options() { + const data = await this.listGroups(); + + return data.map(({ + id: value, name: label, + }) => ({ + label, + value, + })); + }, + }, + mapId: { + type: "string", + label: "Map ID", + description: "The ID of the map", + async options() { + const data = await this.listMaps(); + + return data.map(({ + id: value, name: label, + }) => ({ + label, + value, + })); + }, + }, + roleId: { + type: "string", + label: "Role ID", + description: "ID of role to update.", + async options() { + const data = await this.listRoles(); + + return data.map(({ + id: value, name: label, + }) => ({ + label, + value, + })); + }, + }, + memberId: { + type: "string", + label: "Member ID", + description: "The ID of the member", + async options() { + const data = await this.listPeers(); + + return data.map(({ + id: value, firstName, lastName, + }) => ({ + label: `${firstName} ${lastName}`, + value, + })); + }, + }, + name: { + type: "string", + label: "Name", + description: "The name of the role to be created", + }, + electable: { + type: "boolean", + label: "Electable", + description: "Whether the role is electable or not", + }, + external: { + type: "boolean", + label: "External", + description: "Set to `true` if node is outside of the organization", + }, + color: { + type: "string", + label: "Color", + description: "The choice of color for the node in hexadecimal string format, e.g. `#f3a935`", + }, + shape: { + type: "string", + label: "Shape", + description: "Specifies the shape of the node that determines the visual representation of the node within the interface", + options: SHAPE_OPTIONS, + optional: true, + }, + customFields: { + type: "object", + label: "Custom Fields", + description: "The custom fields for a group/role. You can add the properties from the predefined custom fields. [See the documentation](https://api.peerdom.org/v1/docs#tag/Roles/operation/postRole) for further details.", + }, + groupEmail: { + type: "string", + label: "Group Email", + description: "Email for node (group/role) communication", + optional: true, + }, + + circleId: { + type: "string", + label: "Circle ID", + description: "The ID of the circle (organization)", + }, + roleName: { + type: "string", + label: "Role Name", + description: "The name of the role to be created", + }, + description: { + type: "string", + label: "Description", + description: "Optional description for the role", + optional: true, + }, + linkedDomains: { + type: "string[]", + label: "Linked Domains", + description: "Optional linked domains for the role", + optional: true, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://api.peerdom.org/v1"; + }, + _headers() { + return { + "content-type": "application/json", + "x-api-key": `${this.$auth.api_key}`, + }; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: this._baseUrl() + path, + headers: this._headers(), + ...opts, + }); + }, + listGroups(opts = {}) { + return this._makeRequest({ + path: "/groups", + ...opts, + }); + }, + listMaps(opts = {}) { + return this._makeRequest({ + path: "/maps", + ...opts, + }); + }, + listRoles(opts = {}) { + return this._makeRequest({ + path: "/roles", + ...opts, + }); + }, + listPeers(opts = {}) { + return this._makeRequest({ + path: "/peers", + ...opts, + }); + }, + createRole(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/roles", + ...opts, + }); + }, + updateRole({ + roleId, ...opts + }) { + return this._makeRequest({ + method: "PUT", + path: `/roles/${roleId}`, + ...opts, + }); + }, + async assignMemberToRole({ + roleId, ...opts + }) { + return this._makeRequest({ + method: "POST", + path: `/roles/${roleId}/peers`, + ...opts, + }); + }, + async *paginate({ + fn, params = {}, maxResults = null, ...opts + }) { + let hasMore = false; + let count = 0; + let page = 0; + + do { + params.limit = LIMIT; + params.offset = (LIMIT * page++) + 1; + const data = await fn({ + params, + ...opts, + }); + for (const d of data) { + yield d; + + if (maxResults && ++count === maxResults) { + return count; + } + } + + hasMore = data.length; + + } while (hasMore); }, }, }; diff --git a/components/peerdom/sources/common/base.mjs b/components/peerdom/sources/common/base.mjs new file mode 100644 index 0000000000000..03ee88ac7525e --- /dev/null +++ b/components/peerdom/sources/common/base.mjs @@ -0,0 +1,61 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import peerdom from "../../peerdom.app.mjs"; + +export default { + props: { + peerdom, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + methods: { + _getLastId() { + return this.db.get("lastId") || ""; + }, + _setLastId(lastId) { + this.db.set("lastId", lastId); + }, + sortItems(items) { + return items; + }, + async emitEvent(maxResults = false) { + const lastId = this._getLastId(); + const fn = this.getFunction(); + + const response = this.sortItems(await fn()); + + let responseArray = []; + for (const item of response.reverse()) { + if (item.id == lastId) break; + responseArray.push(item); + } + + if (responseArray.length) { + if (maxResults && (responseArray.length > maxResults)) { + responseArray.length = maxResults; + } + this._setLastId(responseArray[0].id); + } + + for (const item of responseArray.reverse()) { + this.$emit(item, { + id: item.id, + summary: this.getSummary(item), + ts: Date.now(), + }); + } + }, + }, + hooks: { + async deploy() { + await this.emitEvent(25); + }, + }, + async run() { + await this.emitEvent(); + }, +}; diff --git a/components/peerdom/sources/new-member/new-member.mjs b/components/peerdom/sources/new-member/new-member.mjs new file mode 100644 index 0000000000000..3a5d172f972b8 --- /dev/null +++ b/components/peerdom/sources/new-member/new-member.mjs @@ -0,0 +1,22 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "peerdom-new-member", + name: "New Member Added", + description: "Emit new event when a new member is added to a group. [See the documentation](https://api.peerdom.org/v1/docs#tag/Peers/operation/getPeers)", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + getFunction() { + return this.peerdom.listPeers; + }, + getSummary(item) { + return `New Member: ${item.firstName} ${item.lastName || ""}`; + }, + }, + sampleEmit, +}; diff --git a/components/peerdom/sources/new-member/test-event.mjs b/components/peerdom/sources/new-member/test-event.mjs new file mode 100644 index 0000000000000..0ad7248f09309 --- /dev/null +++ b/components/peerdom/sources/new-member/test-event.mjs @@ -0,0 +1,19 @@ +export default { + "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", + "firstName": "string", + "lastName": "string", + "nickName": "string", + "slug": "string", + "avatarUrl": "string", + "birthdate": "2019-08-24T14:15:22Z", + "contribution": 0, + "contributionUnit": "string", + "customFields": [ + { + "name": "string", + "values": [ + "string" + ] + } + ] +} \ No newline at end of file diff --git a/components/peerdom/sources/new-role/new-role.mjs b/components/peerdom/sources/new-role/new-role.mjs new file mode 100644 index 0000000000000..5741b1141cb89 --- /dev/null +++ b/components/peerdom/sources/new-role/new-role.mjs @@ -0,0 +1,25 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "peerdom-new-role", + name: "New Role Created", + description: "Emit new event when a new role is created in a circle. [See the documentation](https://api.peerdom.org/v1/docs)", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + getFunction() { + return this.peerdom.listRoles; + }, + getSummary(item) { + return `New Role Created: ${item.name}`; + }, + sortItems(items) { + return items.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt)); + }, + }, + sampleEmit, +}; diff --git a/components/peerdom/sources/new-role/test-event.mjs b/components/peerdom/sources/new-role/test-event.mjs new file mode 100644 index 0000000000000..86faf17132eeb --- /dev/null +++ b/components/peerdom/sources/new-role/test-event.mjs @@ -0,0 +1,50 @@ +export default { + "id": "497f6eca-6276-4993-bfeb-53cbbbba6f08", + "name": "string", + "color": "string", + "createdAt": "2019-08-24T14:15:22Z", + "parentId": "70850378-7d3c-4f45-91b7-942d4dfbbd43", + "slug": "string", + "external": true, + "electable": true, + "salaryLevel": "string", + "mirrored": true, + "shape": "string", + "customFields": [ + { + "name": "string", + "values": [ + "string" + ] + } + ], + "holders": [ + { + "peerId": "60a07d40-746d-414c-b70b-908ca16e7459", + "contribution": 0, + "contributionUnit": "string" + } + ], + "goals": [ + { + "endedAt": "2019-05-17", + "startedAt": "2019-05-17", + "archived": true, + "data": { + "order": "string", + "name": "string", + "isComplete": true, + "subgoals": [ + { + "name": "string", + "percentComplete": 0, + "isComplete": true, + "amountComplete": 0, + "targetAmount": 0 + } + ] + } + } + ], + "groupEmail": "string" +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 17593dfe05b88..0d93145ab8f9d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9514,7 +9514,11 @@ importers: specifier: ^2.0.0 version: 2.0.0 - components/peerdom: {} + components/peerdom: + dependencies: + '@pipedream/platform': + specifier: ^3.0.3 + version: 3.0.3 components/pendo: {}