Create a label for new group if not exists

This commit is contained in:
Hongbo Wu 2022-12-08 18:20:58 +08:00
parent ce7cb1efac
commit 9f260c1442
2 changed files with 31 additions and 0 deletions

View file

@ -38,6 +38,7 @@ import { In } from 'typeorm'
import { getPageByParam } from '../../elastic/pages'
import { enqueueRecommendation } from '../../utils/createTask'
import { env } from '../../env'
import { createLabel } from '../../services/labels'
export const createGroupResolver = authorized<
CreateGroupSuccess,
@ -71,6 +72,9 @@ export const createGroupResolver = authorized<
expiresInDays: input.expiresInDays,
})
// create a new label for the group
await createLabel(userData, { name: input.name })
const inviteUrl = getInviteUrl(invite)
const user = userDataToUser(userData)

View file

@ -76,3 +76,30 @@ export const getLabelsByIds = async (
select: ['id', 'name', 'color', 'description', 'createdAt'],
})
}
export const createLabel = async (
user: User,
label: {
name: string
color?: string
description?: string
}
): Promise<Label> => {
const existingLabel = await getRepository(Label).findOneBy({
user: { id: user.id },
name: ILike(label.name),
})
if (existingLabel) {
return existingLabel
}
// create a new label and assign a random color if not provided
label.color =
label.color || `#${Math.floor(Math.random() * 16777215).toString(16)}`
return getRepository(Label).save({
...label,
user: { id: user.id },
})
}