Skip to content

Commit cfee3e2

Browse files
Merge pull request #77 from uwblueprint/colin/edit-email-subs
Admin Dashboard: Edit Email Subscriptions
2 parents 6f9a81c + 93036fb commit cfee3e2

File tree

7 files changed

+629
-46
lines changed

7 files changed

+629
-46
lines changed

app/api/users/[id]/route.ts

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,30 @@ export async function GET(
1818

1919
const searchParams = request.nextUrl.searchParams;
2020
const getAbsences = searchParams.get('getAbsences') === 'true';
21+
const getMailingLists = searchParams.get('getMailingLists') === 'true';
2122

2223
try {
2324
const user = await prisma.user.findUnique({
2425
where: { id: validatedId },
25-
include: { absences: getAbsences },
26+
include: {
27+
absences: getAbsences,
28+
mailingLists: getMailingLists
29+
? {
30+
include: {
31+
subject: {
32+
select: {
33+
id: true,
34+
name: true,
35+
abbreviation: true,
36+
colorGroup: {
37+
select: { colorCodes: true },
38+
},
39+
},
40+
},
41+
},
42+
}
43+
: false,
44+
},
2645
});
2746

2847
if (!user) {
@@ -50,12 +69,79 @@ export async function PATCH(
5069
}
5170

5271
try {
53-
const { email, firstName, lastName, role } = await request.json();
72+
const { email, firstName, lastName, role, mailingListSubjectIds } =
73+
await request.json();
5474

55-
const updatedUser = await prisma.user.update({
56-
where: { id: validatedId },
57-
data: { email, firstName, lastName, role },
58-
});
75+
// Update basic user data if provided
76+
const userData: any = {};
77+
if (email !== undefined) userData.email = email;
78+
if (firstName !== undefined) userData.firstName = firstName;
79+
if (lastName !== undefined) userData.lastName = lastName;
80+
if (role !== undefined) userData.role = role;
81+
82+
let updatedUser;
83+
84+
// If mailingListSubjectIds is provided, we need to update the mailing lists
85+
if (mailingListSubjectIds !== undefined) {
86+
if (!Array.isArray(mailingListSubjectIds)) {
87+
return NextResponse.json(
88+
{ error: 'mailingListSubjectIds must be an array' },
89+
{ status: 400 }
90+
);
91+
}
92+
93+
// First update the user data
94+
updatedUser = await prisma.user.update({
95+
where: { id: validatedId },
96+
data: userData,
97+
});
98+
99+
// Then delete all existing mailing lists for this user
100+
await prisma.mailingList.deleteMany({
101+
where: { userId: validatedId },
102+
});
103+
104+
// Then create new entries for each subject ID
105+
if (mailingListSubjectIds.length > 0) {
106+
const mailingListData = mailingListSubjectIds.map((subjectId) => ({
107+
userId: validatedId,
108+
subjectId,
109+
}));
110+
111+
await prisma.mailingList.createMany({
112+
data: mailingListData,
113+
});
114+
}
115+
116+
// Return the updated user with mailing lists
117+
updatedUser = await prisma.user.findUnique({
118+
where: { id: validatedId },
119+
include: {
120+
mailingLists: {
121+
include: {
122+
subject: {
123+
select: {
124+
id: true,
125+
name: true,
126+
abbreviation: true,
127+
colorGroup: {
128+
select: {
129+
colorCodes: true,
130+
},
131+
},
132+
},
133+
},
134+
},
135+
},
136+
},
137+
});
138+
} else {
139+
// Just update user data without touching mailing lists
140+
updatedUser = await prisma.user.update({
141+
where: { id: validatedId },
142+
data: userData,
143+
});
144+
}
59145

60146
return NextResponse.json(updatedUser);
61147
} catch (error) {

app/api/users/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export async function GET(request: NextRequest) {
1616
select: {
1717
name: true,
1818
abbreviation: true,
19+
archived: true,
1920
colorGroup: {
2021
select: { colorCodes: true },
2122
},

src/components/EditableRoleCell.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ const EditableRoleCell = ({ role, onRoleChange }: EditableRoleCellProps) => {
133133
color="neutralGray.600"
134134
/>
135135
) : (
136-
isHovered && <Icon as={FiEdit2} color="neutralGray.600" />
136+
<Icon
137+
as={FiEdit2}
138+
color="neutralGray.600"
139+
opacity={isHovered ? 1 : 0}
140+
transition="opacity 0.3s ease-in-out"
141+
/>
137142
)}
138143
</Box>
139144
</Box>

0 commit comments

Comments
 (0)