Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/api/scim/v2/Groups/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ function scimError(detail: string, status: number) {
}

function toScimGroupResponse(
group: { id: string; displayName: string },
group: { id: string; displayName: string; externalId?: string | null },
members: Array<{ value: string; display?: string }> = [],
) {
return {
schemas: ["urn:ietf:params:scim:schemas:core:2.0:Group"],
id: group.id,
...(group.externalId ? { externalId: group.externalId } : {}),
displayName: group.displayName,
members,
};
Expand Down Expand Up @@ -342,7 +343,6 @@ export async function DELETE(
const affectedUserIds = group.members.map((m) => m.userId);

// Delete group and reconcile all affected users in a single transaction
// to prevent stale group_mapping TeamMembers if a crash occurs mid-way
await prisma.$transaction(async (tx) => {
await tx.scimGroup.delete({ where: { id } });
for (const userId of affectedUserIds) {
Expand Down
33 changes: 19 additions & 14 deletions src/app/api/scim/v2/Groups/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ interface ScimGroupResponse {
}

function toScimGroupResponse(
group: { id: string; displayName: string },
group: { id: string; displayName: string; externalId?: string | null },
members: Array<{ value: string; display?: string }> = [],
): ScimGroupResponse {
): ScimGroupResponse & { externalId?: string } {
return {
schemas: ["urn:ietf:params:scim:schemas:core:2.0:Group"],
id: group.id,
...(group.externalId ? { externalId: group.externalId } : {}),
displayName: group.displayName,
members,
};
Expand Down Expand Up @@ -100,48 +101,52 @@ export async function POST(req: NextRequest) {
return scimError("displayName is required", 400);
}

const { group, memberResponses, isNew } = await prisma.$transaction(async (tx) => {
const { group, memberResponses, auditAction } = await prisma.$transaction(async (tx) => {
const existing = await tx.scimGroup.findUnique({
where: { displayName },
});

let scimGroup;
let adopted = false;
let action: "scim.group_created" | "scim.group_adopted" | null = null;

if (existing) {
scimGroup = existing;
adopted = true;
if (body.externalId && body.externalId !== existing.externalId) {
scimGroup = await tx.scimGroup.update({
where: { id: existing.id },
data: { externalId: body.externalId },
});
action = "scim.group_adopted";
}
// If nothing changed, skip audit (avoids flooding on every sync cycle)
} else {
scimGroup = await tx.scimGroup.create({
data: {
displayName,
externalId: body.externalId ?? null,
},
});
action = "scim.group_created";
}

const members = await processGroupMembers(tx, scimGroup.id, body.members);

return { group: scimGroup, memberResponses: members, isNew: !adopted };
return { group: scimGroup, memberResponses: members, auditAction: action };
});

await writeAuditLog({
userId: null,
action: isNew ? "scim.group_created" : "scim.group_adopted",
entityType: "ScimGroup",
entityId: group.id,
metadata: { displayName },
});
if (auditAction) {
await writeAuditLog({
userId: null,
action: auditAction,
entityType: "ScimGroup",
entityId: group.id,
metadata: { displayName },
});
}

return NextResponse.json(
toScimGroupResponse(group, memberResponses),
{ status: isNew ? 201 : 200 },
{ status: auditAction === "scim.group_created" ? 201 : 200 },
);
} catch (error) {
const message =
Expand Down
Loading