All files / convex/lib supportTickets.ts

100% Statements 130/130
100% Branches 19/19
100% Functions 7/7
100% Lines 130/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 1311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 2x 2x 2x 2x 1x 1x 2x 1x 1x 4x 4x 4x 2x 2x 2x 4x 4x 1x 6x 6x 2x 2x 2x 2x 2x 6x  
import { redactLogText } from "@workspace/logger"
import { isDiscordSnowflake } from "@workspace/shared/discordRuntimeConfig"
import { ConvexError, v, type Infer } from "convex/values"
 
import {
  guildSupportEscalationPolicy,
  guildSupportTargetType,
  guildSupportTranscriptPolicy,
} from "../dbTables/guildSupportConfigs"
 
export const SUPPORT_MESSAGE_MAX_LENGTH = 1_000
 
export function rejectSupportTicketOperation(): never {
  throw new ConvexError({
    code: "SUPPORT_TICKETS_DISABLED",
    message:
      "Support tickets are temporarily disabled while they are being rebuilt and tested.",
  })
}
 
export const openSupportTicketInput = v.object({
  requesterDiscordUserId: v.string(),
  discordGuildId: v.optional(v.string()),
  message: v.optional(v.string()),
})
 
const supportTicketRoute = v.object({
  targetId: v.string(),
  targetType: guildSupportTargetType,
  staffRoleIds: v.array(v.string()),
  threadId: v.optional(v.string()),
})
 
export const openSupportTicketResult = v.union(
  v.object({
    status: v.literal("guildSupportUnavailable"),
    reason: v.union(
      v.literal("notConfigured"),
      v.literal("disabled"),
      v.literal("unknownGuild"),
      v.literal("botLeft")
    ),
  }),
  v.object({
    status: v.union(v.literal("opened"), v.literal("resumed")),
    ticketId: v.id("supportTickets"),
    scope: v.union(v.literal("jcn"), v.literal("guild")),
    route: v.optional(supportTicketRoute),
    submittedMessage: v.optional(v.string()),
    messageStored: v.boolean(),
  })
)
 
export type OpenSupportTicketInput = Infer<typeof openSupportTicketInput>
 
export type GuildSupportConfigForRouting = {
  enabled: boolean
  staffRoleIds: string[]
  targetId?: string
  targetType: Infer<typeof guildSupportTargetType>
  transcriptPolicy: Infer<typeof guildSupportTranscriptPolicy>
  escalationPolicy: Infer<typeof guildSupportEscalationPolicy>
}
 
export function normalizeSupportTicketInput(
  input: OpenSupportTicketInput
): OpenSupportTicketInput {
  assertDiscordSnowflake("requesterDiscordUserId", input.requesterDiscordUserId)
 
  if (input.discordGuildId !== undefined) {
    assertDiscordSnowflake("discordGuildId", input.discordGuildId)
  }
 
  const message = normalizeSupportMessage(input.message)
 
  return {
    requesterDiscordUserId: input.requesterDiscordUserId,
    ...(input.discordGuildId !== undefined
      ? { discordGuildId: input.discordGuildId }
      : {}),
    ...(message !== undefined ? { message } : {}),
  }
}
 
export function normalizeSupportMessage(
  value: string | undefined
): string | undefined {
  const message = value?.trim()
 
  if (!message) {
    return undefined
  }
 
  if (message.length > SUPPORT_MESSAGE_MAX_LENGTH) {
    throw new ConvexError({
      code: "SUPPORT_MESSAGE_TOO_LONG",
      message: `Support messages must be ${SUPPORT_MESSAGE_MAX_LENGTH} characters or fewer.`,
    })
  }
 
  return redactLogText(message)
}
 
export function buildActiveSupportTicketKey(args: {
  requesterDiscordUserId: string
  discordGuildId?: string
}): string {
  return args.discordGuildId
    ? `guild:${args.discordGuildId}:${args.requesterDiscordUserId}`
    : `jcn:${args.requesterDiscordUserId}`
}
 
export function getGuildSupportUnavailableReason(
  config: GuildSupportConfigForRouting | null
): "notConfigured" | "disabled" | null {
  if (!config || !config.targetId || config.staffRoleIds.length === 0) {
    return "notConfigured"
  }
 
  return config.enabled ? null : "disabled"
}
 
function assertDiscordSnowflake(field: string, value: string): void {
  if (!isDiscordSnowflake(value)) {
    throw new ConvexError({
      code: "INVALID_DISCORD_SNOWFLAKE",
      message: `${field} must be a valid Discord snowflake.`,
    })
  }
}