1import { 2 AuthService, 3 UserRole 4} from "./chunk-QGEJYSW7.js"; 5import { 6 ApiUrlService, 7 StorageService 8} from "./chunk-MAIAEXGG.js"; 9import { 10 BehaviorSubject, 11 HttpClient, 12 Injectable, 13 catchError, 14 map, 15 setClassMetadata, 16 tap, 17 throwError, 18 ɵɵdefineInjectable, 19 ɵɵinject 20} from "./chunk-UQ6LLOJ6.js"; 21import { 22 __async 23} from "./chunk-CO64IJQE.js"; 24 25// src/app/services/user-campus/user-campus.service.ts 26var _UserCampusService = class _UserCampusService { 27 constructor(http, apiUrlService, storageService, authService) { 28 this.http = http; 29 this.apiUrlService = apiUrlService; 30 this.storageService = storageService; 31 this.authService = authService; 32 this.ACCESSIBLE_CAMPUSES_KEY = "accessible_campus_ids"; 33 this.IS_SUPER_ADMIN_KEY = "is_super_admin"; 34 this.accessibleCampusIdsSubject = new BehaviorSubject([]); 35 this.accessibleCampusIds$ = this.accessibleCampusIdsSubject.asObservable(); 36 this.accessibleCampusesSubject = new BehaviorSubject([]); 37 this.accessibleCampuses$ = this.accessibleCampusesSubject.asObservable(); 38 this.isSuperAdminSubject = new BehaviorSubject(false); 39 this.isSuperAdmin$ = this.isSuperAdminSubject.asObservable(); 40 this.apiUrl = this.apiUrlService.getSelectedUrl() + "/api/v1/UserCampus"; 41 this.apiUrlService.selectedUrl$.subscribe((url) => { 42 this.apiUrl = url + "/api/v1/UserCampus"; 43 }); 44 this.loadStoredCampusAccess(); 45 } 46 loadStoredCampusAccess() { 47 return __async(this, null, function* () { 48 try { 49 yield this.storageService.onInitialized; 50 const campusIdsJson = yield this.storageService.get(this.ACCESSIBLE_CAMPUSES_KEY); 51 const isSuperAdmin = yield this.storageService.get(this.IS_SUPER_ADMIN_KEY); 52 if (campusIdsJson) { 53 const campusIds = JSON.parse(campusIdsJson); 54 this.accessibleCampusIdsSubject.next(campusIds); 55 } 56 if (isSuperAdmin === "true") { 57 this.isSuperAdminSubject.next(true); 58 } 59 } catch (error) { 60 console.error("Failed to load stored campus access", error); 61 } 62 }); 63 } 64 /** 65 * Get accessible campus IDs for the current user 66 */ 67 getMyAccessibleCampuses() { 68 return this.http.get(`${this.apiUrl}/me/campuses`).pipe(tap((response) => __async(this, null, function* () { 69 yield this.storageService.onInitialized; 70 yield this.storageService.set(this.ACCESSIBLE_CAMPUSES_KEY, JSON.stringify(response.campusIds)); 71 yield this.storageService.set(this.IS_SUPER_ADMIN_KEY, response.isSuperAdmin ? "true" : "false"); 72 this.accessibleCampusIdsSubject.next(response.campusIds); 73 this.accessibleCampusesSubject.next(response.campuses || []); 74 this.isSuperAdminSubject.next(response.isSuperAdmin); 75 })), catchError((error) => { 76 console.error("Failed to get accessible campuses", error); 77 return throwError(() => error); 78 })); 79 } 80 /** 81 * Get campus assignments for a specific user (Admin only) 82 */ 83 getUserCampuses(userId) { 84 return this.http.get(`${this.apiUrl}/user/${userId}`).pipe(map((response) => response.assignments), catchError((error) => { 85 console.error("Failed to get user campuses", error); 86 return throwError(() => error); 87 })); 88 } 89 /** 90 * Get user assignments for a specific campus (Admin only) 91 */ 92 getCampusUsers(campusId) { 93 return this.http.get(`${this.apiUrl}/campus/${campusId}`).pipe(map((response) => response.assignments), catchError((error) => { 94 console.error("Failed to get campus users", error); 95 return throwError(() => error); 96 })); 97 } 98 /** 99 * Assign a user to a campus (Admin only) 100 */ 101 assignUserToCampus(userId, campusId) { 102 return this.http.post(`${this.apiUrl}`, { userId, campusId }).pipe(catchError((error) => { 103 console.error("Failed to assign user to campus", error); 104 return throwError(() => error); 105 })); 106 } 107 /** 108 * Remove a user from a campus (Admin only) 109 */ 110 removeUserFromCampus(userId, campusId) { 111 return this.http.delete(`${this.apiUrl}/${userId}/${campusId}`).pipe(catchError((error) => { 112 console.error("Failed to remove user from campus", error); 113 return throwError(() => error); 114 })); 115 } 116 /** 117 * Sync user's campus assignments (Admin only) 118 */ 119 syncUserCampuses(userId, campusIds) { 120 return this.http.put(`${this.apiUrl}/sync/${userId}`, { campusIds }).pipe(map((response) => response.assignments), catchError((error) => { 121 console.error("Failed to sync user campuses", error); 122 return throwError(() => error); 123 })); 124 } 125 /** 126 * Check if current user can access a specific campus 127 */ 128 canAccessCampus(campusId) { 129 if (this.isSuperAdminSubject.value) { 130 return true; 131 } 132 return this.accessibleCampusIdsSubject.value.includes(campusId); 133 } 134 /** 135 * Get current accessible campus IDs (synchronous) 136 */ 137 get accessibleCampusIds() { 138 return this.accessibleCampusIdsSubject.value; 139 } 140 /** 141 * Check if current user is SuperAdmin (synchronous) 142 */ 143 get isSuperAdmin() { 144 var _a; 145 if (((_a = this.authService.currentUserValue) == null ? void 0 : _a.role) === UserRole.SuperAdmin) { 146 return true; 147 } 148 return this.isSuperAdminSubject.value; 149 } 150 get accessibleCampuses() { 151 return this.accessibleCampusesSubject.value; 152 } 153 /** 154 * Clear stored campus access (on logout) 155 */ 156 clearCampusAccess() { 157 return __async(this, null, function* () { 158 yield this.storageService.onInitialized; 159 yield this.storageService.remove(this.ACCESSIBLE_CAMPUSES_KEY); 160 yield this.storageService.remove(this.IS_SUPER_ADMIN_KEY); 161 this.accessibleCampusIdsSubject.next([]); 162 this.accessibleCampusesSubject.next([]); 163 this.isSuperAdminSubject.next(false); 164 }); 165 } 166}; 167_UserCampusService.\u0275fac = function UserCampusService_Factory(__ngFactoryType__) { 168 return new (__ngFactoryType__ || _UserCampusService)(\u0275\u0275inject(HttpClient), \u0275\u0275inject(ApiUrlService), \u0275\u0275inject(StorageService), \u0275\u0275inject(AuthService)); 169}; 170_UserCampusService.\u0275prov = /* @__PURE__ */ \u0275\u0275defineInjectable({ token: _UserCampusService, factory: _UserCampusService.\u0275fac, providedIn: "root" }); 171var UserCampusService = _UserCampusService; 172(() => { 173 (typeof ngDevMode === "undefined" || ngDevMode) && setClassMetadata(UserCampusService, [{ 174 type: Injectable, 175 args: [{ 176 providedIn: "root" 177 }] 178 }], () => [{ type: HttpClient }, { type: ApiUrlService }, { type: StorageService }, { type: AuthService }], null); 179})(); 180 181export { 182 UserCampusService 183};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.