187 lines
6.4 KiB
TypeScript
187 lines
6.4 KiB
TypeScript
import { prisma } from "../prisma";
|
|
import { getCurrentUser } from "../user";
|
|
import { ActFormData } from "./validation";
|
|
import { ActBlockRecord, ArticleContent, AppointmentClause } from "./types";
|
|
import { Prisma } from "@/lib/generated/prisma/client";
|
|
|
|
const actTypeSelect = {
|
|
id: true, name: true, code: true, allowedIssuerKind: true, allowedIssuerInstitutionCode: true,
|
|
} as const;
|
|
const institutionSelect = { id: true, name: true, kind: true, code: true, appointedByInstitutionId: true } as const;
|
|
const regionSelect = { id: true, name: true } as const;
|
|
|
|
export async function getActTypes() {
|
|
return prisma.actType.findMany({ select: actTypeSelect });
|
|
}
|
|
|
|
export async function getInstitutions() {
|
|
return prisma.institution.findMany({ where: { isActive: true }, select: institutionSelect });
|
|
}
|
|
|
|
export async function getRegions() {
|
|
return prisma.region.findMany({ select: regionSelect });
|
|
}
|
|
|
|
export async function getActTypeById(id: string) {
|
|
return prisma.actType.findUnique({ where: { id }, select: actTypeSelect });
|
|
}
|
|
|
|
export async function getInstitutionById(id: string) {
|
|
return prisma.institution.findUnique({ where: { id }, select: institutionSelect });
|
|
}
|
|
|
|
export async function createAct(data: ActFormData) {
|
|
const user = await getCurrentUser();
|
|
if (!user) throw new Error("Failed to fetch user");
|
|
|
|
return prisma.$transaction(async (tx) => {
|
|
const act = await tx.act.create({
|
|
data: {
|
|
actTypeId: data.actTypeId,
|
|
issuingInstitutionId: data.issuingInstitutionId,
|
|
regionId: data.regionId || null,
|
|
authorId: user.id,
|
|
title: data.title,
|
|
legalStatus: "DRAFT",
|
|
},
|
|
});
|
|
|
|
const version = await tx.actVersion.create({
|
|
data: { actId: act.id, isCurrent: true, createdBy: user.id },
|
|
});
|
|
|
|
let position = 0;
|
|
const blocks: { versionId: string; stableKey: string; type: string; position: number; content: object }[] = [];
|
|
|
|
if (data.preambleText?.trim()) {
|
|
blocks.push({
|
|
versionId: version.id,
|
|
stableKey: "preamble",
|
|
type: "PREAMBLE",
|
|
position: position++,
|
|
content: { text: data.preambleText },
|
|
});
|
|
}
|
|
|
|
for (const article of data.articles) {
|
|
blocks.push({
|
|
versionId: version.id,
|
|
stableKey: article.id,
|
|
type: "ARTICLE",
|
|
position: position++,
|
|
content: {
|
|
title: article.title ?? null,
|
|
intro: article.intro ?? null,
|
|
markerStyle: article.markerStyle,
|
|
clauses: article.clauses,
|
|
},
|
|
});
|
|
}
|
|
|
|
await tx.actBlock.createMany({ data: blocks });
|
|
|
|
return act;
|
|
});
|
|
}
|
|
|
|
export async function getActs() {
|
|
return prisma.act.findMany({
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
numberInSeries: true,
|
|
numberYear: true,
|
|
createdAt: true,
|
|
legalStatus: true,
|
|
actType: { select: actTypeSelect },
|
|
issuingInstitution: { select: institutionSelect },
|
|
},
|
|
orderBy: { createdAt: "desc" },
|
|
});
|
|
}
|
|
|
|
export async function getActById(id: string) {
|
|
return prisma.act.findUnique({ where: { id } });
|
|
}
|
|
|
|
export async function getActVersions(actId: string) {
|
|
return prisma.actVersion.findMany({ where: { actId }, orderBy: { createdAt: "desc" } });
|
|
}
|
|
|
|
export async function getCurrentActVersion(actId: string) {
|
|
return prisma.actVersion.findFirst({ where: { actId, isCurrent: true } });
|
|
}
|
|
|
|
export async function getActApprovals(actVersionId: string) {
|
|
return prisma.actApproval.findMany({
|
|
where: { actVersionId },
|
|
select: { id: true, phaseKey: true, decision: true, citizenshipId: true, name: true, role: true, decidedAt: true },
|
|
orderBy: { decidedAt: "asc" },
|
|
});
|
|
}
|
|
|
|
export async function recordApproval(
|
|
actId: string, actVersionId: string, phaseKey: string,
|
|
citizenshipId: string, name: string, role: string, isFinal: boolean
|
|
) {
|
|
return prisma.$transaction(async (tx) => {
|
|
await tx.actApproval.create({ data: { actVersionId, phaseKey, decision: "APPROVE", citizenshipId, name, role } });
|
|
const act = await tx.act.findUniqueOrThrow({ where: { id: actId } });
|
|
if (!isFinal) return act;
|
|
|
|
const acts = await prisma.act.findMany({ where: { issuingInstitutionId: act.issuingInstitutionId, legalStatus: "IN_FORCE", numberYear: new Date().getFullYear() }, orderBy: { updatedAt: "desc" } });
|
|
await applyAppointmentEffects(tx, actId, actVersionId);
|
|
return tx.act.update({ where: { id: actId }, data: { legalStatus: "IN_FORCE", numberInSeries: acts[0]?.numberInSeries || 0 + 1, numberYear: new Date().getFullYear() } });
|
|
});
|
|
}
|
|
|
|
export async function recordVeto(
|
|
actId: string, actVersionId: string, phaseKey: string,
|
|
citizenshipId: string, name: string, role: string
|
|
) {
|
|
return prisma.$transaction(async (tx) => {
|
|
await tx.actApproval.create({ data: { actVersionId, phaseKey, decision: "VETO", citizenshipId, name, role } });
|
|
return tx.act.update({ where: { id: actId }, data: { legalStatus: "NOT_ADOPTED" } });
|
|
});
|
|
}
|
|
|
|
export async function getActBlocks(versionId: string): Promise<ActBlockRecord[]> {
|
|
return prisma.actBlock.findMany({
|
|
where: { versionId },
|
|
orderBy: { position: "asc" },
|
|
}) as Promise<ActBlockRecord[]>;
|
|
}
|
|
|
|
async function applyAppointmentEffects(tx: Prisma.TransactionClient, actId: string, versionId: string) {
|
|
const articleBlocks = await tx.actBlock.findMany({ where: { versionId, type: "ARTICLE" } });
|
|
|
|
const appointmentClauses = articleBlocks.flatMap((block) => {
|
|
const content = block.content as unknown as ArticleContent;
|
|
return content.clauses.filter((c): c is AppointmentClause => c.kind === "APPOINTMENT");
|
|
});
|
|
|
|
for (const clause of appointmentClauses) {
|
|
if (clause.action === "ASSIGN") {
|
|
await tx.citizenshipPosition.updateMany({
|
|
where: { positionId: clause.positionId, resignedAt: null },
|
|
data: { resignedAt: new Date() },
|
|
});
|
|
await tx.citizenshipPosition.create({
|
|
data: { citizenshipId: clause.citizenshipId, positionId: clause.positionId, actId },
|
|
});
|
|
} else {
|
|
await tx.citizenshipPosition.updateMany({
|
|
where: { positionId: clause.positionId, citizenshipId: clause.citizenshipId, resignedAt: null },
|
|
data: { resignedAt: new Date() },
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function putActIntoForce(actId: string) {
|
|
return prisma.$transaction(async (tx) => {
|
|
const version = await tx.actVersion.findFirstOrThrow({ where: { actId, isCurrent: true } });
|
|
await applyAppointmentEffects(tx, actId, version.id);
|
|
return tx.act.update({ where: { id: actId }, data: { legalStatus: "IN_FORCE" } });
|
|
});
|
|
} |