all repos — stealth-developers @ d198bbf452eb549845999f475e608f713fe774df

apps/api/src/lib/db.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
import db, { TicketWithRelations, ticketWithRelations } from "@stealth-developers/db";

export interface PaginationOptions {
	limit?: number;
	offset?: number;
}

export async function getTicketsByGuildId(
	guildId: number,
	userId: number | undefined,
	options: PaginationOptions = {},
): Promise<TicketWithRelations[]> {
	const { limit, offset } = options;

	const ticketsList = await db.query.tickets.findMany({
		where: {
			guildId: guildId,
			...(userId !== undefined ? { subject: userId } : {}),
		},
		orderBy: (table, { desc }) => [desc(table.localId)],
		limit: limit,
		offset: offset,
		with: ticketWithRelations,
	});

	return ticketsList;
}

export async function getLinkedActors(userId: number) {
	const actors = await db.query.actors.findMany({
		where: {
			userId: userId,
		},
		with: {
			guild: true,
		},
	});

	return actors;
}