pkgs/db/src/utils/ticket.ts (view raw)
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
import { randomUUID } from "node:crypto";
import { and, desc, eq } from "drizzle-orm";
import db, {
tickets,
ticketMessages,
ticketAttachments,
ticketParticipants,
ticketComments,
type Ticket,
} from "..";
// fetch
export async function getTicket(ticketId: string): Promise<Ticket | undefined>;
export async function getTicket(guildId: number, ticketId: number): Promise<Ticket | undefined>;
export async function getTicket(
idOrGuildId: string | number,
ticketId?: number,
): Promise<Ticket | undefined> {
return typeof idOrGuildId === "string"
? getTicketById(idOrGuildId)
: getTicketByGuild(idOrGuildId, ticketId);
}
export async function getTicketById(id: string) {
const [ticket] = await db.select().from(tickets).where(eq(tickets.id, id));
return ticket;
}
export async function getTicketByGuild(guildId: number, localId?: number) {
if (localId === undefined) return undefined;
const [ticket] = await db
.select()
.from(tickets)
.where(and(eq(tickets.guildId, guildId), eq(tickets.localId, localId)));
return ticket;
}
export async function getTicketByChannelId(channelId: string) {
const [ticket] = await db.select().from(tickets).where(eq(tickets.channelId, channelId));
return ticket;
}
export async function editTicket(ticketId: string, data: Partial<Ticket>) {
const [result] = await db.update(tickets).set(data).where(eq(tickets.id, ticketId)).returning();
return result;
}
// standard ops
export async function createTicket(data: {
guildId: number;
openedBy: number;
subject: number;
status: string;
channelId?: string | null;
openReason?: string | null;
}) {
const id = randomUUID();
return await db.transaction(async (tx) => {
const [lastTicket] = await tx
.select({ localId: tickets.localId })
.from(tickets)
.where(eq(tickets.guildId, data.guildId))
.orderBy(desc(tickets.localId))
.limit(1);
const nextLocalId = lastTicket ? lastTicket.localId + 1 : 1;
const [newTicket] = await tx
.insert(tickets)
.values({
id: id,
guildId: data.guildId,
localId: nextLocalId,
status: data.status,
channelId: data.channelId,
openedBy: data.openedBy,
subject: data.subject,
openReason: data.openReason,
openedAt: new Date(),
})
.returning();
if (!newTicket) throw new Error("Failed to create ticket");
await tx.insert(ticketParticipants).values({
ticketId: newTicket.id,
actorId: data.openedBy,
role: data.openedBy === data.subject ? "subject" : "participant",
createdAt: new Date(),
});
return newTicket;
});
}
export async function closeTicket(data: {
ticketId: string;
closedBy: number;
reason?: string | null;
status?: string;
}) {
const [closedTicket] = await db
.update(tickets)
.set({
status: data.status ?? "closed",
closedAt: new Date(),
closedBy: data.closedBy,
closeReason: data.reason,
})
.where(eq(tickets.id, data.ticketId))
.returning();
return closedTicket;
}
export async function claimTicket(ticketId: string, actorId: number) {
const [ticket] = await db
.update(tickets)
.set({
claimedBy: actorId,
claimedAt: new Date(),
})
.where(eq(tickets.id, ticketId))
.returning();
return ticket;
}
export async function unclaimTicket(ticketId: string) {
const [ticket] = await db
.update(tickets)
.set({
claimedBy: null,
claimedAt: null,
})
.where(eq(tickets.id, ticketId))
.returning();
return ticket;
}
// messages
export async function createTicketMessageWithAttachments(data: {
ticketId: string;
actorId: number;
publicId: string;
messageId?: string | null;
content: string;
actorRole: "participant" | "moderator" | "subject";
attachments?: {
publicId: string;
fileName: string;
fileType: string;
fileSize: number;
}[];
}) {
return await db.transaction(async (tx) => {
const [message] = await tx
.insert(ticketMessages)
.values({
publicId: data.publicId,
messageId: data.messageId,
ticketId: data.ticketId,
actorId: data.actorId,
content: data.content,
actorRole: data.actorRole,
createdAt: new Date(),
})
.returning();
if (data.attachments && data.attachments.length > 0) {
await tx.insert(ticketAttachments).values(
data.attachments.map((att) => ({
publicId: att.publicId,
messageId: message?.id,
ticketId: data.ticketId,
actorId: data.actorId,
fileName: att.fileName,
fileType: att.fileType,
fileSize: att.fileSize,
})),
);
}
const ticketUpdate: Partial<Ticket> = {
lastMessageAt: new Date(),
};
if (data.actorRole === "moderator") {
ticketUpdate.hasModeratorMessage = true;
} else if (data.actorRole === "subject") {
ticketUpdate.hasAuthorMessage = true;
}
await tx.update(tickets).set(ticketUpdate).where(eq(tickets.id, data.ticketId));
return message;
});
}
export async function getTicketTranscript(ticketId: string) {
return await db.query.tickets.findFirst({
where: {
id: ticketId,
},
with: {
opener: true,
subjectActor: true,
claimedByActor: true,
participants: {
with: {
actor: true,
},
},
messages: {
orderBy: (messages, { asc }) => [asc(messages.createdAt)],
with: {
actor: true,
attachments: true,
},
},
},
});
}
// participants
export async function addTicketParticipant(data: {
ticketId: string;
actorId: number;
role: "participant" | "moderator" | "subject";
reason?: string | null;
}) {
const [participant] = await db
.insert(ticketParticipants)
.values({
ticketId: data.ticketId,
actorId: data.actorId,
role: data.role,
reason: data.reason,
createdAt: new Date(),
})
.returning();
return participant;
}
export async function removeTicketParticipant(ticketId: string, actorId: number) {
const [removed] = await db
.delete(ticketParticipants)
.where(and(eq(ticketParticipants.ticketId, ticketId), eq(ticketParticipants.actorId, actorId)))
.returning();
return removed;
}
export async function getTicketParticipants(ticketId: string) {
return await db.query.ticketParticipants.findMany({
where: {
ticketId: ticketId,
},
with: {
actor: true,
},
});
}
// comments
export async function getTicketComments(ticketId: string) {
return await db.query.ticketComments.findMany({
where: {
ticketId: ticketId,
},
with: {
actor: true,
},
});
}
export async function addTicketComment(data: {
ticketId: string;
actorId: number;
content: string;
}) {
const [comment] = await db
.insert(ticketComments)
.values({
id: randomUUID(),
ticketId: data.ticketId,
actorId: data.actorId,
content: data.content,
createdAt: new Date(),
updatedAt: new Date(),
})
.returning();
return comment;
}
export async function removeTicketComment(commentId: number) {
const [removed] = await db
.delete(ticketComments)
.where(eq(ticketComments.id, commentId))
.returning();
return removed;
}
export async function editTicketComment(commentId: number, content: string) {
const [updated] = await db
.update(ticketComments)
.set({
content: content,
updatedAt: new Date(),
})
.where(eq(ticketComments.id, commentId))
.returning();
return updated;
}
|