1+ import config from '@app/config/config' ;
2+ import { readToken } from '@app/services/localStorage.service' ;
3+ import {
4+ AllowedUsersSettings ,
5+ AllowedUsersApiResponse ,
6+ AllowedUsersNpubsResponse ,
7+ BulkImportRequest ,
8+ AllowedUsersNpub
9+ } from '@app/types/allowedUsers.types' ;
10+
11+ // Settings Management
12+ export const getAllowedUsersSettings = async ( ) : Promise < AllowedUsersSettings > => {
13+ const token = readToken ( ) ;
14+ const response = await fetch ( `${ config . baseURL } /api/settings/allowed_users` , {
15+ headers : {
16+ 'Authorization' : `Bearer ${ token } ` ,
17+ } ,
18+ } ) ;
19+
20+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
21+
22+ const text = await response . text ( ) ;
23+ try {
24+ const data : AllowedUsersApiResponse = JSON . parse ( text ) ;
25+
26+ // Transform tiers from backend format to frontend format
27+ const transformedSettings = {
28+ ...data . allowed_users ,
29+ tiers : data . allowed_users . tiers . map ( tier => ( {
30+ data_limit : ( tier as any ) . datalimit || tier . data_limit || '' ,
31+ price : tier . price
32+ } ) )
33+ } ;
34+
35+ return transformedSettings ;
36+ } catch ( jsonError ) {
37+ throw new Error ( `Invalid JSON response: ${ text } ` ) ;
38+ }
39+ } ;
40+
41+ export const updateAllowedUsersSettings = async ( settings : AllowedUsersSettings ) : Promise < { success : boolean , message : string } > => {
42+ const token = readToken ( ) ;
43+
44+ // Transform to nested format as expected by backend
45+ const nestedSettings = {
46+ "allowed_users" : {
47+ "mode" : settings . mode ,
48+ "read_access" : {
49+ "enabled" : settings . read_access . enabled ,
50+ "scope" : settings . read_access . scope
51+ } ,
52+ "write_access" : {
53+ "enabled" : settings . write_access . enabled ,
54+ "scope" : settings . write_access . scope
55+ } ,
56+ "tiers" : settings . tiers . map ( tier => ( {
57+ "datalimit" : tier . data_limit || "1 GB per month" , // Backend expects 'datalimit' not 'data_limit', fallback for empty values
58+ "price" : tier . price || "0"
59+ } ) )
60+ }
61+ } ;
62+
63+ console . log ( 'Sending to backend:' , JSON . stringify ( nestedSettings , null , 2 ) ) ;
64+
65+ const response = await fetch ( `${ config . baseURL } /api/settings/allowed_users` , {
66+ method : 'POST' ,
67+ headers : {
68+ 'Content-Type' : 'application/json' ,
69+ 'Authorization' : `Bearer ${ token } ` ,
70+ } ,
71+ body : JSON . stringify ( nestedSettings ) ,
72+ } ) ;
73+
74+ const text = await response . text ( ) ;
75+ console . log ( 'Backend response:' , response . status , text ) ;
76+
77+ if ( ! response . ok ) {
78+ console . error ( 'Backend error:' , response . status , text ) ;
79+ throw new Error ( `HTTP error! status: ${ response . status } , response: ${ text } ` ) ;
80+ }
81+
82+ try {
83+ return JSON . parse ( text ) ;
84+ } catch ( jsonError ) {
85+ throw new Error ( `Invalid JSON response: ${ text } ` ) ;
86+ }
87+ } ;
88+
89+ // Read NPUBs Management
90+ export const getReadNpubs = async ( page = 1 , pageSize = 20 ) : Promise < AllowedUsersNpubsResponse > => {
91+ const token = readToken ( ) ;
92+ const response = await fetch ( `${ config . baseURL } /api/allowed-npubs/read?page=${ page } &pageSize=${ pageSize } ` , {
93+ headers : {
94+ 'Authorization' : `Bearer ${ token } ` ,
95+ } ,
96+ } ) ;
97+
98+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
99+
100+ const text = await response . text ( ) ;
101+ try {
102+ const data = JSON . parse ( text ) ;
103+ // Transform backend response to expected format
104+ return {
105+ npubs : data . npubs || [ ] ,
106+ total : data . pagination ?. total || 0 ,
107+ page : data . pagination ?. page || page ,
108+ pageSize : data . pagination ?. pageSize || pageSize
109+ } ;
110+ } catch ( jsonError ) {
111+ throw new Error ( `Invalid JSON response: ${ text } ` ) ;
112+ }
113+ } ;
114+
115+ export const addReadNpub = async ( npub : string , tier : string ) : Promise < { success : boolean , message : string } > => {
116+ const token = readToken ( ) ;
117+ const response = await fetch ( `${ config . baseURL } /api/allowed-npubs/read` , {
118+ method : 'POST' ,
119+ headers : {
120+ 'Content-Type' : 'application/json' ,
121+ 'Authorization' : `Bearer ${ token } ` ,
122+ } ,
123+ body : JSON . stringify ( { npub, tier } ) ,
124+ } ) ;
125+
126+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
127+
128+ const text = await response . text ( ) ;
129+ try {
130+ return JSON . parse ( text ) ;
131+ } catch ( jsonError ) {
132+ throw new Error ( `Invalid JSON response: ${ text } ` ) ;
133+ }
134+ } ;
135+
136+ export const removeReadNpub = async ( npub : string ) : Promise < { success : boolean , message : string } > => {
137+ const token = readToken ( ) ;
138+ const response = await fetch ( `${ config . baseURL } /api/allowed-npubs/read/${ npub } ` , {
139+ method : 'DELETE' ,
140+ headers : {
141+ 'Authorization' : `Bearer ${ token } ` ,
142+ } ,
143+ } ) ;
144+
145+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
146+
147+ const text = await response . text ( ) ;
148+ try {
149+ return JSON . parse ( text ) ;
150+ } catch ( jsonError ) {
151+ throw new Error ( `Invalid JSON response: ${ text } ` ) ;
152+ }
153+ } ;
154+
155+ // Write NPUBs Management
156+ export const getWriteNpubs = async ( page = 1 , pageSize = 20 ) : Promise < AllowedUsersNpubsResponse > => {
157+ const token = readToken ( ) ;
158+ const response = await fetch ( `${ config . baseURL } /api/allowed-npubs/write?page=${ page } &pageSize=${ pageSize } ` , {
159+ headers : {
160+ 'Authorization' : `Bearer ${ token } ` ,
161+ } ,
162+ } ) ;
163+
164+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
165+
166+ const text = await response . text ( ) ;
167+ try {
168+ const data = JSON . parse ( text ) ;
169+ // Transform backend response to expected format
170+ return {
171+ npubs : data . npubs || [ ] ,
172+ total : data . pagination ?. total || 0 ,
173+ page : data . pagination ?. page || page ,
174+ pageSize : data . pagination ?. pageSize || pageSize
175+ } ;
176+ } catch ( jsonError ) {
177+ throw new Error ( `Invalid JSON response: ${ text } ` ) ;
178+ }
179+ } ;
180+
181+ export const addWriteNpub = async ( npub : string , tier : string ) : Promise < { success : boolean , message : string } > => {
182+ const token = readToken ( ) ;
183+ const response = await fetch ( `${ config . baseURL } /api/allowed-npubs/write` , {
184+ method : 'POST' ,
185+ headers : {
186+ 'Content-Type' : 'application/json' ,
187+ 'Authorization' : `Bearer ${ token } ` ,
188+ } ,
189+ body : JSON . stringify ( { npub, tier } ) ,
190+ } ) ;
191+
192+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
193+
194+ const text = await response . text ( ) ;
195+ try {
196+ return JSON . parse ( text ) ;
197+ } catch ( jsonError ) {
198+ throw new Error ( `Invalid JSON response: ${ text } ` ) ;
199+ }
200+ } ;
201+
202+ export const removeWriteNpub = async ( npub : string ) : Promise < { success : boolean , message : string } > => {
203+ const token = readToken ( ) ;
204+ const response = await fetch ( `${ config . baseURL } /api/allowed-npubs/write/${ npub } ` , {
205+ method : 'DELETE' ,
206+ headers : {
207+ 'Authorization' : `Bearer ${ token } ` ,
208+ } ,
209+ } ) ;
210+
211+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
212+
213+ const text = await response . text ( ) ;
214+ try {
215+ return JSON . parse ( text ) ;
216+ } catch ( jsonError ) {
217+ throw new Error ( `Invalid JSON response: ${ text } ` ) ;
218+ }
219+ } ;
220+
221+ // Bulk Import
222+ export const bulkImportNpubs = async ( importData : BulkImportRequest ) : Promise < { success : boolean , message : string , imported : number , failed : number } > => {
223+ const token = readToken ( ) ;
224+ const response = await fetch ( `${ config . baseURL } /api/allowed-npubs/bulk-import` , {
225+ method : 'POST' ,
226+ headers : {
227+ 'Content-Type' : 'application/json' ,
228+ 'Authorization' : `Bearer ${ token } ` ,
229+ } ,
230+ body : JSON . stringify ( importData ) ,
231+ } ) ;
232+
233+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
234+
235+ const text = await response . text ( ) ;
236+ try {
237+ return JSON . parse ( text ) ;
238+ } catch ( jsonError ) {
239+ throw new Error ( `Invalid JSON response: ${ text } ` ) ;
240+ }
241+ } ;
0 commit comments