Add add label action in the rule engine

This commit is contained in:
Hongbo Wu
2022-11-23 10:09:42 +08:00
parent 9bcb2e3825
commit 5f07f8cc64
3 changed files with 59 additions and 0 deletions

View File

@ -19,6 +19,7 @@ interface PubSubRequestBody {
}
export interface PubSubData {
id: string
userId: string
type: EntityType
subscription?: string

View File

@ -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)
}
}

View File

@ -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
}
}
}