diff --git a/packages/rule-handler/src/index.ts b/packages/rule-handler/src/index.ts index bdfffe4ae..ab2a3d92b 100644 --- a/packages/rule-handler/src/index.ts +++ b/packages/rule-handler/src/index.ts @@ -19,6 +19,7 @@ interface PubSubRequestBody { } export interface PubSubData { + id: string userId: string type: EntityType subscription?: string diff --git a/packages/rule-handler/src/label.ts b/packages/rule-handler/src/label.ts new file mode 100644 index 000000000..948c193be --- /dev/null +++ b/packages/rule-handler/src/label.ts @@ -0,0 +1,44 @@ +import axios from 'axios' +import { getAuthToken } from './index' + +export const addLabels = async ( + userId: string, + apiEndpoint: string, + jwtSecret: string, + pageId: string, + labelIds: string[] +) => { + const auth = await getAuthToken(userId, jwtSecret) + + const data = JSON.stringify({ + query: `mutation SetLabels($input: SetLabelsInput!) { + setLabels(input: $input) { + ... on SetLabelsSuccess { + labels { + id + } + } + ... on SetLabelsError { + errorCodes + } + } + }`, + variables: { + input: { + pageId, + labelIds, + }, + }, + }) + + try { + await axios.post(`${apiEndpoint}/graphql`, data, { + headers: { + Cookie: `auth=${auth};`, + 'Content-Type': 'application/json', + }, + }) + } catch (e) { + console.error(e) + } +} diff --git a/packages/rule-handler/src/rule.ts b/packages/rule-handler/src/rule.ts index 8e84f303c..ea83e2401 100644 --- a/packages/rule-handler/src/rule.ts +++ b/packages/rule-handler/src/rule.ts @@ -2,6 +2,7 @@ import { sendNotification } from './notification' import { getAuthToken, PubSubData } from './index' import axios from 'axios' import { parse, SearchParserKeyWordOffset } from 'search-query-parser' +import { addLabels } from './label' export enum RuleActionType { AddLabel = 'ADD_LABEL', @@ -134,6 +135,18 @@ export const triggerActions = async ( for (const action of rule.actions) { switch (action.type) { case RuleActionType.AddLabel: + if (action.params.length === 0) { + console.log('No label id provided') + continue + } + await addLabels( + userId, + apiEndpoint, + jwtSecret, + data.id, + action.params + ) + break case RuleActionType.Archive: case RuleActionType.MarkAsRead: continue @@ -141,6 +154,7 @@ export const triggerActions = async ( for (const message of action.params) { await sendNotification(userId, apiEndpoint, jwtSecret, message) } + break } } }