Files
eu/lib/positions/repository.ts
T
2026-07-28 17:05:45 +03:00

47 lines
1.6 KiB
TypeScript

import { prisma } from "../prisma";
import { Position } from "./types";
const positionSelect = {
id: true,
name: true,
institutionId: true,
institution: { select: { id: true, name: true, kind: true } },
capabilities: { select: { capability: true } },
} as const;
export async function getPositions() {
return prisma.position.findMany({ select: positionSelect, orderBy: { name: "asc" } });
}
export async function getPositionById(id: string) {
return prisma.position.findUnique({ where: { id }, select: positionSelect });
}
export async function createPosition(position: Omit<Position, "id">) {
return prisma.position.create({ data: position });
}
// Current (non-resigned) holder of a position, if any.
export async function getCurrentHolder(positionId: string) {
return prisma.citizenshipPosition.findFirst({
where: { positionId, resignedAt: null },
include: { citizenship: { include: { user: { select: { firstName: true, secondName: true } } } } },
});
}
export async function getCitizenshipPositions(citizenshipId: string) {
return prisma.citizenshipPosition.findMany({
where: { citizenshipId },
include: { position: { select: positionSelect } },
orderBy: { assignedAt: "desc" },
});
}
export async function getUserPositions(userId: string) {
return prisma.citizenshipPosition.findMany({
where: { citizenship: { userId }, resignedAt: null },
include: { position: { select: positionSelect }, act: { select: { title: true, numberInSeries: true, numberYear: true, actType: { select: { name: true } } } } },
orderBy: { assignedAt: "desc" },
});
}