161 lines
3.4 KiB
TypeScript
161 lines
3.4 KiB
TypeScript
import { Group, GroupUser } from "@toolctrl/db/prisma-browser";
|
|
import { db } from "~/utils/db.server";
|
|
import { UserSimple } from "../users.db.server";
|
|
import UserModelHelper from "~/utils/helpers/models/UserModelHelper";
|
|
|
|
export type GroupWithDetails = Group & {
|
|
users: (GroupUser & { user: UserSimple })[];
|
|
};
|
|
|
|
export async function getAllGroups(tenantId: string | null): Promise<GroupWithDetails[]> {
|
|
return await db.group.findMany({
|
|
where: {
|
|
tenantId,
|
|
},
|
|
include: {
|
|
users: {
|
|
include: {
|
|
...UserModelHelper.includeSimpleUser,
|
|
},
|
|
},
|
|
},
|
|
orderBy: [
|
|
{
|
|
name: "asc",
|
|
},
|
|
],
|
|
});
|
|
}
|
|
|
|
export async function getGroups(tenantId: string | null, ids: string[]): Promise<GroupWithDetails[]> {
|
|
return await db.group.findMany({
|
|
where: {
|
|
tenantId,
|
|
id: {
|
|
in: ids,
|
|
},
|
|
},
|
|
include: {
|
|
users: {
|
|
include: {
|
|
...UserModelHelper.includeSimpleUser,
|
|
},
|
|
},
|
|
},
|
|
orderBy: [
|
|
{
|
|
name: "asc",
|
|
},
|
|
],
|
|
});
|
|
}
|
|
|
|
export async function getMyGroups(userId: string, tenantId: string | null): Promise<GroupWithDetails[]> {
|
|
return await db.group.findMany({
|
|
where: {
|
|
tenantId,
|
|
OR: [
|
|
{
|
|
createdByUserId: userId,
|
|
},
|
|
{
|
|
users: {
|
|
some: {
|
|
userId,
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
include: {
|
|
users: {
|
|
include: {
|
|
...UserModelHelper.includeSimpleUser,
|
|
},
|
|
},
|
|
},
|
|
orderBy: [
|
|
{
|
|
name: "asc",
|
|
},
|
|
],
|
|
});
|
|
}
|
|
|
|
export async function getGroup(id: string, tenantId?: string | null): Promise<GroupWithDetails | null> {
|
|
return await db.group.findFirst({
|
|
where: {
|
|
id,
|
|
...(tenantId !== undefined ? { tenantId } : {}),
|
|
},
|
|
include: {
|
|
users: {
|
|
include: {
|
|
...UserModelHelper.includeSimpleUser,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function createGroup(data: { createdByUserId: string; tenantId: string | null; name: string; description: string; color: number }) {
|
|
return await db.group.create({
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function updateGroup(id: string, data: { name: string; description: string; color: number }, tenantId?: string | null) {
|
|
if (tenantId !== undefined) {
|
|
return await db.group.updateMany({
|
|
where: {
|
|
id,
|
|
tenantId,
|
|
},
|
|
data,
|
|
});
|
|
}
|
|
return await db.group.update({
|
|
where: {
|
|
id,
|
|
},
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function deleteGroup(id: string, tenantId?: string | null) {
|
|
if (tenantId !== undefined) {
|
|
return await db.group.deleteMany({
|
|
where: {
|
|
id,
|
|
tenantId,
|
|
},
|
|
});
|
|
}
|
|
return await db.group.delete({
|
|
where: {
|
|
id,
|
|
},
|
|
});
|
|
}
|
|
|
|
// R-P2b — Gruppen-Bulk-Ops (N:M). Idempotent: createMany skipDuplicates stuetzt sich auf den
|
|
// Unique-Index [groupId, userId], doppelte Mitgliedschaften werden uebersprungen.
|
|
export async function addUsersToGroup(groupId: string, userIds: string[]) {
|
|
if (userIds.length === 0) {
|
|
return { count: 0 };
|
|
}
|
|
return await db.groupUser.createMany({
|
|
data: userIds.map((userId) => ({ groupId, userId })),
|
|
skipDuplicates: true,
|
|
});
|
|
}
|
|
|
|
export async function removeUsersFromGroup(groupId: string, userIds: string[]) {
|
|
if (userIds.length === 0) {
|
|
return { count: 0 };
|
|
}
|
|
return await db.groupUser.deleteMany({
|
|
where: { groupId, userId: { in: userIds } },
|
|
});
|
|
}
|