@@ -146,9 +146,10 @@ export async function getProjectConfigs(
146146 }
147147}
148148
149- export async function encryptProjectAction (
149+ async function toggleProjectEncryption (
150150 projectId : string ,
151- apiKey : string
151+ apiKey : string ,
152+ action : "encrypt" | "decrypt"
152153) : Promise < { success ?: boolean ; error ?: string } > {
153154 try {
154155 await getCurrentUser ( ) ;
@@ -166,17 +167,14 @@ export async function encryptProjectAction(
166167 return { error : "Project not found or access denied" } ;
167168 }
168169 if ( membership . role !== "owner" ) {
169- return { error : " Only organization owners can enable encryption" } ;
170+ return { error : ` Only organization owners can ${ action === "encrypt" ? " enable" : "disable" } encryption` } ;
170171 }
171172
172- const baseUrl = process . env . ACONTEXT_API_BASE_URL ?? "https://admin.acontext.app" ;
173- const resp = await fetch ( `${ baseUrl } /admin/v1/project/encrypt` , {
174- method : "POST" ,
175- headers : { Authorization : `Bearer ${ apiKey } ` , "Content-Type" : "application/json" } ,
176- } ) ;
177- if ( ! resp . ok ) {
178- const body = await resp . json ( ) . catch ( ( ) => ( { } ) ) ;
179- throw new Error ( body . msg || `HTTP ${ resp . status } ` ) ;
173+ const client = new AcontextClient ( ) ;
174+ if ( action === "encrypt" ) {
175+ await client . encryptProject ( projectId , apiKey ) ;
176+ } else {
177+ await client . decryptProject ( projectId , apiKey ) ;
180178 }
181179
182180 const encodedProjectId = encodeId ( projectId ) ;
@@ -185,53 +183,23 @@ export async function encryptProjectAction(
185183 return { success : true } ;
186184 } catch ( error ) {
187185 return {
188- error : `Failed to encrypt project: ${ error instanceof Error ? error . message : "Unknown error" } ` ,
186+ error : `Failed to ${ action } project: ${ error instanceof Error ? error . message : "Unknown error" } ` ,
189187 } ;
190188 }
191189}
192190
193- export async function decryptProjectAction (
191+ export async function encryptProjectAction (
194192 projectId : string ,
195193 apiKey : string
196194) : Promise < { success ?: boolean ; error ?: string } > {
197- try {
198- await getCurrentUser ( ) ;
199-
200- const project = await getProject ( projectId ) ;
201- if ( ! project ) {
202- return { error : "Project not found" } ;
203- }
204-
205- const membership = await getOrganizationMembershipForCurrentUser (
206- project . organization_id ,
207- "role"
208- ) ;
209- if ( ! membership ) {
210- return { error : "Project not found or access denied" } ;
211- }
212- if ( membership . role !== "owner" ) {
213- return { error : "Only organization owners can disable encryption" } ;
214- }
215-
216- const baseUrl = process . env . ACONTEXT_API_BASE_URL ?? "https://admin.acontext.app" ;
217- const resp = await fetch ( `${ baseUrl } /admin/v1/project/decrypt` , {
218- method : "POST" ,
219- headers : { Authorization : `Bearer ${ apiKey } ` , "Content-Type" : "application/json" } ,
220- } ) ;
221- if ( ! resp . ok ) {
222- const body = await resp . json ( ) . catch ( ( ) => ( { } ) ) ;
223- throw new Error ( body . msg || `HTTP ${ resp . status } ` ) ;
224- }
225-
226- const encodedProjectId = encodeId ( projectId ) ;
227- revalidatePath ( `/project/${ encodedProjectId } ` , "layout" ) ;
195+ return toggleProjectEncryption ( projectId , apiKey , "encrypt" ) ;
196+ }
228197
229- return { success : true } ;
230- } catch ( error ) {
231- return {
232- error : `Failed to decrypt project: ${ error instanceof Error ? error . message : "Unknown error" } ` ,
233- } ;
234- }
198+ export async function decryptProjectAction (
199+ projectId : string ,
200+ apiKey : string
201+ ) : Promise < { success ?: boolean ; error ?: string } > {
202+ return toggleProjectEncryption ( projectId , apiKey , "decrypt" ) ;
235203}
236204
237205export async function updateProjectConfigs (
0 commit comments