Skip to content

Commit

Permalink
feat(schema): add tag editor management API
Browse files Browse the repository at this point in the history
  • Loading branch information
devformatters authored and Zeck Li committed Sep 17, 2020
1 parent 17a4f20 commit 69efba8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2100,11 +2100,14 @@ input UpdateNotificationSettingInput {
input UpdateTagSettingInput {
id: ID!
type: UpdateTagSettingType!
editor: ID
}

enum UpdateTagSettingType {
adopt
leave
add_editor
remove_editor
}

input UpdateUserInfoInput {
Expand Down
3 changes: 3 additions & 0 deletions src/definitions/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2273,11 +2273,14 @@ export interface GQLPutTagInput {
export interface GQLUpdateTagSettingInput {
id: string
type: GQLUpdateTagSettingType
editor?: string
}

export const enum GQLUpdateTagSettingType {
adopt = 'adopt',
leave = 'leave',
add_editor = 'add_editor',
remove_editor = 'remove_editor',
}

export interface GQLAddArticlesTagsInput {
Expand Down
59 changes: 55 additions & 4 deletions src/mutations/article/updateTagSetting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _uniq from 'lodash/uniq'
import _without from 'lodash/without'

import { CACHE_KEYWORD, NODE_TYPES, USER_STATE } from 'common/enums'
import { environment } from 'common/environment'
Expand All @@ -8,6 +9,7 @@ import {
ForbiddenError,
TagNotFoundError,
UserInputError,
UserNotFoundError,
} from 'common/errors'
import { fromGlobalId, isFeatureEnabled } from 'common/utils'
import {
Expand All @@ -17,7 +19,7 @@ import {

const resolver: MutationToUpdateTagSettingResolver = async (
_,
{ input: { id, type } },
{ input: { id, type, editor } },
{ viewer, dataSources: { systemService, tagService, userService } }
) => {
if (!viewer.id) {
Expand All @@ -35,9 +37,11 @@ const resolver: MutationToUpdateTagSettingResolver = async (
throw new TagNotFoundError('tag not found')
}

const isOwner = tag.owner === viewer.id

let params: Record<string, any> = {}
switch (type) {
case GQLUpdateTagSettingType.adopt:
case GQLUpdateTagSettingType.adopt: {
// check feature is enabled
const feature = await systemService.getFeatureFlag('tag_adoption')
if (feature && !isFeatureEnabled(feature.flag, viewer)) {
Expand All @@ -51,7 +55,8 @@ const resolver: MutationToUpdateTagSettingResolver = async (

params = { owner: viewer.id, editors: _uniq([...tag.editors, viewer.id]) }
break
case GQLUpdateTagSettingType.leave:
}
case GQLUpdateTagSettingType.leave: {
// if tag has no owner or owner is not viewer, throw error
if (!tag.owner || (tag.owner && tag.owner !== viewer.id)) {
throw new ForbiddenError('viewer has no permission')
Expand All @@ -63,9 +68,55 @@ const resolver: MutationToUpdateTagSettingResolver = async (
: (tag.editors || []).filter((item: string) => item !== viewer.id)
params = { owner: null, editors }
break
default:
}
case GQLUpdateTagSettingType.add_editor: {
if (!isOwner) {
throw new ForbiddenError('viewer has no permission')
}
if (!editor) {
throw new UserInputError('editor is invalid')
}

const editors = _without(tag.editors, tag.owner, environment.mattyId)
if (editors.length >= 4) {
throw new UserInputError('number of editors reaches limit')
}

const { id: editorId } = fromGlobalId(editor)
if (isOwner && viewer.id === editorId) {
throw new UserInputError('cannot add self')
}
const user = await userService.baseFindById(editorId)
if (!user) {
throw new UserNotFoundError('editor not found')
}
params = { editors: _uniq([...editors, editorId]) }
break
}
case GQLUpdateTagSettingType.remove_editor: {
if (!isOwner) {
throw new ForbiddenError('viewer has no permission')
}
if (!editor) {
throw new UserInputError('editor is invalid')
}

const { id: editorId } = fromGlobalId(editor)
if (isOwner && viewer.id === editorId) {
throw new UserInputError('cannot remove self')
}
const user = await userService.baseFindById(editorId)
if (!user) {
throw new UserNotFoundError('editor not found')
}
const editors = _without(tag.editors, editorId)
params = { editors }
break
}
default: {
throw new UserInputError('unknown update tag type')
break
}
}

const updatedTag = await tagService.baseUpdate(tagId, params)
Expand Down
3 changes: 3 additions & 0 deletions src/types/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ export default /* GraphQL */ `
input UpdateTagSettingInput {
id: ID!
type: UpdateTagSettingType!
editor: ID
}
input AddArticlesTagsInput {
Expand Down Expand Up @@ -451,5 +452,7 @@ export default /* GraphQL */ `
enum UpdateTagSettingType {
adopt
leave
add_editor
remove_editor
}
`

0 comments on commit 69efba8

Please sign in to comment.