Skip to content

Commit c2e5efd

Browse files
authored
Merge pull request #80 from codegasms/shubh-groups-integration
Fix groups crud integration
2 parents cfed72d + 290107f commit c2e5efd

File tree

6 files changed

+2091
-9
lines changed

6 files changed

+2091
-9
lines changed

app/[orgId]/groups/page.tsx

+24-6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const fields: Field[] = [
6161
{ name: "users", label: "Users (one email per line)", type: "textarea" },
6262
];
6363

64+
6465
export default function GroupsPage() {
6566
const params = useParams();
6667
const orgId = params.orgId as string;
@@ -132,39 +133,56 @@ export default function GroupsPage() {
132133

133134
const saveGroup = async (group: Group) => {
134135
try {
136+
// Create a copy of the group object to avoid mutating the original
137+
const groupToSave = { ...group };
138+
139+
// Convert users from string to array of strings (splitting at newlines)
140+
if (typeof groupToSave.users === 'string') {
141+
groupToSave.users = groupToSave.users
142+
.split('\n')
143+
.map(user => user.trim())
144+
.filter(user => user.length > 0); // Remove empty lines
145+
}
146+
147+
console.log("groups", groupToSave);
148+
135149
const url = selectedGroup
136150
? `/api/orgs/${orgId}/groups/${group.nameId}`
137151
: `/api/orgs/${orgId}/groups`;
138-
152+
139153
const response = await fetch(url, {
140154
method: selectedGroup ? "PATCH" : "POST",
141155
headers: {
142156
"Content-Type": "application/json",
143157
},
144-
body: JSON.stringify(group),
158+
body: JSON.stringify(groupToSave),
145159
});
146-
160+
147161
if (!response.ok) {
148162
const errorData = await response.json().catch(() => ({}));
149163
throw new Error(formatValidationErrors(errorData));
150164
}
151-
165+
152166
const savedGroup = await response.json();
153-
167+
// console.log('saved groups',savedGroup);
168+
169+
154170
if (selectedGroup) {
155171
setGroups(groups.map((g) => (g.id === savedGroup.id ? savedGroup : g)));
156172
toast({
157173
title: "Success",
158174
description: "Group updated successfully",
159175
});
160176
} else {
177+
console.log("savedgroups",savedGroup);
178+
161179
setGroups([...groups, savedGroup]);
162180
toast({
163181
title: "Success",
164182
description: "Group created successfully",
165183
});
166184
}
167-
185+
168186
setIsEditorOpen(false);
169187
} catch (error) {
170188
console.error("Error saving group:", error);

app/[orgId]/submissions/page.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export default function SubmissionsPage({
3030
const fetchSubmissions = useCallback(async () => {
3131
try {
3232
const response = await fetch(`/api/orgs/${params.orgId}/submissions`);
33+
console.log('submission',response);
34+
3335
if (!response.ok) {
3436
const errorData = await response.json().catch(() => ({}));
3537
throw new Error(formatValidationErrors(errorData));

app/api/orgs/[orgId]/groups/[groupId]/service.ts

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export async function updateGroupMembers(
4646
.where(and(eq(groups.id, groupId), eq(groups.orgId, orgId)))
4747
.limit(1);
4848

49+
console.log("group selected",group);
50+
4951
if (group.length === 0) {
5052
throw new Error("Group not found");
5153
}
@@ -56,8 +58,11 @@ export async function updateGroupMembers(
5658
.from(users)
5759
.where(inArray(users.email, emails));
5860

61+
console.log("users to keep",users_to_keep);
62+
5963
const user_ids_to_keep = users_to_keep.map((u) => u.id);
6064

65+
console.log("users ids to keep",user_ids_to_keep);
6166
// Remove members not in the new list
6267
await tx
6368
.delete(groupMemberships)

app/api/orgs/[orgId]/groups/route.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,29 @@ export async function POST(
2525
{ params }: { params: { orgId: string } },
2626
) {
2727
try {
28+
// console.log("request",await request.json());
29+
30+
2831
const orgId = await getOrgIdFromNameId(NameIdSchema.parse(params.orgId));
29-
const { emails, ...rest } = createGroupSchema.parse(await request.json());
30-
32+
const requestData = await request.json();
33+
console.log("request data",requestData);
34+
35+
// Rename 'users' to 'emails' in the request data
36+
const { users, ...restRequestData } = requestData;
37+
const processedData = {
38+
...restRequestData,
39+
emails: users, // Rename 'users' to 'emails'
40+
};
41+
console.log("emails",processedData.emails);
42+
console.log("emails",restRequestData);
43+
44+
// Now validate with your schema
45+
const { emails, ...rest } = createGroupSchema.parse(processedData);
46+
47+
console.log("emails", emails);
48+
console.log("rest", rest);
49+
50+
3151
const group = await groupsService.createGroup(orgId, rest);
3252

3353
if (emails) {

app/api/orgs/[orgId]/submissions/route.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ export async function GET(
1919

2020
// Get numeric orgId
2121
const orgId = await getOrgIdFromNameId(orgNameId);
22-
22+
console.log("orgid=",orgId);
23+
2324
const submissions = await submissionService.getOrgSubmissions(orgId);
25+
console.log('sub',submissions);
26+
27+
2428
return NextResponse.json(submissions);
2529
} catch (error) {
2630
if (error instanceof z.ZodError) {

0 commit comments

Comments
 (0)