all repos — stealth-developers @ b30621f89da1d2cbca3735c0662931f8f0608758

src/database/queries.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
import { type Guild, type Ticket, db, guild, manager, tickets } from "@/database";
import type { Snowflake } from "discord.js";
import { and, eq, sql } from "drizzle-orm";

type QueryResult<T, WillCreate extends boolean = false> =
	| { exists: true; data: T }
	| (WillCreate extends true ? { exists: false; data: T } : { exists: false; data: T | null });

export async function getGuild(guildId: Snowflake): Promise<QueryResult<Guild, true>> {
	const result = await db.select().from(guild).where(eq(guild.guildId, guildId)).execute();

	if (!result || result.length === 0) {
		const newGuild = await db.insert(guild).values({ guildId }).returning().execute();
		return {
			exists: false,
			data: newGuild[0],
		};
	}

	return {
		exists: true,
		data: result[0],
	};
}

export async function getTicketMessage(guildId: Snowflake): Promise<QueryResult<string, false>> {
	const guild = await getGuild(guildId);
	if (!guild || !guild.data.ticket_message) {
		return {
			exists: false,
			data: null,
		};
	}

	return {
		exists: true,
		data: guild.data.ticket_message,
	};
}

export async function getTicket(
	guildId: Snowflake,
	channelId: Snowflake,
): Promise<QueryResult<Ticket, false>> {
	const guild = await getGuild(guildId);
	if (!guild || !guild.data.ticket_message) {
		return {
			exists: false,
			data: null,
		};
	}

	const ticket = await db
		.selectDistinct()
		.from(tickets)
		.where(and(eq(tickets.guildId, guildId), eq(tickets.channelId, channelId)))
		.execute();

	if (!ticket || ticket.length === 0) {
		return {
			exists: false,
			data: null,
		};
	}

	return {
		exists: true,
		data: ticket[0],
	};
}

export async function getTicketById(
	guildId: Snowflake | null,
	id: number,
): Promise<QueryResult<Ticket, false>> {
	// const guild = await getGuild(guildId);
	// if (!guild || !guild.data.ticket_message) {
	// 	return {
	// 		exists: false,
	// 		data: null,
	// 	};
	// }

	const ticket = await db
		.select()
		.from(tickets)
		// .where(and(eq(tickets.guildId, guildId), eq(tickets.id, id)))
		.where(eq(tickets.id, id))
		.execute();

	if (!ticket || ticket.length === 0) {
		return {
			exists: false,
			data: null,
		};
	}

	return {
		exists: true,
		data: ticket[0],
	};
}

export async function getTicketByNumericId(
	guildId: Snowflake,
	numericId: number,
): Promise<QueryResult<Ticket, false>> {
	const guild = await getGuild(guildId);
	if (!guild || !guild.data.ticket_message) {
		return {
			exists: false,
			data: null,
		};
	}

	const ticket = await db
		.select()
		.from(tickets)
		.where(and(eq(tickets.guildId, guildId), eq(tickets.ticketNumber, numericId)))
		.execute();

	if (!ticket || ticket.length === 0) {
		return {
			exists: false,
			data: null,
		};
	}

	return {
		exists: true,
		data: ticket[0],
	};
}

export async function getManagerRoleIds(guildId: Snowflake): Promise<Snowflake[]> {
	const rows = await db
		.select({ roleId: manager.roleId })
		.from(manager)
		.where(eq(manager.guildId, guildId))
		.execute();

	return rows.map((r) => r.roleId);
}

export async function countTickets(guildId: Snowflake) {
	const result = await db
		.select({ count: sql<number>`COUNT(*)` })
		.from(tickets)
		.where(eq(tickets.guildId, guildId))
		.execute();

	return result[0].count;
}