-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeleteProjectMember.ts
More file actions
116 lines (98 loc) · 3.49 KB
/
deleteProjectMember.ts
File metadata and controls
116 lines (98 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { resolver } from "@blitzjs/rpc"
import db from "db"
import { DeleteProjectMemberSchema } from "../schemas"
import countProjectManagers from "../queries/countProjectManagers"
export default resolver.pipe(
resolver.zod(DeleteProjectMemberSchema),
resolver.authorize(),
async ({ id }, ctx) => {
// Find the project member to be deleted
const projectMemberToDelete = await db.projectMember.findUnique({
where: { id },
include: { users: true },
})
// Check if projectMemberToDelete is undefined or has no users
if (!projectMemberToDelete) {
throw new Error("Contributor not found")
}
// Ensure there's exactly one user associated with this project member
if (projectMemberToDelete.users.length !== 1) {
throw new Error("Invalid number of users associated with this project member")
}
// Get the userId from the associated users array
const userId = projectMemberToDelete.users[0]!.id
// Reconstruct possible display names used in notification messages
const user = projectMemberToDelete.users[0]
const possibleDisplayNames: string[] = []
if (user!.firstName && user!.lastName) {
possibleDisplayNames.push(`${user!.firstName} ${user!.lastName}`)
}
if (user!.username) {
possibleDisplayNames.push(user!.username)
}
const notificationMarker = " (former contributor)"
// Check if the project member has any privileges related to the project
const projectPrivilege = await db.projectPrivilege.findFirst({
where: {
userId: userId,
projectId: projectMemberToDelete.projectId,
},
})
if (!projectPrivilege) {
throw new Error("Project privilege not found for the user")
}
// Count the number of project managers in the project using the countProjectManagers query
const projectManagerCount = await countProjectManagers(
{
projectId: projectMemberToDelete.projectId,
},
ctx
)
// Check if the projectMember to delete is the last project manager
if (projectPrivilege.privilege === "PROJECT_MANAGER" && projectManagerCount <= 1) {
throw new Error("Cannot delete the last project manager on the project.")
}
// Delete project widgets associated with this user and project
await db.projectWidget.deleteMany({
where: {
userId: userId,
projectId: projectMemberToDelete.projectId,
},
})
// Proceed to delete the project privilege
await db.projectPrivilege.delete({
where: { id: projectPrivilege.id },
})
// Annotate existing notifications that reference this contributor by name
if (possibleDisplayNames.length > 0) {
const notifications = await db.notification.findMany({
where: {
projectId: projectMemberToDelete.projectId,
announcement: false,
},
})
await Promise.all(
notifications
.filter(
(n) =>
!n.message.includes(notificationMarker) &&
possibleDisplayNames.some((name) => n.message.includes(name))
)
.map((n) =>
db.notification.update({
where: { id: n.id },
data: {
message: `${n.message}${notificationMarker}`,
},
})
)
)
}
// Delete the project member
const projectMember = await db.projectMember.update({
where: { id: projectMemberToDelete.id },
data: { deleted: true },
})
return projectMember
}
)