diff --git a/packages/api/src/resolvers/labels/index.ts b/packages/api/src/resolvers/labels/index.ts index cd5ec824b..2e3b9c6d2 100644 --- a/packages/api/src/resolvers/labels/index.ts +++ b/packages/api/src/resolvers/labels/index.ts @@ -90,17 +90,22 @@ export const createLabelResolver = authorized< CreateLabelError, MutationCreateLabelArgs >(async (_, { input }, { authTrx, uid }) => { - const existingLabel = await labelRepository.findByName(input.name, uid) - if (existingLabel) { + const label = await authTrx(async (tx) => { + const repo = tx.withRepository(labelRepository) + const existingLabel = await repo.findByName(input.name, uid) + if (existingLabel) { + return null + } + + return repo.createLabel(input, uid) + }) + + if (!label) { return { errorCodes: [CreateLabelErrorCode.LabelAlreadyExists], } } - const label = await authTrx(async (tx) => - tx.withRepository(labelRepository).createLabel(input, uid) - ) - analytics.capture({ distinctId: uid, event: 'label_created',