11'use server' ;
22
33import Ajv from "ajv" ;
4- import { getUser } from "./data/user" ;
5- import { auth } from "./auth" ;
6- import { notAuthenticated , notFound , ServiceError , unexpectedError } from "./lib/serviceError" ;
4+ import { auth , getCurrentUserOrg } from "./auth" ;
5+ import { notAuthenticated , notFound , ServiceError , unexpectedError } from "@/lib/serviceError" ;
76import { prisma } from "@/prisma" ;
87import { StatusCodes } from "http-status-codes" ;
9- import { ErrorCode } from "./lib/errorCodes" ;
8+ import { ErrorCode } from "@/lib/errorCodes" ;
9+ import { isServiceError } from "@/lib/utils" ;
1010import { githubSchema } from "@sourcebot/schemas/v3/github.schema" ;
1111import { encrypt } from "@sourcebot/crypto"
1212
@@ -15,31 +15,9 @@ const ajv = new Ajv({
1515} ) ;
1616
1717export const createSecret = async ( key : string , value : string ) : Promise < { success : boolean } | ServiceError > => {
18- const session = await auth ( ) ;
19- if ( ! session ) {
20- return notAuthenticated ( ) ;
21- }
22-
23- const user = await getUser ( session . user . id ) ;
24- if ( ! user ) {
25- return unexpectedError ( "User not found" ) ;
26- }
27- const orgId = user . activeOrgId ;
28- if ( ! orgId ) {
29- return unexpectedError ( "User has no active org" ) ;
30- }
31-
32- // @todo : refactor this into a shared function
33- const membership = await prisma . userToOrg . findUnique ( {
34- where : {
35- orgId_userId : {
36- userId : session . user . id ,
37- orgId,
38- }
39- } ,
40- } ) ;
41- if ( ! membership ) {
42- return notFound ( ) ;
18+ const orgId = await getCurrentUserOrg ( ) ;
19+ if ( isServiceError ( orgId ) ) {
20+ return orgId ;
4321 }
4422
4523 try {
@@ -62,30 +40,9 @@ export const createSecret = async (key: string, value: string): Promise<{ succes
6240}
6341
6442export const getSecrets = async ( ) : Promise < { createdAt : Date ; key : string ; } [ ] | ServiceError > => {
65- const session = await auth ( ) ;
66- if ( ! session ) {
67- return notAuthenticated ( ) ;
68- }
69-
70- const user = await getUser ( session . user . id ) ;
71- if ( ! user ) {
72- return unexpectedError ( "User not found" ) ;
73- }
74- const orgId = user . activeOrgId ;
75- if ( ! orgId ) {
76- return unexpectedError ( "User has no active org" ) ;
77- }
78-
79- const membership = await prisma . userToOrg . findUnique ( {
80- where : {
81- orgId_userId : {
82- userId : session . user . id ,
83- orgId,
84- }
85- } ,
86- } ) ;
87- if ( ! membership ) {
88- return notFound ( ) ;
43+ const orgId = await getCurrentUserOrg ( ) ;
44+ if ( isServiceError ( orgId ) ) {
45+ return orgId ;
8946 }
9047
9148 const secrets = await prisma . secret . findMany ( {
@@ -105,30 +62,9 @@ export const getSecrets = async (): Promise<{ createdAt: Date; key: string; }[]
10562}
10663
10764export const deleteSecret = async ( key : string ) : Promise < { success : boolean } | ServiceError > => {
108- const session = await auth ( ) ;
109- if ( ! session ) {
110- return notAuthenticated ( ) ;
111- }
112-
113- const user = await getUser ( session . user . id ) ;
114- if ( ! user ) {
115- return unexpectedError ( "User not found" ) ;
116- }
117- const orgId = user . activeOrgId ;
118- if ( ! orgId ) {
119- return unexpectedError ( "User has no active org" ) ;
120- }
121-
122- const membership = await prisma . userToOrg . findUnique ( {
123- where : {
124- orgId_userId : {
125- userId : session . user . id ,
126- orgId,
127- }
128- } ,
129- } ) ;
130- if ( ! membership ) {
131- return notFound ( ) ;
65+ const orgId = await getCurrentUserOrg ( ) ;
66+ if ( isServiceError ( orgId ) ) {
67+ return orgId ;
13268 }
13369
13470 await prisma . secret . delete ( {
@@ -206,31 +142,9 @@ export const switchActiveOrg = async (orgId: number): Promise<{ id: number } | S
206142}
207143
208144export const createConnection = async ( config : string ) : Promise < { id : number } | ServiceError > => {
209- const session = await auth ( ) ;
210- if ( ! session ) {
211- return notAuthenticated ( ) ;
212- }
213-
214- const user = await getUser ( session . user . id ) ;
215- if ( ! user ) {
216- return unexpectedError ( "User not found" ) ;
217- }
218- const orgId = user . activeOrgId ;
219- if ( ! orgId ) {
220- return unexpectedError ( "User has no active org" ) ;
221- }
222-
223- // @todo : refactor this into a shared function
224- const membership = await prisma . userToOrg . findUnique ( {
225- where : {
226- orgId_userId : {
227- userId : session . user . id ,
228- orgId,
229- }
230- } ,
231- } ) ;
232- if ( ! membership ) {
233- return notFound ( ) ;
145+ const orgId = await getCurrentUserOrg ( ) ;
146+ if ( isServiceError ( orgId ) ) {
147+ return orgId ;
234148 }
235149
236150 let parsedConfig ;
0 commit comments