1
1
"use server" ;
2
2
3
- import { Board , IBoard } from "@/db/models/Board" ;
4
- import dbConnect from "@/db/dbConnect" ;
5
- import { ObjectId } from "mongoose" ;
6
- import { IUser , User } from "@/db/models/User" ;
7
- import { ITeam , Team } from "@/db/models/Team" ;
8
- import { TeamMember } from "@/db/models/TeamMember" ;
9
- import { IDoc , Doc } from "@/db/models/Doc" ;
3
+ import { prisma } from "@/db/prisma" ;
4
+ import { Board , Team , User } from "@prisma/client" ;
10
5
11
- export async function createTeam ( {
6
+ export async function createBoard ( {
12
7
name,
13
8
teamId,
14
9
isMergeRequestRequired,
15
- docId ,
10
+ docUrl ,
16
11
} : {
17
12
name : string ;
18
- teamId : ObjectId ;
13
+ teamId : string ;
19
14
isMergeRequestRequired ?: boolean ;
20
- docId ?: string ;
21
- } ) : Promise < IBoard | null > {
15
+ docUrl ?: string ;
16
+ } ) : Promise < Board | null > {
22
17
try {
23
- await dbConnect ( ) ;
24
- const board = await Board . create ( {
25
- name,
26
- teamId,
27
- isMergeRequestRequired,
28
- docId,
18
+ const board = await prisma . board . create ( {
19
+ data : {
20
+ name,
21
+ teamId,
22
+ isMergeRequestRequired : isMergeRequestRequired ?? true ,
23
+ docUrl,
24
+ } ,
29
25
} ) ;
30
26
return board ;
31
27
} catch ( e ) {
@@ -34,60 +30,81 @@ export async function createTeam({
34
30
}
35
31
}
36
32
37
- export async function getBoardById ( id : string ) : Promise < IBoard | null > {
33
+ export async function deleteBoard ( boardId : string ) : Promise < Board | null > {
34
+ try {
35
+ const board = await prisma . board . delete ( {
36
+ where : { id : boardId } ,
37
+ } ) ;
38
+ return board ;
39
+ } catch ( e ) {
40
+ console . error ( e ) ;
41
+ return null ;
42
+ }
43
+ }
44
+
45
+ export async function getBoardById ( id : string ) : Promise < Board | null > {
38
46
try {
39
47
if ( ! id ) {
40
48
console . error ( "Board ID is required" ) ;
41
49
return null ;
42
50
}
43
- await dbConnect ( ) ;
44
- const board = await Board . findById ( id ) ;
45
- return JSON . parse ( JSON . stringify ( board ) ) ;
51
+ const board = await prisma . board . findUnique ( {
52
+ where : { id } ,
53
+ } ) ;
54
+ return board ;
46
55
} catch ( e ) {
47
56
console . error ( e ) ;
48
57
return null ;
49
58
}
50
59
}
51
60
52
- export async function getUser ( email : string ) : Promise < IUser | null > {
61
+ export async function getUser ( email : string ) : Promise < User | null > {
53
62
try {
54
- await dbConnect ( ) ;
55
- return await User . findOne ( { email : email } ) ;
63
+ return await prisma . user . findUnique ( {
64
+ where : { email } ,
65
+ } ) ;
56
66
} catch ( error ) {
57
67
console . error ( "Failed to fetch user:" , error ) ;
58
68
return null ;
59
69
}
60
70
}
61
71
62
- export async function getUserTeams ( userId : string ) : Promise < ITeam [ ] | null > {
72
+ export async function getUserTeams ( userId : string ) : Promise < Team [ ] | null > {
63
73
try {
64
- await dbConnect ( ) ;
65
- const userTeamMemberships = await TeamMember . find ( { userId } ) ;
66
- const teamIds = userTeamMemberships . map ( ( membership ) => membership . teamId ) ;
67
- const teams = await Team . find ( { _id : { $in : teamIds } } ) ;
68
- return JSON . parse ( JSON . stringify ( teams ) ) ;
74
+ const teams = await prisma . team . findMany ( {
75
+ where : {
76
+ members : {
77
+ some : {
78
+ userId,
79
+ } ,
80
+ } ,
81
+ } ,
82
+ } ) ;
83
+ return teams ;
69
84
} catch ( e ) {
70
85
console . error ( e ) ;
71
86
return null ;
72
87
}
73
88
}
74
89
75
- export async function getTeamBoards ( teamId : string ) : Promise < IBoard [ ] | null > {
90
+ export async function getTeamBoards ( teamId : string ) : Promise < Board [ ] | null > {
76
91
try {
77
- await dbConnect ( ) ;
78
- const boards = await Board . find ( { teamId } ) ;
79
- return JSON . parse ( JSON . stringify ( boards ) ) ;
92
+ const boards = await prisma . board . findMany ( {
93
+ where : { teamId } ,
94
+ } ) ;
95
+ return boards ;
80
96
} catch ( e ) {
81
97
console . error ( e ) ;
82
98
return null ;
83
99
}
84
100
}
85
101
86
- export async function getTeam ( id : string ) : Promise < ITeam | null > {
102
+ export async function getTeam ( id : string ) : Promise < Team | null > {
87
103
try {
88
- await dbConnect ( ) ;
89
- const team = await Team . findById ( id ) ;
90
- return JSON . parse ( JSON . stringify ( team ) ) ;
104
+ const team = await prisma . team . findUnique ( {
105
+ where : { id } ,
106
+ } ) ;
107
+ return team ;
91
108
} catch ( e ) {
92
109
console . error ( e ) ;
93
110
return null ;
@@ -96,15 +113,17 @@ export async function getTeam(id: string): Promise<ITeam | null> {
96
113
97
114
export async function updateBoard (
98
115
boardId : string ,
99
- updateData : Partial < IBoard >
100
- ) {
116
+ updateData : Partial < Board >
117
+ ) : Promise < Board | null > {
101
118
if ( ! boardId ) {
102
119
console . error ( "Board ID is required" ) ;
103
120
return null ;
104
121
}
105
122
try {
106
- await dbConnect ( ) ;
107
- return await Board . findByIdAndUpdate ( boardId , updateData ) ;
123
+ return await prisma . board . update ( {
124
+ where : { id : boardId } ,
125
+ data : updateData ,
126
+ } ) ;
108
127
} catch ( e ) {
109
128
console . error ( e ) ;
110
129
return null ;
@@ -113,8 +132,10 @@ export async function updateBoard(
113
132
114
133
export async function getBoardDocUrl ( boardId : string ) : Promise < string | null > {
115
134
try {
116
- await dbConnect ( ) ;
117
- const board = await Board . findById ( boardId ) ;
135
+ const board = await prisma . board . findUnique ( {
136
+ where : { id : boardId } ,
137
+ select : { docUrl : true } ,
138
+ } ) ;
118
139
return board ?. docUrl || null ;
119
140
} catch ( e ) {
120
141
console . error ( e ) ;
0 commit comments