Add mark page as read and archived action in rule engine
This commit is contained in:
82
packages/rule-handler/src/page.ts
Normal file
82
packages/rule-handler/src/page.ts
Normal file
@ -0,0 +1,82 @@
|
||||
import { getAuthToken } from './index'
|
||||
import axios from 'axios'
|
||||
|
||||
export const archivePage = async (
|
||||
pageId: string,
|
||||
userId: string,
|
||||
apiEndpoint: string,
|
||||
jwtSecret: string
|
||||
) => {
|
||||
const auth = await getAuthToken(userId, jwtSecret)
|
||||
|
||||
const data = JSON.stringify({
|
||||
query: `mutation ArchivePage($input: ArchivePageInput!) {
|
||||
archivePage(input: $input) {
|
||||
... on ArchivePageSuccess {
|
||||
page {
|
||||
id
|
||||
}
|
||||
}
|
||||
... on ArchivePageError {
|
||||
errorCodes
|
||||
}
|
||||
}
|
||||
}`,
|
||||
variables: {
|
||||
input: {
|
||||
pageId,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await axios.post(`${apiEndpoint}/graphql`, data, {
|
||||
headers: {
|
||||
Cookie: `auth=${auth};`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
export const markPageAsRead = async (
|
||||
pageId: string,
|
||||
userId: string,
|
||||
apiEndpoint: string,
|
||||
jwtSecret: string
|
||||
) => {
|
||||
const auth = await getAuthToken(userId, jwtSecret)
|
||||
|
||||
const data = JSON.stringify({
|
||||
query: `mutation MarkPageAsRead($input: MarkPageAsReadInput!) {
|
||||
markPageAsRead(input: $input) {
|
||||
... on MarkPageAsReadSuccess {
|
||||
page {
|
||||
id
|
||||
}
|
||||
}
|
||||
... on MarkPageAsReadError {
|
||||
errorCodes
|
||||
}
|
||||
}
|
||||
}`,
|
||||
variables: {
|
||||
input: {
|
||||
pageId,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
await axios.post(`${apiEndpoint}/graphql`, data, {
|
||||
headers: {
|
||||
Cookie: `auth=${auth};`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@ import { getAuthToken, PubSubData } from './index'
|
||||
import axios from 'axios'
|
||||
import { parse, SearchParserKeyWordOffset } from 'search-query-parser'
|
||||
import { addLabels } from './label'
|
||||
import { archivePage, markPageAsRead } from './page'
|
||||
|
||||
export enum RuleActionType {
|
||||
AddLabel = 'ADD_LABEL',
|
||||
@ -148,8 +149,19 @@ export const triggerActions = async (
|
||||
)
|
||||
break
|
||||
case RuleActionType.Archive:
|
||||
if (!data.id) {
|
||||
console.log('invalid data for archive action')
|
||||
continue
|
||||
}
|
||||
await archivePage(userId, apiEndpoint, jwtSecret, data.id)
|
||||
break
|
||||
case RuleActionType.MarkAsRead:
|
||||
continue
|
||||
if (!data.id) {
|
||||
console.log('invalid data for mark as read action')
|
||||
continue
|
||||
}
|
||||
await markPageAsRead(userId, apiEndpoint, jwtSecret, data.id)
|
||||
break
|
||||
case RuleActionType.SendNotification:
|
||||
for (const message of action.params) {
|
||||
await sendNotification(userId, apiEndpoint, jwtSecret, message)
|
||||
|
||||
Reference in New Issue
Block a user