mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Create a rule after creating or joining a group
This commit is contained in:
parent
9f260c1442
commit
b942303b18
2 changed files with 66 additions and 1 deletions
|
|
@ -39,6 +39,8 @@ import { getPageByParam } from '../../elastic/pages'
|
|||
import { enqueueRecommendation } from '../../utils/createTask'
|
||||
import { env } from '../../env'
|
||||
import { createLabel } from '../../services/labels'
|
||||
import { createRule } from '../../services/rules'
|
||||
import { RuleActionType } from '../../entity/rule'
|
||||
|
||||
export const createGroupResolver = authorized<
|
||||
CreateGroupSuccess,
|
||||
|
|
@ -73,7 +75,23 @@ export const createGroupResolver = authorized<
|
|||
})
|
||||
|
||||
// create a new label for the group
|
||||
await createLabel(userData, { name: input.name })
|
||||
const label = await createLabel(userData, { name: input.name })
|
||||
|
||||
// create a rule to add the label to all pages in the group
|
||||
await createRule(userData, {
|
||||
name: input.name,
|
||||
actions: [
|
||||
{
|
||||
type: RuleActionType.AddLabel,
|
||||
params: [label.id],
|
||||
},
|
||||
{
|
||||
type: RuleActionType.SendNotification,
|
||||
params: [input.name],
|
||||
},
|
||||
],
|
||||
filter: `recommendedBy:${group.name}`,
|
||||
})
|
||||
|
||||
const inviteUrl = getInviteUrl(invite)
|
||||
const user = userDataToUser(userData)
|
||||
|
|
@ -267,6 +285,25 @@ export const joinGroupResolver = authorized<
|
|||
|
||||
const group = await joinGroup(user, inviteCode)
|
||||
|
||||
// create a new label for the group
|
||||
const label = await createLabel(user, { name: group.name })
|
||||
|
||||
// create a rule to add the label to all pages in the group
|
||||
await createRule(user, {
|
||||
name: group.name,
|
||||
actions: [
|
||||
{
|
||||
type: RuleActionType.AddLabel,
|
||||
params: [label.id],
|
||||
},
|
||||
{
|
||||
type: RuleActionType.SendNotification,
|
||||
params: [group.name],
|
||||
},
|
||||
],
|
||||
filter: `recommendedBy:${group.name}`,
|
||||
})
|
||||
|
||||
return {
|
||||
group,
|
||||
}
|
||||
|
|
|
|||
28
packages/api/src/services/rules.ts
Normal file
28
packages/api/src/services/rules.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { User } from '../entity/user'
|
||||
import { Rule, RuleAction } from '../entity/rule'
|
||||
import { getRepository } from '../entity/utils'
|
||||
import { ILike } from 'typeorm'
|
||||
|
||||
export const createRule = async (
|
||||
user: User,
|
||||
rule: {
|
||||
name: string
|
||||
description?: string
|
||||
actions: RuleAction[]
|
||||
filter: string
|
||||
}
|
||||
): Promise<Rule> => {
|
||||
const existingRule = await getRepository(Rule).findOneBy({
|
||||
user: { id: user.id },
|
||||
name: ILike(rule.name),
|
||||
})
|
||||
|
||||
if (existingRule) {
|
||||
return existingRule
|
||||
}
|
||||
|
||||
return getRepository(Rule).save({
|
||||
...rule,
|
||||
user: { id: user.id },
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue