mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working on add/delete users to workspace. Edit workspaces and some permissions.
This commit is contained in:
@@ -8,6 +8,10 @@ import {Organization} from "@prisma/client";
|
||||
import {z} from "zod";
|
||||
import {v4 as uuidv4} from "uuid";
|
||||
import {db} from "@/db";
|
||||
import {
|
||||
OrganizationFormSchema
|
||||
} from "@/components/wrappers/dashboard/organization/OrganizationForm/organization-form.schema";
|
||||
import {ProjectSchema} from "@/components/wrappers/dashboard/projects/ProjectsForm/ProjectForm.schema";
|
||||
|
||||
|
||||
const verifySlugUniqueness = async (slug: string) => {
|
||||
@@ -71,6 +75,95 @@ export const createOrganizationAction = userAction
|
||||
|
||||
|
||||
|
||||
export const updateOrganizationAction = userAction
|
||||
.schema(
|
||||
z.object({
|
||||
data: OrganizationFormSchema,
|
||||
organizationId: z.string()
|
||||
}))
|
||||
.action(async ({parsedInput, ctx}): Promise<ServerActionResult<Organization>> => {
|
||||
try {
|
||||
|
||||
const newUserList = parsedInput.data.users
|
||||
|
||||
const organization = await prisma.organization.findFirst({
|
||||
where:{
|
||||
id: parsedInput.organizationId,
|
||||
},
|
||||
include: {
|
||||
users : {}
|
||||
}
|
||||
})
|
||||
|
||||
const existingItemIds = organization.users.map((user) => user.userId);
|
||||
const usersToAdd = newUserList.filter(
|
||||
(id) => !existingItemIds.includes(id)
|
||||
);
|
||||
const usersToRemove = existingItemIds.filter(
|
||||
(id) => !newUserList.includes(id)
|
||||
);
|
||||
|
||||
console.log(usersToAdd);
|
||||
console.log(usersToRemove);
|
||||
|
||||
|
||||
if (usersToAdd.length > 0) {
|
||||
|
||||
await prisma.userOrganization.createMany({
|
||||
data: usersToAdd.map((userId) => ({
|
||||
userId: userId,
|
||||
organizationId: organization.id,
|
||||
})),
|
||||
skipDuplicates: true, // Optional: to avoid duplicate insertion errors
|
||||
});
|
||||
}
|
||||
if (usersToRemove.length > 0) {
|
||||
await prisma.userOrganization.deleteMany({
|
||||
where: {
|
||||
userId: { in: usersToRemove },
|
||||
},
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
const updatedOrganization = await prisma.organization.update({
|
||||
where:{
|
||||
id: organization.id
|
||||
},
|
||||
data:{
|
||||
name: parsedInput.data.name,
|
||||
slug: parsedInput.data.slug,
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return {
|
||||
success: true,
|
||||
value: updatedOrganization,
|
||||
actionSuccess: {
|
||||
message: "Organization has been successfully updated.",
|
||||
messageParams: {organizationId: updatedOrganization.id},
|
||||
},
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
actionError: {
|
||||
message: "Failed to update organization.",
|
||||
status: 500,
|
||||
cause: error.message ?? "Unknown error",
|
||||
messageParams: {message: "Error updating the organization"},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const deleteOrganizationAction = userAction
|
||||
.schema(z.string())
|
||||
.action(async ({parsedInput, ctx}): Promise<ServerActionResult<Organization>> => {
|
||||
|
||||
Reference in New Issue
Block a user