Fix some issue

This commit is contained in:
Hongbo Wu 2022-12-08 18:50:36 +08:00
parent b942303b18
commit 04cb360c2f
4 changed files with 36 additions and 46 deletions

View file

@ -25,6 +25,7 @@ import {
} from '../../generated/graphql'
import {
createGroup,
createLabelAndRuleForGroup,
getInviteUrl,
getRecommendationGroups,
joinGroup,
@ -38,9 +39,6 @@ import { In } from 'typeorm'
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,
@ -74,24 +72,7 @@ export const createGroupResolver = authorized<
expiresInDays: input.expiresInDays,
})
// create a new label for the group
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}`,
})
await createLabelAndRuleForGroup(uid, group.name)
const inviteUrl = getInviteUrl(invite)
const user = userDataToUser(userData)
@ -285,24 +266,7 @@ 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}`,
})
await createLabelAndRuleForGroup(user.id, group.name)
return {
group,

View file

@ -8,6 +8,9 @@ import { RecommendationGroup, User as GraphqlUser } from '../generated/graphql'
import { getRepository } from '../entity/utils'
import { homePageURL } from '../env'
import { userDataToUser } from '../utils/helpers'
import { createLabel } from './labels'
import { createRule } from './rules'
import { RuleActionType } from '../entity/rule'
export const createGroup = async (input: {
admin: User
@ -200,3 +203,27 @@ export const leaveGroup = async (
return true
})
}
export const createLabelAndRuleForGroup = async (
userId: string,
groupName: string
) => {
// create a new label for the group
const label = await createLabel(userId, { name: groupName })
// create a rule to add the label to all pages in the group
await createRule(userId, {
name: groupName,
actions: [
{
type: RuleActionType.AddLabel,
params: [label.id],
},
{
type: RuleActionType.SendNotification,
params: [groupName],
},
],
filter: `recommendedBy:"${groupName}"`,
})
}

View file

@ -78,7 +78,7 @@ export const getLabelsByIds = async (
}
export const createLabel = async (
user: User,
userId: string,
label: {
name: string
color?: string
@ -86,7 +86,7 @@ export const createLabel = async (
}
): Promise<Label> => {
const existingLabel = await getRepository(Label).findOneBy({
user: { id: user.id },
user: { id: userId },
name: ILike(label.name),
})
@ -100,6 +100,6 @@ export const createLabel = async (
return getRepository(Label).save({
...label,
user: { id: user.id },
user: { id: userId },
})
}

View file

@ -1,10 +1,9 @@
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,
userId: string,
rule: {
name: string
description?: string
@ -13,7 +12,7 @@ export const createRule = async (
}
): Promise<Rule> => {
const existingRule = await getRepository(Rule).findOneBy({
user: { id: user.id },
user: { id: userId },
name: ILike(rule.name),
})
@ -23,6 +22,6 @@ export const createRule = async (
return getRepository(Rule).save({
...rule,
user: { id: user.id },
user: { id: userId },
})
}