-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathsupport.ts
More file actions
65 lines (57 loc) · 1.98 KB
/
support.ts
File metadata and controls
65 lines (57 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import { createSupportRepository } from '@/core/modules/support/repository.server'
import { throwTRPCErrorFromRepoError } from '@/core/server/adapters/errors'
import { withTeamAuthedRequestRepository } from '@/core/server/api/middlewares/repository'
import { createTRPCRouter } from '@/core/server/trpc/init'
import { protectedTeamProcedure } from '@/core/server/trpc/procedures'
import { fileSchema } from '@/core/shared/schemas/file'
const E2B_API_KEY_REGEX = /e2b_[a-f0-9]{40}/i
const supportRepositoryProcedure = protectedTeamProcedure.use(
withTeamAuthedRequestRepository(
createSupportRepository,
(supportRepository) => ({
supportRepository,
})
)
)
export const supportRouter = createTRPCRouter({
contactSupport: supportRepositoryProcedure
.input(
z.object({
description: z.string().min(1),
files: z.array(fileSchema).max(5).optional(),
})
)
.mutation(async ({ ctx, input }) => {
const { teamId, user } = ctx
const email = user.email
if (!email) {
throw new Error('Email not found')
}
if (E2B_API_KEY_REGEX.test(input.description)) {
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'Your message contains an API key. Please remove it before sending.',
})
}
const teamResult = await ctx.supportRepository.getTeamSupportData()
if (!teamResult.ok) {
throwTRPCErrorFromRepoError(teamResult.error)
}
const createResult = await ctx.supportRepository.createSupportThread({
description: input.description,
files: input.files,
teamId,
teamName: teamResult.data.name,
customerEmail: email,
accountOwnerEmail: teamResult.data.email,
customerTier: teamResult.data.tier,
})
if (!createResult.ok) {
throwTRPCErrorFromRepoError(createResult.error)
}
return createResult.data
}),
})