Skip to content

Commit fc86ebe

Browse files
committed
chore(database): remove old database.ts file
1 parent 89d188e commit fc86ebe

File tree

9 files changed

+278
-276
lines changed

9 files changed

+278
-276
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"dev": "concurrently --names \"WEB,API,BOT\" --prefix-colors \"blue,green,magenta\" \"cd web && bun dev\" \"cd api && cargo watch -x \\\"run -- --dev\\\"\" \"bun --watch . --dev\"",
2929
"dev:bot": "bun --watch . --dev",
3030
"test:jetstream": "bun --watch src/utils/bluesky/jetstream.ts",
31-
"lint": "eslint . --ext .ts -c .eslintrc.json",
31+
"lint": "eslint src/ --ext .ts -c .eslintrc.json --debug",
3232
"lint:fix": "eslint . --ext .ts -c .eslintrc.json --fix",
3333
"cargo:api:dev": "cd api && cargo watch -x \"run -- --dev\"",
3434
"web:dev": "concurrently --names \"WEB,API\" --prefix-colors \"blue,green\" \"cd web && bun dev\" \"cd api && cargo watch -x \\\"run -- --dev\\\"\"",

src/db/audit.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
export enum AuditResolution {
2-
AUDIT_RESOLUTION_FAIL = "fail",
3-
AUDIT_RESOLUTION_SUCCESS = "success",
4-
AUDIT_RESOLUTION_ERROR = "error",
5-
AUDIT_RESOLUTION_NULL = "null",
6-
}
7-
8-
export enum AuditType {
9-
GUILD_CHECKED_YOUTUBE_CHANNEL = "guild_checked_youtube_channel",
10-
}
1+
import {
2+
dbAuditLogsEventTypeEnum,
3+
dbAuditLogsSuccessTypeEnum,
4+
dbAuditLogsTable,
5+
} from "./schema";
6+
import { db } from "./db";
117

12-
export async function addAuditEntry(
13-
type: AuditType,
14-
resolution: AuditResolution,
15-
) {
16-
console.log("Adding audit entry:", type, resolution);
17-
throw new Error("Not implemented");
8+
export async function dbAuditLogCreate(
9+
guildId: string,
10+
eventType: (typeof dbAuditLogsEventTypeEnum)["enumValues"][number],
11+
successType: (typeof dbAuditLogsSuccessTypeEnum)["enumValues"][number],
12+
data: Record<string, unknown> | null = null,
13+
): Promise<void> {
14+
try {
15+
await db.insert(dbAuditLogsTable).values({
16+
guildId,
17+
eventType,
18+
successType,
19+
data,
20+
occurredAt: new Date(),
21+
});
22+
} catch (error) {
23+
console.error("Error creating audit log entry:", error);
24+
}
1825
}

src/db/discord.ts

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,149 @@ export async function discordAddGuildTrackingUser(
134134
return { success: false, data: [] };
135135
}
136136
}
137+
138+
// Get all the Discord guilds that are tracking either YouTube or Twitch channels
139+
export async function discordGetAllGuildsTrackingChannel(
140+
platform: Platform,
141+
platformUserId: string,
142+
): Promise<
143+
| {
144+
success: true;
145+
data: (typeof dbGuildYouTubeSubscriptionsTable.$inferSelect)[];
146+
}
147+
| {
148+
success: true;
149+
data: (typeof dbGuildTwitchSubscriptionsTable.$inferSelect)[];
150+
}
151+
| { success: false; data: [] }
152+
> {
153+
try {
154+
if (platform === Platform.YouTube) {
155+
const result = await db
156+
.select()
157+
.from(dbGuildYouTubeSubscriptionsTable)
158+
.where(
159+
eq(
160+
dbGuildYouTubeSubscriptionsTable.youtubeChannelId,
161+
platformUserId,
162+
),
163+
);
164+
165+
return {
166+
success: true,
167+
data: result,
168+
};
169+
} else if (platform === Platform.Twitch) {
170+
const result = await db
171+
.select()
172+
.from(dbGuildTwitchSubscriptionsTable)
173+
.where(
174+
eq(
175+
dbGuildTwitchSubscriptionsTable.twitchChannelId,
176+
platformUserId,
177+
),
178+
);
179+
180+
return {
181+
success: true,
182+
data: result,
183+
};
184+
} else {
185+
console.error("Invalid platform provided for tracking guilds.");
186+
187+
return { success: false, data: [] };
188+
}
189+
} catch (error) {
190+
console.error("Error getting all guilds tracking channels:", error);
191+
192+
return { success: false, data: [] };
193+
}
194+
}
195+
196+
// Get all tracked in the guild
197+
export async function discordGetAllTrackedInGuild(guildId: string): Promise<
198+
| {
199+
success: true;
200+
data: {
201+
youtubeSubscriptions: (typeof dbGuildYouTubeSubscriptionsTable.$inferSelect)[];
202+
twitchSubscriptions: (typeof dbGuildTwitchSubscriptionsTable.$inferSelect)[];
203+
};
204+
}
205+
| { success: false; data: null }
206+
> {
207+
try {
208+
const youtubeSubscriptions = await db
209+
.select()
210+
.from(dbGuildYouTubeSubscriptionsTable)
211+
.where(eq(dbGuildYouTubeSubscriptionsTable.guildId, guildId));
212+
213+
const twitchSubscriptions = await db
214+
.select()
215+
.from(dbGuildTwitchSubscriptionsTable)
216+
.where(eq(dbGuildTwitchSubscriptionsTable.guildId, guildId));
217+
218+
return {
219+
success: true,
220+
data: {
221+
youtubeSubscriptions,
222+
twitchSubscriptions,
223+
},
224+
};
225+
} catch (error) {
226+
console.error(
227+
"Error getting all tracked subscriptions in guild:",
228+
error,
229+
);
230+
231+
return { success: false, data: null };
232+
}
233+
}
234+
235+
// Remove tracking for a specific channel in a guild
236+
export async function discordRemoveGuildTrackingChannel(
237+
guildId: string,
238+
platform: Platform,
239+
platformUserId: string,
240+
): Promise<{ success: boolean; data: [] }> {
241+
console.log(
242+
`Removing guild ${guildId} tracking for user ${platformUserId} on platform ${platform}`,
243+
);
244+
245+
try {
246+
if (platform === Platform.YouTube) {
247+
await db
248+
.delete(dbGuildYouTubeSubscriptionsTable)
249+
.where(
250+
and(
251+
eq(dbGuildYouTubeSubscriptionsTable.guildId, guildId),
252+
eq(
253+
dbGuildYouTubeSubscriptionsTable.youtubeChannelId,
254+
platformUserId,
255+
),
256+
),
257+
);
258+
} else if (platform === Platform.Twitch) {
259+
await db
260+
.delete(dbGuildTwitchSubscriptionsTable)
261+
.where(
262+
and(
263+
eq(dbGuildTwitchSubscriptionsTable.guildId, guildId),
264+
eq(
265+
dbGuildTwitchSubscriptionsTable.twitchChannelId,
266+
platformUserId,
267+
),
268+
),
269+
);
270+
} else {
271+
console.error("Invalid platform provided for removal.");
272+
273+
return { success: false, data: [] };
274+
}
275+
276+
return { success: true, data: [] };
277+
} catch (error) {
278+
console.error("Error removing guild tracking channel:", error);
279+
280+
return { success: false, data: [] };
281+
}
282+
}

src/db/twitch.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,23 @@ export async function addNewStreamerToTrack(
6666
return { success: false };
6767
}
6868
}
69+
70+
// Update a channel to be live or not
71+
export async function twitchUpdateIsLive(
72+
channelId: string,
73+
isLive: boolean,
74+
): Promise<{ success: boolean; data?: typeof dbTwitchTable.$inferSelect }> {
75+
try {
76+
const [updated] = await db
77+
.update(dbTwitchTable)
78+
.set({ twitchChannelIsLive: isLive })
79+
.where(eq(dbTwitchTable.twitchChannelId, channelId))
80+
.returning();
81+
82+
return { success: true, data: updated };
83+
} catch (error) {
84+
console.error("Error updating channel live status:", error);
85+
86+
return { success: false };
87+
}
88+
}

src/db/youtube.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import getChannelDetails from "../utils/youtube/getChannelDetails";
88
import { dbYouTubeTable } from "./schema";
99
import { db } from "./db";
1010

11+
// Get all the YouTube channels that are being tracked
1112
export async function dbYouTubeGetAllChannelsToTrack(): Promise<{
1213
success: boolean;
1314
data: (typeof dbYouTubeTable.$inferSelect)[];
@@ -103,3 +104,51 @@ export async function addNewChannelToTrack(
103104
return { success: false, data: [] };
104105
}
105106
}
107+
108+
// Update the latest video ID for a channel
109+
export async function youtubeUpdateVideoId(
110+
channelId: string,
111+
videoId: string,
112+
contentType: PlaylistType,
113+
updateTime: Date,
114+
): Promise<{ success: boolean; data?: typeof dbYouTubeTable.$inferSelect }> {
115+
try {
116+
const updateData: Record<string, unknown> = {
117+
latestAllId: null,
118+
latestVideoId: null,
119+
latestShortId: null,
120+
latestStreamId: null,
121+
};
122+
123+
switch (contentType) {
124+
case PlaylistType.Video:
125+
updateData.latestVideoId = videoId;
126+
updateData.latestVideoIdUpdated = updateTime;
127+
break;
128+
case PlaylistType.Short:
129+
updateData.latestShortId = videoId;
130+
updateData.latestShortIdUpdated = updateTime;
131+
break;
132+
case PlaylistType.Stream:
133+
updateData.latestStreamId = videoId;
134+
updateData.latestStreamIdUpdated = updateTime;
135+
break;
136+
}
137+
138+
// Always update the "all" column regardless of the content type
139+
updateData.latestAllId = videoId;
140+
updateData.latestAllIdUpdated = updateTime;
141+
142+
const [updated] = await db
143+
.update(dbYouTubeTable)
144+
.set(updateData)
145+
.where(eq(dbYouTubeTable.youtubeChannelId, channelId))
146+
.returning();
147+
148+
return { success: true, data: updated };
149+
} catch (error) {
150+
console.error("Error updating YouTube video ID:", error);
151+
152+
return { success: false };
153+
}
154+
}

src/types/database.d.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)