all repos — stealth-developers @ 14d97e857cddc21919a4158fe9daa53d1112a264

pkgs/db/src/utils/index.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
import { eq, and } from "drizzle-orm";
import db, { actors, sessions, users, guilds } from "..";

export async function getUser(id: number) {
	const [result] = await db.select().from(users).where(eq(users.id, id));
	return result;
}

export async function getUserByDiscordId(discordId: string) {
	const [result] = await db.select().from(users).where(eq(users.discordId, discordId));
	return result;
}

export async function getActor(id: number) {
	const [result] = await db.select().from(actors).where(eq(actors.id, id));
	return result;
}

export async function getActorByDiscordId(discordId: string, guildId: number) {
	const [result] = await db
		.select()
		.from(actors)
		.where(and(eq(actors.discordId, discordId), eq(actors.guildId, guildId)));
	return result;
}

export async function getActorByDiscordIdAndGuildDiscordId(
	discordId: string,
	guildDiscordId: string,
) {
	const [guild] = await db.select().from(guilds).where(eq(guilds.discordId, guildDiscordId));
	if (!guild) return null;
	return getActorByDiscordId(discordId, guild.id);
}

export async function getUserSessions(id: number) {
	const result = await db.select().from(sessions).where(eq(sessions.userId, id));
	return result;
}

export async function getSession(id: string) {
	const [result] = await db.select().from(sessions).where(eq(sessions.id, id));
	return result;
}

export async function upsertGuild(guild: { discordId: string; name: string; icon: string | null }) {
	const [upsertedGuild] = await db
		.insert(guilds)
		.values({
			discordId: guild.discordId,
			name: guild.name,
			icon: guild.icon,
			createdAt: new Date(),
			updatedAt: new Date(),
		})
		.onConflictDoUpdate({
			target: guilds.discordId,
			set: {
				name: guild.name,
				icon: guild.icon,
				updatedAt: new Date(),
			},
		})
		.returning();

	return upsertedGuild;
}

export async function upsertUser(user: {
	username: string;
	displayName: string | null;
	id: string;
	avatar: string | null;
}) {
	const [upsertedUser] = await db
		.insert(users)
		.values({
			discordId: user.id,
			username: user.username,
			displayName: user.displayName,
			avatar: user.avatar,
			createdAt: new Date(),
			updatedAt: new Date(),
		})
		.onConflictDoUpdate({
			target: users.discordId,
			set: {
				username: user.username,
				displayName: user.displayName,
				avatar: user.avatar,
				updatedAt: new Date(),
			},
		})
		.returning();

	return upsertedUser;
}

export async function upsertActor(data: {
	guild: {
		discordId: string;
		name: string;
		icon: string | null;
	};
	user: {
		discordId: string;
		username: string;
		displayName: string | null;
		avatar: string | null;
	};
	moderator: boolean;
}) {
	const dbGuild = await upsertGuild(data.guild);
	const dbUser = await upsertUser({
		id: data.user.discordId,
		username: data.user.username,
		displayName: data.user.displayName,
		avatar: data.user.avatar,
	});

	if (!dbGuild || !dbUser) {
		throw new Error("Failed to upsert guild or user");
	}

	const [actor] = await db
		.insert(actors)
		.values({
			guildId: dbGuild.id,
			userId: dbUser.id,
			discordId: data.user.discordId,
			username: data.user.username,
			displayName: data.user.displayName,
			avatar: data.user.avatar,
			moderator: data.moderator,
			createdAt: new Date(),
			updatedAt: new Date(),
		})
		.onConflictDoUpdate({
			target: [actors.guildId, actors.userId],
			set: {
				username: data.user.username,
				displayName: data.user.displayName,
				avatar: data.user.avatar,
				moderator: data.moderator,
				updatedAt: new Date(),
			},
		})
		.returning();

	return actor;
}

export async function anonymiseUser(id: number) {
	const result = await db
		.update(actors)
		.set({
			username: `Anonymised User`,
			displayName: null,
			avatar: null,
			updatedAt: new Date(),
		})
		.where(eq(actors.id, id))
		.returning();

	return result;
}