1+ import { Router , Request , Response } from "express" ;
2+ import { GlobalService } from "../../database/schemas/Service" ;
3+
4+ const router = Router ( ) ;
5+
6+ // Add a new global service
7+ router . post ( "/add" , async ( req : Request , res : Response ) => {
8+ const { key, name, alwaysEnabled = false } = req . body ;
9+
10+ if ( ! key || ! name ) {
11+ return res . status ( 400 ) . json ( { error : "Missing 'key' or 'name'" } ) ;
12+ }
13+
14+ try {
15+ const existing = await GlobalService . findOne ( { key } ) ;
16+ if ( existing ) {
17+ return res . status ( 400 ) . json ( { error : "Service with this key already exists" } ) ;
18+ }
19+
20+ const newService = new GlobalService ( {
21+ key,
22+ name,
23+ alwaysEnabled,
24+ enabledUsers : [ ] // stores user.email
25+ } ) ;
26+
27+ await newService . save ( ) ;
28+ res . status ( 201 ) . json ( { message : "Service added" , service : newService } ) ;
29+ } catch ( err ) {
30+ console . error ( err ) ;
31+ res . status ( 500 ) . json ( { error : "Internal Server Error" } ) ;
32+ }
33+ } ) ;
34+
35+ // Get all services enabled for a specific user (by email)
36+ router . get ( "/enabled/:email" , async ( req : Request , res : Response ) => {
37+ const email = req . params . email ;
38+
39+ try {
40+ const services = await GlobalService . find (
41+ {
42+ $or : [
43+ { alwaysEnabled : true } ,
44+ { enabledUsers : email }
45+ ]
46+ } ,
47+ {
48+ enabledUsers : 0 // Exclude enabledUsers field from the result
49+ }
50+ ) ;
51+
52+ res . status ( 200 ) . json ( { data : { services } } ) ;
53+ } catch ( err ) {
54+ res . status ( 500 ) . json ( { error : "Internal Server Error" } ) ;
55+ }
56+ } ) ;
57+
58+ // Toggle a service on/off for a user (by email)
59+ router . post ( "/toggle" , async ( req : Request , res : Response ) => {
60+ const { email, key, enabled } = req . body ;
61+
62+ if ( ! email || ! key ) {
63+ return res . status ( 400 ) . json ( { error : "Missing 'email' or 'service key'" } ) ;
64+ }
65+
66+ try {
67+ const service = await GlobalService . findOne ( { key } ) ;
68+
69+ if ( ! service ) {
70+ return res . status ( 404 ) . json ( { error : "Service not found" } ) ;
71+ }
72+
73+ if ( service . alwaysEnabled ) {
74+ return res . status ( 400 ) . json ( { error : "Cannot toggle an always-enabled service" } ) ;
75+ }
76+
77+ // Update based on the requested 'enabled' value instead of toggling
78+ const update = enabled
79+ ? { $addToSet : { enabledUsers : email } } // Add the user if not already there
80+ : { $pull : { enabledUsers : email } } ; // Remove the user
81+
82+ await GlobalService . updateOne ( { key } , update ) ;
83+
84+ res . status ( 200 ) . json ( {
85+ message : `Service ${ enabled ? 'enabled' : 'disabled' } ` ,
86+ changed : true
87+ } ) ;
88+
89+ } catch ( err ) {
90+ console . error ( err ) ;
91+ res . status ( 500 ) . json ( { error : "Internal Server Error" } ) ;
92+ }
93+ } ) ;
94+
95+ // Get all available services (no user filtering)
96+ router . get ( "/all" , async ( req : Request , res : Response ) => {
97+ try {
98+ const services = await GlobalService . find ( { } , { enabledUsers : 0 } ) ; // exclude enabledUsers field
99+ res . status ( 200 ) . json ( { data : { services } } ) ;
100+ } catch ( err ) {
101+ console . error ( err ) ;
102+ res . status ( 500 ) . json ( { error : "Internal Server Error" } ) ;
103+ }
104+ } ) ;
105+
106+ router . get ( "/alladmin" , async ( req : Request , res : Response ) => {
107+ try {
108+ const servicesAdmin = await GlobalService . find ( { } ) ;
109+ res . status ( 200 ) . json ( { data : { servicesAdmin } } ) ;
110+ } catch ( err ) {
111+ console . error ( err ) ;
112+ res . status ( 500 ) . json ( { error : "Internal Server Error" } ) ;
113+ }
114+ } ) ;
115+
116+ export default router ;
0 commit comments