1+ import { eq , and } from "drizzle-orm" ;
2+
13import { Platform } from "../types/types" ;
2- import { pool } from "../utils/database" ;
4+
5+ import { db } from "./db" ;
6+ import {
7+ dbGuildYouTubeSubscriptionsTable ,
8+ dbGuildTwitchSubscriptionsTable ,
9+ } from "./schema" ;
310
411export async function checkIfGuildIsTrackingUserAlready (
512 platform : Platform ,
613 userId : string ,
714 guildId : string ,
8- ) : Promise < { success : boolean ; data : any [ ] | null } > {
15+ ) : Promise <
16+ | {
17+ success : true ;
18+ data :
19+ | ( typeof dbGuildYouTubeSubscriptionsTable . $inferSelect ) [ ]
20+ | ( typeof dbGuildTwitchSubscriptionsTable . $inferSelect ) [ ]
21+ | null ;
22+ }
23+ | { success : false ; data : null }
24+ > {
925 console . log (
1026 `Checking if guild ${ guildId } is tracking user ${ userId } on platform ${ platform } ` ,
1127 ) ;
1228
13- let query : string | null = null ;
14-
15- if ( platform === Platform . YouTube ) {
16- query = `
17- SELECT * FROM guild_youtube_subscriptions
18- WHERE youtube_channel_id = $1 AND guild_id = $2
19- ` ;
20- } else if ( platform === Platform . Twitch ) {
21- query = `
22- SELECT * FROM guild_twitch_subscriptions
23- WHERE twitch_user_id = $1 AND guild_id = $2
24- ` ;
25- }
26-
27- if ( ! query ) {
28- console . error ( "Invalid platform provided for tracking check." ) ;
29-
30- return { success : false , data : null } ;
31- }
32-
3329 try {
34- const client = await pool . connect ( ) ;
35- const result = await client . query ( query , [ userId , guildId ] ) ;
36-
37- client . release ( ) ;
38-
39- if ( result . rows . length > 0 ) {
40- return { success : true , data : result . rows } ;
30+ let result : any [ ] = [ ] ;
31+
32+ if ( platform === Platform . YouTube ) {
33+ result = await db
34+ . select ( )
35+ . from ( dbGuildYouTubeSubscriptionsTable )
36+ . where (
37+ and (
38+ eq (
39+ dbGuildYouTubeSubscriptionsTable . youtubeChannelId ,
40+ userId ,
41+ ) ,
42+ eq ( dbGuildYouTubeSubscriptionsTable . guildId , guildId ) ,
43+ ) ,
44+ ) ;
45+ } else if ( platform === Platform . Twitch ) {
46+ result = await db
47+ . select ( )
48+ . from ( dbGuildTwitchSubscriptionsTable )
49+ . where (
50+ and (
51+ eq (
52+ dbGuildTwitchSubscriptionsTable . twitchChannelId ,
53+ userId ,
54+ ) ,
55+ eq ( dbGuildTwitchSubscriptionsTable . guildId , guildId ) ,
56+ ) ,
57+ ) ;
4158 } else {
42- return { success : true , data : null } ;
59+ console . error ( "Invalid platform provided for tracking check." ) ;
60+
61+ return { success : false , data : null } ;
4362 }
63+
64+ return {
65+ success : true ,
66+ data : result . length > 0 ? result : null ,
67+ } ;
4468 } catch ( error ) {
4569 console . error ( "Error checking if guild is tracking user:" , error ) ;
4670
@@ -65,60 +89,44 @@ export async function discordAddGuildTrackingUser(
6589 `Adding guild ${ guildId } tracking for user ${ platformUserId } on platform ${ platform } ` ,
6690 ) ;
6791
68- let query : string | null = null ;
69- let params : any [ ] = [ ] ;
70-
71- if ( platform === Platform . YouTube ) {
72- if (
73- youtubeTrackVideos === undefined ||
74- youtubeTrackVideos === null ||
75- youtubeTrackShorts === undefined ||
76- youtubeTrackShorts === null ||
77- youtubeTrackLive === undefined ||
78- youtubeTrackLive === null
79- ) {
80- console . error (
81- "YouTube tracking options must be provided for YouTube subscriptions." ,
82- ) ;
92+ try {
93+ if ( platform === Platform . YouTube ) {
94+ if (
95+ youtubeTrackVideos == null ||
96+ youtubeTrackShorts == null ||
97+ youtubeTrackLive == null
98+ ) {
99+ console . error (
100+ "YouTube tracking options must be provided for YouTube subscriptions." ,
101+ ) ;
102+
103+ return { success : false , data : [ ] } ;
104+ }
105+
106+ await db . insert ( dbGuildYouTubeSubscriptionsTable ) . values ( {
107+ youtubeChannelId : platformUserId ,
108+ guildId,
109+ notificationChannelId : guildChannelId ,
110+ notificationRoleId : roleId ,
111+ isDm,
112+ trackVideos : youtubeTrackVideos ,
113+ trackShorts : youtubeTrackShorts ,
114+ trackStreams : youtubeTrackLive ,
115+ } ) ;
116+ } else if ( platform === Platform . Twitch ) {
117+ await db . insert ( dbGuildTwitchSubscriptionsTable ) . values ( {
118+ twitchChannelId : platformUserId ,
119+ guildId,
120+ notificationChannelId : guildChannelId ,
121+ notificationRoleId : roleId ,
122+ isDm,
123+ } ) ;
124+ } else {
125+ console . error ( "Invalid platform provided." ) ;
83126
84127 return { success : false , data : [ ] } ;
85128 }
86129
87- query = `
88- INSERT INTO guild_youtube_subscriptions (
89- youtube_channel_id, guild_id, notification_channel_id, notification_role_id, is_dm,
90- track_videos, track_shorts, track_streams
91- ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
92- ` ;
93- params = [
94- platformUserId ,
95- guildId ,
96- guildChannelId ,
97- roleId ,
98- isDm ,
99- youtubeTrackVideos ?? false ,
100- youtubeTrackShorts ?? false ,
101- youtubeTrackLive ?? false ,
102- ] ;
103- } else if ( platform === Platform . Twitch ) {
104- query = `
105- INSERT INTO guild_twitch_subscriptions (
106- twitch_channel_id, guild_id, notification_channel_id, notification_role_id, is_dm
107- ) VALUES ($1, $2, $3, $4, $5)
108- ` ;
109- params = [ platformUserId , guildId , guildChannelId , roleId , isDm ] ;
110- }
111-
112- if ( ! query ) {
113- return { success : false , data : [ ] } ;
114- }
115-
116- try {
117- const client = await pool . connect ( ) ;
118-
119- await client . query ( query , params ) ;
120- client . release ( ) ;
121-
122130 return { success : true , data : [ ] } ;
123131 } catch ( error ) {
124132 console . error ( "Error adding guild tracking user:" , error ) ;
0 commit comments