Create a rule after creating or joining a group

This commit is contained in:
Hongbo Wu 2022-12-08 18:35:22 +08:00
parent 9f260c1442
commit b942303b18
2 changed files with 66 additions and 1 deletions

View file

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

View 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 },
})
}