103 lines
2.3 KiB
TypeScript
103 lines
2.3 KiB
TypeScript
import { RowPermission } from "@toolctrl/db/prisma-browser";
|
|
import { db } from "~/utils/db.server";
|
|
|
|
export type RowPermissionsWithDetails = RowPermission & {
|
|
tenant: { id: string; name: string } | null;
|
|
role?: { id: string; name: string } | null;
|
|
group?: { id: string; name: string } | null;
|
|
user?: { id: string; email: string } | null;
|
|
};
|
|
export async function getRowPermissions(rowId: string): Promise<RowPermissionsWithDetails[]> {
|
|
return await db.rowPermission.findMany({
|
|
where: {
|
|
rowId,
|
|
},
|
|
include: {
|
|
tenant: { select: { id: true, name: true } },
|
|
role: { select: { id: true, name: true } },
|
|
group: { select: { id: true, name: true } },
|
|
user: { select: { id: true, email: true } },
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getRowPermissionByTenant(rowId: string, tenantId?: string | null) {
|
|
return await db.rowPermission.findFirst({
|
|
where: {
|
|
rowId,
|
|
tenantId,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getRowPermissionByGroups(rowId: string, groups: string[]) {
|
|
return await db.rowPermission.findFirst({
|
|
where: {
|
|
rowId,
|
|
groupId: {
|
|
in: groups,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getRowPermissionByRoles(rowId: string, roles: string[]) {
|
|
return await db.rowPermission.findFirst({
|
|
where: {
|
|
rowId,
|
|
roleId: {
|
|
in: roles,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getRowPermissionByUsers(rowId: string, users: string[]) {
|
|
return await db.rowPermission.findFirst({
|
|
where: {
|
|
rowId,
|
|
userId: {
|
|
in: users,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function createRowPermission(data: {
|
|
rowId: string;
|
|
tenantId?: string | null;
|
|
roleId?: string | null;
|
|
groupId?: string | null;
|
|
userId?: string | null;
|
|
public?: boolean | null;
|
|
access: string;
|
|
}) {
|
|
return await db.rowPermission.create({
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function updateRowPermissionForRow(rowId: string, id: string, data: { access: string }) {
|
|
return await db.rowPermission.updateMany({
|
|
where: { id, rowId },
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function deleteRowPermission(rowId: string) {
|
|
return await db.rowPermission.deleteMany({
|
|
where: {
|
|
rowId,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function deleteRowPermissionByIdForRow(rowId: string, id: string) {
|
|
return await db.rowPermission.deleteMany({
|
|
where: {
|
|
id,
|
|
rowId,
|
|
},
|
|
});
|
|
}
|