temp
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { AppError } from "../action-result";
|
||||
import { prisma } from "../prisma";
|
||||
import { requireAdmin } from "../user/require";
|
||||
import { formAction } from "../form-action";
|
||||
import z from "zod";
|
||||
|
||||
const createPositionSchema = z.object({
|
||||
name: z.string().min(1, "Name of a position cannot be empty"),
|
||||
institutionId: z.string().min(1, "Incorrect institution is provided"),
|
||||
});
|
||||
|
||||
// Defines an office (e.g. "Mayor of X") within an institution. This does NOT
|
||||
// assign anyone to it - filling/vacating a position happens through an
|
||||
// appointment act, see lib/acts.
|
||||
export const createPosition = formAction(createPositionSchema, async (data) => {
|
||||
await requireAdmin();
|
||||
|
||||
const institution = await prisma.institution.findUnique({ where: { id: data.institutionId } });
|
||||
if (!institution) {
|
||||
throw new AppError("Incorrect institution is provided");
|
||||
}
|
||||
|
||||
await prisma.position.create({ data: { name: data.name, institutionId: data.institutionId } });
|
||||
revalidatePath("/account/positions");
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
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" },
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
export type Position = {
|
||||
id: string;
|
||||
name: string;
|
||||
institutionId: string;
|
||||
}
|
||||
|
||||
export type CitizenshipPosition = {
|
||||
id: string;
|
||||
citizenshipId: string;
|
||||
positionId: string;
|
||||
assignedAt: Date;
|
||||
resignedAt: Date | null;
|
||||
actId: string | null;
|
||||
}
|
||||
Reference in New Issue
Block a user