wrap select in a auth transaction

This commit is contained in:
Hongbo Wu 2024-04-16 11:26:29 +08:00
parent a9779a51e8
commit d79cc1c39f

View file

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